I have a C/C++ function that returns two arrays each the size that is unknown before the call. I need to call this function from JavaScript. (For simplicity, one array is returned in the example).
extern "C" {
void produce_object_3d(float* verts, int *num_verts);
}
Note that JavaScript does not know std::vector
and boost:array
and other types. I currently pre-allocate some space, but it will not work. Here is the code on the JavaScript side:
var verts_address = Module._malloc(FLOAT_SIZE*3*max_verts);
var nv_address = Module._malloc(INT_SIZE*1);
//
produce_object_3d (verts_address, nv_address);
//
var nverts = Module.HEAPU32[nv_address/INT_SIZE];
var verts = Module.HEAPF32.subarray(verts_address/FLOAT_SIZE, verts_address/FLOAT_SIZE + 3*nverts);
This is not efficient. Also what if the size of the result is large and there is not enough memory pre-allocated?