Calling a C Style Function Pointer in WebAssembly from JavaScript

Is there a way to access function pointers living inside the WebAssembly module?

For example, given the following “module” compiled into WebAssembly:

extern void set_callback(void (*callback)(void *arg), void *arg);

static void callback(void *arg)
{
  /* ... */
}

int main() {
  set_callback(&callback, 0);
  return 0;
}

Can a do_callbackJavaScript implementation call a callback without having to rely on exporting the intermediate C function to make the actual function call?

var instance = new WebAssembly.Instance(module, {
  memory: /* ... */
  env: {
    set_callback: function set_callback(callbackptr, argptr) {
      // We only got the pointer, is there any  
    },
  },
}); 

By exporting an intermediate function, I mean that I could add an internal function with public visibility.

do_callback(void (*callback)(void *arg), void *arg)
{
  callback();
} 

Then the JavaScript function set_callbackcan call the function pointer through the delegate function do_callback.

function set_callback(callbackptr, argptr) {
  instance.exports.do_callback(callbackptr, argptr);
}

But, it is preferable to do this without missing this explicit indirect relation, is this possible using function tables?

+6
1

Javascript.

. Javascript, integer . Table.prototype.get(), .

...

set_callback: function set_callback(callbackptr, argptr) {
  tbl.get(callbackptr)(argptr);
},

...

MDN "": https://developer.mozilla.org/en-US/docs/WebAssembly/Using_the_JavaScript_API#Tables

: , .

fptr.c emcc fptr.c -Os -s WASM=1 -s SIDE_MODULE=1 -o fptr.wasm

typedef int (*fptr_type)(void);

extern void pass_fptr_to_js(fptr_type fptr);

static int callback_0(void)
{
    return 26;
}

static int callback_1(void)
{
    return 42;
}

void run_test()
{
    pass_fptr_to_js(callback_0);
    pass_fptr_to_js(callback_1);
}

fptr.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebAssembly Experiment</title>
</head>
<body>
    <h3>Check the console.</h3>
    <script type="text/javascript">
        fetch('fptr.wasm').then(function(response) {
            response.arrayBuffer().then(function(buffer) {
                WebAssembly.compile(buffer).then(function(module) {
                    var imports = {};

                    imports.env = {};

                    imports.env.memoryBase = 0;
                    imports.env.memory = new WebAssembly.Memory({ initial: 256 });
                    imports.env.tableBase = 0;
                    imports.env.table = new WebAssembly.Table({ initial: 4, element: 'anyfunc' });

                    imports.env["abort"] = function() {
                        console.error("ABORT");
                    };

                    imports.env["_pass_fptr_to_js"] = function(fptr) {
                        console.log("table index: " + fptr + ", return value: " + imports.env.table.get(fptr)());
                    };

                    WebAssembly.instantiate(module, imports).then(function(instance) {
                        instance.exports["__post_instantiate"]();
                        instance.exports["_run_test"]();
                    });
                });
            });
        });
    </script>
</body>
</html>
0

All Articles