1

当使用 emcc 的 MODULARIZE=1 选项时,有没有办法为 extern sendToJs函数提供函数:

emcc 编译命令

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

测试.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(...);
4

1 回答 1

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(...);
于 2017-07-24T02:51:36.360 回答