4 ms·
Somewhat related: You can also read and manipulate the contents of JavaScript typed arrays in V8: // js var x = new Float64Array([1, 2, 3]); bindin
by megalodon 11y ago
Somewhat related: You can also read and manipulate the contents of JavaScript typed arrays in V8:
// js
var x = new Float64Array([1, 2, 3]);
binding(x);
// c++
void binding(const v8::FunctionCallbackInfo<v8::Value>& info) {
void *data = info[0].As<v8::Float64Array>()->Buffer()->GetContents().Data();
double *x = reinterpret_cast<double*>(data);
// do whatever, e.g.
x[0] = 0;
}
- dkopi 11y agoBe ultra careful when doing this. First of all - make sure to get, validate and store the buffer's length (ByteLength()) Secondly - GetContents() is only for temporary access to a buffer. if you store that pointer and use it later (//do whatever), there's a great chance it will get garbage collected. Make sure to Externalize() the array if you intend on keeping the pointer for later.
- zbjornson 11y agoOr use the relatively new NaN helper TypedArrayContents: https://github.com/nodejs/nan/blob/master/doc/v8_misc.md#api_nan_typedarray_contents https://github.com/nodejs/nan/blob/master/doc/v8_misc.md#api...