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:
@@ -182,24 +182,26 @@ pub fn define_struct(args: TokenStream) -> TokenStream {
|
||||
}
|
||||
|
||||
let format_str = format!("{}({})", name, formats.join(", "));
|
||||
let wrap = format_ident!("wrap{}", shim_arg_decls.len() + 1);
|
||||
ctor_externs.push(quote! {
|
||||
let my_cx = cx.clone();
|
||||
let #name_ident = wasmtime::Func::#wrap(
|
||||
let #name_ident = wasmtime::Func::wrap(
|
||||
store,
|
||||
move |mem: crate::WasiCallerMemory #(,#shim_arg_decls)*| -> #ret_ty {
|
||||
move |caller: wasmtime::Caller<'_> #(,#shim_arg_decls)*| -> #ret_ty {
|
||||
log::trace!(
|
||||
#format_str,
|
||||
#(#format_args),*
|
||||
);
|
||||
unsafe {
|
||||
let memory = match mem.get() {
|
||||
Ok(e) => e,
|
||||
Err(e) => #handle_early_error,
|
||||
let memory = match caller.get_export("memory") {
|
||||
Some(wasmtime::Extern::Memory(m)) => m,
|
||||
_ => {
|
||||
let e = wasi_common::wasi::__WASI_ERRNO_INVAL;
|
||||
#handle_early_error
|
||||
}
|
||||
};
|
||||
hostcalls::#name_ident(
|
||||
&mut my_cx.borrow_mut(),
|
||||
memory,
|
||||
memory.data_unchecked_mut(),
|
||||
#(#hostcall_args),*
|
||||
) #cvt_ret
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user