简体   繁体   中英

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

When using the MODULARIZE=1 option with emcc, is there a way to supply a function for the extern sendToJs function:

emcc compile command

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

This seems to be working:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM