Node C ++ Addon - How to access a typed array (Float32Array) when it was passed as an argument?

I would like to use the V8 Float32Array data structure. How to initialize it?

I will also be interested in direct access to data in memory. How can I do that?

+4
source share
1 answer

Updated

The best way now is to use an assistant Nan::TypedArrayContents.

assert(args[i]->IsFloat32Array());
Local<Float32Array> myarr = args[i].As<Float32Array>();
Nan::TypedArrayContents<float> dest(myarr);
// Now use dest, e.g. (*dest)[0]

A good example of this is in node-canvas .


Original answer that shows why the helper is useful

API v8 , node/io.js. , , 0.12 io.js & lt; 3.0:

assert(args[i]->IsFloat32Array()); // These type-check methods are available.
Local<Float32Array> myarr = args[i].As<Float32Array>();
void* dataPtr = myarr->GetIndexedPropertiesExternalArrayData();

io.js> = 3.0 (v8 4.3) ArrayBuffer::GetContents. ( , 3.0.) .

0.10 TypedArrays . :

Local<Object> buffer = args[i]->Get(NanNew("buffer"))->ToObject();
void* dataPtr = buffer->GetPointerFromInternalField(0);

:

 Float32Array::New(ArrayBuffer::New(Isolate::GetCurrent(), size * 4), 0, size);
+6

All Articles