@@ -16,7 +16,7 @@ use crate::browser_module;
16
16
use crate :: vm_class:: { stored_vm_from_wasm, WASMVirtualMachine } ;
17
17
18
18
// Currently WASM do not support multithreading. We should change this once it is enabled.
19
- thread_local ! ( static JS_FUNC : RefCell <Option <js_sys:: Function >> = RefCell :: new( None ) ) ;
19
+ thread_local ! ( static JS_FUNCS : RefCell <Vec <js_sys:: Function >> = RefCell :: new( vec! [ ] ) ) ;
20
20
21
21
#[ wasm_bindgen( inline_js = r"
22
22
export class PyError extends Error {
@@ -154,25 +154,6 @@ pub fn pyresult_to_jsresult(vm: &VirtualMachine, result: PyResult) -> Result<JsV
154
154
. map_err ( |err| py_err_to_js_err ( vm, & err) )
155
155
}
156
156
157
- fn js_func_to_py ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
158
- JS_FUNC . with ( |func| {
159
- let this = Object :: new ( ) ;
160
- for ( k, v) in args. kwargs {
161
- Reflect :: set ( & this, & k. into ( ) , & py_to_js ( vm, v) ) . expect ( "property to be settable" ) ;
162
- }
163
- let js_args = Array :: new ( ) ;
164
- for v in args. args {
165
- js_args. push ( & py_to_js ( vm, v) ) ;
166
- }
167
- func. borrow ( )
168
- . as_ref ( )
169
- . unwrap ( )
170
- . apply ( & this, & js_args)
171
- . map ( |val| js_to_py ( vm, val) )
172
- . map_err ( |err| js_err_to_py_err ( vm, & err) )
173
- } )
174
- }
175
-
176
157
pub fn js_to_py ( vm : & VirtualMachine , js_val : JsValue ) -> PyObjectRef {
177
158
if js_val. is_object ( ) {
178
159
if let Some ( promise) = js_val. dyn_ref :: < Promise > ( ) {
@@ -215,8 +196,32 @@ pub fn js_to_py(vm: &VirtualMachine, js_val: JsValue) -> PyObjectRef {
215
196
}
216
197
} else if js_val. is_function ( ) {
217
198
let func = js_sys:: Function :: from ( js_val) ;
218
- JS_FUNC . with ( |thread_func| thread_func. replace ( Some ( func. clone ( ) ) ) ) ;
219
- vm. ctx . new_method ( js_func_to_py)
199
+ let idx = JS_FUNCS . with ( |funcs| {
200
+ let mut funcs = funcs. borrow_mut ( ) ;
201
+ funcs. push ( func) ;
202
+ funcs. len ( ) - 1
203
+ } ) ;
204
+ vm. ctx
205
+ . new_method ( move |vm : & VirtualMachine , args : PyFuncArgs | -> PyResult {
206
+ JS_FUNCS . with ( |funcs| {
207
+ let this = Object :: new ( ) ;
208
+ for ( k, v) in args. kwargs {
209
+ Reflect :: set ( & this, & k. into ( ) , & py_to_js ( vm, v) )
210
+ . expect ( "property to be settable" ) ;
211
+ }
212
+ let js_args = Array :: new ( ) ;
213
+ for v in args. args {
214
+ js_args. push ( & py_to_js ( vm, v) ) ;
215
+ }
216
+ funcs
217
+ . borrow ( )
218
+ . get ( idx)
219
+ . unwrap ( )
220
+ . apply ( & this, & js_args)
221
+ . map ( |val| js_to_py ( vm, val) )
222
+ . map_err ( |err| js_err_to_py_err ( vm, & err) )
223
+ } )
224
+ } )
220
225
} else if let Some ( err) = js_val. dyn_ref :: < js_sys:: Error > ( ) {
221
226
js_err_to_py_err ( vm, err) . into_object ( )
222
227
} else if js_val. is_undefined ( ) {
0 commit comments