Node C ++插件-在将类型数组作为参数传递时,如何访问类型数组(Float32Array)? [英] Node C++ Addon - How do I access a Typed Array (Float32Array) when it's beenn passed as an argument?
问题描述
我想利用V8 Float32Array数据结构.我该如何初始化?
I'd like to make use of the V8 Float32Array data structure. How can I initialise it?
我也对直接内存访问数据感兴趣.那怎么办?
I'd also be interested in direct memory access to the data. How could that be done?
推荐答案
已更新
The best way now is to use the helper 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]
在节点画布.
原始答案,它显示了为什么帮助程序有用
v8 API现在正在快速变化,因此这取决于您的node/io.js版本.要访问作为参数提供的数组中的数据,这应通过io.js< 3.0:节点0.12起作用:
The v8 API is changing rapidly right now so this depends on your version of node/io.js. To access the data from the array provided as an argument, this should work for node 0.12 through io.js <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发布时进行更新.)文档为
In io.js >=3.0 (v8 4.3) you should instead use ArrayBuffer::GetContents
. (I haven't used this yet and will update this when 3.0 is released.) Docs are here.
在节点0.10中,对TypedArrays进行了自制.这是一种实现方式:
In node 0.10, TypedArrays were home-brewed. This was one way of doing it:
Local<Object> buffer = args[i]->Get(NanNew("buffer"))->ToObject();
void* dataPtr = buffer->GetPointerFromInternalField(0);
构造类型数组可以通过以下方式完成:
Constructing a typed array can be done with:
Float32Array::New(ArrayBuffer::New(Isolate::GetCurrent(), size * 4), 0, size);
这篇关于Node C ++插件-在将类型数组作为参数传递时,如何访问类型数组(Float32Array)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!