Stream: git-wasmtime

Topic: wasmtime / issue #5830 Error loading component - Error: i...


view this post on Zulip Wasmtime GitHub notifications bot (Feb 18 2023 at 14:49):

Giom-fm opened issue #5830:

Hello

I am currently experimenting with the component model and encounter the following problem.
I know that the component model is not stable but maybe someone can help me.
To reproduce the error I did the following:

wasm-tools 1.0.24
wit-bindgen 0.3.0
wasmtime 7.0.0
wasi_snapshot_preview1.wasm 7736fd0

First I wrote a piece of rust code and compiled it to wasm32-wasi.

// wit/host.wit
default world hello-world {
  import name: func() -> string
  export greet: func()
}
// src/lib.rs
wit_bindgen::generate!("host");
struct MyWorld;
impl HelloWorld for MyWorld {
    fn greet() -> () {
        println!("Hello {}", self::name())
    }
}
export_hello_world!(MyWorld);

Then I created a component from the module using the wasm-tools, the wit file and the adapter file.

Now I want to load the wasm component on the host side and execute the greet function. For this I use the code I found on this Page

// src/mains.rs
use anyhow::Result;
use wasmtime::{Config, Engine, Store};
use wasmtime::component::*;

bindgen!({world: "host", path: "components/wasm-test/wit"});

struct MyState {
    name: String,
}

impl HelloWorldImports for MyState {
    fn name(&mut self) -> Result<String> {
        Ok(self.name.clone())
    }
}


fn main() -> Result<()> {
    let mut config = Config::new();
    config.wasm_component_model(true);
    let engine = Engine::new(&config)?;
    let component = Component::from_file(&engine, "target/wasm32-wasi/debug/wasm_component_test.wat")?;

    let mut linker = Linker::new(&engine);
    HelloWorld::add_to_linker(&mut linker, |state: &mut MyState| state)?;

    let mut store = Store::new(
        &engine,
        MyState {
            name: "me".to_string(),
        },
    );
    let (bindings, _) = HelloWorld::instantiate(&mut store, &component, &linker)?;

    bindings.call_greet(&mut store)?;
    Ok(())
}

Unfortunately the following error message occurs:

Error: import `wasi-stderr` has the wrong type

Caused by:
    0: instance export `print` has the wrong type
    1: expected func found nothing

What am I doing wrong here?
When loading the component in the host code, don't I also have to load the adapter file as well?

view this post on Zulip Wasmtime GitHub notifications bot (Feb 18 2023 at 18:24):

pchickey commented on issue #5830:

This error message isn't ideal.

You also need to add a host implementation of wasi snapshot 2 to the linker, that is currently found in preview2-prototyping/host.

However, please expect that everything in preview2-prototyping is still not mature: assume it is broken unless there are specifically tests in there passing.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 18 2023 at 18:24):

pchickey closed issue #5830:

Hello

I am currently experimenting with the component model and encounter the following problem.
I know that the component model is not stable but maybe someone can help me.
To reproduce the error I did the following:

wasm-tools 1.0.24
wit-bindgen 0.3.0
wasmtime 7.0.0
wasi_snapshot_preview1.wasm 7736fd0

First I wrote a piece of rust code and compiled it to wasm32-wasi.

// wit/host.wit
default world hello-world {
  import name: func() -> string
  export greet: func()
}
// src/lib.rs
wit_bindgen::generate!("host");
struct MyWorld;
impl HelloWorld for MyWorld {
    fn greet() -> () {
        println!("Hello {}", self::name())
    }
}
export_hello_world!(MyWorld);

Then I created a component from the module using the wasm-tools, the wit file and the adapter file.

Now I want to load the wasm component on the host side and execute the greet function. For this I use the code I found on this Page

// src/mains.rs
use anyhow::Result;
use wasmtime::{Config, Engine, Store};
use wasmtime::component::*;

bindgen!({world: "host", path: "components/wasm-test/wit"});

struct MyState {
    name: String,
}

impl HelloWorldImports for MyState {
    fn name(&mut self) -> Result<String> {
        Ok(self.name.clone())
    }
}


fn main() -> Result<()> {
    let mut config = Config::new();
    config.wasm_component_model(true);
    let engine = Engine::new(&config)?;
    let component = Component::from_file(&engine, "target/wasm32-wasi/debug/wasm_component_test.wat")?;

    let mut linker = Linker::new(&engine);
    HelloWorld::add_to_linker(&mut linker, |state: &mut MyState| state)?;

    let mut store = Store::new(
        &engine,
        MyState {
            name: "me".to_string(),
        },
    );
    let (bindings, _) = HelloWorld::instantiate(&mut store, &component, &linker)?;

    bindings.call_greet(&mut store)?;
    Ok(())
}

Unfortunately the following error message occurs:

Error: import `wasi-stderr` has the wrong type

Caused by:
    0: instance export `print` has the wrong type
    1: expected func found nothing

What am I doing wrong here?
When loading the component in the host code, don't I also have to load the adapter file as well?

view this post on Zulip Wasmtime GitHub notifications bot (Feb 18 2023 at 18:24):

pchickey edited a comment on issue #5830:

This error message isn't ideal. The import is missing, rather than of the wrong type.

You also need to add a host implementation of wasi snapshot 2 to the linker, that is currently found in preview2-prototyping/host.

However, please expect that everything in preview2-prototyping is still not mature: assume it is broken unless there are specifically tests in there passing.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 21 2023 at 17:34):

pchickey commented on issue #5830:

Additional discussion https://bytecodealliance.zulipchat.com/#narrow/stream/206238-general/topic/.E2.9C.94.20Guest.20missing.20imports


Last updated: Oct 23 2024 at 20:03 UTC