我刚经历Calling JavaScript from C/C++,并按照他们的指示进行。 我可以从C ++调用Js方法
C ++
#include <iostream>
#include <string>
#include <emscripten.h>
#include <emscripten/bind.h>
EM_JS(void, call_js, (), {
jsMethod();
});
bool callJsBack()
{
call_js();
return true;
}
EMSCRIPTEN_BINDINGS(module)
{
emscripten::function("callJsBack", &callJsBack);
}
Js
<script>
var Module = {
onRuntimeInitialized: function() {
console.log('Module.callJsBack(): ' + Module.callJsBack());
}
};
function jsMethod() {
alert('I am a js method!');
}
</script>
我想对jsMethod()进行参数化(希望在调用jsMethod()时从C ++传递字符串)。
function jsMethod(msg) {
alert(msg);
}
我没有找到实现此要求的任何示例或建议。
答案 0 :(得分:0)
找到答案:
C ++
EM_JS(void, call_js_agrs, (const char *title, int lentitle, const char *msg, int lenmsg), {
jsMethodAgrs(UTF8ToString(title, lentitle), UTF8ToString(msg, lenmsg));
});
bool callJsBackWithAgrs()
{
const std::string title = "Hello from C++";
const std::string msg = "This string is passed as a paramter from C++ code!";
call_js_agrs(title.c_str(), title.length(), msg.c_str(), msg.length());
return true;
}
EMSCRIPTEN_BINDINGS(module)
{
emscripten::function("callJsBackWithAgrs", &callJsBackWithAgrs);
}
JS:
var Module = {
onRuntimeInitialized: function() {
Module.callJsBackWithAgrs();
}
};
function jsMethodAgrs(title, msg) {
alert(title + '\n' + msg);
}
完整的工作示例: CallJsFromCpp