Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to import Javascript functions in emcc compiled wasm code?

I read https://developer.mozilla.org/en-US/docs/WebAssembly/Using_the_JavaScript_API and http://webassembly.org/docs/js/, and it seems all examples I've found for calling Javascript functions from wasm are written in the LISP-inspired syntax.

Is it possible to import JS functions in wasm using emscripten C?

like image 339
Chris Khoo Avatar asked Oct 29 '22 09:10

Chris Khoo


1 Answers

Yes, Emscripten documents how to do this. The easiest thing is to declare the JS function as extern "C" in C++. It'll be in your wasm module's exports, which you then need to pass in as an import.

As another example, the wasm waterfall build musl in JavaScript, and runs some GCC tests which it compiles to wasm. All C library functions are in JS, and wasm calls into JS for them. No magic: the C library functions are just declared as extern "C" and then passed in as an import.

like image 135
JF Bastien Avatar answered Jan 02 '23 20:01

JF Bastien