bytecodealliance / bytecodealliance/wit-bindgen

How to integrate wit with wasmtime?

Offen
#567 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Rust
Sterne
1.5k
Forks
288
Ø Merge
6 Std. 32 Min.
Gemergte PRs (30 T.)
19

Beschreibung

# 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?

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Rechercherichtung

Beginne mit dem README-Demo, wit/host.wit, src/lib.rs und dem Wasmtime-Host-Code mit wasmtime 8.0.1. Untersuche die generierten Guest-Imports und die Linker-Registrierung für den `$root::print`-Import und prüfe anschließend, wie String-Parameter an der Wasmtime-Grenze dargestellt werden. Als erledigt gilt die Dokumentation einer funktionierenden Host/Guest-Integration und eines Beispiels für einen String-Callback.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
rust, wasm
Bereich
api, backend
Issue-Typ
Dokumentation
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.