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

Upgrade to Nan 2.0.0 #566

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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 .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ node_js:
- '0.10'
- '0.11'
- '0.12'
- 'iojs-v3.0.0'
compiler: clang
env:
global:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"async": "0.9.0",
"bindings": "1.2.1",
"debug": "^2.1.1",
"nan": "~1.8.4",
"nan": "~2.0.0",
"node-pre-gyp": "0.6.x",
"optimist": "~0.6.1",
"sf": "0.1.7"
Expand Down
406 changes: 199 additions & 207 deletions src/serialport.cpp

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions src/serialport.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void EIO_AfterList(uv_work_t* req);
NAN_METHOD(Open);
void EIO_Open(uv_work_t* req);
void EIO_AfterOpen(uv_work_t* req);
void AfterOpenSuccess(int fd, NanCallback* dataCallback, NanCallback* disconnectedCallback, NanCallback* errorCallback);
void AfterOpenSuccess(int fd, Nan::Callback* dataCallback, Nan::Callback* disconnectedCallback, Nan::Callback* errorCallback);

NAN_METHOD(Update);
void EIO_Update(uv_work_t* req);
Expand All @@ -58,7 +58,7 @@ NAN_METHOD(Drain);
void EIO_Drain(uv_work_t* req);
void EIO_AfterDrain(uv_work_t* req);

SerialPortParity ToParityEnum(const v8::Handle<v8::String>& str);
SerialPortParity ToParityEnum(const v8::Local<v8::String>& str);
SerialPortStopBits ToStopBitEnum(double stopBits);

