@@ -5,7 +5,9 @@ use self::error::{
55} ;
66use self :: ser:: serialize_to_js;
77use self :: string:: { str_from_ident, IntoJsString } ;
8- use self :: syscall:: { call_call_reducer, call_describe_module, call_reducer_fun, resolve_sys_module, FnRet } ;
8+ use self :: syscall:: {
9+ call_call_reducer, call_describe_module, get_hook, resolve_sys_module, FnRet , HookFunction , ModuleHookKey ,
10+ } ;
911use super :: module_common:: { build_common_module_from_raw, run_describer, ModuleCommon } ;
1012use super :: module_host:: { CallProcedureParams , CallReducerParams , Module , ModuleInfo , ModuleRuntime } ;
1113use super :: UpdateDatabaseResult ;
@@ -18,6 +20,7 @@ use crate::host::{ReducerCallResult, Scheduler};
1820use crate :: module_host_context:: { ModuleCreationContext , ModuleCreationContextLimited } ;
1921use crate :: replica_context:: ReplicaContext ;
2022use crate :: util:: asyncify;
23+ use anyhow:: Context as _;
2124use core:: str;
2225use itertools:: Either ;
2326use spacetimedb_datastore:: locking_tx_datastore:: MutTxId ;
@@ -329,12 +332,13 @@ fn startup_instance_worker<'scope>(
329332 scope : & mut PinScope < ' scope , ' _ > ,
330333 program : Arc < str > ,
331334 module_or_mcc : Either < ModuleCommon , ModuleCreationContextLimited > ,
332- ) -> anyhow:: Result < ( Local < ' scope , Function > , Either < ModuleCommon , ModuleCommon > ) > {
335+ ) -> anyhow:: Result < ( HookFunction < ' scope > , Either < ModuleCommon , ModuleCommon > ) > {
333336 // Start-up the user's module.
334337 eval_user_module_catch ( scope, & program) . map_err ( DescribeError :: Setup ) ?;
335338
336339 // Find the `__call_reducer__` function.
337- let call_reducer_fun = catch_exception ( scope, |scope| Ok ( call_reducer_fun ( scope) ?) ) . map_err ( |( e, _) | e) ?;
340+ let call_reducer_fun =
341+ get_hook ( scope, ModuleHookKey :: CallReducer ) . context ( "The `spacetimedb/server` module was never imported" ) ?;
338342
339343 // If we don't have a module, make one.
340344 let module_common = match module_or_mcc {
@@ -578,7 +582,7 @@ fn call_reducer<'scope>(
578582 instance_common : & mut InstanceCommon ,
579583 replica_ctx : & ReplicaContext ,
580584 scope : & mut PinScope < ' scope , ' _ > ,
581- fun : Local < ' scope , Function > ,
585+ fun : HookFunction < ' _ > ,
582586 tx : Option < MutTxId > ,
583587 params : CallReducerParams ,
584588) -> ( super :: ReducerCallResult , bool ) {
@@ -655,26 +659,30 @@ fn extract_description<'scope>(
655659 |a, b, c| log_traceback ( replica_ctx, a, b, c) ,
656660 || {
657661 catch_exception ( scope, |scope| {
658- let def = call_describe_module ( scope) ?;
662+ let describe_module = get_hook ( scope, ModuleHookKey :: DescribeModule )
663+ . context ( "The `spacetimedb/server` package was never imported into the module" ) ?;
664+ let def = call_describe_module ( scope, describe_module) ?;
659665 Ok ( def)
660666 } )
661667 . map_err ( |( e, _) | e)
662668 . map_err ( Into :: into)
663669 } ,
664670 )
665671}
672+
666673#[ cfg( test) ]
667674mod test {
668675 use super :: to_value:: test:: with_scope;
669676 use super :: * ;
677+ use crate :: host:: v8:: error:: { ErrorOrException , ExceptionThrown } ;
670678 use crate :: host:: wasm_common:: module_host_actor:: ReducerOp ;
671679 use crate :: host:: ArgsTuple ;
672680 use spacetimedb_lib:: { ConnectionId , Identity } ;
673681 use spacetimedb_primitives:: ReducerId ;
674682
675683 fn with_module_catch < T > (
676684 code : & str ,
677- logic : impl for < ' scope > FnOnce ( & mut PinScope < ' scope , ' _ > ) -> ExcResult < T > ,
685+ logic : impl for < ' scope > FnOnce ( & mut PinScope < ' scope , ' _ > ) -> Result < T , ErrorOrException < ExceptionThrown > > ,
678686 ) -> anyhow:: Result < T > {
679687 with_scope ( |scope| {
680688 eval_user_module_catch ( scope, code) . unwrap ( ) ;
@@ -691,7 +699,7 @@ mod test {
691699 fn call_call_reducer_works ( ) {
692700 let call = |code| {
693701 with_module_catch ( code, |scope| {
694- let fun = call_reducer_fun ( scope) ? ;
702+ let fun = get_hook ( scope, ModuleHookKey :: CallReducer ) . unwrap ( ) ;
695703 let op = ReducerOp {
696704 id : ReducerId ( 42 ) ,
697705 name : "foobar" ,
@@ -700,7 +708,7 @@ mod test {
700708 timestamp : Timestamp :: from_micros_since_unix_epoch ( 24 ) ,
701709 args : & ArgsTuple :: nullary ( ) ,
702710 } ;
703- call_call_reducer ( scope, fun, op)
711+ Ok ( call_call_reducer ( scope, fun, op) ? )
704712 } )
705713 } ;
706714
@@ -769,7 +777,11 @@ js error Uncaught Error: foobar
769777 },
770778 })
771779 "# ;
772- let raw_mod = with_module_catch ( code, call_describe_module) . map_err ( |e| e. to_string ( ) ) ;
780+ let raw_mod = with_module_catch ( code, |scope| {
781+ let describe_module = get_hook ( scope, ModuleHookKey :: DescribeModule ) . unwrap ( ) ;
782+ call_describe_module ( scope, describe_module)
783+ } )
784+ . map_err ( |e| e. to_string ( ) ) ;
773785 assert_eq ! ( raw_mod, Ok ( RawModuleDef :: V9 ( <_>:: default ( ) ) ) ) ;
774786 }
775787}
0 commit comments