Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build for Node 12 / Electron 6 #74

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/NodeClipboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void ClipboardWrap::SetText (const FunctionCallbackInfo<Value>& 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));
}
Expand Down Expand Up @@ -100,7 +100,7 @@ void ClipboardWrap::GetSequence (const FunctionCallbackInfo<Value>& args)

////////////////////////////////////////////////////////////////////////////////

void ClipboardWrap::Initialize (Handle<Object> exports)
void ClipboardWrap::Initialize (Local<Object> exports)
{
// Get the current isolated V8 instance
Isolate* isolate = Isolate::GetCurrent();
Expand All @@ -121,5 +121,5 @@ void ClipboardWrap::Initialize (Handle<Object> 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);
}
2 changes: 1 addition & 1 deletion src/NodeClipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ class ClipboardWrap : public ObjectWrap
static void GetSequence (const FunctionCallbackInfo<Value>& args);

public:
static void Initialize (Handle<Object> exports);
static void Initialize (Local<Object> exports);
};
60 changes: 42 additions & 18 deletions src/NodeCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -219,7 +257,7 @@ enum RobotType
////////////////////////////////////////////////////////////////////////////////

template <class T>
inline T* UnwrapRobot (Handle<Value> value)
inline T* UnwrapRobot (Local<Value> value)
{
// Get the current isolated V8 instance
Isolate* isolate = Isolate::GetCurrent();
Expand All @@ -228,30 +266,16 @@ inline T* UnwrapRobot (Handle<Value> 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
Expand Down
51 changes: 25 additions & 26 deletions src/NodeImage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ void ImageWrap::Create (const FunctionCallbackInfo<Value>& 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])));
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -106,8 +106,8 @@ void ImageWrap::GetPixel (const FunctionCallbackInfo<Value>& 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);
Expand All @@ -120,12 +120,12 @@ void ImageWrap::SetPixel (const FunctionCallbackInfo<Value>& 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])));
}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -135,10 +135,10 @@ void ImageWrap::Fill (const FunctionCallbackInfo<Value>& 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]))));
}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -151,7 +151,7 @@ void ImageWrap::Swap (const FunctionCallbackInfo<Value>& 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));
}
Expand All @@ -167,8 +167,8 @@ void ImageWrap::Flip (const FunctionCallbackInfo<Value>& 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));
}

Expand All @@ -178,7 +178,7 @@ void ImageWrap::Equals (const FunctionCallbackInfo<Value>& args)
{
ISOLATE;
auto* wrapper1 = ObjectWrap::Unwrap<ImageWrap> (args .Holder());
auto* wrapper2 = ObjectWrap::Unwrap<ImageWrap> (args[0]->ToObject());
auto* wrapper2 = ObjectWrap::Unwrap<ImageWrap> (TO_OBJECT (args[0]));
RETURN_BOOL (wrapper1->mImage == wrapper2->mImage);
}

Expand All @@ -201,13 +201,13 @@ void ImageWrap::New (const FunctionCallbackInfo<Value>& args)
{
// Normalize the size argument
auto s = NEW_INSTANCE(
Local<Function>::New(isolate, JsSize),
Local<Function>::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;
Expand All @@ -226,7 +226,7 @@ void ImageWrap::New (const FunctionCallbackInfo<Value>& args)

////////////////////////////////////////////////////////////////////////////////

void ImageWrap::Initialize (Handle<Object> exports)
void ImageWrap::Initialize (Local<Object> exports)
{
// Get the current isolated V8 instance
Isolate* isolate = Isolate::GetCurrent();
Expand Down Expand Up @@ -256,7 +256,6 @@ void ImageWrap::Initialize (Handle<Object> 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));
}
2 changes: 1 addition & 1 deletion src/NodeImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ImageWrap : public ObjectWrap
static void New (const FunctionCallbackInfo<Value>& args);

public:
static void Initialize (Handle<Object> exports);
static void Initialize (Local<Object> exports);

public:
Image mImage;
Expand Down
41 changes: 20 additions & 21 deletions src/NodeKeyboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ void KeyboardWrap::Click (const FunctionCallbackInfo<Value>& 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
{
// Args should be string
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));
Expand All @@ -54,9 +54,9 @@ void KeyboardWrap::Press (const FunctionCallbackInfo<Value>& 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]));
}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -69,9 +69,9 @@ void KeyboardWrap::Release (const FunctionCallbackInfo<Value>& 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]));
}

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -84,7 +84,7 @@ void KeyboardWrap::Compile (const FunctionCallbackInfo<Value>& 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;
Expand All @@ -97,9 +97,9 @@ void KeyboardWrap::Compile (const FunctionCallbackInfo<Value>& 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);
Expand All @@ -123,7 +123,7 @@ void KeyboardWrap::GetState (const FunctionCallbackInfo<Value>& 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);
Expand All @@ -134,7 +134,7 @@ void KeyboardWrap::GetState (const FunctionCallbackInfo<Value>& args)
{
RETURN_BOOL (Keyboard::GetState
// Get info about a single key
((Key) args[0]->Int32Value()));
((Key) INT32_VALUE (args[0])));
}

THROW (Type, "Invalid arguments");
Expand All @@ -149,7 +149,7 @@ void KeyboardWrap::New (const FunctionCallbackInfo<Value>& 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;
Expand All @@ -166,7 +166,7 @@ void KeyboardWrap::New (const FunctionCallbackInfo<Value>& args)

////////////////////////////////////////////////////////////////////////////////

void KeyboardWrap::Initialize (Handle<Object> exports)
void KeyboardWrap::Initialize (Local<Object> exports)
{
// Get the current isolated V8 instance
Isolate* isolate = Isolate::GetCurrent();
Expand All @@ -184,7 +184,6 @@ void KeyboardWrap::Initialize (Handle<Object> exports)
NODE_SET_METHOD ((Local<Template>) tpl, "getState", GetState);

// Assign function template to our class creator
constructor.Reset (isolate, tpl->GetFunction());
exports->Set
(NEW_STR ("Keyboard"), tpl->GetFunction());
constructor.Reset (isolate, GET_FUNCTION (tpl));
OBJECT_SET (exports, NEW_STR ("Keyboard"), GET_FUNCTION (tpl));
}
2 changes: 1 addition & 1 deletion src/NodeKeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class KeyboardWrap : public ObjectWrap
static void New (const FunctionCallbackInfo<Value>& args);

public:
static void Initialize (Handle<Object> exports);
static void Initialize (Local<Object> exports);

public:
Keyboard mKeyboard;
Expand Down
Loading