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

问题描述

我无法在另一个JavaScript文件调用C函数,它给出了错误“在运行时初始化之前调用please refer to this link

我按照给定链接中的描述在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"]"

test.js文件中的代码

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

当我运行node test时出现错误

abort(Assertion Failed: native function `int_sqrt` called before runtime initialization)

解决方法

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

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

我在使用 emscripten 时遇到了同样的问题,这对我有用:

<script>
    Module.onRuntimeInitialized = () => { Module.functionName(param); }
</script>

其中 functionName 是您要调用的函数的名称,param 是您要传递给它的值。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...