@@ -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 , ModuleHook ,
10+ } ;
911use super :: module_common:: { build_common_module_from_raw, run_describer, ModuleCommon } ;
1012use super :: module_host:: { 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 ;
@@ -322,12 +325,13 @@ fn startup_instance_worker<'scope>(
322325 scope : & mut PinScope < ' scope , ' _ > ,
323326 program : Arc < str > ,
324327 module_or_mcc : Either < ModuleCommon , ModuleCreationContextLimited > ,
325- ) -> anyhow:: Result < ( Local < ' scope , Function > , Either < ModuleCommon , ModuleCommon > ) > {
328+ ) -> anyhow:: Result < ( HookFunction < ' scope > , Either < ModuleCommon , ModuleCommon > ) > {
326329 // Start-up the user's module.
327330 eval_user_module_catch ( scope, & program) . map_err ( DescribeError :: Setup ) ?;
328331
329332 // Find the `__call_reducer__` function.
330- let call_reducer_fun = catch_exception ( scope, |scope| Ok ( call_reducer_fun ( scope) ?) ) . map_err ( |( e, _) | e) ?;
333+ let call_reducer_fun =
334+ get_hook ( scope, ModuleHook :: CallReducer ) . context ( "The `spacetimedb/server` module was never imported" ) ?;
331335
332336 // If we don't have a module, make one.
333337 let module_common = match module_or_mcc {
@@ -571,7 +575,7 @@ fn call_reducer<'scope>(
571575 instance_common : & mut InstanceCommon ,
572576 replica_ctx : & ReplicaContext ,
573577 scope : & mut PinScope < ' scope , ' _ > ,
574- fun : Local < ' scope , Function > ,
578+ fun : HookFunction < ' _ > ,
575579 tx : Option < MutTxId > ,
576580 params : CallReducerParams ,
577581) -> ( super :: ReducerCallResult , bool ) {
@@ -648,14 +652,18 @@ fn extract_description<'scope>(
648652 |a, b, c| log_traceback ( replica_ctx, a, b, c) ,
649653 || {
650654 catch_exception ( scope, |scope| {
651- let def = call_describe_module ( scope) ?;
655+ let Some ( describe_module) = get_hook ( scope, ModuleHook :: DescribeModule ) else {
656+ return Ok ( RawModuleDef :: V9 ( Default :: default ( ) ) ) ;
657+ } ;
658+ let def = call_describe_module ( scope, describe_module) ?;
652659 Ok ( def)
653660 } )
654661 . map_err ( |( e, _) | e)
655662 . map_err ( Into :: into)
656663 } ,
657664 )
658665}
666+
659667#[ cfg( test) ]
660668mod test {
661669 use super :: to_value:: test:: with_scope;
@@ -684,7 +692,7 @@ mod test {
684692 fn call_call_reducer_works ( ) {
685693 let call = |code| {
686694 with_module_catch ( code, |scope| {
687- let fun = call_reducer_fun ( scope) ? ;
695+ let fun = get_hook ( scope, ModuleHook :: CallReducer ) . unwrap ( ) ;
688696 let op = ReducerOp {
689697 id : ReducerId ( 42 ) ,
690698 name : "foobar" ,
@@ -762,7 +770,11 @@ js error Uncaught Error: foobar
762770 },
763771 })
764772 "# ;
765- let raw_mod = with_module_catch ( code, call_describe_module) . map_err ( |e| e. to_string ( ) ) ;
773+ let raw_mod = with_module_catch ( code, |scope| {
774+ let describe_module = get_hook ( scope, ModuleHook :: DescribeModule ) . unwrap ( ) ;
775+ call_describe_module ( scope, describe_module)
776+ } )
777+ . map_err ( |e| e. to_string ( ) ) ;
766778 assert_eq ! ( raw_mod, Ok ( RawModuleDef :: V9 ( <_>:: default ( ) ) ) ) ;
767779 }
768780}
0 commit comments