Support Func imports with zero shims (#839)
* Move `Func` to its own file * Support `Func` imports with zero shims This commit extends the `Func` type in the `wasmtime` crate with static `wrap*` constructors. The goal of these constructors is to create a `Func` type which has zero shims associated with it, creating as small of a layer as possible between wasm code and calling imported Rust code. This is achieved by creating an `extern "C"` shim function which matches the ABI of what Cranelift will generate, and then the host function is passed directly into an `InstanceHandle` to get called later. This also enables enough inlining opportunities that LLVM will be able to see all functions and inline everything to the point where your function is called immediately from wasm, no questions asked.
This commit is contained in:
@@ -13,7 +13,9 @@ use self::memory::create_handle_with_memory;
|
||||
use self::table::create_handle_with_table;
|
||||
use super::{Callable, FuncType, GlobalType, MemoryType, Store, TableType, Val};
|
||||
use anyhow::Result;
|
||||
use std::any::Any;
|
||||
use std::rc::Rc;
|
||||
use wasmtime_runtime::VMFunctionBody;
|
||||
|
||||
pub use self::global::GlobalState;
|
||||
|
||||
@@ -27,6 +29,20 @@ pub fn generate_func_export(
|
||||
Ok((instance, export))
|
||||
}
|
||||
|
||||
/// 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: *const VMFunctionBody,
|
||||
store: &Store,
|
||||
state: Box<dyn Any>,
|
||||
) -> Result<(wasmtime_runtime::InstanceHandle, wasmtime_runtime::Export)> {
|
||||
let instance = func::create_handle_with_raw_function(ft, func, store, state)?;
|
||||
let export = instance.lookup("trampoline").expect("trampoline export");
|
||||
Ok((instance, export))
|
||||
}
|
||||
|
||||
pub fn generate_global_export(
|
||||
gt: &GlobalType,
|
||||
val: Val,
|
||||
|
||||
Reference in New Issue
Block a user