* Remove `WrappedCallable` indirection At this point `Func` has evolved quite a bit since inception and the `WrappedCallable` trait I don't believe is needed any longer. This should help clean up a few entry points by having fewer traits in play. * Remove the `Callable` trait This commit removes the `wasmtime::Callable` trait, changing the signature of `Func::new` to take an appropriately typed `Fn`. Additionally the function now always takes `&Caller` like `Func::wrap` optionally can, to empower `Func::new` to have the same capabilities of `Func::wrap`. * Add a test for an already-fixed issue Closes #849 * rustfmt * Update more locations for `Callable` * rustfmt * Remove a stray leading borrow * Review feedback * Remove unneeded `wasmtime_call_trampoline` shim
96 lines
2.8 KiB
Rust
96 lines
2.8 KiB
Rust
//! Utility module to create trampolines in/out WebAssembly module.
|
|
|
|
mod create_handle;
|
|
mod func;
|
|
mod global;
|
|
mod memory;
|
|
mod table;
|
|
|
|
use self::func::create_handle_with_function;
|
|
use self::global::create_global;
|
|
use self::memory::create_handle_with_memory;
|
|
use self::table::create_handle_with_table;
|
|
use crate::{FuncType, GlobalType, MemoryType, Store, TableType, Trap, Val};
|
|
use anyhow::Result;
|
|
use std::any::Any;
|
|
use wasmtime_runtime::{VMContext, VMFunctionBody, VMTrampoline};
|
|
|
|
pub fn generate_func_export(
|
|
ft: &FuncType,
|
|
func: Box<dyn Fn(*mut VMContext, *mut u128) -> Result<(), Trap>>,
|
|
store: &Store,
|
|
) -> Result<(
|
|
wasmtime_runtime::InstanceHandle,
|
|
wasmtime_runtime::ExportFunction,
|
|
VMTrampoline,
|
|
)> {
|
|
let (instance, trampoline) = create_handle_with_function(ft, func, store)?;
|
|
match instance.lookup("trampoline").expect("trampoline export") {
|
|
wasmtime_runtime::Export::Function(f) => Ok((instance, f, trampoline)),
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
|
|
/// Note that this is `unsafe` since `func` must be a valid function pointer and
|
|
/// have a signature which matches `ft`, otherwise the returned
|
|
/// instance/export/etc may exhibit undefined behavior.
|
|
pub unsafe fn generate_raw_func_export(
|
|
ft: &FuncType,
|
|
func: *mut [VMFunctionBody],
|
|
trampoline: VMTrampoline,
|
|
store: &Store,
|
|
state: Box<dyn Any>,
|
|
) -> Result<(
|
|
wasmtime_runtime::InstanceHandle,
|
|
wasmtime_runtime::ExportFunction,
|
|
)> {
|
|
let instance = func::create_handle_with_raw_function(ft, func, trampoline, store, state)?;
|
|
match instance.lookup("trampoline").expect("trampoline export") {
|
|
wasmtime_runtime::Export::Function(f) => Ok((instance, f)),
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
|
|
pub fn generate_global_export(
|
|
store: &Store,
|
|
gt: &GlobalType,
|
|
val: Val,
|
|
) -> Result<(
|
|
wasmtime_runtime::InstanceHandle,
|
|
wasmtime_runtime::ExportGlobal,
|
|
)> {
|
|
let instance = create_global(store, gt, val)?;
|
|
match instance.lookup("global").expect("global export") {
|
|
wasmtime_runtime::Export::Global(g) => Ok((instance, g)),
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
|
|
pub fn generate_memory_export(
|
|
store: &Store,
|
|
m: &MemoryType,
|
|
) -> Result<(
|
|
wasmtime_runtime::InstanceHandle,
|
|
wasmtime_runtime::ExportMemory,
|
|
)> {
|
|
let instance = create_handle_with_memory(store, m)?;
|
|
match instance.lookup("memory").expect("memory export") {
|
|
wasmtime_runtime::Export::Memory(m) => Ok((instance, m)),
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
|
|
pub fn generate_table_export(
|
|
store: &Store,
|
|
t: &TableType,
|
|
) -> Result<(
|
|
wasmtime_runtime::InstanceHandle,
|
|
wasmtime_runtime::ExportTable,
|
|
)> {
|
|
let instance = create_handle_with_table(store, t)?;
|
|
match instance.lookup("table").expect("table export") {
|
|
wasmtime_runtime::Export::Table(t) => Ok((instance, t)),
|
|
_ => unreachable!(),
|
|
}
|
|
}
|