You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now from Javascript if I call Module.consumeTest({x: 2}) it fails with the error TypeError: Missing field: "y".
I would need to call Module.consumeTest({x: 2, y: undefined}) for this to work.
The whole point of making y optional is being able to omit it. Also, having to pass undefined is very brittle, because JSON serialization/deserialization drops undefined fields.
My proposal is to make .field aware of std::optional and don't complain when an optional field is missing. Does this make sense?
Aside: It would also be nice for the .d.ts typescript definitions to use optional fields as well (i.e. y?: number).
The text was updated successfully, but these errors were encountered:
I took a look at libembind.js, and the fix doesn't require complex wiring. _embind_finalize_value_object already knows which fields are optional - it's an explicit property in each field inside fieldTypes. Passing the optional property down to the fields map that's sent to toWireType is enough for it to be able to ignore missing optional fields.
Since I'm not familiar with the code of libembind.js, I don't know if this could have other unintended consequences, but at first glance it seems to be a low-risk change.
Version of emscripten/emsdk:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.2 (7591f1c)
clang version 21.0.0git (https:/github.com/llvm/llvm-project 9534d27e3321a3b9e6e79fe6328445575bf26b7b)
Target: wasm32-unknown-emscripten
Thread model: posix
Here is a simple repro case:
Now from Javascript if I call
Module.consumeTest({x: 2})
it fails with the errorTypeError: Missing field: "y"
.I would need to call
Module.consumeTest({x: 2, y: undefined})
for this to work.The whole point of making
y
optional is being able to omit it. Also, having to passundefined
is very brittle, because JSON serialization/deserialization drops undefined fields.My proposal is to make
.field
aware ofstd::optional
and don't complain when an optional field is missing. Does this make sense?Aside: It would also be nice for the
.d.ts
typescript definitions to use optional fields as well (i.e.y?: number
).The text was updated successfully, but these errors were encountered: