在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(...);
答案 0 :(得分:0)
这似乎有效:
<强>使用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(...);