在使用 "MODULARIZE=1" 导出的 Emscripten WebAssembly 模块中为外部函数提供 JS 函数?

Supplying a JS function for extern function in Emscripten WebAssembly module exported with "MODULARIZE=1"?

在 emcc 中使用 MODULARIZE=1 选项时,有没有办法为 extern sendToJs 函数提供一个函数:

emcc编译命令

emcc test.cpp -O3 -s WASM=1 -s MODULARIZE=1 -o test.js

test.cpp

...
extern void sendToJs(int num);
...

Javascript

const Module = require('test.js');

Module({
        wasmBinary: wasmBinary,

        // I was hoping this kind of thing might work:
        env: {
            _sendToJs: num => console.log('fromWasm', num)
        }
    })
    .then(...);

这似乎有效:

Javascript:

Module({
    wasmBinary: wasmBinary,

    // Override instantiateWasm
    instantiateWasm: (info, receiveInstance) => {
        info.env._sendToJs = (num) => console.log('sendToJs', num);

        WebAssembly.instantiate(wasmBinary, info)
            .then(response => receiveInstance(response.instance))
            .catch(error => console.error(error));

            return true;
        }
})
.then(...);