Skip to content

embind: tsgen function params name #20141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/embind/embind.js
Original file line number Diff line number Diff line change
Expand Up @@ -984,10 +984,11 @@ var LibraryEmbind = {
_embind_register_function__deps: [
'$craftInvokerFunction', '$exposePublicSymbol', '$heap32VectorToArray',
'$readLatin1String', '$replacePublicSymbol', '$embind__requireFunction',
'$throwUnboundTypeError', '$whenDependentTypesAreResolved'],
'$throwUnboundTypeError', '$whenDependentTypesAreResolved', '$getFunctionName'],
_embind_register_function: (name, argCount, rawArgTypesAddr, signature, rawInvoker, fn, isAsync) => {
var argTypes = heap32VectorToArray(argCount, rawArgTypesAddr);
name = readLatin1String(name);
name = getFunctionName(name);

rawInvoker = embind__requireFunction(signature, rawInvoker);

Expand Down Expand Up @@ -1983,7 +1984,7 @@ var LibraryEmbind = {
_embind_register_class_function__deps: [
'$craftInvokerFunction', '$heap32VectorToArray', '$readLatin1String',
'$embind__requireFunction', '$throwUnboundTypeError',
'$whenDependentTypesAreResolved'],
'$whenDependentTypesAreResolved', '$getFunctionName'],
_embind_register_class_function: (rawClassType,
methodName,
argCount,
Expand All @@ -1995,6 +1996,7 @@ var LibraryEmbind = {
isAsync) => {
var rawArgTypes = heap32VectorToArray(argCount, rawArgTypesAddr);
methodName = readLatin1String(methodName);
methodName = getFunctionName(methodName);
rawInvoker = embind__requireFunction(invokerSignature, rawInvoker);

whenDependentTypesAreResolved([], [rawClassType], function(classType) {
Expand Down Expand Up @@ -2117,7 +2119,7 @@ var LibraryEmbind = {
_embind_register_class_class_function__deps: [
'$craftInvokerFunction', '$ensureOverloadTable', '$heap32VectorToArray',
'$readLatin1String', '$embind__requireFunction', '$throwUnboundTypeError',
'$whenDependentTypesAreResolved'],
'$whenDependentTypesAreResolved', '$getFunctionName'],
_embind_register_class_class_function: (rawClassType,
methodName,
argCount,
Expand All @@ -2128,6 +2130,7 @@ var LibraryEmbind = {
isAsync) => {
var rawArgTypes = heap32VectorToArray(argCount, rawArgTypesAddr);
methodName = readLatin1String(methodName);
methodName = getFunctionName(methodName);
rawInvoker = embind__requireFunction(invokerSignature, rawInvoker);
whenDependentTypesAreResolved([], [rawClassType], function(classType) {
classType = classType[0];
Expand Down
23 changes: 22 additions & 1 deletion src/embind/embind_shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,28 @@ var LibraryEmbindShared = {
_free(ptr);
return rv;
},

$getFunctionName__deps: [],
$getFunctionName: (signature) => {
signature = signature.trim();
const argsIndex = signature.indexOf("(");
if (argsIndex !== -1) {
assert(signature[signature.length - 1] == ")", "Parentheses for argument names should match.");
return signature.substr(0, argsIndex);
} else {
return signature;
}
},
$getFunctionArgsName__deps: [],
$getFunctionArgsName: (signature) => {
signature = signature.trim();
const argsIndex = signature.indexOf("(") + 1;
if (argsIndex !== 0) {
assert(signature[signature.length - 1] == ")", "Parentheses for argument names should match.");
return signature.substr(argsIndex, signature.length - argsIndex - 1).replaceAll(" ", "").split(",").filter(n => n.length);
} else {
return [];
}
},
$heap32VectorToArray: (count, firstElement) => {
var array = [];
for (var i = 0; i < count; i++) {
Expand Down
16 changes: 12 additions & 4 deletions src/embind/embind_ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,22 +257,30 @@ var LibraryEmbind = {
$registerIntegerType: (id) => {
registerType(id, new IntegerType(id));
},
$createFunctionDefinition__deps: ['$FunctionDefinition', '$heap32VectorToArray', '$readLatin1String', '$Argument', '$whenDependentTypesAreResolved'],
$createFunctionDefinition__deps: ['$FunctionDefinition', '$heap32VectorToArray', '$readLatin1String', '$Argument', '$whenDependentTypesAreResolved', '$getFunctionName', '$getFunctionArgsName'],
$createFunctionDefinition: (name, argCount, rawArgTypesAddr, hasThis, cb) => {
const argTypes = heap32VectorToArray(argCount, rawArgTypesAddr);
name = readLatin1String(name);

whenDependentTypesAreResolved([], argTypes, function(argTypes) {
whenDependentTypesAreResolved([], argTypes, function (argTypes) {
const argsName = getFunctionArgsName(name);
name = getFunctionName(name);
const returnType = argTypes[0];
let thisType = null;
let argStart = 1;
if (hasThis) {
thisType = argTypes[1];
argStart = 2;
}
if (argsName.length)
assert(argsName.length == (argTypes.length - hasThis - 1), 'Argument names should match number of parameters.');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added assertions for argument names to match expected number of parameters

const args = [];
for (let i = argStart; i < argTypes.length; i++) {
args.push(new Argument(`_${i - argStart}`, argTypes[i]));
for (let i = argStart, x = 0; i < argTypes.length; i++) {
if (x < argsName.length) {
args.push(new Argument(argsName[x++], argTypes[i]));
} else {
args.push(new Argument(`_${i - argStart}`, argTypes[i]));
}
}
const funcDef = new FunctionDefinition(name, returnType, args, thisType);
cb(funcDef);
Expand Down
4 changes: 4 additions & 0 deletions test/other/embind_tsgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,13 @@ EMSCRIPTEN_BINDINGS(Test) {
.function("functionTwo", &Test::function_two)
.function("functionThree", &Test::function_three)
.function("functionFour", &Test::function_four)
.function("functionFive(x, y)", &Test::function_one)
.function("functionSix(str)", &Test::function_three)
.function("constFn", &Test::const_fn)
.property("x", &Test::getX, &Test::setX)
.property("y", &Test::getY)
.class_function("staticFunction", &Test::static_function)
.class_function("staticFunctionWithParam(x)", &Test::static_function)
.class_property("staticProperty", &Test::static_property)
;

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

function("smart_ptr_function", &smart_ptr_function);
function("smart_ptr_function_with_params(foo)", &smart_ptr_function);

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

Expand Down
5 changes: 4 additions & 1 deletion test/other/embind_tsgen.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ export interface Test {
functionOne(_0: number, _1: number): number;
functionTwo(_0: number, _1: number): number;
functionFour(_0: boolean): number;
functionFive(x: number, y: number): number;
constFn(): number;
functionThree(_0: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): number;
functionSix(str: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): number;
delete(): void;
}

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

export interface MainModule {
Test: {new(): Test; staticFunction(_0: number): number; staticProperty: number};
Test: {new(): Test; staticFunction(_0: number): number; staticFunctionWithParam(x: number): number; staticProperty: number};
class_returning_fn(): Test;
class_unique_ptr_returning_fn(): Test;
a_class_instance: Test;
Expand All @@ -75,4 +77,5 @@ export interface MainModule {
an_int: number;
global_fn(_0: number, _1: number): number;
smart_ptr_function(_0: ClassWithSmartPtrConstructor): number;
smart_ptr_function_with_params(foo: ClassWithSmartPtrConstructor): number;
}