Add a first-class way of accessing caller's exports (#1290)
* Add a first-class way of accessing caller's exports This commit is a continuation of #1237 and updates the API of `Func` to allow defining host functions which have easy access to a caller's memory in particular. The new APIs look like so: * The `Func::wrap*` family of functions was condensed into one `Func::wrap` function. * The ABI layer of conversions in `WasmTy` were removed * An optional `Caller<'_>` argument can be at the front of all host-defined functions now. The old way the wasi bindings looked up memory has been removed and is now replaced with the `Caller` type. The `Caller` type has a `get_export` method on it which allows looking up a caller's export by name, allowing you to get access to the caller's memory easily, and even during instantiation. * Add a temporary note * Move some docs
This commit is contained in:
@@ -16,58 +16,3 @@ pub fn is_wasi_module(name: &str) -> bool {
|
||||
// trick.
|
||||
name.starts_with("wasi")
|
||||
}
|
||||
|
||||
/// This is an internal structure used to acquire a handle on the caller's
|
||||
/// wasm memory buffer.
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
/// In general this type is wildly unsafe. We need to update the wasi crates to
|
||||
/// probably work with more `wasmtime`-like APIs to grip with the unsafety
|
||||
/// around dealing with caller memory.
|
||||
struct WasiCallerMemory {
|
||||
base: *mut u8,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl wasmtime::WasmTy for WasiCallerMemory {
|
||||
type Abi = ();
|
||||
|
||||
fn push(_dst: &mut Vec<wasmtime::ValType>) {}
|
||||
|
||||
fn matches(_tys: impl Iterator<Item = wasmtime::ValType>) -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn from_abi(vmctx: *mut wasmtime_runtime::VMContext, _abi: ()) -> Self {
|
||||
unsafe {
|
||||
match wasmtime_runtime::InstanceHandle::from_vmctx(vmctx).lookup("memory") {
|
||||
Some(wasmtime_runtime::Export::Memory(m)) => WasiCallerMemory {
|
||||
base: (*m.definition).base,
|
||||
len: (*m.definition).current_length,
|
||||
},
|
||||
_ => WasiCallerMemory {
|
||||
base: std::ptr::null_mut(),
|
||||
len: 0,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn into_abi(self) {}
|
||||
unsafe fn load(_ptr: &mut *const u128) {}
|
||||
unsafe fn store(_abi: Self::Abi, _ptr: *mut u128) {}
|
||||
}
|
||||
|
||||
impl WasiCallerMemory {
|
||||
unsafe fn get(&self) -> Result<&mut [u8], wasi_common::wasi::__wasi_errno_t> {
|
||||
if self.base.is_null() {
|
||||
Err(wasi_common::wasi::__WASI_ERRNO_INVAL)
|
||||
} else {
|
||||
Ok(std::slice::from_raw_parts_mut(self.base, self.len))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user