From f72cc86fc500fe05d9b8dad5ffd757decf99b590 Mon Sep 17 00:00:00 2001 From: Luis Blanco Date: Fri, 9 Aug 2019 14:12:07 +0300 Subject: [PATCH] Fix getArrayData --- include/addon-tools.hpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/include/addon-tools.hpp b/include/addon-tools.hpp index cac4534..f710c4a 100644 --- a/include/addon-tools.hpp +++ b/include/addon-tools.hpp @@ -305,21 +305,25 @@ inline Type* getArrayData(Napi::Env env, Napi::Object obj, int *num = nullptr) { Type *data = nullptr; - if (num) { - *num = 0; - } - - if ( ! obj.IsArrayBuffer() ) { + if (data.IsTypedArray()) { + Napi::ArrayBuffer arr = obj.As().ArrayBuffer(); + if (num) { + *num = arr.ByteLength() / sizeof(Type); + } + data = static_cast(arr.Data()); + } else if (data.IsArrayBuffer()) { + Napi::ArrayBuffer arr = obj.As(); + if (num) { + *num = arr.ByteLength() / sizeof(Type); + } + data = static_cast(arr.Data()); + } else { + if (num) { + *num = 0; + } JS_THROW("Argument must be of type `TypedArray`."); - return data; } - Napi::ArrayBuffer arr = obj.As(); - if (num) { - *num = arr.ByteLength() / sizeof(Type); - } - data = static_cast(arr.Data()); - return data; }