mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-12 20:41:24 +00:00
Handle JSON.stringify(undefined)
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
This commit is contained in:
@ -2626,7 +2626,11 @@ impl<'a> Context<'a> {
|
||||
|
||||
Intrinsic::JsonSerialize => {
|
||||
assert_eq!(args.len(), 1);
|
||||
format!("JSON.stringify({})", args[0])
|
||||
// Turns out `JSON.stringify(undefined) === undefined`, so if
|
||||
// we're passed `undefined` reinterpret it as `null` for JSON
|
||||
// purposes.
|
||||
prelude.push_str(&format!("const obj = {};\n", args[0]));
|
||||
"JSON.stringify(obj === undefined ? null : obj)".to_string()
|
||||
}
|
||||
|
||||
Intrinsic::AnyrefHeapLiveCount => {
|
||||
|
Reference in New Issue
Block a user