diff --git a/client/executor/src/native_executor.rs b/client/executor/src/native_executor.rs index b164b427e306d..fd80faf803350 100644 --- a/client/executor/src/native_executor.rs +++ b/client/executor/src/native_executor.rs @@ -281,7 +281,7 @@ where let mut allocation_stats_out = AssertUnwindSafe(allocation_stats_out); with_externalities_safe(&mut **ext, move || { - preregister_builtin_ext(module.clone()); + preregister_builtin_ext::(module.clone()); let (result, allocation_stats) = instance.call_with_allocation_stats(export_name.into(), call_data); **allocation_stats_out = allocation_stats; @@ -354,7 +354,7 @@ where ext, |module, mut instance, _onchain_version, mut ext| { with_externalities_safe(&mut **ext, move || { - preregister_builtin_ext(module.clone()); + preregister_builtin_ext::(module.clone()); instance.call_export(method, data) }) }, @@ -567,7 +567,10 @@ impl RuntimeInstanceSpawn { /// Pre-registers the built-in extensions to the currently effective externalities. /// /// Meant to be called each time before calling into the runtime. -fn preregister_builtin_ext(module: Arc) { +fn preregister_builtin_ext(module: Arc) { + if !H::ENABLE_RUNTIME_TASKS { + return; + } sp_externalities::with_externalities(move |mut ext| { if let Some(runtime_spawn) = RuntimeInstanceSpawn::with_externalities_and_module(module, ext) @@ -633,7 +636,7 @@ impl CodeExecutor for NativeElseWasmExecut } with_externalities_safe(&mut **ext, move || { - preregister_builtin_ext(module.clone()); + preregister_builtin_ext::(module.clone()); instance.call_export(method, data) }) } diff --git a/primitives/wasm-interface/src/lib.rs b/primitives/wasm-interface/src/lib.rs index 246c1abaeae3b..46b75258fdc35 100644 --- a/primitives/wasm-interface/src/lib.rs +++ b/primitives/wasm-interface/src/lib.rs @@ -417,6 +417,17 @@ if_wasmtime_is_enabled! { /// Something that provides implementations for host functions. pub trait HostFunctions: 'static + Send + Sync { + /// A special flag for trying to enable `RuntimeTasks` by + /// pre register some extensions when it's true value. + /// + /// # Note + /// + /// Developer could set it be false by disable some pre register ops every time + /// before execute wasm. + /// + /// Default to true. + const ENABLE_RUNTIME_TASKS: bool = true; + /// Returns the host functions `Self` provides. fn host_functions() -> Vec<&'static dyn Function>; @@ -453,15 +464,17 @@ impl HostFunctions for Tuple { /// A wrapper which merges two sets of host functions, and allows the second set to override /// the host functions from the first set. -pub struct ExtendedHostFunctions { +pub struct ExtendedHostFunctions { phantom: PhantomData<(Base, Overlay)>, } -impl HostFunctions for ExtendedHostFunctions +impl HostFunctions for ExtendedHostFunctions where Base: HostFunctions, Overlay: HostFunctions, { + const ENABLE_RUNTIME_TASKS: bool = Base::ENABLE_RUNTIME_TASKS && Overlay::ENABLE_RUNTIME_TASKS && ENABLE_RUNTIME_TASKS; + fn host_functions() -> Vec<&'static dyn Function> { let mut base = Base::host_functions(); let overlay = Overlay::host_functions();