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?
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)
code red art has marked this topic as resolved.
Last updated: Nov 22 2024 at 16:03 UTC