4

I am trying to place a Javascript function inside a WebAssembly.Table, where the element type of the table is required to be funcref. The following lines can be executed in node but raise an exception when I try to put the function inside the table:

table = new WebAssembly.Table({initial: 1, element: "anyfunc"})
func = function() { return 42 }
table.set(0, func)

The exception is:

TypeError: WebAssembly.Table.set(): Argument 1 is invalid for table of type funcref

Is it possible to convert from a Javascript function to a funcref?

1 Answer 1

4

This doesn't work because Wasm wouldn't be able to tell what type to assume for this function. The upcoming WebAssembly.Function constructor will fill that gap:

let func_i32 = new WebAssembly.Function({parameters: [], results: ["i32"]]}, func)
table.set(0, func_i32)

However, this is not yet standardised, so not yet available in browsers (at least not without turning on some flags).

Edit: The only way currently is to import a JS function. You can abuse that to convert a JS function into a Wasm function by funnelling it through a little auxiliary module:

(module
  (func (export "f") (import "m" "f") (result i32))
)

If you convert that module to binary and instantiate it with your function as import, then you get out a proper Wasm function:

let instance = new WebAssembly.Instance(new WebAssembly.Module(binary), {m: {f: func}})
let func_i32 = instance.exports.f
table.set(0, func_i32)  // works
Sign up to request clarification or add additional context in comments.

4 Comments

I was studying the Emscripten runtime and creating a small auxiliary module is exactly the approach that they take.
Are there any browsers that currently support WebAssembly.Function behind a flag?
Chrome does, I believe Firefox as well.
I think I found it for Chrome: enable-experimental-webassembly-features specifically: chromium.googlesource.com/v8/v8/+/master/src/wasm/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.