Skip to content

Commit

Permalink
Build for Node 12 / Electron 6
Browse files Browse the repository at this point in the history
  • Loading branch information
p120ph37 committed Oct 9, 2019
1 parent a77999e commit 368467a
Show file tree
Hide file tree
Showing 20 changed files with 462 additions and 437 deletions.
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);
};
95 changes: 63 additions & 32 deletions src/NodeCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand All @@ -64,32 +80,28 @@ ROBOT_NS_USE_ALL;
_jsArgs[1] = NEW_INT (g), \
_jsArgs[2] = NEW_INT (b), \
_jsArgs[3] = NEW_INT (a), \
Local<Function>::New \
(isolate, JsColor)->NewInstance (4, _jsArgs) \
NEW_INSTANCE(Local<Function>::New(isolate, JsColor), 4, _jsArgs) \
)

#define NEW_RANGE( min, max ) \
( \
_jsArgs[0] = NEW_INT (min), \
_jsArgs[1] = NEW_INT (max), \
Local<Function>::New \
(isolate, JsRange)->NewInstance (2, _jsArgs) \
NEW_INSTANCE(Local<Function>::New(isolate, JsRange), 2, _jsArgs) \
)

#define NEW_POINT( x, y ) \
( \
_jsArgs[0] = NEW_INT (x), \
_jsArgs[1] = NEW_INT (y), \
Local<Function>::New \
(isolate, JsPoint)->NewInstance (2, _jsArgs) \
NEW_INSTANCE(Local<Function>::New(isolate, JsPoint), 2, _jsArgs) \
)

#define NEW_SIZE( w, h ) \
( \
_jsArgs[0] = NEW_INT (w), \
_jsArgs[1] = NEW_INT (h), \
Local<Function>::New \
(isolate, JsSize)->NewInstance (2, _jsArgs) \
NEW_INSTANCE(Local<Function>::New(isolate, JsSize), 2, _jsArgs) \
)

#define NEW_BOUNDS( x, y, w, h ) \
Expand All @@ -98,14 +110,47 @@ ROBOT_NS_USE_ALL;
_jsArgs[1] = NEW_INT (y), \
_jsArgs[2] = NEW_INT (w), \
_jsArgs[3] = NEW_INT (h), \
Local<Function>::New \
(isolate, JsBounds)->NewInstance (4, _jsArgs) \
NEW_INSTANCE(Local<Function>::New(isolate, JsBounds), 4, _jsArgs) \
)

#define NEW_MODULE Local<Function>::New (isolate, JsModule )->NewInstance()
#define NEW_SEGMENT Local<Function>::New (isolate, JsSegment)->NewInstance()
#define NEW_STATS Local<Function>::New (isolate, JsStats )->NewInstance()
#define NEW_REGION Local<Function>::New (isolate, JsRegion )->NewInstance()
#define NEW_MODULE NEW_INSTANCE(Local<Function>::New(isolate, JsModule ), 0, NULL)
#define NEW_SEGMENT NEW_INSTANCE(Local<Function>::New(isolate, JsSegment), 0, NULL)
#define NEW_STATS NEW_INSTANCE(Local<Function>::New(isolate, JsStats ), 0, NULL)
#define NEW_REGION NEW_INSTANCE(Local<Function>::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

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

Expand Down Expand Up @@ -212,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 @@ -221,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
57 changes: 28 additions & 29 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 @@ -200,14 +200,14 @@ void ImageWrap::New (const FunctionCallbackInfo<Value>& args)
else
{
// Normalize the size argument
auto s = Local<Function>::New
(isolate, JsSize)->NewInstance
(2, (_jsArgs[0] = args[0],
_jsArgs[1] = args[1], _jsArgs));
auto s = NEW_INSTANCE(
Local<Function>::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;
Expand All @@ -218,15 +218,15 @@ void ImageWrap::New (const FunctionCallbackInfo<Value>& 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)));
}
}

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

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
Loading

0 comments on commit 368467a

Please sign in to comment.