是否可以在 emcc 编译的 wasm 代码中导入 Javascript 函数?

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

我读了 https://developer.mozilla.org/en-US/docs/WebAssembly/Using_the_JavaScript_API and http://webassembly.org/docs/js/,似乎我找到的所有从 wasm 调用 Javascript 函数的例子都是用 LISP 启发的语法编写的。

是否可以使用 emscripten C 在 wasm 中导入 JS 函数?

是的,Emscripten documents how to do this。最简单的就是在C++中声明JS函数为extern "C"。它将在您的 wasm 模块的导出中,然后您需要将其作为导入传入。

再举一个例子,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.