From 2fa32d8e5d9c46f16c98fc069752baa687d572fd Mon Sep 17 00:00:00 2001 From: Luis Blanco Date: Thu, 8 Aug 2019 16:32:06 +0300 Subject: [PATCH 1/3] Fix macros --- include/addon-tools.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/addon-tools.hpp b/include/addon-tools.hpp index 4551566..cac4534 100644 --- a/include/addon-tools.hpp +++ b/include/addon-tools.hpp @@ -12,9 +12,9 @@ #define JS_STR(VAL) Napi::String::New(env, VAL) #define JS_NUM(VAL) Napi::Number::New(env, static_cast(VAL)) #define JS_EXT(VAL) Napi::External::New(env, reinterpret_cast(VAL)) -#define JS_BOOL(VAL) Napi::Boolean::New(env, VAL) -#define JS_FUN(VAL) Napi::Function::Function(env, VAL) -#define JS_OBJ(VAL) Napi::Object::Object(env, VAL) +#define JS_BOOL(VAL) Napi::Boolean::New(env, static_cast(VAL)) +#define JS_FUN(VAL) Napi::Function::New(env, VAL) +#define JS_OBJ(VAL) Napi::Object::New(env, VAL) #define RET_VALUE(VAL) return VAL; @@ -74,7 +74,7 @@ #define USE_INT32_ARG(I, VAR, DEF) \ CHECK_LET_ARG(I, IsNumber(), "Int32"); \ - int VAR = IS_ARG_EMPTY(I) ? (DEF) : info[I].Int32Value(); + int VAR = IS_ARG_EMPTY(I) ? (DEF) : info[I].ToNumber().Int32Value(); #define LET_INT32_ARG(I, VAR) USE_INT32_ARG(I, VAR, 0) From f72cc86fc500fe05d9b8dad5ffd757decf99b590 Mon Sep 17 00:00:00 2001 From: Luis Blanco Date: Fri, 9 Aug 2019 14:12:07 +0300 Subject: [PATCH 2/3] 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; } From 2ae137d034dd7ac95055c4ac90b8d1106c87aee5 Mon Sep 17 00:00:00 2001 From: Luis Blanco Date: Fri, 9 Aug 2019 14:37:10 +0300 Subject: [PATCH 3/3] Fix getArrayData --- include/addon-tools.hpp | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/include/addon-tools.hpp b/include/addon-tools.hpp index f710c4a..041b5fe 100644 --- a/include/addon-tools.hpp +++ b/include/addon-tools.hpp @@ -306,11 +306,14 @@ inline Type* getArrayData(Napi::Env env, Napi::Object obj, int *num = nullptr) { Type *data = nullptr; if (data.IsTypedArray()) { - Napi::ArrayBuffer arr = obj.As().ArrayBuffer(); + Napi::TypedArray ta = obj.As(); + size_t offset = ta.ByteOffset(); + Napi::ArrayBuffer arr = ta.ArrayBuffer(); if (num) { *num = arr.ByteLength() / sizeof(Type); } - data = static_cast(arr.Data()); + uint8_t *base = arr.Data(); + data = static_cast(base + offset); } else if (data.IsArrayBuffer()) { Napi::ArrayBuffer arr = obj.As(); if (num) { @@ -357,24 +360,14 @@ inline void *getData(Napi::Env env, Napi::Object obj) { void *pixels = nullptr; - if (obj.IsArrayBuffer()) { - pixels = getArrayData(env, obj); - } else if (obj.IsTypedArray()) { - pixels = getArrayData( - env, - obj.As().ArrayBuffer() - ); + if (obj.IsTypedArray() || obj.IsArrayBuffer()) { + pixels = getArrayData(env, obj); } else if (obj.Has("data")) { Napi::Object data = obj.Get("data").As(); - if (data.IsArrayBuffer()) { - pixels = getArrayData(env, data); + if (data.IsTypedArray() || data.IsArrayBuffer()) { + pixels = getArrayData(env, data); } else if (data.IsBuffer()) { - pixels = getBufferData(env, data); - } else if (data.IsTypedArray()) { - pixels = getArrayData( - env, - data.As().ArrayBuffer() - ); + pixels = getBufferData(env, data); } }