javascript - Passing JS number array to emscripten C++ without reinterpret_cast -
have large number arrays in js want pass c++ processing. imho efficient way let js write directly c++ heap , pass pointer argument within direct call, like:
var size = 4096, bpe = float64array.bytes_per_element, buf = module._malloc(size * bpe), numbers = module.heapf64.subarray(buf / bpe, buf / bpe + size), i; // populate array , process numbers: parseresult(result, numbers); module.myfunc(buf, size);
the c++ functions process numbers like:
void origfunc(double *buf, unsigned int size) { // process data ... } void myfunc(uintptr_t bufaddr, unsigned int size) { origfunc(reinterpret_cast<double*>(bufaddr), size); }
that works expected wonder if there chance call origfunc
directly javascript rid of myfunc
, ugly reinterpret_cast
.
when tried bind origfunc via:
emscripten_bindings(test) { function("origfunc", &origfunc, emscripten::allow_raw_pointers()); }
... , call directly:
module.origfunc(buf, size);
i error:
uncaught unboundtypeerror: cannot call origfunc due unbound types: pd
is general restriction of emscripten or there "less dirty" solutions reinterpret_cast
work around?
you can use static_cast
if you
specify function takes
void *
ratheruintptr_t
;don't use
emscripten_bindings
, useemscripten_keepalive
+cwrap
/ccall
way of communicating js->c++ . reason,emscripten_bindings
way resulted ingettypename not defined
exception when tried it.
so function looks like:
extern "c" int emscripten_keepalive myfunc(void *bufaddr, unsigned int size) { origfunc(static_cast<double *>(bufaddr), size); return 0; }
which can called javascript by
module.ccall('myfunc', 'number' ['number', 'number'], [buf, size]);
Comments
Post a Comment