struct OpenBatonPlatformOptions
Expand All @@ -69,10 +69,10 @@ OpenBatonPlatformOptions* ParsePlatformOptions(const v8::Local<v8::Object>& opti
struct OpenBaton {
public:
char path[1024];
NanCallback* callback;
NanCallback* dataCallback;
NanCallback* disconnectedCallback;
NanCallback* errorCallback;
Nan::Callback* callback;
Nan::Callback* dataCallback;
Nan::Callback* disconnectedCallback;
Nan::Callback* errorCallback;
int fd;
int result;
int baudRate;
Expand All @@ -96,8 +96,8 @@ struct WriteBaton {
char* bufferData;
size_t bufferLength;
size_t offset;
v8::Persistent<v8::Object> buffer;
NanCallback* callback;
Nan::Persistent<v8::Object> buffer;
Nan::Callback* callback;
int result;
char errorString[ERROR_STRING_SIZE];
};
Expand Down Expand Up @@ -144,7 +144,7 @@ struct QueuedWrite {
struct CloseBaton {
public:
int fd;
NanCallback* callback;
Nan::Callback* callback;
char errorString[ERROR_STRING_SIZE];
};

Expand All @@ -161,23 +161,23 @@ struct ListResultItem {

struct ListBaton {
public:
NanCallback* callback;
Nan::Callback* callback;
std::list<ListResultItem*> results;
char errorString[ERROR_STRING_SIZE];
};

struct FlushBaton {
public:
int fd;
NanCallback* callback;
Nan::Callback* callback;
int result;
char errorString[ERROR_STRING_SIZE];
};

struct SetBaton {
public:
int fd;
NanCallback* callback;
Nan::Callback* callback;
int result;
char errorString[ERROR_STRING_SIZE];
bool rts;
Expand All @@ -191,7 +191,7 @@ struct SetBaton {
struct DrainBaton {
public:
int fd;
NanCallback* callback;
Nan::Callback* callback;
int result;
char errorString[ERROR_STRING_SIZE];
};
Expand Down
61 changes: 29 additions & 32 deletions src/serialport_poller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

using namespace v8;

static v8::Persistent<v8::FunctionTemplate> serialportpoller_constructor;
static Nan::Persistent<v8::FunctionTemplate> serialportpoller_constructor;

SerialportPoller::SerialportPoller() : ObjectWrap() {};
SerialportPoller::SerialportPoller() : Nan::ObjectWrap() {};
SerialportPoller::~SerialportPoller() {
// printf("~SerialportPoller\n");
delete callback_;
Expand All @@ -23,12 +23,12 @@ void _serialportReadable(uv_poll_t *req, int status, int events) {
}

void SerialportPoller::callCallback(int status) {
NanScope();
Nan::HandleScope scope;
// uv_work_t* req = new uv_work_t;

// Call the callback to go read more data...

v8::Handle<v8::Value> argv[1];
v8::Local<v8::Value> argv[1];
if(status != 0) {
// error handling changed in libuv, see:
// https://github.com/joyent/libuv/commit/3ee4d3f183331
Expand All @@ -39,9 +39,9 @@ void SerialportPoller::callCallback(int status) {
const char* err_string = uv_strerror(errno);
#endif
snprintf(this->errorString, sizeof(this->errorString), "Error %s on polling", err_string);
argv[0] = v8::Exception::Error(NanNew<v8::String>(this->errorString));
argv[0] = v8::Exception::Error(Nan::New<v8::String>(this->errorString).ToLocalChecked());
} else {
argv[0] = NanUndefined();
argv[0] = Nan::Undefined();
}

callback_->Call(1, argv);
Expand All @@ -50,55 +50,54 @@ void SerialportPoller::callCallback(int status) {


void SerialportPoller::Init(Handle<Object> target) {
NanScope();
Nan::HandleScope scope;

// Prepare constructor template
Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(New);
tpl->SetClassName(NanNew<String>("SerialportPoller"));
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New<String>("SerialportPoller").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);


// Prototype

// SerialportPoller.close()
tpl->PrototypeTemplate()->Set(NanNew<String>("close"),
NanNew<FunctionTemplate>(Close)->GetFunction());
Nan::SetPrototypeTemplate(tpl, "close",
Nan::GetFunction(Nan::New<FunctionTemplate>(Close)).ToLocalChecked());

// SerialportPoller.start()
tpl->PrototypeTemplate()->Set(NanNew<String>("start"),
NanNew<FunctionTemplate>(Start)->GetFunction());
Nan::SetPrototypeTemplate(tpl, "start",
Nan::GetFunction(Nan::New<FunctionTemplate>(Start)).ToLocalChecked());

NanAssignPersistent<FunctionTemplate>(serialportpoller_constructor, tpl);
serialportpoller_constructor.Reset(tpl);

target->Set(NanNew<String>("SerialportPoller"), tpl->GetFunction());
Nan::Set(target, Nan::New<String>("SerialportPoller").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
}

NAN_METHOD(SerialportPoller::New) {
NanScope();

if(!args[0]->IsInt32()) {
NanThrowTypeError("First argument must be an fd");
NanReturnUndefined();
if(!info[0]->IsInt32()) {
Nan::ThrowTypeError("First argument must be an fd");
return;
}

if(!args[1]->IsFunction()) {
NanThrowTypeError("Third argument must be a function");
NanReturnUndefined();
if(!info[1]->IsFunction()) {
Nan::ThrowTypeError("Third argument must be a function");
return;
}

SerialportPoller* obj = new SerialportPoller();
obj->fd_ = args[0]->ToInt32()->Int32Value();
obj->callback_ = new NanCallback(args[1].As<v8::Function>());
obj->fd_ = info[0]->ToInt32()->Int32Value();
obj->callback_ = new Nan::Callback(info[1].As<v8::Function>());
// obj->callCallback();

obj->Wrap(args.This());
obj->Wrap(info.This());

obj->poll_handle_.data = obj;
/*int r = */uv_poll_init(uv_default_loop(), &obj->poll_handle_, obj->fd_);

uv_poll_start(&obj->poll_handle_, UV_READABLE, _serialportReadable);

NanReturnValue(args.This());
info.GetReturnValue().Set(info.This());
}

void SerialportPoller::_start() {
Expand All @@ -111,20 +110,18 @@ void SerialportPoller::_stop() {


NAN_METHOD(SerialportPoller::Start) {
NanScope();

SerialportPoller* obj = ObjectWrap::Unwrap<SerialportPoller>(args.This());
SerialportPoller* obj = Nan::ObjectWrap::Unwrap<SerialportPoller>(info.This());
obj->_start();

NanReturnUndefined();
return;
}
NAN_METHOD(SerialportPoller::Close) {
NanScope();

SerialportPoller* obj = ObjectWrap::Unwrap<SerialportPoller>(args.This());
SerialportPoller* obj = Nan::ObjectWrap::Unwrap<SerialportPoller>(info.This());
obj->_stop();

// DO SOMETHING!

NanReturnUndefined();
return;
}
4 changes: 2 additions & 2 deletions src/serialport_poller.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <nan.h>
#include "serialport.h"

class SerialportPoller : public node::ObjectWrap {
class SerialportPoller : public Nan::ObjectWrap {
public:
static void Init(v8::Handle<v8::Object> target);

Expand All @@ -29,7 +29,7 @@ class SerialportPoller : public node::ObjectWrap {
int fd_;
char errorString[ERROR_STRING_SIZE];

NanCallback* callback_;
Nan::Callback* callback_;
};

#endif
8 changes: 4 additions & 4 deletions src/serialport_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ struct UnixPlatformOptions : OpenBatonPlatformOptions {
};

OpenBatonPlatformOptions* ParsePlatformOptions(const v8::Local<v8::Object>& options) {
NanScope();
Nan::HandleScope scope;

UnixPlatformOptions* result = new UnixPlatformOptions();
result->vmin = options->Get(NanNew<v8::String>("vmin"))->ToInt32()->Int32Value();
result->vtime = options->Get(NanNew<v8::String>("vtime"))->ToInt32()->Int32Value();
result->vmin = Nan::Get(options, Nan::New<v8::String>("vmin").ToLocalChecked()).ToLocalChecked()->ToInt32()->Int32Value();
result->vtime = Nan::Get(options, Nan::New<v8::String>("vtime").ToLocalChecked()).ToLocalChecked()->ToInt32()->Int32Value();

return result;
}
Expand All @@ -53,7 +53,7 @@ int ToBaudConstant(int baudRate);
int ToDataBitsConstant(int dataBits);
int ToStopBitsConstant(SerialPortStopBits stopBits);

void AfterOpenSuccess(int fd, NanCallback* dataCallback, NanCallback* disconnectedCallback, NanCallback* errorCallback) {
void AfterOpenSuccess(int fd, Nan::Callback* dataCallback, Nan::Callback* disconnectedCallback, Nan::Callback* errorCallback) {
delete dataCallback;
delete errorCallback;
delete disconnectedCallback;
Expand Down
18 changes: 9 additions & 9 deletions src/serialport_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ struct WatchPortBaton {
char errorString[ERROR_STRING_SIZE];
DWORD errorCode;
bool disconnected;
NanCallback* dataCallback;
NanCallback* errorCallback;
NanCallback* disconnectedCallback;
Nan::Callback* dataCallback;
Nan::Callback* errorCallback;
Nan::Callback* disconnectedCallback;
};

void EIO_Update(uv_work_t* req) {
Expand Down Expand Up @@ -291,7 +291,7 @@ void DisposeWatchPortCallbacks(WatchPortBaton* data) {
}

void EIO_AfterWatchPort(uv_work_t* req) {
NanScope();
Nan::HandleScope scope;

WatchPortBaton* data = static_cast<WatchPortBaton*>(req->data);
if(data->disconnected) {
Expand All @@ -301,16 +301,16 @@ void EIO_AfterWatchPort(uv_work_t* req) {
}

if(data->bytesRead > 0) {
v8::Handle<v8::Value> argv[1];
argv[0] = NanNewBufferHandle(data->buffer, data->bytesRead);
v8::Local<v8::Value> argv[1];
argv[0] = Nan::NewBuffer(data->buffer, data->bytesRead).ToLocalChecked();
data->dataCallback->Call(1, argv);
} else if(data->errorCode > 0) {
if(data->errorCode == ERROR_INVALID_HANDLE && IsClosingHandle((int)data->fd)) {
DisposeWatchPortCallbacks(data);
goto cleanup;
} else {
v8::Handle<v8::Value> argv[1];
argv[0] = NanError(data->errorString);
v8::Local<v8::Value> argv[1];
argv[0] = Nan::Error(data->errorString);
data->errorCallback->Call(1, argv);
Sleep(100); // prevent the errors from occurring too fast
}
Expand All @@ -322,7 +322,7 @@ void EIO_AfterWatchPort(uv_work_t* req) {
delete req;
}

void AfterOpenSuccess(int fd, NanCallback* dataCallback, NanCallback* disconnectedCallback, NanCallback* errorCallback) {
void AfterOpenSuccess(int fd, Nan::Callback* dataCallback, Nan::Callback* disconnectedCallback, Nan::Callback* errorCallback) {
WatchPortBaton* baton = new WatchPortBaton();
memset(baton, 0, sizeof(WatchPortBaton));
baton->fd = (HANDLE)fd;
Expand Down
6 changes: 3 additions & 3 deletions src/win/enumser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ History: PJN / 23-02-1999 Code now uses QueryDosDevice if running on NT to deter
the ports at all. It should operate a lot faster in addition.
PJN / 12-12-1999 Fixed a problem in the Win9x code path when trying to detect
deactivated IRDA-ports. When trying to open those, you will
get the error-code ERROR_GEN_FAILURE.�
get the error-code ERROR_GEN_FAILURE.�
PJN / 17-05-2000 Code now uses GetDefaultCommConfig in all cases to detect
the ports.
PJN / 29-03-2001 1. Reverted code to use CreateFile or QueryDosDevice as it is
Expand Down Expand Up @@ -963,7 +963,7 @@ BOOL CEnumerateSerial::UsingWMI(CSimpleArray<UINT>& ports, CSimpleArray<CString>
for (ULONG n=0; n<uReturned; n++)
{
ATL::CComVariant varProperty1;
HRESULT hrGet = apObj[n]->Get(L"DeviceID", 0, &varProperty1, NULL, NULL);
HRESULT hrGet = Nan::Get(apObj[n], L"DeviceID", 0, &varProperty1, NULL, NULL);
if (SUCCEEDED(hrGet) && (varProperty1.vt == VT_BSTR) && (wcslen(varProperty1.bstrVal) > 3))
{
//If it looks like "COMX" then add it to the array which will be returned
Expand All @@ -979,7 +979,7 @@ BOOL CEnumerateSerial::UsingWMI(CSimpleArray<UINT>& ports, CSimpleArray<CString>

//Also get the friendly name of the port
ATL::CComVariant varProperty2;
if (SUCCEEDED(apObj[n]->Get(L"Name", 0, &varProperty2, NULL, NULL)) && (varProperty2.vt == VT_BSTR))
if Nan::Get((SUCCEEDED(apObj[n], L"Name", 0, &varProperty2, NULL, NULL)) && (varProperty2.vt == VT_BSTR))
{
#if defined CENUMERATESERIAL_USE_STL
#if defined _UNICODE
Expand Down
Loading