Skip to content

Commit d515cd5

Browse files
embind: tsgen function params name (emscripten-core#20141)
* embind: tsgen custom param names * embind: tsgen custom param names * fix: added assertions * feat: added tests for param name generation * fix: error message for unmatching parantheses --------- Co-authored-by: Tayyab Ejaz <[email protected]>
1 parent b65dc32 commit d515cd5

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

src/embind/embind.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -995,10 +995,11 @@ var LibraryEmbind = {
995995
_embind_register_function__deps: [
996996
'$craftInvokerFunction', '$exposePublicSymbol', '$heap32VectorToArray',
997997
'$readLatin1String', '$replacePublicSymbol', '$embind__requireFunction',
998-
'$throwUnboundTypeError', '$whenDependentTypesAreResolved'],
998+
'$throwUnboundTypeError', '$whenDependentTypesAreResolved', '$getFunctionName'],
999999
_embind_register_function: (name, argCount, rawArgTypesAddr, signature, rawInvoker, fn, isAsync) => {
10001000
var argTypes = heap32VectorToArray(argCount, rawArgTypesAddr);
10011001
name = readLatin1String(name);
1002+
name = getFunctionName(name);
10021003

10031004
rawInvoker = embind__requireFunction(signature, rawInvoker);
10041005

@@ -1993,7 +1994,7 @@ var LibraryEmbind = {
19931994
_embind_register_class_function__deps: [
19941995
'$craftInvokerFunction', '$heap32VectorToArray', '$readLatin1String',
19951996
'$embind__requireFunction', '$throwUnboundTypeError',
1996-
'$whenDependentTypesAreResolved'],
1997+
'$whenDependentTypesAreResolved', '$getFunctionName'],
19971998
_embind_register_class_function: (rawClassType,
19981999
methodName,
19992000
argCount,
@@ -2005,6 +2006,7 @@ var LibraryEmbind = {
20052006
isAsync) => {
20062007
var rawArgTypes = heap32VectorToArray(argCount, rawArgTypesAddr);
20072008
methodName = readLatin1String(methodName);
2009+
methodName = getFunctionName(methodName);
20082010
rawInvoker = embind__requireFunction(invokerSignature, rawInvoker);
20092011

20102012
whenDependentTypesAreResolved([], [rawClassType], function(classType) {
@@ -2127,7 +2129,7 @@ var LibraryEmbind = {
21272129
_embind_register_class_class_function__deps: [
21282130
'$craftInvokerFunction', '$ensureOverloadTable', '$heap32VectorToArray',
21292131
'$readLatin1String', '$embind__requireFunction', '$throwUnboundTypeError',
2130-
'$whenDependentTypesAreResolved'],
2132+
'$whenDependentTypesAreResolved', '$getFunctionName'],
21312133
_embind_register_class_class_function: (rawClassType,
21322134
methodName,
21332135
argCount,
@@ -2138,6 +2140,7 @@ var LibraryEmbind = {
21382140
isAsync) => {
21392141
var rawArgTypes = heap32VectorToArray(argCount, rawArgTypesAddr);
21402142
methodName = readLatin1String(methodName);
2143+
methodName = getFunctionName(methodName);
21412144
rawInvoker = embind__requireFunction(invokerSignature, rawInvoker);
21422145
whenDependentTypesAreResolved([], [rawClassType], function(classType) {
21432146
classType = classType[0];

src/embind/embind_shared.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,28 @@ var LibraryEmbindShared = {
124124
_free(ptr);
125125
return rv;
126126
},
127-
127+
$getFunctionName__deps: [],
128+
$getFunctionName: (signature) => {
129+
signature = signature.trim();
130+
const argsIndex = signature.indexOf("(");
131+
if (argsIndex !== -1) {
132+
assert(signature[signature.length - 1] == ")", "Parentheses for argument names should match.");
133+
return signature.substr(0, argsIndex);
134+
} else {
135+
return signature;
136+
}
137+
},
138+
$getFunctionArgsName__deps: [],
139+
$getFunctionArgsName: (signature) => {
140+
signature = signature.trim();
141+
const argsIndex = signature.indexOf("(") + 1;
142+
if (argsIndex !== 0) {
143+
assert(signature[signature.length - 1] == ")", "Parentheses for argument names should match.");
144+
return signature.substr(argsIndex, signature.length - argsIndex - 1).replaceAll(" ", "").split(",").filter(n => n.length);
145+
} else {
146+
return [];
147+
}
148+
},
128149
$heap32VectorToArray: (count, firstElement) => {
129150
var array = [];
130151
for (var i = 0; i < count; i++) {

src/embind/embind_ts.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,22 +257,30 @@ var LibraryEmbind = {
257257
$registerIntegerType: (id) => {
258258
registerType(id, new IntegerType(id));
259259
},
260-
$createFunctionDefinition__deps: ['$FunctionDefinition', '$heap32VectorToArray', '$readLatin1String', '$Argument', '$whenDependentTypesAreResolved'],
260+
$createFunctionDefinition__deps: ['$FunctionDefinition', '$heap32VectorToArray', '$readLatin1String', '$Argument', '$whenDependentTypesAreResolved', '$getFunctionName', '$getFunctionArgsName'],
261261
$createFunctionDefinition: (name, argCount, rawArgTypesAddr, hasThis, cb) => {
262262
const argTypes = heap32VectorToArray(argCount, rawArgTypesAddr);
263263
name = typeof name === 'string' ? name : readLatin1String(name);
264264

265-
whenDependentTypesAreResolved([], argTypes, function(argTypes) {
265+
whenDependentTypesAreResolved([], argTypes, function (argTypes) {
266+
const argsName = getFunctionArgsName(name);
267+
name = getFunctionName(name);
266268
const returnType = argTypes[0];
267269
let thisType = null;
268270
let argStart = 1;
269271
if (hasThis) {
270272
thisType = argTypes[1];
271273
argStart = 2;
272274
}
275+
if (argsName.length)
276+
assert(argsName.length == (argTypes.length - hasThis - 1), 'Argument names should match number of parameters.');
273277
const args = [];
274-
for (let i = argStart; i < argTypes.length; i++) {
275-
args.push(new Argument(`_${i - argStart}`, argTypes[i]));
278+
for (let i = argStart, x = 0; i < argTypes.length; i++) {
279+
if (x < argsName.length) {
280+
args.push(new Argument(argsName[x++], argTypes[i]));
281+
} else {
282+
args.push(new Argument(`_${i - argStart}`, argTypes[i]));
283+
}
276284
}
277285
const funcDef = new FunctionDefinition(name, returnType, args, thisType);
278286
cb(funcDef);

test/other/embind_tsgen.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,13 @@ EMSCRIPTEN_BINDINGS(Test) {
8989
.function("functionTwo", &Test::function_two)
9090
.function("functionThree", &Test::function_three)
9191
.function("functionFour", &Test::function_four)
92+
.function("functionFive(x, y)", &Test::function_one)
93+
.function("functionSix(str)", &Test::function_three)
9294
.function("constFn", &Test::const_fn)
9395
.property("x", &Test::getX, &Test::setX)
9496
.property("y", &Test::getY)
9597
.class_function("staticFunction", &Test::static_function)
98+
.class_function("staticFunctionWithParam(x)", &Test::static_function)
9699
.class_property("staticProperty", &Test::static_property)
97100
;
98101

@@ -144,6 +147,7 @@ EMSCRIPTEN_BINDINGS(Test) {
144147
.function("fn", &ClassWithSmartPtrConstructor::fn);
145148

146149
function("smart_ptr_function", &smart_ptr_function);
150+
function("smart_ptr_function_with_params(foo)", &smart_ptr_function);
147151

148152
class_<BaseClass>("BaseClass").function("fn", &BaseClass::fn);
149153

test/other/embind_tsgen.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ export interface Test {
44
functionOne(_0: number, _1: number): number;
55
functionTwo(_0: number, _1: number): number;
66
functionFour(_0: boolean): number;
7+
functionFive(x: number, y: number): number;
78
constFn(): number;
89
functionThree(_0: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): number;
10+
functionSix(str: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): number;
911
delete(): void;
1012
}
1113

@@ -58,7 +60,7 @@ export interface DerivedClass extends BaseClass {
5860
export type ValArr = [ number, number, number ];
5961

6062
export interface MainModule {
61-
Test: {new(): Test; staticFunction(_0: number): number; staticProperty: number};
63+
Test: {new(): Test; staticFunction(_0: number): number; staticFunctionWithParam(x: number): number; staticProperty: number};
6264
class_returning_fn(): Test;
6365
class_unique_ptr_returning_fn(): Test;
6466
a_class_instance: Test;
@@ -75,4 +77,5 @@ export interface MainModule {
7577
an_int: number;
7678
global_fn(_0: number, _1: number): number;
7779
smart_ptr_function(_0: ClassWithSmartPtrConstructor): number;
80+
smart_ptr_function_with_params(foo: ClassWithSmartPtrConstructor): number;
7881
}

0 commit comments

Comments
 (0)