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

Replace node::MakeCallback with AsyncResource call #26

Open
wants to merge 6 commits into
base: master
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ npm-debug.log
build/
node_modules/

.idea
4 changes: 2 additions & 2 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
'targets': [
{
'target_name': 'binding',
'target_name': 'xpc-connection',
'conditions': [
['OS=="mac"', {
'sources': [
Expand All @@ -25,4 +25,4 @@
]
}
]
}
}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var events = require('events');

var binding = require('./build/Release/binding.node');
var binding = require('bindings')('xpc-connection.node');
var XpcConnection = binding.XpcConnection;

inherits(XpcConnection, events.EventEmitter);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"darwin"
],
"dependencies": {
"bindings": "~1.3.0",
"nan": "^2.4.0"
}
}
30 changes: 16 additions & 14 deletions src/XpcConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ NAN_MODULE_INIT(XpcConnection::Init) {
Nan::SetPrototypeMethod(tmpl, "setup", Setup);
Nan::SetPrototypeMethod(tmpl, "sendMessage", SendMessage);

target->Set(Nan::New("XpcConnection").ToLocalChecked(), tmpl->GetFunction());
Nan::Set(target, Nan::New("XpcConnection").ToLocalChecked(), Nan::GetFunction(tmpl).ToLocalChecked());
}

XpcConnection::XpcConnection(std::string serviceName) :
node::ObjectWrap(),
serviceName(serviceName) {
serviceName(serviceName),
asyncResource("XpcConnection") {

this->asyncHandle = new uv_async_t;

Expand Down Expand Up @@ -100,7 +101,7 @@ xpc_object_t XpcConnection::ValueToXpcObject(Local<Value> value) {
xpc_object_t xpcObject = NULL;

if (value->IsInt32() || value->IsUint32()) {
xpcObject = xpc_int64_create(value->IntegerValue());
xpcObject = xpc_int64_create(Nan::To<int64_t>(value).FromJust());
} else if (value->IsString()) {
Nan::Utf8String valueString(value);

Expand All @@ -110,17 +111,18 @@ xpc_object_t XpcConnection::ValueToXpcObject(Local<Value> value) {

xpcObject = XpcConnection::ArrayToXpcObject(valueArray);
} else if (node::Buffer::HasInstance(value)) {
Local<Object> valueObject = value->ToObject();
Local<Object> valueObject = Nan::To<Object>(value).ToLocalChecked();

if (valueObject->HasRealNamedProperty(Nan::New("isUuid").ToLocalChecked())) {

if (Nan::HasRealNamedProperty(valueObject, Nan::New("isUuid").ToLocalChecked()).FromJust()) {
uuid_t *uuid = (uuid_t *)node::Buffer::Data(valueObject);

xpcObject = xpc_uuid_create(*uuid);
} else {
xpcObject = xpc_data_create(node::Buffer::Data(valueObject), node::Buffer::Length(valueObject));
}
} else if (value->IsObject()) {
Local<Object> valueObject = value->ToObject();
Local<Object> valueObject = Nan::To<Object>(value).ToLocalChecked();

xpcObject = XpcConnection::ObjectToXpcObject(valueObject);
} else {
Expand All @@ -132,15 +134,15 @@ xpc_object_t XpcConnection::ValueToXpcObject(Local<Value> value) {
xpc_object_t XpcConnection::ObjectToXpcObject(Local<Object> object) {
xpc_object_t xpcObject = xpc_dictionary_create(NULL, NULL, 0);

Local<Array> propertyNames = object->GetPropertyNames();
Local<Array> propertyNames = Nan::GetPropertyNames(object).ToLocalChecked();

for(uint32_t i = 0; i < propertyNames->Length(); i++) {
Local<Value> propertyName = propertyNames->Get(i);
Local<Value> propertyName = Nan::Get(propertyNames, i).ToLocalChecked();

if (propertyName->IsString()) {
Nan::Utf8String propertyNameString(propertyName);

Local<Value> propertyValue = object->GetRealNamedProperty(propertyName->ToString());
Local<Value> propertyValue = Nan::GetRealNamedProperty(object, Nan::To<String>(propertyName).ToLocalChecked()).ToLocalChecked();

xpc_object_t xpcValue = XpcConnection::ValueToXpcObject(propertyValue);
xpc_dictionary_set_value(xpcObject, *propertyNameString, xpcValue);
Expand All @@ -157,7 +159,7 @@ xpc_object_t XpcConnection::ArrayToXpcObject(Local<Array> array) {
xpc_object_t xpcArray = xpc_array_create(NULL, 0);

for(uint32_t i = 0; i < array->Length(); i++) {
Local<Value> value = array->Get(i);
Local<Value> value = Nan::Get(array, i).ToLocalChecked();

xpc_object_t xpcValue = XpcConnection::ValueToXpcObject(value);
xpc_array_append_value(xpcArray, xpcValue);
Expand Down Expand Up @@ -197,7 +199,7 @@ Local<Object> XpcConnection::XpcDictionaryToObject(xpc_object_t xpcDictionary) {
Local<Object> object = Nan::New<Object>();

xpc_dictionary_apply(xpcDictionary, ^bool(const char *key, xpc_object_t value) {
object->Set(Nan::New<String>(key).ToLocalChecked(), XpcConnection::XpcObjectToValue(value));
Nan::Set(object, Nan::New<String>(key).ToLocalChecked(), XpcConnection::XpcObjectToValue(value));

return true;
});
Expand All @@ -209,7 +211,7 @@ Local<Array> XpcConnection::XpcArrayToArray(xpc_object_t xpcArray) {
Local<Array> array = Nan::New<Array>();

xpc_array_apply(xpcArray, ^bool(size_t index, xpc_object_t value) {
array->Set(Nan::New<Number>(index), XpcConnection::XpcObjectToValue(value));
Nan::Set(array, Nan::New<Number>(index), XpcConnection::XpcObjectToValue(value));

return true;
});
Expand Down Expand Up @@ -251,7 +253,7 @@ void XpcConnection::processEventQueue() {
Nan::New(message).ToLocalChecked()
};

Nan::MakeCallback(Nan::New<Object>(this->This), Nan::New("emit").ToLocalChecked(), 2, argv);
this->asyncResource.runInAsyncScope(Nan::New<Object>(this->This), Nan::New("emit").ToLocalChecked(), 2, argv);
} else if (eventType == XPC_TYPE_DICTIONARY) {
Local<Object> eventObject = XpcConnection::XpcDictionaryToObject(event);

Expand All @@ -260,7 +262,7 @@ void XpcConnection::processEventQueue() {
eventObject
};

Nan::MakeCallback(Nan::New<Object>(this->This), Nan::New("emit").ToLocalChecked(), 2, argv);
this->asyncResource.runInAsyncScope(Nan::New<Object>(this->This), Nan::New("emit").ToLocalChecked(), 2, argv);
}

xpc_release(event);
Expand Down
1 change: 1 addition & 0 deletions src/XpcConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class XpcConnection : public node::ObjectWrap {
xpc_connection_t xpcConnnection;

Nan::Persistent<v8::Object> This;
Nan::AsyncResource asyncResource;

uv_async_t* asyncHandle;
uv_mutex_t eventQueueMutex;
Expand Down