@@ -58,8 +58,8 @@ impl StoredVirtualMachine {
58
58
setup_browser_module ( vm) ;
59
59
}
60
60
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 {
63
63
f ( vm)
64
64
}
65
65
} ) ;
@@ -78,7 +78,7 @@ impl StoredVirtualMachine {
78
78
/// Add a hook to add builtins or frozen modules to the RustPython VirtualMachine while it's
79
79
/// initializing.
80
80
pub fn add_init_func ( f : fn ( & mut VirtualMachine ) ) {
81
- VM_INIT_FUNCS . with ( |cell| cell . borrow_mut ( ) . push ( f) )
81
+ VM_INIT_FUNCS . with_borrow_mut ( |funcs| funcs . push ( f) )
82
82
}
83
83
84
84
// 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 {
97
97
. expect ( "VirtualMachine inside of WASM crate should have wasm_id set" )
98
98
}
99
99
pub ( 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 )
103
102
. expect ( "VirtualMachine is not valid" )
104
103
. clone ( )
105
104
} )
106
105
}
107
106
pub ( crate ) fn weak_vm ( vm : & VirtualMachine ) -> Weak < StoredVirtualMachine > {
108
107
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" ) ) )
111
109
}
112
110
113
111
#[ wasm_bindgen( js_name = vmStore) ]
@@ -116,8 +114,7 @@ pub struct VMStore;
116
114
#[ wasm_bindgen( js_class = vmStore) ]
117
115
impl VMStore {
118
116
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| {
121
118
if !vms. contains_key ( & id) {
122
119
let stored_vm =
123
120
StoredVirtualMachine :: new ( id. clone ( ) , inject_browser_module. unwrap_or ( true ) ) ;
@@ -128,14 +125,7 @@ impl VMStore {
128
125
}
129
126
130
127
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 } ) )
139
129
}
140
130
141
131
pub fn get ( id : String ) -> JsValue {
@@ -146,24 +136,19 @@ impl VMStore {
146
136
}
147
137
148
138
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
+ drop ( stored_vm) ;
161
146
}
162
147
} ) ;
163
148
}
164
149
165
150
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 ( ) )
167
152
}
168
153
}
169
154
@@ -179,10 +164,7 @@ impl WASMVirtualMachine {
179
164
where
180
165
F : FnOnce ( & StoredVirtualMachine ) -> R ,
181
166
{
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 ( ) ) ;
186
168
f ( & stored_vm)
187
169
}
188
170
@@ -202,7 +184,7 @@ impl WASMVirtualMachine {
202
184
}
203
185
204
186
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 ) )
206
188
}
207
189
208
190
pub ( crate ) fn push_held_rc (
0 commit comments