From 368467aceb6bfcc53f313e7886688e3830ccf1d7 Mon Sep 17 00:00:00 2001 From: Aaron Meriwether Date: Wed, 9 Oct 2019 15:32:42 -0500 Subject: [PATCH] Build for Node 12 / Electron 6 --- src/NodeClipboard.cc | 6 +- src/NodeClipboard.h | 2 +- src/NodeCommon.h | 95 ++++++++++----- src/NodeImage.cc | 57 +++++---- src/NodeImage.h | 2 +- src/NodeKeyboard.cc | 43 ++++--- src/NodeKeyboard.h | 2 +- src/NodeMemory.cc | 189 +++++++++++++++-------------- src/NodeMemory.h | 2 +- src/NodeMouse.cc | 49 ++++---- src/NodeMouse.h | 2 +- src/NodeProcess.cc | 65 +++++----- src/NodeProcess.h | 2 +- src/NodeRobot.cc | 280 +++++++++++++++++++++---------------------- src/NodeRobot.rc | 9 +- src/NodeScreen.cc | 25 ++-- src/NodeScreen.h | 2 +- src/NodeWindow.cc | 63 +++++----- src/NodeWindow.h | 2 +- src/robot | 2 +- 20 files changed, 462 insertions(+), 437 deletions(-) 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 7a196b4..4deab81 100644 --- a/src/NodeCommon.h +++ b/src/NodeCommon.h @@ -47,10 +47,26 @@ ROBOT_NS_USE_ALL; //////////////////////////////////////////////////////////////////////////////// +#if NODE_MODULE_VERSION >= 46 + + #define NEW_INSTANCE( f, argc, argv ) f->NewInstance(v8::Context::New(isolate), argc, argv).ToLocalChecked() + +#else + + #define NEW_INSTANCE( f, argc, argv ) f->NewInstance(argc, argv) + +#endif + +//////////////////////////////////////////////////////////////////////////////// + #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 ) @@ -64,32 +80,28 @@ ROBOT_NS_USE_ALL; _jsArgs[1] = NEW_INT (g), \ _jsArgs[2] = NEW_INT (b), \ _jsArgs[3] = NEW_INT (a), \ - Local::New \ - (isolate, JsColor)->NewInstance (4, _jsArgs) \ + NEW_INSTANCE(Local::New(isolate, JsColor), 4, _jsArgs) \ ) #define NEW_RANGE( min, max ) \ ( \ _jsArgs[0] = NEW_INT (min), \ _jsArgs[1] = NEW_INT (max), \ - Local::New \ - (isolate, JsRange)->NewInstance (2, _jsArgs) \ + NEW_INSTANCE(Local::New(isolate, JsRange), 2, _jsArgs) \ ) #define NEW_POINT( x, y ) \ ( \ _jsArgs[0] = NEW_INT (x), \ _jsArgs[1] = NEW_INT (y), \ - Local::New \ - (isolate, JsPoint)->NewInstance (2, _jsArgs) \ + NEW_INSTANCE(Local::New(isolate, JsPoint), 2, _jsArgs) \ ) #define NEW_SIZE( w, h ) \ ( \ _jsArgs[0] = NEW_INT (w), \ _jsArgs[1] = NEW_INT (h), \ - Local::New \ - (isolate, JsSize)->NewInstance (2, _jsArgs) \ + NEW_INSTANCE(Local::New(isolate, JsSize), 2, _jsArgs) \ ) #define NEW_BOUNDS( x, y, w, h ) \ @@ -98,14 +110,47 @@ ROBOT_NS_USE_ALL; _jsArgs[1] = NEW_INT (y), \ _jsArgs[2] = NEW_INT (w), \ _jsArgs[3] = NEW_INT (h), \ - Local::New \ - (isolate, JsBounds)->NewInstance (4, _jsArgs) \ + NEW_INSTANCE(Local::New(isolate, JsBounds), 4, _jsArgs) \ ) -#define NEW_MODULE Local::New (isolate, JsModule )->NewInstance() -#define NEW_SEGMENT Local::New (isolate, JsSegment)->NewInstance() -#define NEW_STATS Local::New (isolate, JsStats )->NewInstance() -#define NEW_REGION Local::New (isolate, JsRegion )->NewInstance() +#define NEW_MODULE NEW_INSTANCE(Local::New(isolate, JsModule ), 0, NULL) +#define NEW_SEGMENT NEW_INSTANCE(Local::New(isolate, JsSegment), 0, NULL) +#define NEW_STATS NEW_INSTANCE(Local::New(isolate, JsStats ), 0, NULL) +#define NEW_REGION NEW_INSTANCE(Local::New(isolate, JsRegion ), 0, NULL) + +//////////////////////////////////////////////////////////////////////////////// + +#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 //////////////////////////////////////////////////////////////////////////////// @@ -212,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(); @@ -221,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 9bad137..57e2e65 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); } @@ -200,14 +200,14 @@ void ImageWrap::New (const FunctionCallbackInfo& args) else { // Normalize the size argument - auto s = Local::New - (isolate, JsSize)->NewInstance - (2, (_jsArgs[0] = args[0], - _jsArgs[1] = args[1], _jsArgs)); + auto s = NEW_INSTANCE( + Local::New(isolate, JsSize), + 2, (_jsArgs[0] = args[0], + _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; @@ -218,7 +218,7 @@ void ImageWrap::New (const FunctionCallbackInfo& args) { auto ctor = NEW_CTOR (Image); // Return as a new instance - RETURN (ctor->NewInstance (2, + RETURN (NEW_INSTANCE(ctor, 2, (_jsArgs[0] = args[0], _jsArgs[1] = args[1], _jsArgs))); } @@ -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 514696a..07817d5 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; @@ -160,13 +160,13 @@ void KeyboardWrap::New (const FunctionCallbackInfo& args) { auto ctor = NEW_CTOR (Keyboard); // Return as a new instance - RETURN (ctor->NewInstance()); + RETURN (NEW_INSTANCE(ctor, 0, NULL)); } } //////////////////////////////////////////////////////////////////////////////// -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