After having built a component I would like to make use of it from a Rust host. My question is: what dependencies do I have to use?
My current assumption is to use a dependency like wasmtime = { version = "9.0.4", features = ["component-model"] }
.
This works fine to call functions but types like records do not seem to be recognized, e.g. I am not able to use Test from the following interface:
interface decoding {
record test {
t1: u32,
}
}
default world decoder {
export exports: self.decoding
}
someone more qualified should answer, but my understanding is that most tooling uses the wit syntax of wasmtime-8 and the new syntax is used by wasmtime-11 (current main branch) and I'm entirely unsure what to do about wasmtime 9 and 10
also wit.md hasn't been updated and I only know about the new syntax because wasi-host uses it internally, I would also like to know where the new wit format is documented
In theory all you need is wasmtime
-the-crate once you have the component itself you'd like to run. The syntax you're showing here should be compatible with Wasmtime 9 (not Wasmtime 10 though). There may be bugs in bindgen!
, though, so it's recommended to use the latest Wasmtime (10 being released today) if possible
Thank you, sounds reasonable. I will test/experiment with Wasmtime 10. In case, something seems still weird, I will come back if that is ok.
..having tried a minimal example based on wasmtime = { version = "10.0.0", features = ["component-model"] }
, my first impression is that quite a few things have changed:
bindgen!
refuses to compile nowdefault
world refuses to compileMy initial problem, though, persists: regardless what types I define in the interface, they do not seem to be defined/considered . For example, the record mytest
, the type t2
and the union fancyunion
are not generated for the following interface:
// without `package` specifier this does not compile
package sth:local
interface decoding {
record mytest {
t1: u32,
}
type t2 = tuple<u32, u64>
union fancyunion {
u64,
string,
}
my-function: func() -> (a: u32, b: float32)
}
// `default world decoder` does NOT compile
world decoder {
export decoding
}
When I cargo expand
there is no hint to mentioned types except in a closing string. The function definition of my-function
, in contrast, is generated as expected. Based on everything I see in the general description of WIT as well as in the examples my expectation is that type definitions should be generated. How am I wrong?
just curious, does anything change if my-function uses and/or returns the other types in the interface? I have never tried defining types that are used nowhere in a function
Oh yes types are generated on a liveness basis right now and those aren't live so they aren't generated.
Confirmed! Following Ramon's suggestion, e.g. my-function: func(my: mytest) -> t2
, it shows up in cargo expand
now and I can use
it :+1: :pray:
May I ask you for a further hint? Given I have the following interface (based on previous discussion):
interface decode {
record some-handle {
name: string,
id1: u64,
id2: u32,
}
decode-given-handle: func(handles: list<some-handle>, payload: list<u8>) -> list<tuple<string>>
}
world decoder {
export decode
}
The essentials are: The list of a record shall be passed as parameter to a function. The compiler expects a &[&T] which is also nicely illustrated in the examples.
My problem is: I do not know how to call the corresponding function such that the compiler accepts it. My usual error message is like:
the size for values of type `[&SomeHandle]` cannot be known at compilation time
How would you call function decode_given_handle
from a Rust host?
ok, that works (finally):
// let some_handles_ref :&Vec<SomeHandle> ..
let some_handle_refs: Vec<&SomeHandle> = some_handles_ref.iter().collect();
bindings.interface0.call_decode_given_handle(&mut store, &some_handle_refs, &payload)
Last updated: Nov 22 2024 at 16:03 UTC