diff --git a/src/NodeClipboard.cc b/src/NodeClipboard.cc index dcc63cf..5fe3d5f 100644 --- a/src/NodeClipboard.cc +++ b/src/NodeClipboard.cc @@ -53,7 +53,7 @@ void ClipboardWrap::SetText (const FunctionCallbackInfo& args) if (!args[0]->IsString()) THROW (Type, "Invalid arguments"); - String::Utf8Value value (args[0]); + UTF8_VAR (value, args[0]); auto text = *value ? *value : ""; RETURN_BOOL (Clipboard::SetText (text)); } @@ -100,7 +100,7 @@ void ClipboardWrap::GetSequence (const FunctionCallbackInfo& args) //////////////////////////////////////////////////////////////////////////////// -void ClipboardWrap::Initialize (Handle exports) +void ClipboardWrap::Initialize (Local exports) { // Get the current isolated V8 instance Isolate* isolate = Isolate::GetCurrent(); @@ -121,5 +121,5 @@ void ClipboardWrap::Initialize (Handle exports) NODE_SET_METHOD (result, "getSequence", GetSequence); // Export clipboard functions inside object - exports->Set (NEW_STR ("Clipboard"), result); + OBJECT_SET (exports, NEW_STR ("Clipboard"), result); } diff --git a/src/NodeClipboard.h b/src/NodeClipboard.h index ae80f9c..a789350 100644 --- a/src/NodeClipboard.h +++ b/src/NodeClipboard.h @@ -38,5 +38,5 @@ class ClipboardWrap : public ObjectWrap static void GetSequence (const FunctionCallbackInfo& args); public: - static void Initialize (Handle exports); + static void Initialize (Local exports); }; diff --git a/src/NodeCommon.h b/src/NodeCommon.h index 76779d6..2f9d002 100644 --- a/src/NodeCommon.h +++ b/src/NodeCommon.h @@ -62,7 +62,11 @@ ROBOT_NS_USE_ALL; #define NEW_INT( value ) Integer::New (isolate, value ) #define NEW_NUM( value ) Number ::New (isolate, value ) #define NEW_BOOL(value ) Boolean::New (isolate, value ) +#if NODE_MODULE_VERSION >= 67 +#define NEW_STR( value ) String ::NewFromUtf8 (isolate, value, v8::NewStringType::kNormal).ToLocalChecked() +#else #define NEW_STR( value ) String ::NewFromUtf8 (isolate, value ) +#endif #define NEW_OBJ Object ::New (isolate ) #define NEW_ARR( length) Array ::New (isolate, length) #define NEW_NULL Null (isolate ) @@ -116,6 +120,40 @@ ROBOT_NS_USE_ALL; //////////////////////////////////////////////////////////////////////////////// +#if NODE_MODULE_VERSION >= 70 +#define BOOLEAN_VALUE( value ) (value->BooleanValue (isolate)) +#define UTF8_VAR( var, value ) String::Utf8Value var (isolate, value) +#else +#define BOOLEAN_VALUE( value ) (value->BooleanValue()) +#define UTF8_VAR( var, value ) String::Utf8Value var (value) +#endif + +#if NODE_MODULE_VERSION >= 67 +#define TO_OBJECT( value ) (value->ToObject (isolate->GetCurrentContext()).ToLocalChecked()) +#define NUMBER_VALUE( value ) (value->NumberValue (isolate->GetCurrentContext()).ToChecked()) +#define INT32_VALUE( value ) (value->Int32Value (isolate->GetCurrentContext()).ToChecked()) +#define UINT32_VALUE( value ) (value->Uint32Value (isolate->GetCurrentContext()).ToChecked()) +#define OBJECT_GET( map, key ) (map->Get (isolate->GetCurrentContext(), key).ToLocalChecked()) +#define OBJECT_SET( map, key, value ) (map->Set (isolate->GetCurrentContext(), key, value).ToChecked()) +#define GET_FUNCTION( tpl ) (tpl->GetFunction (isolate->GetCurrentContext()).ToLocalChecked()) +#else +#define TO_OBJECT( value ) (value->ToObject()) +#define NUMBER_VALUE( value ) (value->NumberValue()) +#define INT32_VALUE( value ) (value->Int32Value()) +#define UINT32_VALUE( value ) (value->Uint32Value()) +#define OBJECT_GET( map, key ) (map->Get (key)) +#define OBJECT_SET( map, key, value ) (map->Set (key, value)) +#define GET_FUNCTION( tpl ) (tpl->GetFunction()) +#endif + +#if NODE_MODULE_VERSION >= 48 +#define GET_PRIVATE( obj, key ) ((obj->GetPrivate (isolate->GetCurrentContext(), Private::ForApi (isolate, NEW_STR (key)))).ToLocalChecked()) +#else +#define GET_PRIVATE( obj, key ) (obj->GetHiddenValue (NEW_STR (key))) +#endif + +//////////////////////////////////////////////////////////////////////////////// + #define RETURN( value ) { args.GetReturnValue().Set (value); return; } #define RETURN_INT( value ) RETURN (NEW_INT (value)); @@ -219,7 +257,7 @@ enum RobotType //////////////////////////////////////////////////////////////////////////////// template -inline T* UnwrapRobot (Handle value) +inline T* UnwrapRobot (Local value) { // Get the current isolated V8 instance Isolate* isolate = Isolate::GetCurrent(); @@ -228,30 +266,16 @@ inline T* UnwrapRobot (Handle value) if (!value->IsObject()) return nullptr; // Retrieve the local object - auto obj = value->ToObject(); - -#if NODE_MODULE_VERSION >= 48 - - auto context = isolate->GetCurrentContext(); - auto privateKeyValue = Private::ForApi - (isolate, NEW_STR ("_ROBOT_TYPE")); - - auto type = obj->GetPrivate (context, - privateKeyValue).ToLocalChecked(); - -#else + auto obj = TO_OBJECT (value); // Convert and get hidden type - auto type = obj->GetHiddenValue - (NEW_STR ("_ROBOT_TYPE")); - -#endif + auto type = GET_PRIVATE (obj, "_ROBOT_TYPE"); // The value must contain a handle if (type.IsEmpty()) return nullptr; // Compare hidden type with class type - if (type->Int32Value() != T::ClassType) + if (INT32_VALUE (type) != T::ClassType) return nullptr; // Return the final unwrapped class diff --git a/src/NodeImage.cc b/src/NodeImage.cc index 8b1e392..cb4a28f 100644 --- a/src/NodeImage.cc +++ b/src/NodeImage.cc @@ -35,8 +35,8 @@ void ImageWrap::Create (const FunctionCallbackInfo& args) ISOWRAP (Image, args.Holder()); RETURN_BOOL (mImage->Create - ((uint16) args[0]->Int32Value(), - (uint16) args[1]->Int32Value())); + ((uint16) INT32_VALUE (args[0]), + (uint16) INT32_VALUE (args[1]))); } //////////////////////////////////////////////////////////////////////////////// @@ -106,8 +106,8 @@ void ImageWrap::GetPixel (const FunctionCallbackInfo& args) ISOWRAP (Image, args.Holder()); Color color = mImage->GetPixel - ((uint16) args[0]->Int32Value(), - (uint16) args[1]->Int32Value()); + ((uint16) INT32_VALUE (args[0]), + (uint16) INT32_VALUE (args[1])); RETURN_COLOR (color.R, color.G, color.B, color.A); @@ -120,12 +120,12 @@ void ImageWrap::SetPixel (const FunctionCallbackInfo& args) ISOWRAP (Image, args.Holder()); mImage->SetPixel - ((uint16) args[0]->Int32Value(), - (uint16) args[1]->Int32Value(), - Color ((uint8 ) args[2]->Int32Value(), - (uint8 ) args[3]->Int32Value(), - (uint8 ) args[4]->Int32Value(), - (uint8 ) args[5]->Int32Value())); + ((uint16) INT32_VALUE (args[0]), + (uint16) INT32_VALUE (args[1]), + Color ((uint8 ) INT32_VALUE (args[2]), + (uint8 ) INT32_VALUE (args[3]), + (uint8 ) INT32_VALUE (args[4]), + (uint8 ) INT32_VALUE (args[5]))); } //////////////////////////////////////////////////////////////////////////////// @@ -135,10 +135,10 @@ void ImageWrap::Fill (const FunctionCallbackInfo& args) ISOWRAP (Image, args.Holder()); RETURN_BOOL (mImage->Fill (Color - ((uint8) args[0]->Int32Value(), - (uint8) args[1]->Int32Value(), - (uint8) args[2]->Int32Value(), - (uint8) args[3]->Int32Value()))); + ((uint8) INT32_VALUE (args[0]), + (uint8) INT32_VALUE (args[1]), + (uint8) INT32_VALUE (args[2]), + (uint8) INT32_VALUE (args[3])))); } //////////////////////////////////////////////////////////////////////////////// @@ -151,7 +151,7 @@ void ImageWrap::Swap (const FunctionCallbackInfo& args) if (!args[0]->IsString()) THROW (Type, "Invalid arguments"); - String::Utf8Value value (args[0]); + UTF8_VAR (value, args[0]); auto swap = *value ? *value : ""; RETURN_BOOL (mImage->Swap (swap)); } @@ -167,8 +167,8 @@ void ImageWrap::Flip (const FunctionCallbackInfo& args) !args[1]->IsBoolean()) THROW (Type, "Invalid arguments"); - bool h = args[0]->BooleanValue(); - bool v = args[1]->BooleanValue(); + bool h = BOOLEAN_VALUE (args[0]); + bool v = BOOLEAN_VALUE (args[1]); RETURN_BOOL (mImage->Flip (h, v)); } @@ -178,7 +178,7 @@ void ImageWrap::Equals (const FunctionCallbackInfo& args) { ISOLATE; auto* wrapper1 = ObjectWrap::Unwrap (args .Holder()); - auto* wrapper2 = ObjectWrap::Unwrap (args[0]->ToObject()); + auto* wrapper2 = ObjectWrap::Unwrap (TO_OBJECT (args[0])); RETURN_BOOL (wrapper1->mImage == wrapper2->mImage); } @@ -201,13 +201,13 @@ void ImageWrap::New (const FunctionCallbackInfo& args) { // Normalize the size argument auto s = NEW_INSTANCE( + Local::New(isolate, JsSize), Local::New (isolate, JsSize), - 2, (_jsArgs[0] = args[0], - _jsArgs[1] = args[1], _jsArgs)); + _jsArgs[1] = args[1], _jsArgs)); wrapper->mImage.Create - ((uint16) s->Get (NEW_STR ("w"))->Int32Value(), - (uint16) s->Get (NEW_STR ("h"))->Int32Value()); + (INT32_VALUE ((uint16) OBJECT_GET (s, NEW_STR ("w"))), + INT32_VALUE ((uint16) OBJECT_GET (s, NEW_STR ("h")))); } REGISTER_ROBOT_TYPE; @@ -226,7 +226,7 @@ void ImageWrap::New (const FunctionCallbackInfo& args) //////////////////////////////////////////////////////////////////////////////// -void ImageWrap::Initialize (Handle exports) +void ImageWrap::Initialize (Local exports) { // Get the current isolated V8 instance Isolate* isolate = Isolate::GetCurrent(); @@ -256,7 +256,6 @@ void ImageWrap::Initialize (Handle exports) NODE_SET_PROTOTYPE_METHOD (tpl, "_equals", Equals ); // Assign function template to our class creator - constructor.Reset (isolate, tpl->GetFunction()); - exports->Set - (NEW_STR ("Image"), tpl->GetFunction()); + constructor.Reset (isolate, GET_FUNCTION (tpl)); + OBJECT_SET (exports, NEW_STR ("Image"), GET_FUNCTION (tpl)); } diff --git a/src/NodeImage.h b/src/NodeImage.h index 8d4dc71..420a13b 100644 --- a/src/NodeImage.h +++ b/src/NodeImage.h @@ -49,7 +49,7 @@ class ImageWrap : public ObjectWrap static void New (const FunctionCallbackInfo& args); public: - static void Initialize (Handle exports); + static void Initialize (Local exports); public: Image mImage; diff --git a/src/NodeKeyboard.cc b/src/NodeKeyboard.cc index 3a00f08..74ecdfd 100644 --- a/src/NodeKeyboard.cc +++ b/src/NodeKeyboard.cc @@ -26,10 +26,10 @@ void KeyboardWrap::Click (const FunctionCallbackInfo& args) { ISOWRAP (Keyboard, args.Holder()); - mKeyboard->AutoDelay.Min = args[1]->Int32Value(); - mKeyboard->AutoDelay.Max = args[2]->Int32Value(); + mKeyboard->AutoDelay.Min = INT32_VALUE (args[1]); + mKeyboard->AutoDelay.Max = INT32_VALUE (args[2]); if (args[0]->IsInt32()) - mKeyboard->Click ((Key) args[0]->Int32Value()); + mKeyboard->Click ((Key) INT32_VALUE (args[0])); else { @@ -37,7 +37,7 @@ void KeyboardWrap::Click (const FunctionCallbackInfo& args) if (!args[0]->IsString()) THROW (Type, "Invalid arguments"); - String::Utf8Value value (args[0]); + UTF8_VAR (value, args[0]); auto keys = *value ? *value : ""; // Perform a series of keycode actions RETURN_BOOL (mKeyboard->Click (keys)); @@ -54,9 +54,9 @@ void KeyboardWrap::Press (const FunctionCallbackInfo& args) if (!args[0]->IsInt32()) THROW (Type, "Invalid arguments"); - mKeyboard->AutoDelay.Min = args[1]->Int32Value(); - mKeyboard->AutoDelay.Max = args[2]->Int32Value(); - mKeyboard->Press ((Key) args[0]->Int32Value()); + mKeyboard->AutoDelay.Min = INT32_VALUE (args[1]); + mKeyboard->AutoDelay.Max = INT32_VALUE (args[2]); + mKeyboard->Press ((Key) INT32_VALUE (args[0])); } //////////////////////////////////////////////////////////////////////////////// @@ -69,9 +69,9 @@ void KeyboardWrap::Release (const FunctionCallbackInfo& args) if (!args[0]->IsInt32()) THROW (Type, "Invalid arguments"); - mKeyboard->AutoDelay.Min = args[1]->Int32Value(); - mKeyboard->AutoDelay.Max = args[2]->Int32Value(); - mKeyboard->Release ((Key) args[0]->Int32Value()); + mKeyboard->AutoDelay.Min = INT32_VALUE (args[1]); + mKeyboard->AutoDelay.Max = INT32_VALUE (args[2]); + mKeyboard->Release ((Key) INT32_VALUE (args[0])); } //////////////////////////////////////////////////////////////////////////////// @@ -84,7 +84,7 @@ void KeyboardWrap::Compile (const FunctionCallbackInfo& args) if (!args[0]->IsString()) THROW (Type, "Invalid arguments"); - String::Utf8Value value (args[0]); + UTF8_VAR (value, args[0]); auto keys = *value ? *value : ""; KeyList list; @@ -97,9 +97,9 @@ void KeyboardWrap::Compile (const FunctionCallbackInfo& args) for (int i = 0; i < length; ++i) { auto obj = NEW_OBJ; - obj->Set (NEW_STR ("down"), NEW_BOOL (list[i].first )); - obj->Set (NEW_STR ("key" ), NEW_INT (list[i].second)); - res->Set (i, obj); + OBJECT_SET (obj, NEW_STR ("down"), NEW_BOOL (list[i].first )); + OBJECT_SET (obj, NEW_STR ("key" ), NEW_INT (list[i].second)); + OBJECT_SET (res, i, obj); } RETURN (res); @@ -123,7 +123,7 @@ void KeyboardWrap::GetState (const FunctionCallbackInfo& args) { // Loop every state and add it to resulting object for (auto i = state.begin(); i != state.end(); ++i) - res->Set (NEW_INT (i->first), NEW_BOOL (i->second)); + OBJECT_SET (res, NEW_INT (i->first), NEW_BOOL (i->second)); } RETURN (res); @@ -134,7 +134,7 @@ void KeyboardWrap::GetState (const FunctionCallbackInfo& args) { RETURN_BOOL (Keyboard::GetState // Get info about a single key - ((Key) args[0]->Int32Value())); + ((Key) INT32_VALUE (args[0]))); } THROW (Type, "Invalid arguments"); @@ -149,7 +149,7 @@ void KeyboardWrap::New (const FunctionCallbackInfo& args) if (args.IsConstructCall()) { (new KeyboardWrap())->Wrap (args.This()); - args.This()->Set (NEW_STR ("autoDelay"), + OBJECT_SET (args.This(), NEW_STR ("autoDelay"), NEW_RANGE ( 40, 90)); REGISTER_ROBOT_TYPE; @@ -166,7 +166,7 @@ void KeyboardWrap::New (const FunctionCallbackInfo& args) //////////////////////////////////////////////////////////////////////////////// -void KeyboardWrap::Initialize (Handle exports) +void KeyboardWrap::Initialize (Local exports) { // Get the current isolated V8 instance Isolate* isolate = Isolate::GetCurrent(); @@ -184,7 +184,6 @@ void KeyboardWrap::Initialize (Handle exports) NODE_SET_METHOD ((Local