Add API to statically assert signature of a Func (#955)

* Add API to statically assert signature of a `Func`

This commit add a family of APIs to `Func` named `getN` where `N` is the
number of arguments. Each function will attempt to statically assert the
signature of a `Func` and, if matching, returns a corresponding closure
which can be used to invoke the underlying function.

The purpose of this commit is to add a highly optimized way to enter a
wasm module, performing type checks up front and avoiding all the costs
of boxing and unboxing arguments within a `Val`. In general this should
be much more optimized than the previous `call` API for entering a wasm
module, if the signature is statically known.

* rustfmt

* Remove stray debugging
This commit is contained in:
Alex Crichton
2020-02-20 09:28:12 -06:00
committed by GitHub
parent b6be99c9e1
commit 80b095f2e2
8 changed files with 316 additions and 80 deletions

View File

@@ -20,7 +20,7 @@ pub fn is_wasi_module(name: &str) -> bool {
/// This is an internal structure used to acquire a handle on the caller's
/// wasm memory buffer.
///
/// This exploits how we can implement `WasmArg` for ourselves locally even
/// This exploits how we can implement `WasmTy` for ourselves locally even
/// though crates in general should not be doing that. This is a crate in
/// the wasmtime project, however, so we should be able to keep up with our own
/// changes.
@@ -33,11 +33,15 @@ struct WasiCallerMemory {
len: usize,
}
impl wasmtime::WasmArg for WasiCallerMemory {
impl wasmtime::WasmTy for WasiCallerMemory {
type Abi = ();
fn push(_dst: &mut Vec<wasmtime::ValType>) {}
fn matches(_tys: impl Iterator<Item = wasmtime::ValType>) -> bool {
true
}
fn from_abi(vmctx: *mut wasmtime_runtime::VMContext, _abi: ()) -> Self {
unsafe {
match wasmtime_runtime::InstanceHandle::from_vmctx(vmctx).lookup("memory") {
@@ -56,6 +60,8 @@ impl wasmtime::WasmArg for WasiCallerMemory {
}
}
}
fn into_abi(self) {}
}
impl WasiCallerMemory {