bytecodealliance / bytecodealliance/wit-bindgen
How to integrate wit with wasmtime?
- Dominant language
- Rust
- Stars
- 1.5k
- Forks
- 286
- Avg merge
- 6h 32m
- Merged PRs (30d)
- 19
Description
# What I want to do
Hi, I am newer to wit-bindgen. I want to define an API for wasm module and the host, then, the wasm module can run in the host and interact by the API.
# What I have tried
## Guest
I use the README demo for my test.
wit/host.wit
```
default world host {
import print: func(msg: string)
export run: func()
}
```
src/lib.rs
```
wit_bindgen::generate!("host");
struct MyHost;
impl Host for MyHost {
fn run() {
print("hello wolrd!");
}
}
export_host!(MyHost);
```
and I compile the lib.rs to wasm file as follow:
```
cargo build --target wasm32-unknown-unknown
```
## Host
```
[dependencies]
wasmtime = "8.0.1"
```
```rust
use wasmtime::*;
fn main() -> wasmtime::Result<()> {
let engine = Engine::default();
// my_wasm.wasm was compiled by the guest above
let module = Module::from_file(&engine, "xxx/my_wasm.wasm")?;
let mut linker = Linker::new(&engine);
linker.func_wrap("host", "print", |caller: Caller<'_, u32>, param: i32| {
println!("{}", param);
println!("my host state is: {}", caller.data());
})?;
let mut store = Store::new(&engine, 0);
let instance = linker.instantiate(&mut store, &module)?;
let hello = instance.get_typed_func::<(), ()>(&mut store, "run")?;
hello.call(&mut store, ())?;
Ok(())
}
```
I got an error...
```
Error: unknown import: `$root::print` has not been defined
```
so how to solve this problem? any ideas?
another question, the code below define the `parm` as i32, when I change the type to String、&str or &String
```
linker.func_wrap("host", "print", |caller: Caller<'_, u32>, param: i32| {
println!("{}", param);
println!("my host state is: {}", caller.data());
})?;
```
I got an error...
```
IntoFunc<_, _, _>` is not satisfied
the trait `IntoFunc<_, _, _>` is not implemented for closure
```
so how to define a host function that can receive a string param?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.