@@ -58,8 +58,8 @@ impl StoredVirtualMachine {
5858 setup_browser_module ( vm) ;
5959 }
6060
61- VM_INIT_FUNCS . with ( |cell | {
62- for f in cell . borrow ( ) . iter ( ) {
61+ VM_INIT_FUNCS . with_borrow ( |funcs | {
62+ for f in funcs {
6363 f ( vm)
6464 }
6565 } ) ;
@@ -78,7 +78,7 @@ impl StoredVirtualMachine {
7878/// Add a hook to add builtins or frozen modules to the RustPython VirtualMachine while it's
7979/// initializing.
8080pub fn add_init_func ( f : fn ( & mut VirtualMachine ) ) {
81- VM_INIT_FUNCS . with ( |cell| cell . borrow_mut ( ) . push ( f) )
A822
div>
81+ VM_INIT_FUNCS . with_borrow_mut ( |funcs| funcs . push ( f) )
8282}
8383
8484// It's fine that it's thread local, since WASM doesn't even have threads yet. thread_local!
@@ -97,17 +97,15 @@ pub fn get_vm_id(vm: &VirtualMachine) -> &str {
9797 . expect ( "VirtualMachine inside of WASM crate should have wasm_id set" )
9898}
9999pub ( crate ) fn stored_vm_from_wasm ( wasm_vm : & WASMVirtualMachine ) -> Rc < StoredVirtualMachine > {
100- STORED_VMS . with ( |cell| {
101- cell. borrow ( )
102- . get ( & wasm_vm. id )
100+ STORED_VMS . with_borrow ( |vms| {
101+ vms. get ( & wasm_vm. id )
103102 . expect ( "VirtualMachine is not valid" )
104103 . clone ( )
105104 } )
106105}
107106pub ( crate ) fn weak_vm ( vm : & VirtualMachine ) -> Weak < StoredVirtualMachine > {
108107 let id = get_vm_id ( vm) ;
109- STORED_VMS
110- . with ( |cell| Rc :: downgrade ( cell. borrow ( ) . get ( id) . expect ( "VirtualMachine is not valid" ) ) )
108+ STORED_VMS . with_borrow ( |vms| Rc :: downgrade ( vms. get ( id) . expect ( "VirtualMachine is not valid" ) ) )
111109}
112110
113111#[ wasm_bindgen( js_name = vmStore) ]
@@ -116,8 +114,7 @@ pub struct VMStore;
116114#[ wasm_bindgen( js_class = vmStore) ]
117115impl VMStore {
118116 pub fn init ( id : String , inject_browser_module : Option < bool > ) -> WASMVirtualMachine {
119- STORED_VMS . with ( |cell| {
120- let mut vms = cell. borrow_mut ( ) ;
117+ STORED_VMS . with_borrow_mut ( |vms| {
121118 if !vms. contains_key ( & id) {
122119 let stored_vm =
123120 StoredVirtualMachine :: new ( id. clone ( ) , inject_browser_module. unwrap_or ( true ) ) ;
@@ -128,14 +125,7 @@ impl VMStore {
128125 }
129126
130127 pub ( crate ) fn _get ( id : String ) -> Option < WASMVirtualMachine > {
131- STORED_VMS . with ( |cell| {
132- let vms = cell. borrow ( ) ;
133- if vms. contains_key ( & id) {
134- Some ( WASMVirtualMachine { id } )
135- } else {
136- None
137- }
138- } )
128+ STORED_VMS . with_borrow ( |vms| vms. contains_key ( & id) . then_some ( WASMVirtualMachine { id } ) )
139129 }
140130
141131 pub fn get ( id : String ) -> JsValue {
@@ -146,24 +136,19 @@ impl VMStore {
146136 }
147137
148138 pub fn destroy ( id : String ) {
149- STORED_VMS . with ( |cell| {
150- use std:: collections:: hash_map:: Entry ;
151- match cell. borrow_mut ( ) . entry ( id) {
152- Entry :: Occupied ( o) => {
153- let ( _k, stored_vm) = o. remove_entry ( ) ;
154- // for f in stored_vm.drop_handlers.iter() {
155- // f();
156- // }
157- // deallocate the VM
158- drop ( stored_vm) ;
159- }
160- Entry :: Vacant ( _v) => { }
139+ STORED_VMS . with_borrow_mut ( |vms| {
140+ if let Some ( stored_vm) = vms. remove ( & id) {
141+ // for f in stored_vm.drop_handlers.iter() {
142+ // f();
143+ // }
144+ // deallocate the VM
145+
639E
drop ( stored_vm) ;
161146 }
162147 } ) ;
163148 }
164149
165150 pub fn ids ( ) -> Vec < JsValue > {
166- STORED_VMS . with ( |cell| cell . borrow ( ) . keys ( ) . map ( |k| k. into ( ) ) . collect ( ) )
151+ STORED_VMS . with_borrow ( |vms| vms . keys ( ) . map ( |k| k. into ( ) ) . collect ( ) )
167152 }
168153}
169154
@@ -179,10 +164,7 @@ impl WASMVirtualMachine {
179164 where
180165 F : FnOnce ( & StoredVirtualMachine ) -> R ,
181166 {
182- let stored_vm = STORED_VMS . with ( |cell| {
183- let mut vms = cell. borrow_mut ( ) ;
184- vms. get_mut ( & self . id ) . unwrap ( ) . clone ( )
185- } ) ;
167+ let stored_vm = STORED_VMS . with_borrow_mut ( |vms| vms. get_mut ( & self . id ) . unwrap ( ) . clone ( ) ) ;
186168 f ( & stored_vm)
187169 }
188170
@@ -202,7 +184,7 @@ impl WASMVirtualMachine {
202184 }
203185
204186 pub fn valid ( & self ) -> bool {
205- STORED_VMS . with ( |cell| cell . borrow ( ) . contains_key ( & self . id ) )
187+ STORED_VMS . with_borrow ( |vms| vms . contains_key ( & self . id ) )
206188 }
207189
208190 pub ( crate ) fn push_held_rc (
0 commit comments