Skip to content

Commit 1eecd60

Browse files
zbjornsonreconbot
authored andcommitted
fix: deprecated c++ functions for update to Node v12 (#1743)
* update ToObject conversions * Use NAN_MODULE_INIT * Update string conversion * Use Nan for callback invocations
1 parent 648ac84 commit 1eecd60

File tree

4 files changed

+41
-43
lines changed

4 files changed

+41
-43
lines changed

packages/bindings/src/darwin_list.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ void EIO_AfterList(uv_work_t* req) {
349349
argv[0] = Nan::Null();
350350
argv[1] = results;
351351
}
352-
data->callback.Call(2, argv);
352+
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 2, argv);
353353

354354
for (std::list<ListResultItem*>::iterator it = data->results.begin(); it != data->results.end(); ++it) {
355355
delete *it;

packages/bindings/src/poller.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void Poller::onData(uv_poll_t* handle, int status, int events) {
6666
int newEvents = obj->events & ~events;
6767
obj->poll(newEvents);
6868

69-
obj->callback.Call(2, argv);
69+
Nan::Call(obj->callback, Nan::GetCurrentContext()->Global(), 2, argv);
7070
}
7171

7272
NAN_MODULE_INIT(Poller::Init) {

packages/bindings/src/serialport.cpp

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ NAN_METHOD(Open) {
3838
Nan::ThrowTypeError("First argument must be a string");
3939
return;
4040
}
41-
v8::String::Utf8Value path(info[0]->ToString());
41+
v8::String::Utf8Value path(Nan::To<v8::String>(info[0]).ToLocalChecked());
4242

4343
// options
4444
if (!info[1]->IsObject()) {
4545
Nan::ThrowTypeError("Second argument must be an object");
4646
return;
4747
}
48-
v8::Local<v8::Object> options = info[1]->ToObject();
48+
v8::Local<v8::Object> options = Nan::To<v8::Object>(info[1]).ToLocalChecked();
4949

5050
// callback
5151
if (!info[2]->IsFunction()) {
@@ -92,7 +92,7 @@ void EIO_AfterOpen(uv_work_t* req) {
9292
argv[1] = Nan::New<v8::Int32>(data->result);
9393
}
9494

95-
data->callback.Call(2, argv);
95+
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 2, argv);
9696
delete data;
9797
delete req;
9898
}
@@ -110,7 +110,7 @@ NAN_METHOD(Update) {
110110
Nan::ThrowTypeError("Second argument must be an object");
111111
return;
112112
}
113-
v8::Local<v8::Object> options = info[1]->ToObject();
113+
v8::Local<v8::Object> options = Nan::To<v8::Object>(info[1]).ToLocalChecked();
114114

115115
if (!Nan::Has(options, Nan::New<v8::String>("baudRate").ToLocalChecked()).FromMaybe(false)) {
116116
Nan::ThrowTypeError("\"baudRate\" must be set on options object");
@@ -147,7 +147,7 @@ void EIO_AfterUpdate(uv_work_t* req) {
147147
argv[0] = Nan::Null();
148148
}
149149

150-
data->callback.Call(1, argv);
150+
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 1, argv);
151151

152152
delete data;
153153
delete req;
@@ -185,7 +185,7 @@ void EIO_AfterClose(uv_work_t* req) {
185185
} else {
186186
argv[0] = Nan::Null();
187187
}
188-
data->callback.Call(1, argv);
188+
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 1, argv);
189189

190190
delete data;
191191
delete req;
@@ -228,7 +228,7 @@ void EIO_AfterFlush(uv_work_t* req) {
228228
argv[0] = Nan::Null();
229229
}
230230

231-
data->callback.Call(1, argv);
231+
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 1, argv);
232232

233233
delete data;
234234
delete req;
@@ -247,7 +247,7 @@ NAN_METHOD(Set) {
247247
Nan::ThrowTypeError("Second argument must be an object");
248248
return;
249249
}
250-
v8::Local<v8::Object> options = info[1]->ToObject();
250+
v8::Local<v8::Object> options = Nan::To<v8::Object>(info[1]).ToLocalChecked();
251251

252252
// callback
253253
if (!info[2]->IsFunction()) {
@@ -282,7 +282,7 @@ void EIO_AfterSet(uv_work_t* req) {
282282
} else {
283283
argv[0] = Nan::Null();
284284
}
285-
data->callback.Call(1, argv);
285+
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 1, argv);
286286

287287
delete data;
288288
delete req;
@@ -333,7 +333,7 @@ void EIO_AfterGet(uv_work_t* req) {
333333
argv[0] = Nan::Null();
334334
argv[1] = results;
335335
}
336-
data->callback.Call(2, argv);
336+
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 2, argv);
337337

338338
delete data;
339339
delete req;
@@ -380,7 +380,7 @@ void EIO_AfterGetBaudRate(uv_work_t* req) {
380380
argv[0] = Nan::Null();
381381
argv[1] = results;
382382
}
383-
data->callback.Call(2, argv);
383+
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 2, argv);
384384

