How to call wasm export functions with function pointer as parameter through wasmer-c-api?

Hi, I have been working on calling wasm export functions recently, and I'm trying to make it though wasmer-c-api. But I've run into a problem that I can't solve. One funtion I'd like to call receives a funtion pointer as parameter which can be presented as follow codes.

(func $call_add (type 3) (param i32 i32 i32) (result i32)

And the origin C code of this wat is like this:

int call_add(int a, int b, int (*add)(int, int)) {
    // ...

And now I want call this function in another C file with wasmer-c-api like this.

int add(int a, int b) { return a + b; }
void call_function() {
    // construct the `args` and `results`
    // how to pass `add`?
    wasm_val_t args_val[3] = { WASM_I32_VAL(1), WASM_I32_VAL(2), WASM_I32_VAL(1) };
    wasm_val_vec_t args = WASM_ARRAY_VEC(args_val);
    wasm_val_t results_val[1] = { WASM_INIT_VAL };
    wasm_val_vec_t res = WASM_ARRAY_VEC(results_val);
    // get the function ...
    // ...
    // call the function
    wasm_func_call(call_add, &args, &res);

You may notice that the thrid args should be wrong here. It should be the index of function add in the wasm table. But the problem is that wasmer-c-api doesn't support wasm_table_get or wasm_table_set. Except for that, we should also grow the table and put the function to the table which is not supported either.

And in real situation, the function pointer is much more complicated. So trying to implement it in the origin wasm is hard. So is there any other ways to make this work?


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.