1
1
use crate :: error:: Error ;
2
2
3
- use std:: borrow:: Borrow ;
3
+ use std:: borrow:: { Borrow , BorrowMut } ;
4
4
use std:: ptr:: NonNull ;
5
5
use std:: sync:: { Arc , RwLock } ;
6
6
use wasmer:: { Instance , Memory , WasmerEnv } ;
76
76
77
77
/// Creates a back reference from a contact to its partent instance
78
78
pub fn set_wasmer_instance ( & self , instance : Option < NonNull < Instance > > ) {
79
- let mut data = self . data . as_ref ( ) . write ( ) . unwrap ( ) ;
80
- data. wasmer_instance = instance;
79
+ self . with_context_data_mut ( |data| {
80
+ data. wasmer_instance = instance;
81
+ } )
81
82
}
82
83
83
84
pub fn with_wasmer_instance < C , R > ( & self , callback : C ) -> Result < R , Error >
@@ -102,14 +103,14 @@ where
102
103
callback ( context_data)
103
104
}
104
105
105
- // fn with_context_data_mut<C, R>(&self, callback: C) -> R
106
- // where
107
- // C: FnOnce(&mut ContextData<Q>) -> R,
108
- // {
109
- // let mut guard = self.data.as_ref().write().unwrap();
110
- // let context_data = guard.borrow_mut();
111
- // callback(context_data)
112
- // }
106
+ fn with_context_data_mut < C , R > ( & self , callback : C ) -> R
107
+ where
108
+ C : FnOnce ( & mut ContextData < Q > ) -> R ,
109
+ {
110
+ let mut guard = self . data . as_ref ( ) . write ( ) . unwrap ( ) ;
111
+ let context_data = guard. borrow_mut ( ) ;
112
+ callback ( context_data)
113
+ }
113
114
114
115
pub fn get_gas_left ( & self ) -> u64 {
115
116
self . with_wasmer_instance ( |instance| {
@@ -140,8 +141,7 @@ where
140
141
}
141
142
142
143
pub fn memory ( & self ) -> Result < Memory , Error > {
143
- let data = self . data . as_ref ( ) . read ( ) . unwrap ( ) ;
144
- match data. wasmer_instance {
144
+ self . with_context_data ( |data| match data. wasmer_instance {
145
145
Some ( instance_ptr) => {
146
146
let instance_ref = unsafe { instance_ptr. as_ref ( ) } ;
147
147
let mut memories: Vec < Memory > =
@@ -153,7 +153,7 @@ where
153
153
}
154
154
}
155
155
_ => Err ( Error :: BadMemorySectionError ) ,
156
- }
156
+ } )
157
157
}
158
158
}
159
159
0 commit comments