【发布时间】:2019-01-09 17:51:11
【问题描述】:
我目前正在使用 Emscripten 将一个基本的 C 函数编译成 JavaScript,以便在 React Native 项目中使用。但是,当我从 React 代码中导入 Module 时,Module 对象为空。这发生在 React 和 React Native 项目中。
在我的终端中使用 node ./index.js 运行 index.js 会返回预期结果。
我正在编译 ping.c 并使用此命令输出 ping.js:
emcc ping.c -o ping.js -s WASM=0 -s EXPORTED_FUNCTIONS='["_pingIt"]'
ping.c:
#include <stdio.h>
#include <emscripten.h>
EMSCRIPTEN_KEEPALIVE
int pingIt() {
return 1;
}
index.js:
let Module = require('./ping.js');
module.exports = Module;
我正在从 index.js 导出模块并将其导入到我的 React 代码中。
当前行为
// Running in React
console.log(Module); // returns {}
预期行为
// Running in React
console.log(Module._pingIt()); // should return 1
【问题讨论】:
-
我不确定 WASM 是否支持 ES6 导入语法。您是否尝试过在您的
index.html页面中添加script标记并将Module作为全局变量访问? -
@seanulus 我的主要目标是让它在 React Native 项目中运行。我在
emcc命令中使用WASM=0选项禁用了WASM。
标签: javascript reactjs react-native emscripten