-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 for Option arguments of user types #2370
Comments
For clarity, this issue should probably be rephrased that it's specifically for passing |
I must admit I certainly did not expect this behavior on JS side, but I understand how it makes sense if you want to make your JS code behave like Rust (how much sense that makes is a different question). Also, I couldn't find any warning about it in the docs (in fact, |
@fjarri That table is for |
That said, here it should certainly be marked as supported in both params and returns. |
Ah, I see, my mistake. |
FWIW |
Yes, I figured that. But honestly, I don't see a point in bringing that specific bit of Rust semantics into JS which misses all the other parts that work together with it, like references or the borrow checker. |
Well it has to do that, otherwise this would be UB on the Rust part. Rust expects that if a parameter is marked to take something by-value, then it won't be accessible again - you're violating that if you still allow access to the same structure on the JS side after the call. |
On the other hand, you're violating the JS calling conventions by invalidating the object, so it's rather a question of priorities. I do not mind a Rust object inside the bindings not being accessible anymore, but why should it affect the JS object? |
Because JS object is merely a wrapper for Rust one. It doesn't have a copy of the struct or something like that, it's just a pointer to the Rust side. When Rust frees the data behind such pointer, it would be worse if JS kept allowing access to the "garbage" that now happens to reside there in memory. |
If you want JS side to have proper JS objects that are merely copies of the Rust ones, it might be that you want Serde integration instead (e.g. check out https://github.com/cloudflare/serde-wasm-bindgen). |
That's where I would disagree. A JS object is a wrapper over a WASM one, that's the reality that a user sees. Rust is merely an implementation detail. There's no call for leaking its semantics into JS, especially those parts of it that don't work well by themselves. But I agree that this discussion is only tangential to this issue. If |
Bump. Any news on this? At the moment the only way to have optional arguments that behave as expected on the JS side (that is, don't get nullified on use) seems to be to use a hacky proc-macro (#2231 (comment)). I think it would be a very useful feature to a lot of people to be able to have |
For anyone who bumps into the same issue: a workaround is possible, see https://docs.rs/wasm-bindgen-derive/latest/wasm_bindgen_derive/#optional-arguments |
Hello,
@alexcrichton @RReverser |
Currently, having a binding that takes an optional argument of a custom struct
requires one to implement
FromWasmAbi
andOptionFromWasmAbi
for&MyStruct
, which are not very well documented, and requireunsafe
code and a good understanding ofwasm-bindgen
internals. It looks like something that can be derived automatically.Alternatively, one would think that the following would work:
This compiles, but leads to unexpected behavior on the JS side (I couldn't find a mention of it in the documentation, which, in fact, states that
Option<T>
parameters are not supported): the first invocation of the binding nullifies the pointer of the JS struct:Output:
The text was updated successfully, but these errors were encountered: