@@ -2,14 +2,14 @@ use bytes::Bytes;
2
2
use prometheus:: IntGauge ;
3
3
use spacetimedb_lib:: db:: raw_def:: v9:: Lifecycle ;
4
4
use spacetimedb_schema:: auto_migrate:: ponder_migrate;
5
- use spacetimedb_schema:: def:: ModuleDef ;
6
5
use std:: sync:: Arc ;
7
6
use std:: time:: Duration ;
8
7
9
8
use super :: instrumentation:: CallTimes ;
10
9
use crate :: database_logger:: { self , SystemLogger } ;
11
10
use crate :: energy:: { EnergyMonitor , EnergyQuanta , ReducerBudget , ReducerFingerprint } ;
12
11
use crate :: host:: instance_env:: InstanceEnv ;
12
+ use crate :: host:: module_common:: { build_common_module_from_raw, ModuleCommon } ;
13
13
use crate :: host:: module_host:: {
14
14
CallReducerParams , DatabaseUpdate , DynModule , EventStatus , Module , ModuleEvent , ModuleFunctionCall , ModuleInfo ,
15
15
ModuleInstance ,
@@ -81,11 +81,8 @@ pub struct ExecuteResult<E> {
81
81
pub ( crate ) struct WasmModuleHostActor < T : WasmModule > {
82
82
module : T :: InstancePre ,
83
83
initial_instance : Option < Box < WasmModuleInstance < T :: Instance > > > ,
84
- replica_context : Arc < ReplicaContext > ,
85
- scheduler : Scheduler ,
84
+ common : ModuleCommon ,
86
85
func_names : Arc < FuncNames > ,
87
- info : Arc < ModuleInfo > ,
88
- energy_monitor : Arc < dyn EnergyMonitor > ,
89
86
}
90
87
91
88
#[ derive( thiserror:: Error , Debug ) ]
@@ -126,56 +123,35 @@ pub enum DescribeError {
126
123
127
124
impl < T : WasmModule > WasmModuleHostActor < T > {
128
125
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 ;
136
126
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 ,
140
130
) ;
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 ( ) ;
147
131
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
+ } ;
148
139
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) ?;
153
142
154
143
let desc = instance. extract_descriptions ( ) ?;
155
144
let desc: RawModuleDef = bsatn:: from_slice ( & desc) . map_err ( DescribeError :: Decode ) ?;
156
145
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) ?;
169
148
170
149
let func_names = Arc :: new ( func_names) ;
171
150
let mut module = WasmModuleHostActor {
172
151
module : uninit_instance,
173
152
initial_instance : None ,
174
153
func_names,
175
- info,
176
- replica_context,
177
- scheduler,
178
- energy_monitor,
154
+ common,
179
155
} ;
180
156
module. initial_instance = Some ( Box :: new ( module. make_from_instance ( instance) ) ) ;
181
157
@@ -187,25 +163,25 @@ impl<T: WasmModule> WasmModuleHostActor<T> {
187
163
fn make_from_instance ( & self , instance : T :: Instance ) -> WasmModuleInstance < T :: Instance > {
188
164
WasmModuleInstance {
189
165
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 ( ) ,
192
168
// will be updated on the first reducer call
193
169
allocated_memory : 0 ,
194
170
metric_wasm_memory_bytes : WORKER_METRICS
195
171
. wasm_memory_bytes
196
- . with_label_values ( & self . info . database_identity ) ,
172
+ . with_label_values ( self . common . database_identity ( ) ) ,
197
173
trapped : false ,
198
174
}
199
175
}
200
176
}
201
177
202
178
impl < T : WasmModule > DynModule for WasmModuleHostActor < T > {
203
179
fn replica_ctx ( & self ) -> & Arc < ReplicaContext > {
204
- & self . replica_context
180
+ self . common . replica_ctx ( )
205
181
}
206
182
207
183
fn scheduler ( & self ) -> & Scheduler {
208
- & self . scheduler
184
+ self . common . scheduler ( )
209
185
}
210
186
}
211
187
@@ -219,11 +195,12 @@ impl<T: WasmModule> Module for WasmModuleHostActor<T> {
219
195
}
220
196
221
197
fn info ( & self ) -> Arc < ModuleInfo > {
222
- self . info . clone ( )
198
+ self . common . info ( )
223
199
}
224
200
225
201
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 ( ) ) ;
227
204
// this shouldn't fail, since we already called module.create_instance()
228
205
// before and it didn't error, and ideally they should be deterministic
229
206
let mut instance = self
0 commit comments