From 5f75536ed6f36ba42c83d59d177f4f931e45df67 Mon Sep 17 00:00:00 2001 From: Tam1SH Date: Sun, 23 Nov 2025 14:26:40 +0300 Subject: [PATCH] Update README and linker documentation for function wrapping in module instances --- crates/wasmtime/README.md | 5 +++-- crates/wasmtime/src/runtime/component/linker.rs | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/wasmtime/README.md b/crates/wasmtime/README.md index 97c004305692..85b7fdaa865e 100644 --- a/crates/wasmtime/README.md +++ b/crates/wasmtime/README.md @@ -84,9 +84,10 @@ fn main() -> Result<()> { // Create a `Linker` which will be later used to instantiate this module. // Host functionality is defined by name within the `Linker`. let mut linker = Linker::new(&engine); - linker.func_wrap("host", "host_func", |caller: Caller<'_, u32>, param: i32| { + linker.root().instance("host").func_wrap("host_func", |ctx: StoreContextMut<'_, ()>, (param,): (i32,)| { println!("Got {} from WebAssembly", param); - println!("my host state is: {}", caller.data()); + println!("my host state is: {}", ctx.data()); + Ok(()) })?; // All wasm objects operate within the context of a "store". Each diff --git a/crates/wasmtime/src/runtime/component/linker.rs b/crates/wasmtime/src/runtime/component/linker.rs index 3ab2bc32bcab..b8a42941680e 100644 --- a/crates/wasmtime/src/runtime/component/linker.rs +++ b/crates/wasmtime/src/runtime/component/linker.rs @@ -400,6 +400,12 @@ impl LinkerInstance<'_, T> { /// types that will come from wasm and `Return` is a value coming from the /// host going back to wasm. /// + /// When defining functions that belong to a specific module instance + /// (rather than the root linker), you must first create the appropriate instance + /// using the [`instance`](LinkerInstance::instance) method and then call `func_wrap` + /// on the returned [`LinkerInstance`]. This is necessary to match the nested + /// instance structure expected by WebAssembly components. + /// /// Additionally the `func` takes a /// [`StoreContextMut`](crate::StoreContextMut) as its first parameter. ///