Skip to content

Commit 9ac854a

Browse files
committed
Use serde_wasm_bindgen for wasm ser/de
1 parent a8d8019 commit 9ac854a

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

Cargo.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/src/stdlib/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod binascii;
33
mod dis;
44
mod imp;
55
mod itertools;
6-
pub(crate) mod json;
6+
mod json;
77
mod keyword;
88
mod marshal;
99
mod math;

vm/src/vm.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -718,14 +718,6 @@ impl VirtualMachine {
718718
})
719719
}
720720

721-
pub fn serialize(&self, obj: PyObjectRef) -> PyResult<String> {
722-
crate::stdlib::json::json_dumps(obj, self)
723-
}
724-
725-
pub fn deserialize(&self, s: &str) -> PyResult {
726-
crate::stdlib::json::json_loads(PyString::from(s).into_ref(self), self)
727-
}
728-
729721
pub fn is_callable(&self, obj: &PyObjectRef) -> bool {
730722
match_class!(obj,
731723
PyFunction => true,

wasm/lib/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ rustpython_vm = { path = "../../vm" }
1616
cfg-if = "0.1.2"
1717
wasm-bindgen = "0.2"
1818
wasm-bindgen-futures = "0.3"
19+
serde-wasm-bindgen = "0.1"
20+
serde = "1.0"
1921
js-sys = "0.3"
2022
futures = "0.1"
2123
num-traits = "0.2"

wasm/lib/src/convert.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use js_sys::{Array, ArrayBuffer, Object, Promise, Reflect, Uint8Array};
22
use num_traits::cast::ToPrimitive;
3+
use serde::{de::DeserializeSeed, ser::Serialize};
4+
use serde_wasm_bindgen;
35
use wasm_bindgen::{closure::Closure, prelude::*, JsCast};
46

57
use rustpython_vm::function::PyFuncArgs;
@@ -130,10 +132,9 @@ pub fn py_to_js(vm: &VirtualMachine, py_obj: PyObjectRef) -> JsValue {
130132
view.slice(0, bytes.len() as u32).into()
131133
}
132134
} else {
133-
match vm.serialize(py_obj) {
134-
Ok(json) => js_sys::JSON::parse(&json).unwrap_or(JsValue::UNDEFINED),
135-
Err(_) => JsValue::UNDEFINED,
136-
}
135+
rustpython_vm::ser_de::PyObjectSerializer::new(vm, &py_obj)
136+
.serialize(&serde_wasm_bindgen::Serializer::new())
137+
.unwrap_or(JsValue::UNDEFINED)
137138
}
138139
}
139140

@@ -224,10 +225,8 @@ pub fn js_to_py(vm: &VirtualMachine, js_val: JsValue) -> PyObjectRef {
224225
// Because `JSON.stringify(undefined)` returns undefined
225226
vm.get_none()
226227
} else {
227-
let json = match js_sys::JSON::stringify(&js_val) {
228-
Ok(json) => String::from(json),
229-
Err(_) => return vm.get_none(),
230-
};
231-
vm.deserialize(&json).unwrap_or_else(|_| vm.get_none())
228+
rustpython_vm::ser_de::PyObjectDeserializer::new(vm)
229+
.deserialize(serde_wasm_bindgen::Deserializer::from(js_val))
230+
.unwrap_or_else(|_| vm.get_none())
232231
}
233232
}

0 commit comments

Comments
 (0)