-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
JsValue::into_serde raises an error when called with undefined #1778
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
Comments
It seems expected then that a call to I'm not sure if there is a way to disable this. |
No, this is not this kind of conversion. The result here is a Also, serialization failure does not throw in general, but only when undefined is passed. For example, when we add the code to log the result: > let pkg = require('./pkg');
undefined
> pkg.hello(42);
'Ok(42)'
> pkg.hello('yoyo');
'Err(Error("invalid type: string \\"yoyo\\", expected i32", line: 1, column: 6))'
> pkg.hello(null);
'Err(Error("invalid type: null, expected i32", line: 1, column: 4))'
> pkg.hello(undefined);
TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type undefined
at Function.byteLength (buffer.js:514:11)
at passStringToWasm (/home/matcegla/oopsie/pkg/oopsie.js:69:24)
at module.exports.__wbindgen_json_serialize (/home/matcegla/oopsie/pkg/oopsie.js:90:18)
at oopsie::hello::hbdd2e1f68530c309 (wasm-function[57]:26)
at hello (wasm-function[67]:16)
at Object.module.exports.hello (/home/matcegla/oopsie/pkg/oopsie.js:48:22)
> |
Turns out that `JSON.stringify(undefined)` doesn't actually return a string, it returns `undefined`! If we're requested to serialize `undefined` into JSON instead just interpret it as `null` which should have the expected semantics of serving as a placeholder for `None`. Closes rustwasm#1778
Thanks for the clear report! I've attempted to fix this in #1783 |
Turns out that `JSON.stringify(undefined)` doesn't actually return a string, it returns `undefined`! If we're requested to serialize `undefined` into JSON instead just interpret it as `null` which should have the expected semantics of serving as a placeholder for `None`. Closes #1778
Describe the Bug
When
JsValue::into_serde
is called on anundefined
value, wasm-bindgen raises a JavaScript exception instead of returning an Err value.Steps to Reproduce
Set up the following project:
Build and package with
wasm-pack build --target nodejs
, launchnode
and try to callhello
withundefined
:Expected Behavior
The
Err
variant is returned. If the target type was anOption<i32>
, the deserialization could also be successful.Actual Behavior
A JavaScript
TypeError
was thrown in the glue code:The text was updated successfully, but these errors were encountered: