I have an emscripten application. I have a javascript file that has a function definition. I load that file into a string and then call emscripten_run_script
on it. Then, I try to call that function later using some inline EM_ASM
call, but it says the function definition can't be found.
我有一个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();
});
However, if I load that javascript file into a string and then append the string with the call to the function
但是,如果我将该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
How can I add a javascript function defined in a file to global scope to be used later on?
如何将文件中定义的javascript函数添加到全局范围以供以后使用?
The javascript file looks like this:
javascript文件如下所示:
function someFunc()
{
}
1 个解决方案
#1
1
In tests I've done this seems to work, which should be equivalent to what you've done:
在我做过的测试中,这似乎有效,这应该等同于你所做的:
#include <stdio.h>
#include <emscripten.h>
int main()
{
char script[] = "someFunc = function() {"
"console.log(\"hello\");"
"};";
emscripten_run_script(script);
EM_ASM({
someFunc();
});
}
Is your script.js
declaring the function to be local in scope (via var someFunc = function(){...};
or somesuch)? emscripten_run_script
isn't exactly like JavaScripts eval
and local variables only exist within the scope of emscripten_run_script
.
你的script.js是否声明函数在范围内是本地的(通过var someFunc = function(){...};或者某些东西)? emscripten_run_script与JavaScripts eval不完全相同,局部变量仅存在于emscripten_run_script的范围内。
#1
1
In tests I've done this seems to work, which should be equivalent to what you've done:
在我做过的测试中,这似乎有效,这应该等同于你所做的:
#include <stdio.h>
#include <emscripten.h>
int main()
{
char script[] = "someFunc = function() {"
"console.log(\"hello\");"
"};";
emscripten_run_script(script);
EM_ASM({
someFunc();
});
}
Is your script.js
declaring the function to be local in scope (via var someFunc = function(){...};
or somesuch)? emscripten_run_script
isn't exactly like JavaScripts eval
and local variables only exist within the scope of emscripten_run_script
.
你的script.js是否声明函数在范围内是本地的(通过var someFunc = function(){...};或者某些东西)? emscripten_run_script与JavaScripts eval不完全相同,局部变量仅存在于emscripten_run_script的范围内。