Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

feat: improve HostFunctions to support disable runtime task host functions #12674

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions client/executor/src/native_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<H>(module.clone());
let (result, allocation_stats) =
instance.call_with_allocation_stats(export_name.into(), call_data);
**allocation_stats_out = allocation_stats;
Expand Down Expand Up @@ -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::<H>(module.clone());
instance.call_export(method, data)
})
},
Expand Down Expand Up @@ -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<dyn WasmModule>) {
fn preregister_builtin_ext<H: HostFunctions>(module: Arc<dyn WasmModule>) {
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)
Expand Down Expand Up @@ -633,7 +636,7 @@ impl<D: NativeExecutionDispatch + 'static> CodeExecutor for NativeElseWasmExecut
}

with_externalities_safe(&mut **ext, move || {
preregister_builtin_ext(module.clone());
preregister_builtin_ext::<D::ExtendHostFunctions>(module.clone());
instance.call_export(method, data)
})
}
Expand Down
17 changes: 15 additions & 2 deletions primitives/wasm-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand Down Expand Up @@ -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<Base, Overlay> {
pub struct ExtendedHostFunctions<Base, Overlay, const ENABLE_RUNTIME_TASKS: bool = true> {
phantom: PhantomData<(Base, Overlay)>,
}

impl<Base, Overlay> HostFunctions for ExtendedHostFunctions<Base, Overlay>
impl<Base, Overlay, const ENABLE_RUNTIME_TASKS: bool> HostFunctions for ExtendedHostFunctions<Base, Overlay, ENABLE_RUNTIME_TASKS>
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();
Expand Down