我在JS中有大量数组,我想传递给C++进行处理.恕我直言,最有效的方法是让JS直接写入C++堆并在直接调用中将指针作为参数传递,如:
var size = 4096,
BPE = Float64Array.BYTES_PER_ELEMENT,
buf = Module._malloc(size * BPE),
numbers = Module.HEAPF64.subarray(buf / BPE, buf / BPE + size),
i;
// Populate the array and process the numbers:
parseResult(result, numbers);
Module.myFunc(buf, size);
Run Code Online (Sandbox Code Playgroud)
用于处理数字的C++函数如下所示:
void origFunc(double *buf, unsigned int size) {
// process the data ...
}
void myFunc(uintptr_t bufAddr, unsigned int size) {
origFunc(reinterpret_cast<double*>(bufAddr), size);
}
Run Code Online (Sandbox Code Playgroud)
这是按预期工作,但我想知道是否有任何机会origFunc直接从Javascript 调用以摆脱myFunc和丑陋 reinterpret_cast.
当我尝试通过以下方式绑定origFunc时:
EMSCRIPTEN_BINDINGS(test) {
function("origFunc", &origFunc, emscripten::allow_raw_pointers());
}
Run Code Online (Sandbox Code Playgroud)
...并直接调用它:
Module.origFunc(buf, size);
Run Code Online (Sandbox Code Playgroud)
我收到错误: …
我用公共枚举绑定一个类
class Foo {
public:
Foo();
enum class Bar { ALPHA, BRAVO }
};
Foo::Foo() { }
EMSCRIPTEN_BINDINGS(Foo) {
.enum_<Foo::Bar>("FooBar")
.value("ALPHA", Foo::Bar::ALPHA)
.value("BRAVO", Foo::Bar::BRAVO);
}
Run Code Online (Sandbox Code Playgroud)
现在我可以通过以下方式访问 Javascript 中的枚举:
Module.FooBar.ALPHA
Run Code Online (Sandbox Code Playgroud)
但实际上我想通过以下方式访问它:
Module.Foo.Bar.ALPHA
Run Code Online (Sandbox Code Playgroud)
有没有机会通过 Emscripten Bindings 实现这一点,或者以下 hackish JS 代码是唯一的方法吗?
Module.Foo.Bar = Module.FooBar;
delete Module.FooBar;
Run Code Online (Sandbox Code Playgroud)