Skip to content

Conversation

@brendandahl
Copy link
Collaborator

This is really two changes, but they both will affect the same code so I did them at once:

  • The number of arguments is now only verified with ASSERTIONS enabled. This will help code size and the speed of embind invoker functions.
  • Allow optional arguments to be omitted.

Fixes #22389

assert.equal(-1, value);
});

test("std::optional args can be omitted", function() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about arguments that have default values?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Embind doesn't really support default C++ arguments. Functions with default arguments just look like regular functions to embind. e.g. foo(int a = 100) is just foo(int a) in embind's view of things.

if (arguments.length !== ${argCount}) {
throwBindingError('function ' + humanName + ' called with ' + arguments.length + ' arguments, expected ${argCount}');
}`;
var invokerFnBody = `return function (${argsList}) {\n`;
Copy link
Collaborator

Choose a reason for hiding this comment

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

As a followup can this be arrow function in some cases?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll look into it. This is also used in constructors so having the scope bound, may be an issue.

This is really two changes, but they both will affect the same code so I
did them at once:
 - The number of arguments is now only verified with ASSERTIONS enabled. This
   will help code size and the speed of embind invoker functions.
 - Allow optional arguments to be omitted.

Fixes emscripten-core#22389
@brendandahl brendandahl merged commit 203bc12 into emscripten-core:main Sep 20, 2024
2 of 13 checks passed
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.

Support omitting optional arguments in js embind bindings (2)

3 participants