Repository navigation
embind: Add helper for registering a std::optional type. - #21076
Merged
brendandahl merged 1 commit intoJan 17, 2024
Merged
Conversation
brendandahl
force-pushed
the
optional-type-with-val
branch
from
January 16, 2024 16:59
1618e63 to
8727c15
Compare
brendandahl
force-pushed
the
optional-type-with-val
branch
from
January 16, 2024 17:55
8727c15 to
c8b52b8
Compare
RReverser
reviewed
Jan 16, 2024
| a_bool: boolean; | ||
| an_int: number; | ||
| global_fn(_0: number, _1: number): number; | ||
| optional_test(_0: Foo | undefined): number | undefined; |
Collaborator
There was a problem hiding this comment.
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.
RReverser
approved these changes
Jan 16, 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is an alternative implementation of #20359. Now we instead use
undefinedfor empty optionals and the value otherwise in JS.Currently this uses a
valto 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