Skip to content
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

Support omitting optional arguments in js embind bindings #21776

Closed
synoet opened this issue Apr 17, 2024 · 1 comment · Fixed by #21786
Closed

Support omitting optional arguments in js embind bindings #21776

synoet opened this issue Apr 17, 2024 · 1 comment · Fixed by #21786
Assignees
Labels

Comments

@synoet
Copy link

synoet commented Apr 17, 2024

Please include the following in your bug report:

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.55 (165133b1cc977f0b9a277e42ef809b823157189c)
clang version 19.0.0git (https:/github.com/llvm/llvm-project 6c7805d5d186a6d1263f90b8033ad85e2d2633d7)
Target: wasm32-unknown-emscripten
Thread model: posix

Issue
When using optional arguments with embind, it would be more intuitive and convenient to allow omitting the optional arguments entirely, rather than requiring passing undefined for the optional parameters.

inline std::string testOptional(std::string str, std::optional<std::string> optionalString)
{
}

EMSCRIPTEN_BINDINGS(test)
{
    register_optional<std::string>();
    function("test", &testOptional);
}

I would expect the generated JavaScript bindings to allow calling the function like this:

testOptional("someString")

Instead of requiring the following:

testOptional("someString", undefined)

I noticed a comment on the original optional implementation pull request (#21076) mentioning plans to support this feature.

@sbc100 sbc100 added the embind label Apr 17, 2024
brendandahl added a commit to brendandahl/emscripten that referenced this issue Apr 18, 2024
This replaces `_0: Foo | undefined` with `_0?: Foo` for any optional
arguments at the end of function, which allows for more idiomatic TS.

Fixes emscripten-core#21776
brendandahl added a commit that referenced this issue Apr 19, 2024
…1786)

This replaces `_0: Foo | undefined` with `_0?: Foo` for any optional
arguments at the end of function, which allows for more idiomatic TS.

Fixes #21776
@synoet
Copy link
Author

synoet commented Apr 25, 2024

Thanks!

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

Successfully merging a pull request may close this issue.

3 participants