Stream: general

Topic: ✔ Component model implement imported wit interface


view this post on Zulip Cole Nelms (Jul 18 2023 at 22:02):

I want to use wit import in wasmtime host. I want to import an interface in my wit world. Then I want to impl the imported interface functions in the wasmtime host. So far I can only impl the world, but I want to impl the imported interface.

I have seen this done with an exported interface in the guest component. It used syntax like: crate::exports::thing::Thing. I can't seem to get the same effect with the imports though.

view this post on Zulip Pat Hickey (Jul 19 2023 at 17:32):

here is someone else trying to do something similar : https://bytecodealliance.zulipchat.com/#narrow/stream/217126-wasmtime/topic/component.20Linker.20and.20instantiate

view this post on Zulip Pat Hickey (Jul 19 2023 at 17:33):

see if anything in there helps you, and if not please give more specifics on what you have done so far and what you are struggling with

view this post on Zulip Karel Hrkal (kajacx) (Jul 20 2023 at 10:31):

@Cole Nelms Hello, I have an example of interface imports and exports in this repo. Look at the guest-rust for the guest code - how to call the imported functions, and runtime-rust-wasmtime for how to define them in the host runtime.

Various projects related to WASM, each in it's own branch, so that I don't have 50 repositories. - GitHub - kajacx/wasm-playground at wit-bindgen

view this post on Zulip Cole Nelms (Jul 21 2023 at 02:25):

@Karel Hrkal (kajacx) It worked thank you. I have a few questions though. Why does the host use wasmtime-wit-bindgen? I deleted it and it seemed to work fine. Also, do you happen to know how to use a wit record inside of a wit interface from rust? I noticed if its just defined in the world it is in the global scope in rust, but I wanted to know how to use it in an interface.

view this post on Zulip Cole Nelms (Jul 21 2023 at 05:25):

@Karel Hrkal (kajacx) Disregard the interface type question I was able to get it working by having a function that takes the type.

view this post on Zulip Karel Hrkal (kajacx) (Jul 21 2023 at 07:07):

"Why does the host use wasmtime-wit-bindgen? I deleted it and it seemed to work fine." I was trying a few different things and forgot to delete it, thanks for the reminder. The wit-component dependency was also unused.

view this post on Zulip Karel Hrkal (kajacx) (Jul 21 2023 at 07:09):

That's good that you managed to get the types working. Did you do it like this? https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md#interfaces-worlds-and-use Or did you use some other method?

view this post on Zulip Cole Nelms (Jul 21 2023 at 20:14):

@Karel Hrkal (kajacx) I happened to actually be using wit-component so I didn't think to point that out.

If the wit file is like this:

package my:example

interface imports {
  record my-type {
    active: bool,
  }
  do-stuff: func(arg: my-type)
}

world my-world {
  import imports
  export run: func()
}

Then in both the rust host and guest I can use the my::example::imports::MyType type.

However, if the wit file looks like this:

package my:example

interface imports {
  record my-type {
    active: bool,
  }
}

world my-world {
  import imports
  export run: func()
}

Then I can no longer use the my::example::imports::MyType type. It seems that the interface needs a function that takes the type in order to import the type.

view this post on Zulip Karel Hrkal (kajacx) (Jul 22 2023 at 07:24):

import doesn't "import" the types in the interface, it specifies that the host will have to implemented the functions in the "imported" interface.

You can "include" a type from an interface with use:

package example:protocol

interface types {
  record point {
    x: s32,
    y: s32,
  }
}

world my-world {
  use types.{point}

  import import-point: func(pnt: point) -> point
  export move-point: func(pnt: point) -> point

  import print: func(msg: string)
  // Say "Hello" to stdout, use with wasi
  export say-hello: func()
}

However, you will have to add an "empty impl" on the host, I am not sure why:

impl example::protocol::types::Host for State {}

view this post on Zulip Pat Hickey (Jul 24 2023 at 03:16):

this is a bug in wasmtime's wit-bindgen at the moment, it emits an trait and requires it in the linker constraints for your world for every interface in the worlds imports, even if the interface contains no functions. nobody has taken the time to fix this bug, but if youd like to work on it, it should be pretty straightforward

view this post on Zulip Cole Nelms (Jul 28 2023 at 04:48):

Thanks @Pat Hickey and @Karel Hrkal (kajacx). This can be marked as solved.

view this post on Zulip Notification Bot (Jul 28 2023 at 22:16):

Bailey Hayes has marked this topic as resolved.


Last updated: Dec 23 2024 at 13:07 UTC