Hello,
I was looking through the examples here: https://github.com/bytecodealliance/wasmtime/blob/main/examples/hello.c
I don't understand how the wasm program "knows" that the registered import is the one for the hello
function.
Other VMs have to name the functions registered (e.g. wasm3 linker). But I find no such thing for wasmtime.
Does this mean that imports are matched in order with the imports provided to this method?:
const wasm_extern_t *imports[] = { wasm_func_as_extern(hello) };
error = wasmtime_instance_new(store, module, imports, 1, &instance, &trap);
This seems like a difficult way to manage an import table. Is there another way?
@Alexandru Ene ah yeah so wasm_instance_new
(and wasmtime_instance_new
) require a 1:1 mapping from the module's imports to the list of imports provided (you can see some info on that at https://bytecodealliance.github.io/wasmtime/api/wasmtime/struct.Instance.html#method.new)
for a more ergonomic or string-based API, though, I'd recommend using the wasmtime_linker_t
type which allows you to specify imports by name and then instantiate a module via that name lookup
Alex, you're absolutely right! Thanks for all the help btw, and sorry for all the newbish questions :D
No worries at all! This is also on us for not having a documented C API and a pretty limited number of examples :)
@Alexandru Ene to add to what Alex said, the example you mentioned uses the official Wasm C-API so that determines how this works. Better documentation would help, of course! :slight_smile:
Yup, I did spend some time even in the linker api as I even had a PR on it, but somehow missed that it is available for linking host functions :D .
Last updated: Dec 23 2024 at 12:05 UTC