@@ -2,14 +2,14 @@ use bytes::Bytes;
22use prometheus:: IntGauge ;
33use spacetimedb_lib:: db:: raw_def:: v9:: Lifecycle ;
44use spacetimedb_schema:: auto_migrate:: ponder_migrate;
5- use spacetimedb_schema:: def:: ModuleDef ;
65use std:: sync:: Arc ;
76use std:: time:: Duration ;
87
98use super :: instrumentation:: CallTimes ;
109use crate :: database_logger:: { self , SystemLogger } ;
1110use crate :: energy:: { EnergyMonitor , EnergyQuanta , ReducerBudget , ReducerFingerprint } ;
1211use crate :: host:: instance_env:: InstanceEnv ;
12+ use crate :: host:: module_common:: { build_common_module_from_raw, ModuleCommon } ;
1313use crate :: host:: module_host:: {
1414 CallReducerParams , DatabaseUpdate , DynModule , EventStatus , Module , ModuleEvent , ModuleFunctionCall , ModuleInfo ,
1515 ModuleInstance ,
@@ -81,11 +81,8 @@ pub struct ExecuteResult<E> {
8181pub ( crate ) struct WasmModuleHostActor < T : WasmModule > {
8282 module : T :: InstancePre ,
8383 initial_instance : Option < Box < WasmModuleInstance < T :: Instance > > > ,
84- replica_context : Arc < ReplicaContext > ,
85- scheduler : Scheduler ,
84+ common : ModuleCommon ,
8685 func_names : Arc < FuncNames > ,
87- info : Arc < ModuleInfo > ,
88- energy_monitor : Arc < dyn EnergyMonitor > ,
8986}
9087
9188#[ derive( thiserror:: Error , Debug ) ]
@@ -126,56 +123,35 @@ pub enum DescribeError {
126123
127124impl < T : WasmModule > WasmModuleHostActor < T > {
128125 pub fn new ( mcc : ModuleCreationContext , module : T ) -> Result < Self , InitializationError > {
129- let ModuleCreationContext {
130- replica_ctx : replica_context,
131- scheduler,
132- program,
133- energy_monitor,
134- } = mcc;
135- let module_hash = program. hash ;
136126 log:: trace!(
137- "Making new module host actor for database {} with module {}" ,
138- replica_context . database_identity,
139- module_hash ,
127+ "Making new WASM module host actor for database {} with module {}" ,
128+ mcc . replica_ctx . database_identity,
129+ mcc . program . hash ,
140130 ) ;
141- let log_tx = replica_context. logger . tx . clone ( ) ;
142-
143- FuncNames :: check_required ( |name| module. get_export ( name) ) ?;
144- let mut func_names = FuncNames :: default ( ) ;
145- module. for_each_export ( |sym, ty| func_names. update_from_general ( sym, ty) ) ?;
146- func_names. preinits . sort_unstable ( ) ;
147131
132+ let func_names = {
133+ FuncNames :: check_required ( |name| module. get_export ( name) ) ?;
134+ let mut func_names = FuncNames :: default ( ) ;
135+ module. for_each_export ( |sym, ty| func_names. update_from_general ( sym, ty) ) ?;
136+ func_names. preinits . sort_unstable ( ) ;
137+ func_names
138+ } ;
148139 let uninit_instance = module. instantiate_pre ( ) ?;
149- let mut instance = uninit_instance. instantiate (
150- InstanceEnv :: new ( replica_context. clone ( ) , scheduler. clone ( ) ) ,
151- & func_names,
152- ) ?;
140+ let instance_env = InstanceEnv :: new ( mcc. replica_ctx . clone ( ) , mcc. scheduler . clone ( ) ) ;
141+ let mut instance = uninit_instance. instantiate ( instance_env, & func_names) ?;
153142
154143 let desc = instance. extract_descriptions ( ) ?;
155144 let desc: RawModuleDef = bsatn:: from_slice ( & desc) . map_err ( DescribeError :: Decode ) ?;
156145
157- // Perform a bunch of validation on the raw definition.
158- let def: ModuleDef = desc. try_into ( ) ?;
159-
160- // Note: assigns Reducer IDs based on the alphabetical order of reducer names.
161- let info = ModuleInfo :: new (
162- def,
163- replica_context. owner_identity ,
164- replica_context. database_identity ,
165- module_hash,
166- log_tx,
167- replica_context. subscriptions . clone ( ) ,
168- ) ;
146+ // Validate and create a common module rom the raw definition.
147+ let common = build_common_module_from_raw ( mcc, desc) ?;
169148
170149 let func_names = Arc :: new ( func_names) ;
171150 let mut module = WasmModuleHostActor {
172151 module : uninit_instance,
173152 initial_instance : None ,
174153 func_names,
175- info,
176- replica_context,
177- scheduler,
178- energy_monitor,
154+ common,
179155 } ;
180156 module. initial_instance = Some ( Box :: new ( module. make_from_instance ( instance) ) ) ;
181157
@@ -187,25 +163,25 @@ impl<T: WasmModule> WasmModuleHostActor<T> {
187163 fn make_from_instance ( & self , instance : T :: Instance ) -> WasmModuleInstance < T :: Instance > {
188164 WasmModuleInstance {
189165 instance,
190- info : self . info . clone ( ) ,
191- energy_monitor : self . energy_monitor . clone ( ) ,
166+ info : self . common . info ( ) ,
167+ energy_monitor : self . common . energy_monitor ( ) ,
192168 // will be updated on the first reducer call
193169 allocated_memory : 0 ,
194170 metric_wasm_memory_bytes : WORKER_METRICS
195171 . wasm_memory_bytes
196- . with_label_values ( & self . info . database_identity ) ,
172+ . with_label_values ( self . common . database_identity ( ) ) ,
197173 trapped : false ,
198174 }
199175 }
200176}
201177
202178impl < T : WasmModule > DynModule for WasmModuleHostActor < T > {
203179 fn replica_ctx ( & self ) -> & Arc < ReplicaContext > {
204- & self . replica_context
180+ self . common . replica_ctx ( )
205181 }
206182
207183 fn scheduler ( & self ) -> & Scheduler {
208- & self . scheduler
184+ self . common . scheduler ( )
209185 }
210186}
211187
@@ -219,11 +195,12 @@ impl<T: WasmModule> Module for WasmModuleHostActor<T> {
219195 }
220196
221197 fn info ( & self ) -> Arc < ModuleInfo > {
222- self . info . clone ( )
198+ self . common . info ( )
223199 }
224200
225201 fn create_instance ( & self ) -> Self :: Instance {
226- let env = InstanceEnv :: new ( self . replica_context . clone ( ) , self . scheduler . clone ( ) ) ;
202+ let common = & self . common ;
203+ let env = InstanceEnv :: new ( common. replica_ctx ( ) . clone ( ) , common. scheduler ( ) . clone ( ) ) ;
227204 // this shouldn't fail, since we already called module.create_instance()
228205 // before and it didn't error, and ideally they should be deterministic
229206 let mut instance = self
0 commit comments