@@ -28,7 +28,10 @@ use crate::{
28
28
runtime_extensions:: {
29
29
bindings:: { self , unchecked_exports, LinkOptions } ,
30
30
hermes:: init:: ComponentInstanceExt as _,
31
- init:: trait_module:: { RteInitModule , RteModule } ,
31
+ init:: {
32
+ trait_event:: { RteEvent , RteInitEvent } ,
33
+ trait_module:: { RteInitModule , RteModule } ,
34
+ } ,
32
35
new_context,
33
36
} ,
34
37
vfs:: Vfs ,
@@ -83,7 +86,13 @@ pub struct Module {
83
86
id : ModuleId ,
84
87
85
88
/// Module's execution counter
89
+ /// CAN NEVER be used outside this structs methods.
90
+ /// NEVER add a getter for it.
91
+ /// ONLY ever use `fetch_add` never read its value otherwise.
86
92
exc_counter : AtomicU32 ,
93
+
94
+ /// The name of the application which owns this module.
95
+ app_name : ApplicationName ,
87
96
}
88
97
89
98
impl Module {
@@ -123,22 +132,33 @@ impl Module {
123
132
engine,
124
133
id,
125
134
exc_counter : AtomicU32 :: new ( 0 ) ,
135
+ app_name : app_name. clone ( ) ,
126
136
} )
127
137
}
128
138
139
+ /// Make a new context, deliberately private. Do not make public.
140
+ fn new_context (
141
+ & self ,
142
+ event_name : & str ,
143
+ vfs : Arc < Vfs > ,
144
+ ) -> HermesRuntimeContext {
145
+ HermesRuntimeContext :: new (
146
+ self . app_name . clone ( ) ,
147
+ self . id . clone ( ) ,
148
+ event_name. to_string ( ) ,
149
+ // **MUST** be the only place exc_counter is read and updated.
150
+ // NEVER read it anywhere else, and never update it anywhere else.
151
+ self . exc_counter . fetch_add ( 1 , Ordering :: SeqCst ) ,
152
+ vfs,
153
+ )
154
+ }
155
+
129
156
/// Initializes the WASM module by calling its `init` function.
130
157
pub ( crate ) fn init (
131
158
& self ,
132
- app_name : ApplicationName ,
133
159
vfs : Arc < Vfs > ,
134
160
) -> anyhow:: Result < ( ) > {
135
- let runtime_ctx = HermesRuntimeContext :: new (
136
- app_name,
137
- self . id . clone ( ) ,
138
- "init_function_call" . to_string ( ) ,
139
- 0 ,
140
- vfs,
141
- ) ;
161
+ let runtime_ctx = self . new_context ( "init_function_call" , vfs) ;
142
162
143
163
new_context ( & runtime_ctx) ;
144
164
@@ -180,15 +200,6 @@ impl Module {
180
200
& self . id
181
201
}
182
202
183
- /// Get the module's execution counter
184
- pub ( crate ) fn exec_counter ( & self ) -> u32 {
185
- // Using the highest memory ordering constraint.
186
- // It provides a highest consistency guarantee and in some cases could decrease
187
- // performance.
188
- // We could revise ordering approach for this case in future.
189
- self . exc_counter . load ( Ordering :: SeqCst )
190
- }
191
-
192
203
/// Executes a Hermes event by calling some WASM function.
193
204
/// This function abstraction over actual execution of the WASM function,
194
205
/// actual definition is inside `HermesEventPayload` trait implementation.
@@ -201,9 +212,21 @@ impl Module {
201
212
pub ( crate ) fn execute_event (
202
213
& self ,
203
214
event : & dyn HermesEventPayload ,
204
- state : HermesRuntimeContext ,
215
+ vfs : Arc < Vfs > ,
205
216
) -> anyhow:: Result < ( ) > {
206
- let mut store = WasmStore :: new ( & self . engine , state) ;
217
+ let runtime_ctx = self . new_context ( event. event_name ( ) , vfs) ;
218
+
219
+ // Advise Runtime Extensions of a new context
220
+ // TODO: Better handle errors.
221
+ if let Err ( err) = RteEvent :: new ( ) . init ( & runtime_ctx) {
222
+ tracing:: error!( "module event initialization failed: {err}" ) ;
223
+ return Err ( err. into ( ) ) ;
224
+ }
225
+
226
+ // TODO: (SJ) Remove when all RTE's are migrated.
227
+ new_context ( & runtime_ctx) ;
228
+
229
+ let mut store = WasmStore :: new ( & self . engine , runtime_ctx. clone ( ) ) ;
207
230
let instance = self
208
231
. pre_instance
209
232
. clone ( )
@@ -212,11 +235,12 @@ impl Module {
212
235
213
236
event. execute ( & mut ModuleInstance { store, instance } ) ?;
214
237
215
- // Using the highest memory ordering constraint.
216
- // It provides a highest consistency guarantee and in some cases could decrease
217
- // performance.
218
- // We could revise ordering approach for this case in future.
219
- self . exc_counter . fetch_add ( 1 , Ordering :: SeqCst ) ;
238
+ // Advise Runtime Extensions that context can be cleaned up.
239
+ if let Err ( err) = RteEvent :: new ( ) . fini ( & runtime_ctx) {
240
+ //TODO(SJ): Maybe need better error handling...
241
+ tracing:: error!( "module event finalization failed: {err}" ) ;
242
+ }
243
+
220
244
Ok ( ( ) )
221
245
}
222
246
}
@@ -247,7 +271,7 @@ pub mod bench {
247
271
) ;
248
272
249
273
b. iter ( || {
250
- module. init ( app_name . clone ( ) , vfs. clone ( ) ) . unwrap ( ) ;
274
+ module. init ( vfs. clone ( ) ) . unwrap ( ) ;
251
275
} ) ;
252
276
}
253
277
0 commit comments