bytecodealliance / bytecodealliance/wasmtime
No matching implementation when load wasm component
- Dominant language
- Rust
- Stars
- 18.6k
- Forks
- 1.8k
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 126
Description
After i follow guide [here](https://component-model.bytecodealliance.org/language-support/rust.html) finishing my first cargo component project, i tried to wrote my own rust program loading wasm component with wasmtime::component module.
Here is my component code:
world.wat:
```
package component:hello-world;
/// An example world for the component to target.
world example {
export hello-world: func(string) -> string;
}
```
lib.rs:
```
#[allow(warnings)]
mod bindings;
use bindings::Guest;
struct Component;
impl Guest for Component {
/// Say hello!
fn hello_world(test: String) -> String {
"Hello, World!".to_string()
}
}
bindings::export!(Component with_types_in bindings);
```
I compile this code to wasm with cli "cargo component build", the i copy the hello_world.wasm file to my rust project.
Here is my app code:
```
use anyhow::Result;
use wasmtime::*;
use std::env;
fn main() -> Result<()> {
let engine = Engine::default();
let mut path = env::current_dir().unwrap();
path.push("hello_world.wasm");
println!("file_path: {:?}", path);
let bytes = std::fs::read(path)?;
let my_component = wasmtime::component::Component::new(&engine, &bytes)?;
let mut linker = wasmtime::component::Linker::new(&engine);
let mut store = Store::new(&engine, 4);
let instance = linker.instantiate(&mut store, &my_component)?;
let func = instance.get_typed_func::<(String,),(String,)>(&mut store, "hello_world")?;
let result = func.call(&mut store, ("hello_world".to_string(),))?;
println!("result: {:?}", result);
Ok(())
}
```
Error ocurred when i tried to run my app.
```
Error: component imports instance `wasi:cli/environment@0.2.3`, but a matching implementation was not found in the linker
Caused by:
0: instance export `get-environment` has the wrong type
1: function implementation is missing
```
So here are my questions:
1. How to import this dependency to wasm? I tried "cargo component add wasi:cli" , but it doesn't work.
2. I wonder if this is the right way to run component?"let result = func.call(&mut store,("hello_world".to_string(),))?; " Can i pass the string to wasm and then get the result?
THANKS!
[hello_world.wasm.zip](https://github.com/user-attachments/files/20508259/hello_world.wasm.zip)
Contributor guide
Assessment
This issue has not been assessed yet.