Stream: git-wasmtime

Topic: wasmtime / issue #8557 Error: expected 4 imports, found 0


view this post on Zulip Wasmtime GitHub notifications bot (May 06 2024 at 15:30):

yIllusionSky opened issue #8557:

Thanks for filing a bug report! Please fill out the TODOs below.

Note: if you want to report a security issue, please read our security policy!

Test Case

TODO: upload Wasm file here

Steps to Reproduce

Expected Results

Output Hello, world!

Actual Results

Error: expected 4 imports, found 0

Versions and Environment

Wasmtime version or commit: latest

Operating system: macos

Architecture: aarch64-apple-darwin

Extra Info

my wasm code:

#[no_mangle]
pub extern fn doing_home() {
     println!("Hello, world!");
     println!("hello");
}

my exec code:

use std::error::Error;
use wasmtime::*;

fn main() -> Result<(), Box<dyn Error>> {
     // An engine stores and configures global compilation settings like
     // optimization level, enabled wasm features, etc.
     let engine = Engine::default();

     // We start off by creating a `Module` which represents a compiled form
     // of our input wasm module. In this case it'll be JIT-compiled after
     // we parse the text format.

     let module = Module::from_file(&engine, "target/wasm32-wasi/debug/doing.wasm")?;

     // A `Store` is what will own instances, functions, globals, etc. All wasm
     // items are stored within a `Store`, and it's what we'll always be using to
     // interact with the wasm world. Custom data can be stored in stores but for
     // now we just use `()`.
     let mut store = Store::new(&engine, ());

     // With a compiled `Module` we can then instantiate it, creating
     // an `Instance` which we can actually poke at functions on.

     let instance = Instance::new(&mut store, &module, &[])?;

     // The `Instance` gives us access to various exported functions and items,
     // which we access here to pull out our `answer` exported function and
     // run it.
     let answer = instance.get_func(&mut store, "doing_home")
         .expect("`answer` was not an exported function");

     // There's a few ways we can call the `answer` `Func` value. The easiest
     // is to statically assert its signature with `typed` (in this case
     // asserting it takes no arguments and returns one i32) and then call it.
     let answer = answer.typed::<(), ()>(&store)?;

     // And finally we can call our function! Note that the error propagation
     // with `?` is done to handle the case where the wasm function traps.
     let result = answer.call(&mut store, ())?;
     println!("Answer: {:?}", result);
     Ok(())
}

view this post on Zulip Wasmtime GitHub notifications bot (May 06 2024 at 15:36):

yIllusionSky edited issue #8557:

Test Case

See end

Steps to Reproduce

Expected Results

Output Hello, world!

Actual Results

Error: expected 4 imports, found 0

Versions and Environment

Wasmtime version or commit: latest

Operating system: macos

Architecture: aarch64-apple-darwin

Extra Info

my wasm code:

#[no_mangle]
pub extern fn doing_home() {
     println!("Hello, world!");
     println!("hello");
}

my exec code:

use std::error::Error;
use wasmtime::*;

fn main() -> Result<(), Box<dyn Error>> {
     // An engine stores and configures global compilation settings like
     // optimization level, enabled wasm features, etc.
     let engine = Engine::default();

     // We start off by creating a `Module` which represents a compiled form
     // of our input wasm module. In this case it'll be JIT-compiled after
     // we parse the text format.

     let module = Module::from_file(&engine, "target/wasm32-wasi/debug/doing.wasm")?;

     // A `Store` is what will own instances, functions, globals, etc. All wasm
     // items are stored within a `Store`, and it's what we'll always be using to
     // interact with the wasm world. Custom data can be stored in stores but for
     // now we just use `()`.
     let mut store = Store::new(&engine, ());

     // With a compiled `Module` we can then instantiate it, creating
     // an `Instance` which we can actually poke at functions on.

     let instance = Instance::new(&mut store, &module, &[])?;

     // The `Instance` gives us access to various exported functions and items,
     // which we access here to pull out our `answer` exported function and
     // run it.
     let answer = instance.get_func(&mut store, "doing_home")
         .expect("`answer` was not an exported function");

     // There's a few ways we can call the `answer` `Func` value. The easiest
     // is to statically assert its signature with `typed` (in this case
     // asserting it takes no arguments and returns one i32) and then call it.
     let answer = answer.typed::<(), ()>(&store)?;

     // And finally we can call our function! Note that the error propagation
     // with `?` is done to handle the case where the wasm function traps.
     let result = answer.call(&mut store, ())?;
     println!("Answer: {:?}", result);
     Ok(())
}

view this post on Zulip Wasmtime GitHub notifications bot (May 06 2024 at 16:13):

alexcrichton commented on issue #8557:

Thanks for the report, and what you're most likely running into here is the fact that WASI imports need to be provided. I'd recommend following this example for instantiating a module that has wasi imports.

view this post on Zulip Wasmtime GitHub notifications bot (May 07 2024 at 00:50):

yIllusionSky closed issue #8557:

Test Case

See end

Steps to Reproduce

Expected Results

Output Hello, world!

Actual Results

Error: expected 4 imports, found 0

Versions and Environment

Wasmtime version or commit: latest

Operating system: macos

Architecture: aarch64-apple-darwin

Extra Info

my wasm code:

#[no_mangle]
pub extern fn doing_home() {
     println!("Hello, world!");
     println!("hello");
}

my exec code:

use std::error::Error;
use wasmtime::*;

fn main() -> Result<(), Box<dyn Error>> {
     // An engine stores and configures global compilation settings like
     // optimization level, enabled wasm features, etc.
     let engine = Engine::default();

     // We start off by creating a `Module` which represents a compiled form
     // of our input wasm module. In this case it'll be JIT-compiled after
     // we parse the text format.

     let module = Module::from_file(&engine, "target/wasm32-wasi/debug/doing.wasm")?;

     // A `Store` is what will own instances, functions, globals, etc. All wasm
     // items are stored within a `Store`, and it's what we'll always be using to
     // interact with the wasm world. Custom data can be stored in stores but for
     // now we just use `()`.
     let mut store = Store::new(&engine, ());

     // With a compiled `Module` we can then instantiate it, creating
     // an `Instance` which we can actually poke at functions on.

     let instance = Instance::new(&mut store, &module, &[])?;

     // The `Instance` gives us access to various exported functions and items,
     // which we access here to pull out our `answer` exported function and
     // run it.
     let answer = instance.get_func(&mut store, "doing_home")
         .expect("`answer` was not an exported function");

     // There's a few ways we can call the `answer` `Func` value. The easiest
     // is to statically assert its signature with `typed` (in this case
     // asserting it takes no arguments and returns one i32) and then call it.
     let answer = answer.typed::<(), ()>(&store)?;

     // And finally we can call our function! Note that the error propagation
     // with `?` is done to handle the case where the wasm function traps.
     let result = answer.call(&mut store, ())?;
     println!("Answer: {:?}", result);
     Ok(())
}

view this post on Zulip Wasmtime GitHub notifications bot (May 07 2024 at 00:50):

yIllusionSky commented on issue #8557:

Thanks for the report, and what you're most likely running into here is the fact that WASI imports need to be provided. I'd recommend following this example for instantiating a module that has wasi imports.

Thanks I have solved my problem


Last updated: Oct 23 2024 at 20:03 UTC