From 912b12b2158a730f9321196e963dd9e93a7539d1 Mon Sep 17 00:00:00 2001 From: Luis Blanco <luisblanco1337@gmail.com> Date: Sun, 11 Aug 2019 13:02:11 +0300 Subject: [PATCH] Fix macros --- include/addon-tools.hpp | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/include/addon-tools.hpp b/include/addon-tools.hpp index 041b5fe..2f17d93 100644 --- a/include/addon-tools.hpp +++ b/include/addon-tools.hpp @@ -5,6 +5,11 @@ #include <napi.h> +#ifdef _WIN32 + #define strcasestr(s, t) strstr(strupr(s), strupr(t)) +#endif + + #define NAPI_ENV Napi::Env env = info.Env(); #define NAPI_HS Napi::HandleScope scope(env); @@ -183,6 +188,23 @@ Napi::Buffer<uint8_t> VAR = info[I].As< Napi::Buffer<uint8_t> >(); +#define REQ_ARRAY_ARG(I, VAR) \ + REQ_OBJ_ARG(I, _obj_##VAR); \ + if ( ! _obj_##VAR.IsArray() ) { \ + JS_THROW("Argument " #I " must be of type `Array`"); \ + } \ + Napi::Array VAR = _obj_##VAR.As<Napi::Array>(); + + +#define REQ_TYPED_ARRAY_ARG(I, VAR) \ + REQ_OBJ_ARG(I, _obj_##VAR); \ + if ( ! _obj_##VAR.IsTypedArray() ) { \ + JS_THROW("Argument " #I " must be of type `TypedArray`"); \ + } \ + Napi::TypedArray VAR = _obj_##VAR.As<Napi::TypedArray>(); + + + #define CTOR_CHECK(T) \ if ( ! info.IsConstructCall() ) \ JS_THROW(T " must be called with the 'new' keyword."); @@ -300,26 +322,26 @@ #define JS_RUN JS_RUN_3 -template<typename Type> +template<typename Type = uint8_t> inline Type* getArrayData(Napi::Env env, Napi::Object obj, int *num = nullptr) { Type *data = nullptr; - if (data.IsTypedArray()) { + if (obj.IsTypedArray()) { Napi::TypedArray ta = obj.As<Napi::TypedArray>(); size_t offset = ta.ByteOffset(); Napi::ArrayBuffer arr = ta.ArrayBuffer(); if (num) { *num = arr.ByteLength() / sizeof(Type); } - uint8_t *base = arr.Data(); - data = static_cast<Type *>(base + offset); - } else if (data.IsArrayBuffer()) { + uint8_t *base = reinterpret_cast<uint8_t *>(arr.Data()); + data = reinterpret_cast<Type *>(base + offset); + } else if (obj.IsArrayBuffer()) { Napi::ArrayBuffer arr = obj.As<Napi::ArrayBuffer>(); if (num) { *num = arr.ByteLength() / sizeof(Type); } - data = static_cast<Type *>(arr.Data()); + data = reinterpret_cast<Type *>(arr.Data()); } else { if (num) { *num = 0; @@ -331,7 +353,7 @@ inline Type* getArrayData(Napi::Env env, Napi::Object obj, int *num = nullptr) { } -template<typename Type> +template<typename Type = uint8_t> inline Type* getBufferData(Napi::Env env, Napi::Object obj, int *num = nullptr) { Type *data = nullptr;