How to use Emscripten compiled JavaScript within React / React Native

后端 未结 1 740
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-29 13:32

I\'m currently using Emscripten to compile a basic C function into JavaScript to use within a React Native project. However, when I import Module from inside Re

相关标签:
1条回答
  • 2020-12-29 14:04

    I stumbled across a MODULARIZE setting in the Emscripten docs here. I edited the emcc command:

    emcc ping.c -o ping.js -s WASM=0 -s ENVIRONMENT=web -s EXTRA_EXPORTED_RUNTIME_METHODS='["cwrap"]' -s MODULARIZE=1
    

    MODULARIZE=1 being the magic bit

    Now within the index.js file:

    let Module = require('./ping.js'); // Your Emscripten JS output file
    let pingIt = Module().cwrap('pingIt'); // Call Module as a function
    
    module.exports = pingIt;
    

    Now in the React component you can import pingIt from '<file-location>'; and call the function like any other pingIt().

    Hope someone finds this useful! I couldn't find many examples of using Emscripten alongside React.

    0 讨论(0)
提交回复
热议问题