我有一个使用emscripten编写的应用程序。我有一个包含函数定义的javascript文件。我将该文件加载到一个字符串中,然后调用emscripten_run_script
。稍后,我尝试使用一些内联的EM_ASM
调用来调用该函数,但是它显示找不到函数定义。
std::ifstream file("script.js"); // script.js has "someFunc" defined
std::string str((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
emscripten_run_script( str.c_str() );
// the below give error "someFunc not defined"
EM_ASM({
someFunc();
});
然而,如果我将该JavaScript文件加载到字符串中,然后使用函数调用附加该字符串。
std::ifstream file("script.js"); // script.js has "someFunc" defined
std::string str((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
auto combinedStr = str + "someFunc();";
emscripten_run_script( combinedStr.c_str() ); // works fine
如何将一个定义在文件中的javascript函数添加到全局作用域以便稍后使用?
这个javascript文件看起来像这样:
function someFunc()
{
}