Stream: git-wasmtime

Topic: wasmtime / issue #3684 Help needed: Instantiate only one ...


view this post on Zulip Wasmtime GitHub notifications bot (Jan 12 2022 at 18:54):

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!

view this post on Zulip Wasmtime GitHub notifications bot (Jan 12 2022 at 18:57):

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.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 12 2022 at 19:01):

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?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 12 2022 at 19:15):

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?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 12 2022 at 19:33):

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?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 12 2022 at 20:52):

al3xfischer commented on issue #3684:

@bjorn3 Thanks for the help.

I was able to instantiate the module by creating "fake" functions.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 12 2022 at 20:52):

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!

view this post on Zulip Wasmtime GitHub notifications bot (Jan 12 2022 at 20:53):

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

view this post on Zulip Wasmtime GitHub notifications bot (Jan 12 2022 at 20:56):

al3xfischer commented on issue #3684:

Thanks for the help.

And also the side node!


Last updated: Oct 23 2024 at 20:03 UTC