Stream: wasmtime

Topic: Wit & Component


view this post on Zulip Christoph Brewing (Jun 20 2023 at 10:14):

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
}

view this post on Zulip Ramon Klass (Jun 20 2023 at 10:16):

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

view this post on Zulip Ramon Klass (Jun 20 2023 at 10:21):

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

view this post on Zulip Alex Crichton (Jun 20 2023 at 14:38):

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

view this post on Zulip Christoph Brewing (Jun 21 2023 at 06:17):

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.

view this post on Zulip Christoph Brewing (Jun 21 2023 at 07:34):

..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:

  1. Without package specifier bindgen! refuses to compile now
  2. A default world refuses to compile

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

Minimal example of wasmtime about generating the wit for a component - GitHub - Finfalter/componenthost: Minimal example of wasmtime about generating the wit for a component
Repository for design and specification of the Component Model - component-model/design/mvp/WIT.md at main · WebAssembly/component-model
A language binding generator for WebAssembly interface types - wit-bindgen/tests/runtime/records.rs at main · bytecodealliance/wit-bindgen

view this post on Zulip Ramon Klass (Jun 21 2023 at 11:51):

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

view this post on Zulip Alex Crichton (Jun 21 2023 at 13:09):

Oh yes types are generated on a liveness basis right now and those aren't live so they aren't generated.

view this post on Zulip Christoph Brewing (Jun 21 2023 at 14:42):

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:

view this post on Zulip Christoph Brewing (Jun 22 2023 at 08:38):

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?

A language binding generator for WebAssembly interface types - wit-bindgen/tests/runtime/lists.rs at main · bytecodealliance/wit-bindgen

view this post on Zulip Christoph Brewing (Jun 22 2023 at 09:37):

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: Oct 23 2024 at 20:03 UTC