Skip to content

Commit e0f222c

Browse files
committed
Don't hold on to a PyObjectRef from a python -> js closure
1 parent 09e2a7a commit e0f222c

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

wasm/lib/src/convert.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ pub fn py_to_js(vm: &mut VirtualMachine, py_obj: PyObjectRef) -> JsValue {
2121
let wasm_vm = WASMVirtualMachine {
2222
id: wasm_id.clone(),
2323
};
24+
let mut py_obj = Some(py_obj);
2425
let closure =
2526
move |args: Option<Array>, kwargs: Option<Object>| -> Result<JsValue, JsValue> {
26-
let py_obj = py_obj.clone();
27-
wasm_vm.assert_valid()?;
27+
let py_obj = match wasm_vm.assert_valid() {
28+
Ok(_) => py_obj.clone().expect("py_obj to be valid if VM is valid"),
29+
Err(err) => {
30+
py_obj = None;
31+
return Err(err);
32+
}
33+
};
2834
let acc_vm = AccessibleVM::from(wasm_vm.clone());
2935
let vm = &mut acc_vm
3036
.upgrade()
@@ -47,7 +53,7 @@ pub fn py_to_js(vm: &mut VirtualMachine, py_obj: PyObjectRef) -> JsValue {
4753
pyresult_to_jsresult(vm, result)
4854
};
4955
let closure = Closure::wrap(Box::new(closure)
50-
as Box<dyn Fn(Option<Array>, Option<Object>) -> Result<JsValue, JsValue>>);
56+
as Box<dyn FnMut(Option<Array>, Option<Object>) -> Result<JsValue, JsValue>>);
5157
let func = closure.as_ref().clone();
5258

5359
// TODO: Come up with a way of managing closure handles

0 commit comments

Comments
 (0)