385385
delete data;
386386
delete req;
@@ -421,7 +421,7 @@ void EIO_AfterDrain(uv_work_t* req) {
421421
} else {
422422
argv[0] = Nan::Null();
423423
}
424-
data->callback.Call(1, argv);
424+
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 1, argv);
425425

426426
delete data;
427427
delete req;
@@ -456,30 +456,28 @@ SerialPortStopBits NAN_INLINE(ToStopBitEnum(double stopBits)) {
456456
return SERIALPORT_STOPBITS_ONE;
457457
}
458458

459-
extern "C" {
460-
void init(v8::Handle<v8::Object> target) {
461-
Nan::HandleScope scope;
462-
Nan::SetMethod(target, "set", Set);
463-
Nan::SetMethod(target, "get", Get);
464-
Nan::SetMethod(target, "getBaudRate", GetBaudRate);
465-
Nan::SetMethod(target, "open", Open);
466-
Nan::SetMethod(target, "update", Update);
467-
Nan::SetMethod(target, "close", Close);
468-
Nan::SetMethod(target, "flush", Flush);
469-
Nan::SetMethod(target, "drain", Drain);
470-
471-
#ifdef __APPLE__
472-
Nan::SetMethod(target, "list", List);
473-
#endif
474-
475-
#ifdef WIN32
476-
Nan::SetMethod(target, "write", Write);
477-
Nan::SetMethod(target, "read", Read);
478-
Nan::SetMethod(target, "list", List);
479-
#else
480-
Poller::Init(target);
481-
#endif
482-
}
459+
NAN_MODULE_INIT(init) {
460+
Nan::HandleScope scope;
461+
Nan::SetMethod(target, "set", Set);
462+
Nan::SetMethod(target, "get", Get);
463+
Nan::SetMethod(target, "getBaudRate", GetBaudRate);
464+
Nan::SetMethod(target, "open", Open);
465+
Nan::SetMethod(target, "update", Update);
466+
Nan::SetMethod(target, "close", Close);
467+
Nan::SetMethod(target, "flush", Flush);
468+
Nan::SetMethod(target, "drain", Drain);
469+
470+
#ifdef __APPLE__
471+
Nan::SetMethod(target, "list", List);
472+
#endif
473+
474+
#ifdef WIN32
475+
Nan::SetMethod(target, "write", Write);
476+
Nan::SetMethod(target, "read", Read);
477+
Nan::SetMethod(target, "list", List);
478+
#else
479+
Poller::Init(target);
480+
#endif
483481
}
484482

485483
NODE_MODULE(serialport, init);

packages/bindings/src/serialport_win.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ NAN_METHOD(Write) {
301301
Nan::ThrowTypeError("Second argument must be a buffer");
302302
return;
303303
}
304-
v8::Local<v8::Object> buffer = info[1]->ToObject();
304+
v8::Local<v8::Object> buffer = Nan::To<v8::Object>(info[1]).ToLocalChecked();
305305
char* bufferData = node::Buffer::Data(buffer);
306306
size_t bufferLength = node::Buffer::Length(buffer);
307307

@@ -387,7 +387,7 @@ void EIO_AfterWrite(uv_async_t* req) {
387387
} else {
388388
argv[0] = Nan::Null();
389389
}
390-
baton->callback.Call(1, argv);
390+
Nan::Call(baton->callback, Nan::GetCurrentContext()->Global(), 1, argv);
391391
baton->buffer.Reset();
392392
delete baton;
393393
}
@@ -405,7 +405,7 @@ NAN_METHOD(Read) {
405405
Nan::ThrowTypeError("Second argument must be a buffer");
406406
return;
407407
}
408-
v8::Local<v8::Object> buffer = info[1]->ToObject();
408+
v8::Local<v8::Object> buffer = Nan::To<v8::Object>(info[1]).ToLocalChecked();
409409
size_t bufferLength = node::Buffer::Length(buffer);
410410

411411
// offset
@@ -571,7 +571,7 @@ void EIO_AfterRead(uv_async_t* req) {
571571
argv[1] = Nan::New<v8::Integer>(static_cast<int>(baton->bytesRead));
572572
}
573573

574-
baton->callback.Call(2, argv);
574+
Nan::Call(baton->callback, Nan::GetCurrentContext()->Global(), 2, argv);
575575
delete baton;
576576
}
577577

@@ -918,7 +918,7 @@ void EIO_AfterList(uv_work_t* req) {
918918
argv[0] = Nan::Null();
919919
argv[1] = results;
920920
}
921-
data->callback.Call(2, argv);
921+
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 2, argv);
922922

923923
for (std::list<ListResultItem*>::iterator it = data->results.begin(); it != data->results.end(); ++it) {
924924
delete *it;

0 commit comments

Comments
 (0)