断言失败:在 .js 文件 ( emscripten ) 中调用 c 函数时,在运行时初始化错误之前调用本机函数 `int_sqrt`

mys*_*ous 6 emscripten asm.js

我无法在另一个 JavaScript 文件中调用 C 函数,它给出错误“在运行时初始化之前调用”, 请参阅此链接

我按照给定链接中的描述在 emscripten 中编译了 C 代码,并在 test.js 文件中使用了生成的 asm.js 文件。用于生成 asm 的命令:-

emcc test/hello.cpp -o hello.html -s EXPORTED_FUNCTIONS="['_int_sqrt']" -s EXPORTED_RUNTIME_METHODS="["ccall", "cwrap"]"
Run Code Online (Sandbox Code Playgroud)

test.js 文件中的代码:

var Module = require('./asm.js');
var test =  Module.cwrap('int_sqrt', 'number', ['number']);
console.log(test(25));
Run Code Online (Sandbox Code Playgroud)

当我运行时node test出现错误

abort(Assertion failed: native function `int_sqrt` called before runtime initialization)
Run Code Online (Sandbox Code Playgroud)

Nou*_*leb 7

你应该等待运行时初始化。
尝试这个:

var Module = require("./lib.js");
var result = Module.onRuntimeInitialized = () => {
    Module.ccall('myFunction', // name of C function 
        null, // return type
        null, // argument types
        null // arguments
   );
}
Run Code Online (Sandbox Code Playgroud)

  • 最终,我得到了答案,我想如果我们在编译时添加 WASM = 0 标志,那么我们可能不会等待运行时 init。非常感谢您的回答,先生。 (3认同)