Skip to content

Commit 1fa55ab

Browse files
committed
Convert a PyJsValue into its JsValue
1 parent 0b3d1c4 commit 1fa55ab

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

wasm/lib/src/convert.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ pub fn py_to_js(vm: &VirtualMachine, py_obj: PyObjectRef) -> JsValue {
130130
let view = Uint8Array::view(&bytes);
131131
view.slice(0, bytes.len() as u32).into()
132132
}
133+
} else if let Some(jsval) = py_obj.payload::<PyJsValue>() {
134+
jsval.value().clone()
135+
} else if let Some(jsfunc) = py_obj.payload::<PyJsFunction>() {
136+
jsfunc.to_function().into()
133137
} else {
134138
match vm.serialize(&py_obj) {
135139
Ok(json) => js_sys::JSON::parse(&json).unwrap_or(JsValue::UNDEFINED),

wasm/lib/src/objjsvalue.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ impl PyJsFunction {
7474
pub fn new(func: Function, this: Option<JsValue>) -> PyJsFunction {
7575
PyJsFunction { func, this }
7676
}
77+
pub fn to_function(&self) -> Function {
78+
if let Some(this) = &self.this {
79+
self.func.bind(&this)
80+
} else {
81+
self.func.clone()
82+
}
83+
}
7784

7885
#[pymethod(name = "__call__")]
7986
fn call(&self, args: Args, vm: &VirtualMachine) -> PyResult {

0 commit comments

Comments
 (0)