在使用“MODULARIZE = 1”导出的Emscripten WebAssembly模块中为extern函数提供JS函数?

时间:2017-07-24 01:05:23

标签: emscripten webassembly

在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(...);

1 个答案:

答案 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(...);
相关问题