Stream: wasmtime

Topic: ✔ Calling a guest function with serialized data


view this post on Zulip Philpax (Nov 07 2022 at 23:41):

Hi there! I'm using wit-bindgen to define host functions that are called by my guest code. At present, I'm calling my guest code through a function fn runtime_exec(); however, I'd like to pass some structured data to that function for the guest runtime to do something with.

The way I'm thinking of doing this is serializing my data with bincode, and deserializing it on the guest side. However, to do this, I need a way to call the function with the serialized data as a byte-array; I'm not entirely sure how to do this, and I was studying what wit-bindgen generates, but I don't see where the guest memory address comes from.

To summarise my query: I'd like to define fn runtime_exec(data: &[u8]) (or similar on the guest side), and call that from the host side with a byte array (some serialized data). What would be the easiest way to do this?

view this post on Zulip Alex Crichton (Nov 08 2022 at 00:23):

In the same way that the host functions are modeled with *.wit the general intention is that you model guest functions the same way, so instead of needing bincode you could do something like runtime-exec: func(your: u32, structured: data) which would automatically generate bindings for the host as well to pass in structured data

view this post on Zulip Alex Crichton (Nov 08 2022 at 00:23):

if you need to model using bincode, though, you can do runtime-exec: func(data: list<u8>)

view this post on Zulip Philpax (Nov 08 2022 at 22:09):

Ah, I'd completely forgotten that was a viable option! I'd ruled out defining guest functions because my version of wit-bindgen doesn't have a way to share definitions between wit files, but as I only need a bytearray here that's fine. (Can't use structured definitions anyway as the data is somewhat freeform and varies between guests)

I'm running into some other issues, but at least I have a way forward. Thanks for the pointer! :bow:

view this post on Zulip Notification Bot (Nov 08 2022 at 22:09):

Philpax has marked this topic as resolved.


Last updated: Nov 22 2024 at 17:03 UTC