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_callbackandrun_callbackare functions that the host-side provides.I expect that the host side will call the function pointer (casted to
u32) whenrun_callbackis 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_funcgiven to the host should be used to index aTablewhich is exported from the module. After loading aFuncfrom 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_callbackandrun_callbackare functions that the host-side provides.I expect that the host side will call the function pointer (casted to
u32) whenrun_callbackis called, what should I do to implement this?
Last updated: Dec 06 2025 at 06:05 UTC