@@ -13,9 +13,16 @@ use std::collections::HashMap;
13
13
use std:: rc:: { Rc , Weak } ;
14
14
use wasm_bindgen:: prelude:: * ;
15
15
16
+ pub trait HeldRcInner { }
17
+
18
+ impl < T > HeldRcInner for T { }
19
+
16
20
pub ( crate ) struct StoredVirtualMachine {
17
21
pub vm : VirtualMachine ,
18
22
pub scope : ScopeRef ,
23
+ /// you can put a Rc in here, keep it as a Weak, and it'll be held only for
24
+ /// as long as the StoredVM is alive
25
+ held_rcs : Vec < Rc < dyn HeldRcInner > > ,
19
26
}
20
27
21
28
impl StoredVirtualMachine {
@@ -27,7 +34,11 @@ impl StoredVirtualMachine {
27
34
setup_browser_module ( & mut vm) ;
28
35
}
29
36
vm. wasm_id = Some ( id) ;
30
- StoredVirtualMachine { vm, scope }
37
+ StoredVirtualMachine {
38
+ vm,
39
+ scope,
40
+ held_rcs : vec ! [ ] ,
41
+ }
31
42
}
32
43
}
33
44
@@ -211,6 +222,17 @@ impl WASMVirtualMachine {
211
222
STORED_VMS . with ( |cell| cell. borrow ( ) . contains_key ( & self . id ) )
212
223
}
213
224
225
+ pub ( crate ) fn push_held_rc < T : HeldRcInner + ' static > (
226
+ & self ,
227
+ rc : Rc < T > ,
228
+ ) -> Result < Weak < T > , JsValue > {
229
+ self . with ( |stored_vm| {
230
+ let weak = Rc :: downgrade ( & rc) ;
231
+ stored_vm. held_rcs . push ( rc) ;
232
+ weak
233
+ } )
234
+ }
235
+
214
236
pub fn assert_valid ( & self ) -> Result < ( ) , JsValue > {
215
237
if self . valid ( ) {
216
238
Ok ( ( ) )
@@ -234,6 +256,7 @@ impl WASMVirtualMachine {
234
256
move |StoredVirtualMachine {
235
257
ref mut vm,
236
258
ref mut scope,
259
+ ..
237
260
} | {
238
261
let value = convert:: js_to_py ( vm, value) ;
239
262
scope. locals . set_item ( & vm. ctx , & name, value) ;
@@ -247,6 +270,7 @@ impl WASMVirtualMachine {
247
270
move |StoredVirtualMachine {
248
271
ref mut vm,
249
272
ref mut scope,
273
+ ..
250
274
} | {
251
275
let print_fn: Box < Fn ( & mut VirtualMachine , PyFuncArgs ) -> PyResult > =
252
276
if let Some ( selector) = stdout. as_string ( ) {
@@ -315,6 +339,7 @@ impl WASMVirtualMachine {
315
339
|StoredVirtualMachine {
316
340
ref mut vm,
317
341
ref mut scope,
342
+ ..
318
343
} | {
319
344
source. push ( '\n' ) ;
320
345
let code =
0 commit comments