Search code examples
cclangwebassemblylld

How to use WebAssembly Reference Types with Clang


I'm writing a WebAssembly application in C using Clang and LLD.

How do I write a C function that takes an externref as a parameter and how do I call this function from JavaScript? How do I use the externref value in C to call an imported function that is defined in JavaScript?


Solution

  • Right now this is not possible, although there is ongoing work in clang and LLVM to make it possible. Currently the closest you can get would be to write your function in assembly. Here's an example, although you would have to look at the other related test files (or clang -cS output) to figure out how to export the function, etc. Unfortunately this assembly format isn't well documented in a central place for WebAssembly.

    https://github.com/llvm/llvm-project/blob/79aad89d8d29029b4e937137e6b8619ee8a4b691/lld/test/wasm/externref.s