Stream: wasmtime

Topic: ✔ How do i return Func from guest function?


view this post on Zulip code red art (Oct 09 2023 at 21:36):

I was just thinking of how i can return a callback function from guest rust to host rust, which can be called at some later point by host.
do i just return the fn pointer as a i32? Then guest could maybe export a "callback caller" fn which will take the ptr (i32) and call it inside guest.
or is it possible to instead return a func somehow?

view this post on Zulip Chris Fallin (Oct 09 2023 at 22:05):

Yes, the usual WASI/LLVM ABI is that function pointers become i32 values that are actually indices into a table of funcrefs (in other words, the value that call_indirect takes as its func argument). So you can return i32 from the guest, and later use that in the host to look up a funcref in the table and call it (or alternately, call into a "trampoline" in the guest that then does the call_indirect, if that's easier)

view this post on Zulip Notification Bot (Oct 09 2023 at 23:36):

code red art has marked this topic as resolved.


Last updated: Dec 23 2024 at 14:03 UTC