Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store Wasm function entities in StoreInner #686

Merged
merged 13 commits into from
Feb 20, 2023
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ impl<'ctx, 'engine, 'func> Executor<'ctx, 'engine, 'func> {
.map(FuncRef::from)
.ok_or(TrapCode::TableOutOfBounds)?;
let func = funcref.func().ok_or(TrapCode::IndirectCallToNull)?;
let actual_signature = self.ctx.get_func_type(func);
let actual_signature = self.ctx.resolve_func(func).ty_dedup();
let expected_signature = self
.ctx
.resolve_instance(self.frame.instance())
Expand Down
33 changes: 18 additions & 15 deletions crates/wasmi/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ pub(crate) use self::{
func_args::{FuncFinished, FuncParams, FuncResults},
func_types::DedupFuncType,
};
use super::{func::FuncEntityInner, AsContext, AsContextMut, Func};
use crate::{
core::{Trap, TrapCode},
func::FuncEntity,
AsContext,
AsContextMut,
Func,
FuncType,
StoreContextMut,
};
Expand Down Expand Up @@ -210,7 +213,7 @@ impl Engine {
pub(crate) fn execute_func<T, Results>(
&self,
ctx: StoreContextMut<T>,
func: Func,
func: &Func,
params: impl CallParams,
results: Results,
) -> Result<<Results as CallResults>::Results, Trap>
Expand Down Expand Up @@ -245,7 +248,7 @@ impl Engine {
pub(crate) fn execute_func_resumable<T, Results>(
&self,
ctx: StoreContextMut<T>,
func: Func,
func: &Func,
params: impl CallParams,
results: Results,
) -> Result<ResumableCallBase<<Results as CallResults>::Results>, Trap>
Expand Down Expand Up @@ -399,7 +402,7 @@ impl EngineInner {
fn execute_func<T, Results>(
&self,
ctx: StoreContextMut<T>,
func: Func,
func: &Func,
params: impl CallParams,
results: Results,
) -> Result<<Results as CallResults>::Results, Trap>
Expand All @@ -418,7 +421,7 @@ impl EngineInner {
fn execute_func_resumable<T, Results>(
&self,
mut ctx: StoreContextMut<T>,
func: Func,
func: &Func,
params: impl CallParams,
results: Results,
) -> Result<ResumableCallBase<<Results as CallResults>::Results>, Trap>
Expand Down Expand Up @@ -447,7 +450,7 @@ impl EngineInner {
host_trap,
}) => Ok(ResumableCallBase::Resumable(ResumableInvocation::new(
ctx.as_context().store.engine().clone(),
func,
*func,
host_func,
host_trap,
stack,
Expand Down Expand Up @@ -586,22 +589,22 @@ impl<'engine> EngineExecutor<'engine> {
fn execute_func<T, Results>(
&mut self,
mut ctx: StoreContextMut<T>,
func: Func,
func: &Func,
params: impl CallParams,
results: Results,
) -> Result<<Results as CallResults>::Results, TaggedTrap>
where
Results: CallResults,
{
self.initialize_args(params);
match func.as_internal(ctx.as_context()) {
FuncEntityInner::Wasm(wasm_func) => {
match ctx.as_context().store.inner.resolve_func(func) {
FuncEntity::Wasm(wasm_func) => {
let mut frame = self.stack.call_wasm_root(wasm_func, &self.res.code_map)?;
let mut cache = InstanceCache::from(frame.instance());
self.execute_wasm_func(ctx.as_context_mut(), &mut frame, &mut cache)?;
}
FuncEntityInner::Host(host_func) => {
let host_func = host_func.clone();
FuncEntity::Host(host_func) => {
let host_func = *host_func;
self.stack
.call_host_root(ctx.as_context_mut(), host_func, &self.res.func_types)?;
}
Expand Down Expand Up @@ -686,13 +689,13 @@ impl<'engine> EngineExecutor<'engine> {
None => return Ok(()),
},
CallOutcome::NestedCall(called_func) => {
match called_func.as_internal(ctx.as_context()) {
FuncEntityInner::Wasm(wasm_func) => {
match ctx.as_context().store.inner.resolve_func(&called_func) {
FuncEntity::Wasm(wasm_func) => {
*frame = self.stack.call_wasm(frame, wasm_func, &self.res.code_map)?;
}
FuncEntityInner::Host(host_func) => {
FuncEntity::Host(host_func) => {
cache.reset_default_memory_bytes();
let host_func = host_func.clone();
let host_func = *host_func;
self.stack
.call_host(
ctx.as_context_mut(),
Expand Down
44 changes: 20 additions & 24 deletions crates/wasmi/src/engine/stack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use crate::{
core::UntypedValue,
func::{HostFuncEntity, WasmFuncEntity},
AsContext,
AsContextMut,
Instance,
StoreContextMut,
};
use core::{
fmt::{self, Display},
Expand Down Expand Up @@ -222,29 +222,23 @@ impl Stack {
}

/// Executes the given host function as root.
pub(crate) fn call_host_root<C>(
pub(crate) fn call_host_root<T>(
&mut self,
ctx: C,
host_func: HostFuncEntity<<C as AsContext>::UserState>,
ctx: StoreContextMut<T>,
host_func: HostFuncEntity,
func_types: &FuncTypeRegistry,
) -> Result<(), Trap>
where
C: AsContextMut,
{
) -> Result<(), Trap> {
self.call_host_impl(ctx, host_func, None, func_types)
}

/// Executes the given host function called by a Wasm function.
pub(crate) fn call_host<C>(
pub(crate) fn call_host<T>(
&mut self,
ctx: C,
ctx: StoreContextMut<T>,
caller: &FuncFrame,
host_func: HostFuncEntity<<C as AsContext>::UserState>,
host_func: HostFuncEntity,
func_types: &FuncTypeRegistry,
) -> Result<(), Trap>
where
C: AsContextMut,
{
) -> Result<(), Trap> {
let instance = caller.instance();
self.call_host_impl(ctx, host_func, Some(instance), func_types)
}
Expand All @@ -256,16 +250,13 @@ impl Stack {
/// - If the host function returns a host side error or trap.
/// - If the value stack overflowed upon pushing parameters or results.
#[inline(never)]
fn call_host_impl<C>(
fn call_host_impl<T>(
&mut self,
mut ctx: C,
host_func: HostFuncEntity<<C as AsContext>::UserState>,
ctx: StoreContextMut<T>,
host_func: HostFuncEntity,
instance: Option<&Instance>,
func_types: &FuncTypeRegistry,
) -> Result<(), Trap>
where
C: AsContextMut,
{
) -> Result<(), Trap> {
// The host function signature is required for properly
// adjusting, inspecting and manipulating the value stack.
let (input_types, output_types) = func_types
Expand Down Expand Up @@ -295,8 +286,13 @@ impl Stack {
// Now we are ready to perform the host function call.
// Note: We need to clone the host function due to some borrowing issues.
// This should not be a big deal since host functions usually are cheap to clone.
host_func
.call(&mut ctx, instance, params_results)
let trampoline = ctx
.as_context()
.store
.resolve_trampoline(host_func.trampoline())
.clone();
trampoline
.call(ctx, instance, params_results)
.map_err(|error| {
// Note: We drop the values that have been temporarily added to
// the stack to act as parameter and result buffer for the
Expand Down
10 changes: 5 additions & 5 deletions crates/wasmi/src/func/into_func.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
super::engine::{FuncFinished, FuncParams, FuncResults},
HostFuncTrampoline,
TrampolineEntity,
};
use crate::{
core::{Trap, ValueType, F32, F64},
Expand All @@ -24,7 +24,7 @@ pub trait IntoFunc<T, Params, Results>: Send + Sync + 'static {

/// Converts the function into its `wasmi` signature and its trampoline.
#[doc(hidden)]
fn into_func(self) -> (FuncType, HostFuncTrampoline<T>);
fn into_func(self) -> (FuncType, TrampolineEntity<T>);
}

macro_rules! impl_into_func {
Expand All @@ -42,7 +42,7 @@ macro_rules! impl_into_func {
type Results = <R as WasmRet>::Ok;

#[allow(non_snake_case)]
fn into_func(self) -> (FuncType, HostFuncTrampoline<T>) {
fn into_func(self) -> (FuncType, TrampolineEntity<T>) {
IntoFunc::into_func(
move |
_: Caller<'_, T>,
Expand All @@ -69,12 +69,12 @@ macro_rules! impl_into_func {
type Results = <R as WasmRet>::Ok;

#[allow(non_snake_case)]
fn into_func(self) -> (FuncType, HostFuncTrampoline<T>) {
fn into_func(self) -> (FuncType, TrampolineEntity<T>) {
let signature = FuncType::new(
<Self::Params as WasmTypeList>::types(),
<Self::Results as WasmTypeList>::types(),
);
let trampoline = HostFuncTrampoline::new(
let trampoline = TrampolineEntity::new(
move |caller: Caller<T>, params_results: FuncParams| -> Result<FuncFinished, Trap> {
let (($($tuple,)*), func_results): (Self::Params, FuncResults) = params_results.decode_params();
let results: Self::Results =
Expand Down
Loading