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.
here is someone else trying to do something similar : https://bytecodealliance.zulipchat.com/#narrow/stream/217126-wasmtime/topic/component.20Linker.20and.20instantiate
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
@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.
@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.
@Karel Hrkal (kajacx) Disregard the interface type question I was able to get it working by having a function that takes the type.
"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.
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?
@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.
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 {}
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
Thanks @Pat Hickey and @Karel Hrkal (kajacx). This can be marked as solved.
Bailey Hayes has marked this topic as resolved.
Last updated: Nov 22 2024 at 17:03 UTC