Stream: git-wasmtime

Topic: wasmtime / issue #5801 How should I call a function point...


view this post on Zulip Wasmtime GitHub notifications bot (Feb 16 2023 at 14:55):

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 and run_callback are functions that the host-side provides.

I expect that the host side will call the function pointer (casted to u32) when run_callback is called, what should I do to implement this?

view this post on Zulip Wasmtime GitHub notifications bot (Feb 16 2023 at 15:24):

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 a Table which is exported from the module. After loading a Func from that table then you can cast it to the appropriate type and then call it.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 16 2023 at 15:24):

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 and run_callback are functions that the host-side provides.

I expect that the host side will call the function pointer (casted to u32) when run_callback is called, what should I do to implement this?


Last updated: Dec 23 2024 at 12:05 UTC