In SpiderMonkey you can use ArrayBuffer.
var buffer = new ArrayBuffer(16)
var int32View = new Int32Array(buffer);
for (var i = 0; i < int32View.length; i++) {
int32View[i] = i * 3
}
array_dump(buffer)
now C++ part:
#include "js/jstypedarray.h"
static JSBool my_array_dump(JSContext *cx, uintN argc, jsval *vp) {
JSObject *obj;
JS_ValueToObject(cx, vp[0 + 2], &obj);
js::ArrayBuffer *A;
A = js::ArrayBuffer::fromJSObject(obj);
int *B = (int*) A->data;
for (int i = 0; i < A->byteLength / 4; i++) printf("%i ", B[i]);
return JS_TRUE;
}
Seems like this way you can pass huge amounts of data between JS and C/C++ with virtualy no overhead.
Of course it would be better to have this clearly explained in SpiderMonkey docs. But as it often happens when you hack into Mozilla projects, you eventually end up digging the includes and sources, so I think this answer might be useful for somebody.