Skip to content
5 changes: 2 additions & 3 deletions crates/core/src/host/v8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::host::wasm_common::module_host_actor::{
InstanceOp, ProcedureExecuteResult, ProcedureOp, ReducerExecuteResult, ReducerOp, ViewExecuteResult, ViewOp,
WasmInstance,
};
use crate::host::wasm_common::{RowIters, TimingSpanSet, DESCRIBE_MODULE_DUNDER};
use crate::host::wasm_common::{RowIters, TimingSpanSet};
use crate::host::{ModuleHost, ReducerCallError, ReducerCallResult, Scheduler};
use crate::module_host_context::{ModuleCreationContext, ModuleCreationContextLimited};
use crate::replica_context::ReplicaContext;
Expand Down Expand Up @@ -871,8 +871,7 @@ fn extract_description<'scope>(
replica_ctx: &ReplicaContext,
) -> Result<RawModuleDef, DescribeError> {
run_describer(
//TODO(shub): make it work with `DESCRIBE_MODULE_DUNDER_V10`
DESCRIBE_MODULE_DUNDER,
hooks.describe_func_name(),
|a, b, c| log_traceback(replica_ctx, a, b, c),
|| {
catch_exception(scope, |scope| {
Expand Down
36 changes: 34 additions & 2 deletions crates/core/src/host/v8/syscall/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::host::v8::error::Throwable;
use crate::host::v8::error::TypeError;
use crate::host::v8::from_value::cast;
use crate::host::v8::string::StringConst;
use crate::host::wasm_common::DESCRIBE_MODULE_DUNDER;
use crate::host::wasm_common::DESCRIBE_MODULE_DUNDER_V10;

/// Returns the hook function `name` on `hooks_obj`.
pub(super) fn get_hook_function<'scope>(
Expand Down Expand Up @@ -49,6 +51,7 @@ pub(in super::super) enum ModuleHookKey {
CallView,
CallAnonymousView,
CallProcedure,
DescribeModuleV10,
}

impl ModuleHookKey {
Expand Down Expand Up @@ -93,18 +96,39 @@ impl HooksInfo {
}
}

#[derive(Copy, Clone)]
/// The describe_module hook function for different `RawModuleDef`s.
pub enum DescribeModuleHook<'scope> {
Legacy(Local<'scope, Function>),
V10(Local<'scope, Function>),
}

#[derive(Copy, Clone)]
/// The actual callable module hook functions and their abi version.
pub(in super::super) struct HookFunctions<'scope> {
pub abi: AbiVersion,
/// describe_module and call_reducer existed in v1.0, but everything else is `Option`al
pub describe_module: Local<'scope, Function>,
pub describe_module: DescribeModuleHook<'scope>,
pub call_reducer: Local<'scope, Function>,
pub call_view: Option<Local<'scope, Function>>,
pub call_view_anon: Option<Local<'scope, Function>>,
pub call_procedure: Option<Local<'scope, Function>>,
}

impl HookFunctions<'_> {
pub(in super::super) fn describe_hook(&self) -> Local<'_, Function> {
match self.describe_module {
DescribeModuleHook::Legacy(f) | DescribeModuleHook::V10(f) => f,
}
}

pub(in super::super) fn describe_func_name(&self) -> &'static str {
match self.describe_module {
DescribeModuleHook::Legacy(_) => DESCRIBE_MODULE_DUNDER,
DescribeModuleHook::V10(_) => DESCRIBE_MODULE_DUNDER_V10,
}
}
}
/// Returns the hook function previously registered in [`register_hooks`].
pub(in super::super) fn get_hooks<'scope>(scope: &mut PinScope<'scope, '_>) -> Option<HookFunctions<'scope>> {
let ctx = scope.get_current_context();
Expand All @@ -118,9 +142,17 @@ pub(in super::super) fn get_hooks<'scope>(scope: &mut PinScope<'scope, '_>) -> O
})
};

let describe_module = if let Some(f) = get(ModuleHookKey::DescribeModule) {
DescribeModuleHook::Legacy(f)
} else if let Some(f) = get(ModuleHookKey::DescribeModuleV10) {
DescribeModuleHook::V10(f)
} else {
return None;
};

Some(HookFunctions {
abi: hooks.abi,
describe_module: get(ModuleHookKey::DescribeModule)?,
describe_module,
call_reducer: get(ModuleHookKey::CallReducer)?,
call_view: get(ModuleHookKey::CallView),
call_view_anon: get(ModuleHookKey::CallAnonymousView),
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/host/v8/syscall/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ pub(super) fn call_describe_module(
hooks: &HookFunctions<'_>,
) -> Result<RawModuleDef, ErrorOrException<ExceptionThrown>> {
// Call the function.
let raw_mod_js = call_free_fun(scope, hooks.describe_module, &[])?;
let raw_mod_js = call_free_fun(scope, hooks.describe_hook(), &[])?;

// Deserialize the raw module.
let raw_mod = cast!(
Expand Down
Loading