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

Conversation

tayyabejaz2000
Copy link
Contributor

@tayyabejaz2000 tayyabejaz2000 commented Aug 28, 2023

  • Custom function parameter names for TS.
  • Parameter names are optional, added just after method name for better TS Typings
int TestFunctionWithOneParam(int x);
int TestFunctionWithTwoParams(float param1, bool param2);
int TestFunctionWithThreeParams(float param1, bool param2, int param3);

EMSCRIPTEN_BINDINGS(ParamsNameGen)
{
  function("TestFunctionWithOneParam", TestFunctionWithOneParam);
  function("TestFunctionWithTwoParams(param1, param2)", TestFunctionWithTwoParams);
  function("TestFunctionWithThreeParams(param1, param3)", TestFunctionWithThreeParams);
  function("TestFunctionWithThreeParams_1(param1,param2,params3,params4)", TestFunctionWithThreeParams);
}
  • Specifying less parameters than expected will use _<n> convention
  • Specifying more parameters will ignore extra parameters
export interface MainModule {
  TestFunctionWithOneParam(_0: number): number;
  TestFunctionWithTwoParams(param1: number, param2: boolean): number;
  TestFunctionWithThreeParams(param1: number, param3: boolean, _2: number): number;
  TestFunctionWithThreeParams_1(param1: number, param2: boolean, params3: number): number;
}
  • Also works for Class Functions/Class Static Functions

@sbc100 sbc100 requested a review from brendandahl August 28, 2023 17:25
@sbc100
Copy link
Collaborator

sbc100 commented Aug 28, 2023

Its a shame we can't extract the argument names from the C/C++ declarations.. but that would be highly non-trivial.

Copy link
Collaborator

@brendandahl brendandahl left a comment

Choose a reason for hiding this comment

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

A few minor things and please add some tests to https://github.com/emscripten-core/emscripten/blob/main/test/other/embind_tsgen.cpp and update the expected defintions.

I would ultimately like to get to a situation where we extract argument names, but I think that will require a clang plugin or extracting the argument names from the debug info.

for (let i = argStart; i < argTypes.length; i++) {
args.push(new Argument(`_${i - argStart}`, argTypes[i]));
if (argsName.length && x < argsName.length) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's add an assert that if argNames are used it matches the expected number of arguments. This will help prevent bindings getting out of sync with their TS definitions.

signature = signature.trim();
const argsIndex = signature.indexOf("(") + 1;
if (argsIndex !== 0) {
assert(signature[signature.length - 1] == ")", "Unmatching paranthesis for argument names.");
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 assertion for unmatching parenthesis in function signature

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

Copy link
Collaborator

@brendandahl brendandahl left a comment

Choose a reason for hiding this comment

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

Still missings tests in https://github.com/emscripten-core/emscripten/blob/main/test/other/embind_tsgen.cpp and update the expected defintions

signature = signature.trim();
const argsIndex = signature.indexOf("(");
if (argsIndex !== -1) {
assert(signature[signature.length - 1] == ")", "Unmatching paranthesis for argument names.");
Copy link
Collaborator

Choose a reason for hiding this comment

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

'Parentheses for argument names should match.'

signature = signature.trim();
const argsIndex = signature.indexOf("(") + 1;
if (argsIndex !== 0) {
assert(signature[signature.length - 1] == ")", "Unmatching paranthesis for argument names.");
Copy link
Collaborator

Choose a reason for hiding this comment

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

'Parentheses for argument names should match.'

@brendandahl brendandahl merged commit d515cd5 into emscripten-core:main Sep 18, 2023
@mrroohian
Copy link
Contributor

mrroohian commented Sep 29, 2023

@tayyabejaz2000 @brendandahl This is a good idea until extracting names is possible. Quick question: what about constructors? Is there a solution available for those too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants