Officeyutong opened issue #5801:
#[link(wasm_import_module = "env")] extern "C" { // We are using `wasm32-wasi`, so pointers are 32bit fn register_callback(ptr: u32); fn run_callback(); } fn callback_func() { println!("Called!"); } fn main() { unsafe { register_callback(callback_func as u32); run_callback(); } }
register_callback
andrun_callback
are functions that the host-side provides.I expect that the host side will call the function pointer (casted to
u32
) whenrun_callback
is called, what should I do to implement this?
alexcrichton commented on issue #5801:
Function pointers in WebAssembly are represented as integers which are indexes into a table in the module. The
callback_func
given to the host should be used to index aTable
which is exported from the module. After loading aFunc
from that table then you can cast it to the appropriate type and then call it.
alexcrichton closed issue #5801:
#[link(wasm_import_module = "env")] extern "C" { // We are using `wasm32-wasi`, so pointers are 32bit fn register_callback(ptr: u32); fn run_callback(); } fn callback_func() { println!("Called!"); } fn main() { unsafe { register_callback(callback_func as u32); run_callback(); } }
register_callback
andrun_callback
are functions that the host-side provides.I expect that the host side will call the function pointer (casted to
u32
) whenrun_callback
is called, what should I do to implement this?
Last updated: Nov 22 2024 at 16:03 UTC