Skip to content

embind: Add helper for registering a std::optional type. #21076

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

brendandahl
Copy link
Collaborator

This is an alternative implementation of #20359. Now we instead use undefined for empty optionals and the value otherwise in JS.

Currently this uses a val to send optionals back and forth. This is probably not the most efficient way of doing it. However, I started writing a custom wire type value that was malloc'd for optionals and it added a ton of code to be able to serialize them back and forth. This seems much cleaner.

Fixes #20082

@brendandahl brendandahl force-pushed the optional-type-with-val branch from 1618e63 to 8727c15 Compare January 16, 2024 16:59
@brendandahl brendandahl force-pushed the optional-type-with-val branch from 8727c15 to c8b52b8 Compare January 16, 2024 17:55
@brendandahl brendandahl requested a review from RReverser January 16, 2024 21:22
@@ -88,6 +88,7 @@ export interface MainModule {
a_bool: boolean;
an_int: number;
global_fn(_0: number, _1: number): number;
optional_test(_0: Foo | undefined): number | undefined;
Copy link
Collaborator

@RReverser RReverser Jan 16, 2024

Choose a reason for hiding this comment

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

It could probably be interesting to add logic to detect std::optional<...> at the end of parameter list (only when not followed by non-optional types) and transform them into _0?: Foo instead, as in, actual optional params. Can be a separate PR though.

@brendandahl brendandahl merged commit b1401d9 into emscripten-core:main Jan 17, 2024
brendandahl pushed a commit that referenced this pull request Oct 7, 2024
Calling `register_optional<std::string>("Optional");` results in:

```
error: no matching function for call to 'register_optional'
    8 |   emscripten::register_optional<std::string>("Optional");
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/path/to/emsdk/upstream/emscripten/cache/sysroot/include/emscripten/bind.h:1880:6: note: candidate function template not viable: requires 0 arguments, but 1 was provided
 1880 | void register_optional() {
      |      ^
1 error generated
```

As shown in later in the doc and in usage in
#21076,
`register_optional` should not take in an argument. Also see the source
code:
https://github.com/emscripten-core/emscripten/blob/df2200953b57af106dc8a597692580b796f542ef/system/include/emscripten/bind.h#L2006
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.

Typescript bindings generation for std::optional types issues
2 participants