使用 emscripten::val 可以在c++函数中可以返回int、string、bool等多种类型。
#include <emscripten/val.h>
#include <emscripten.h>
using namespace std;
val test_func(const string &str)
{
val res = val::undefined();
if (str.compare("int") == 0) res = val(123);
else if (str.compare("string") == 0) res = val("abc");
else if (str.compare("bool") == 0) res = val(true);
return res;
}
EMSCRIPTEN_BINDINGS(my_module) {
emscripten::function("test_func", &test_func);
}
浏览器通过Module模块调用函数: