RinLovesYou edited issue #5626:
Hi there! I'm just starting to explore using wasm to create a plugin framework.
I was starting to play with host import functions, and noticed i was unable to have more than one.
Host
let log_str = Func::wrap(&mut store, exports::log_str); let instance = Instance::new(&mut store, &module, &[ log_str.into() ])?;
Wasm
extern "C" { fn log_str(ptr: i32, len: i32); }
Output
[17:50:14.104] Hello from wasm!
If i now add a second function:
Host
let get_assembly_count = Func::wrap(&mut store, exports::get_assembly_count); let log_str = Func::wrap(&mut store, exports::log_str); let instance = Instance::new(&mut store, &module, &[get_assembly_count.into(), log_str.into() ])?;
Wasm
extern "C" { fn get_assembly_count() -> i32; fn log_str(ptr: i32, len: i32); }
Output
[17:56:14.791] [ERROR] Failed to load mod: incompatible import type for `env::log_str`
I've looked through the documentation/examples/issues and i've never seen this mentioned, so i'm a bit lost
alexcrichton commented on issue #5626:
Thanks for the report! As the documentation for
Instance::new
indicates:The entries in the list of imports are intended to correspond 1:1 with the list of imports returned by Module::imports
You can't rely on the order in the source being the exact same order in the output module. When using
Instance::new
you'd instead need to useModule::imports
to learn what order to pass things in.Instead, though, as
Instance::new
indicates, it's recommended to use theLinker
type which does name-based resolution which is generally easier to work with.
alexcrichton closed issue #5626:
Hi there! I'm just starting to explore using wasm to create a plugin framework.
I was starting to play with host import functions, and noticed i was unable to have more than one.
Host
let log_str = Func::wrap(&mut store, exports::log_str); let instance = Instance::new(&mut store, &module, &[ log_str.into() ])?;
Wasm
extern "C" { fn log_str(ptr: i32, len: i32); }
Output
[17:50:14.104] Hello from wasm!
If i now add a second function:
Host
let get_assembly_count = Func::wrap(&mut store, exports::get_assembly_count); let log_str = Func::wrap(&mut store, exports::log_str); let instance = Instance::new(&mut store, &module, &[get_assembly_count.into(), log_str.into() ])?;
Wasm
extern "C" { fn get_assembly_count() -> i32; fn log_str(ptr: i32, len: i32); }
Output
[17:56:14.791] [ERROR] Failed to load mod: incompatible import type for `env::log_str`
I've looked through the documentation/examples/issues and i've never seen this mentioned, so i'm a bit lost
RinLovesYou commented on issue #5626:
Ahh thank you, the Linker does indeed solve that problem!
Last updated: Dec 23 2024 at 12:05 UTC