How to use Emscripten compiled JavaScript within React / React Native

折月煮酒 提交于 2019-12-03 15:26:51
Chris Poe

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!