33use super :: module_common:: { build_common_module_from_raw, ModuleCommon } ;
44use super :: module_host:: { CallReducerParams , DynModule , Module , ModuleInfo , ModuleInstance , ModuleRuntime } ;
55use super :: UpdateDatabaseResult ;
6- use crate :: host:: wasm_common:: module_host_actor:: InstanceCommon ;
6+ use crate :: host:: wasm_common:: instrumentation:: CallTimes ;
7+ use crate :: host:: wasm_common:: module_host_actor:: {
8+ EnergyStats , ExecuteResult , ExecutionTimings , InstanceCommon , ReducerOp ,
9+ } ;
710use crate :: host:: ArgsTuple ;
811use crate :: { host:: Scheduler , module_host_context:: ModuleCreationContext , replica_context:: ReplicaContext } ;
912use anyhow:: anyhow;
13+ use core:: time:: Duration ;
1014use de:: deserialize_js;
1115use error:: { catch_exception, exception_already_thrown, ExcResult , Throwable } ;
1216use from_value:: cast;
1317use key_cache:: get_or_create_key_cache;
1418use ser:: serialize_to_js;
19+ use spacetimedb_client_api_messages:: energy:: { EnergyQuanta , ReducerBudget } ;
1520use spacetimedb_datastore:: locking_tx_datastore:: MutTxId ;
1621use spacetimedb_datastore:: traits:: Program ;
1722use spacetimedb_lib:: { ConnectionId , Identity , RawModuleDef } ;
1823use std:: sync:: { Arc , LazyLock } ;
19- use v8:: { Function , HandleScope , Local , Value } ;
24+ use v8:: { Context , ContextOptions , ContextScope , Function , HandleScope , Isolate , Local , Value } ;
2025
2126mod de;
2227mod error;
@@ -110,7 +115,7 @@ impl Module for JsModule {
110115 }
111116
112117 fn info ( & self ) -> Arc < ModuleInfo > {
113- todo ! ( )
118+ self . common . info ( ) . clone ( )
114119 }
115120
116121 fn create_instance ( & self ) -> Self :: Instance {
@@ -137,8 +142,42 @@ impl ModuleInstance for JsInstance {
137142 self . common . update_database ( replica_ctx, program, old_module_info)
138143 }
139144
140- fn call_reducer ( & mut self , _tx : Option < MutTxId > , _params : CallReducerParams ) -> super :: ReducerCallResult {
141- todo ! ( )
145+ fn call_reducer ( & mut self , tx : Option < MutTxId > , params : CallReducerParams ) -> super :: ReducerCallResult {
146+ // TODO(centril): snapshots, module->host calls
147+ let mut isolate = Isolate :: new ( <_ >:: default ( ) ) ;
148+ let scope = & mut HandleScope :: new ( & mut isolate) ;
149+ let context = Context :: new ( scope, ContextOptions :: default ( ) ) ;
150+ let scope = & mut ContextScope :: new ( scope, context) ;
151+
152+ self . common . call_reducer_with_tx (
153+ & self . replica_ctx . clone ( ) ,
154+ tx,
155+ params,
156+ // TODO(centril): logging.
157+ |_ty, _fun, _err| { } ,
158+ |tx, op, _budget| {
159+ let call_result = call_call_reducer_from_op ( scope, op) ;
160+ // TODO(centril): energy metrering.
161+ let energy = EnergyStats {
162+ used : EnergyQuanta :: ZERO ,
163+ wasmtime_fuel_used : 0 ,
164+ remaining : ReducerBudget :: ZERO ,
165+ } ;
166+ // TODO(centril): timings.
167+ let timings = ExecutionTimings {
168+ total_duration : Duration :: ZERO ,
169+ wasm_instance_env_call_times : CallTimes :: new ( ) ,
170+ } ;
171+ let exec_result = ExecuteResult {
172+ energy,
173+ timings,
174+ // TODO(centril): memory allocation.
175+ memory_allocation : 0 ,
176+ call_result,
177+ } ;
178+ ( tx, exec_result)
179+ } ,
180+ )
142181 }
143182}
144183
@@ -163,13 +202,25 @@ fn call_free_fun<'scope>(
163202 fun. call ( scope, receiver, args) . ok_or_else ( exception_already_thrown)
164203}
165204
205+ // Calls the `__call_reducer__` function on the global proxy object using `op`.
206+ fn call_call_reducer_from_op ( scope : & mut HandleScope < ' _ > , op : ReducerOp < ' _ > ) -> anyhow:: Result < Result < ( ) , Box < str > > > {
207+ call_call_reducer (
208+ scope,
209+ op. id . into ( ) ,
210+ op. caller_identity ,
211+ op. caller_connection_id ,
212+ op. timestamp . to_micros_since_unix_epoch ( ) ,
213+ op. args ,
214+ )
215+ }
216+
166217// Calls the `__call_reducer__` function on the global proxy object.
167218fn call_call_reducer (
168219 scope : & mut HandleScope < ' _ > ,
169220 reducer_id : u32 ,
170221 sender : & Identity ,
171222 conn_id : & ConnectionId ,
172- timestamp : u64 ,
223+ timestamp : i64 ,
173224 reducer_args : & ArgsTuple ,
174225) -> anyhow:: Result < Result < ( ) , Box < str > > > {
175226 // Get a cached version of the `__call_reducer__` property.
0 commit comments