我正在尝试使用 nan为了计算附加组件中 float 组的内容,然后将其作为 Float32Array
返回。
但是,虽然 args 有 IsNumber()
和 NumberValue()
函数,但它只有一个 IsFloat32Array()
函数,没有 Float32Array()
。
NAN_METHOD(Calc) {
NanScope();
if (args.Length() < 2) {
NanThrowTypeError("Wrong number of arguments");
NanReturnUndefined();
}
if (!args[0]->IsNumber() || !args[1]->IsFloat32Array()) {
NanThrowTypeError("Wrong arguments");
NanReturnUndefined();
}
/* a vector of floats ? */ args[0]-> ???;
double arg1 = args[1]->NumberValue();
// some calculation on the vector
NanReturnValue(/* Return as a Float32Array array */);
}
最佳答案
接受 TypedArray 最好用 Nan::TypedArrayContents
Local<TypedArray> ta = args[0].As<TypedArray>();
Nan::TypedArrayContents<float> vfloat(ta);
float firstElement = (*vfloat)[0];
目前还没有用于构造类型化数组的 NAN 助手,但我在自己的代码中使用了这个助手:
template <typename T> struct V8TypedArrayTraits; // no generic case
template<> struct V8TypedArrayTraits<Float32Array> { typedef float value_type; };
template<> struct V8TypedArrayTraits<Float64Array> { typedef double value_type; };
// etc. v8 doesn't export anything to make this nice.
template <typename T>
Local<T> createTypedArray(size_t size) {
size_t byteLength = size * sizeof(typename V8TypedArrayTraits<T>::value_type);
Local<ArrayBuffer> buffer = ArrayBuffer::New(Isolate::GetCurrent(), byteLength);
Local<T> result = T::New(buffer, 0, size);
return result;
};
关于javascript - 在插件中使用 nan 接收和返回 Float32Array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30605496/