bytecodealliance / bytecodealliance/wit-bindgen
How to integrate wit with wasmtime?
- Lingua principale
- Rust
- Stelle
- 1.5k
- Fork
- 286
- Merge medio
- 6h 32m
- PR unite (30g)
- 19
Descrizione
# 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?
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Direzione di ricerca
Inizia dalla demo del README, wit/host.wit, src/lib.rs e dal codice host di Wasmtime usando wasmtime 8.0.1. Esamina gli import generati del guest e la registrazione del linker per l’import `$root::print`, quindi verifica come vengono rappresentati i parametri stringa al confine di Wasmtime. Il lavoro è completo quando saranno documentati un’integrazione host/guest funzionante e un esempio di callback stringa.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- rust, wasm
- Ambito
- api, backend
- Tipo di issue
- Documentazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100