al3xfischer opened issue #3684:
Hey @all!
I want to instantiate a module that has multiple import.
My gotcha is that I'm only interested in one function of the module that does not need any of the imports.Is there a way to instantiate the module but ignore the fact that I'm not providing the imports?
I used the following code:
fn main() -> Result<(), Box<dyn Error>> { let e = Engine::default(); let module = Module::from_file(&e,"awam.wasm")?; let mut store = Store::new(&e,()); let instance = Instance::new(&mut store, &module, &[])?; let say = instance.get_typed_func::<(),(i32,i32),_>(&mut store,"say")?; let (ptr,length) = say.call(&mut store,())?; dbg!(ptr,length); Ok(()) }
But I get the error msg:
expected 3 import, found 0
I also thought about the
Linker
but I was not able to find an API that might help.Thanks in advance!
bjorn3 commented on issue #3684:
You could provide dummy functions that panic as imports. Or you could use a wasm parser to extract the function you need and produce a new wasm module with this function. A wasm module can only be instantiated as a unit, not in parts.
al3xfischer commented on issue #3684:
If I create a fake function does it have to match the types of the imports or is that not validated when instantiated?
al3xfischer edited a comment on issue #3684:
@bjorn3 If I create a fake function does it have to match the types of the imports or is that not validated when instantiated?
al3xfischer deleted a comment on issue #3684:
@bjorn3 If I create a fake function does it have to match the types of the imports or is that not validated when instantiated?
al3xfischer commented on issue #3684:
@bjorn3 Thanks for the help.
I was able to instantiate the module by creating "fake" functions.
al3xfischer closed issue #3684:
Hey @all!
I want to instantiate a module that has multiple import.
My gotcha is that I'm only interested in one function of the module that does not need any of the imports.Is there a way to instantiate the module but ignore the fact that I'm not providing the imports?
I used the following code:
fn main() -> Result<(), Box<dyn Error>> { let e = Engine::default(); let module = Module::from_file(&e,"awam.wasm")?; let mut store = Store::new(&e,()); let instance = Instance::new(&mut store, &module, &[])?; let say = instance.get_typed_func::<(),(i32,i32),_>(&mut store,"say")?; let (ptr,length) = say.call(&mut store,())?; dbg!(ptr,length); Ok(()) }
But I get the error msg:
expected 3 import, found 0
I also thought about the
Linker
but I was not able to find an API that might help.Thanks in advance!
alexcrichton commented on issue #3684:
As mentioned there's no way you can instantiate only some parts of a module, it's an all-or-nothing process. If your module only imports functions and you'd like to make them all "dummy" imports you can do so with something like:
fn instantiate<T>(module: &Module, store: &mut Store<T>) -> Result<Instance> { let mut imports = Vec::new(); for i in module.imports() { let ty = match i.ty() { ExternType::Func(t) => t, _ => bail!("non-function import"), }; let func = Func::new(&mut *store, ty, |_, _, _| unimplemented!()); imports.push(func.into()); } Instance::new(store, module, &imports) }
Otherwise though you'll need to make sure that all imports are provided when instantiating a module, and when providing "real" imports it's recommended to use the
Linker
type.Also as a side node
@ALL
doesn't actually ping project members, it pings this user: https://github.com/ALL
al3xfischer commented on issue #3684:
Thanks for the help.
And also the side node!
Last updated: Dec 23 2024 at 12:05 UTC