Skip to content

Commit 9b5dbdb

Browse files
zbjornsonreconbot
authored andcommitted
fix: propagate async context in callbacks (#1765)
Fixes #1751
1 parent d4f5128 commit 9b5dbdb

File tree

8 files changed

+84
-71
lines changed

8 files changed

+84
-71
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-
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 2, argv);
352+
data->callback.Call(2, argv, data);
353353

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

packages/bindings/src/darwin_list.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ struct ListResultItem {
2121
std::string productId;
2222
};
2323

24-
struct ListBaton {
24+
struct ListBaton : public Nan::AsyncResource {
25+
ListBaton() : AsyncResource("node-serialport:ListBaton"), errorString() {}
2526
Nan::Callback callback;
2627
std::list<ListResultItem*> results;
2728
char errorString[ERROR_STRING_SIZE];

packages/bindings/src/poller.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <nan.h>
22
#include "./poller.h"
33

4-
Poller::Poller(int fd) {
4+
Poller::Poller(int fd) : AsyncResource("node-serialport:poller") {
55
Nan::HandleScope scope;
66
this->fd = fd;
77
this->poll_handle = new uv_poll_t();
@@ -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-
Nan::Call(obj->callback, Nan::GetCurrentContext()->Global(), 2, argv);
69+
obj->callback.Call(2, argv, obj);
7070
}
7171

7272
NAN_MODULE_INIT(Poller::Init) {

packages/bindings/src/poller.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <nan.h>
55

6-
class Poller : public Nan::ObjectWrap {
6+
class Poller : public Nan::ObjectWrap, public Nan::AsyncResource {
77
public:
88
static NAN_MODULE_INIT(Init);
99
static void onData(uv_poll_t* handle, int status, int events);

packages/bindings/src/serialport.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void EIO_AfterOpen(uv_work_t* req) {
9292
argv[1] = Nan::New<v8::Int32>(data->result);
9393
}
9494

95-
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 2, argv);
95+
data->callback.Call(2, argv, data);
9696
delete data;
9797
delete req;
9898
}
@@ -147,7 +147,7 @@ void EIO_AfterUpdate(uv_work_t* req) {
147147
argv[0] = Nan::Null();
148148
}
149149

150-
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 1, argv);
150+
data->callback.Call(1, argv, data);
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-
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 1, argv);
188+
data->callback.Call(1, argv, data);
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-
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 1, argv);
231+
data->callback.Call(1, argv, data);
232232

233233
delete data;
234234
delete req;
@@ -282,7 +282,7 @@ void EIO_AfterSet(uv_work_t* req) {
282282
} else {
283283
argv[0] = Nan::Null();
284284
}
285-
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 1, argv);
285+
data->callback.Call(1, argv, data);
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-
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 2, argv);
336+
data->callback.Call(2, argv, data);
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-
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 2, argv);
383+
data->callback.Call(2, argv, data);
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-
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 1, argv);
424+
data->callback.Call(1, argv, data);
425425

426426
delete data;
427427
delete req;

packages/bindings/src/serialport.h

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -57,66 +57,75 @@ enum SerialPortStopBits {
5757
SerialPortParity ToParityEnum(const v8::Local<v8::String>& str);
5858
SerialPortStopBits ToStopBitEnum(double stopBits);
5959

60-
struct OpenBaton {
60+
struct OpenBaton : public Nan::AsyncResource {
61+
OpenBaton() :
62+
AsyncResource("node-serialport:OpenBaton"), errorString(), path() {}
6163
char errorString[ERROR_STRING_SIZE];
6264
Nan::Callback callback;
6365
char path[1024];
64-
int fd;
65-
int result;
66-
int baudRate;
67-
int dataBits;
68-
bool rtscts;
69-
bool xon;
70-
bool xoff;
71-
bool xany;
72-
bool dsrdtr;
73-
bool hupcl;
74-
bool lock;
66+
int fd = 0;
67+
int result = 0;
68+
int baudRate = 0;
69+
int dataBits = 0;
70+
bool rtscts = false;
71+
bool xon = false;
72+
bool xoff = false;
73+
bool xany = false;
74+
bool dsrdtr = false;
75+
bool hupcl = false;
76+
bool lock = false;
7577
SerialPortParity parity;
7678
SerialPortStopBits stopBits;
7779
#ifndef WIN32
78-
uint8_t vmin;
79-
uint8_t vtime;
80+
uint8_t vmin = 0;
81+
uint8_t vtime = 0;
8082
#endif
8183
};
8284

83-
struct ConnectionOptionsBaton {
85+
struct ConnectionOptionsBaton : public Nan::AsyncResource {
86+
ConnectionOptionsBaton() :
87+
AsyncResource("node-serialport:ConnectionOptionsBaton"), errorString() {}
8488
char errorString[ERROR_STRING_SIZE];
8589
Nan::Callback callback;
86-
int fd;
87-
int baudRate;
90+
int fd = 0;
91+
int baudRate = 0;
8892
};
8993

90-
struct SetBaton {
91-
int fd;
94+
struct SetBaton : public Nan::AsyncResource {
95+
SetBaton() : AsyncResource("node-serialport:SetBaton"), errorString() {}
96+
int fd = 0;
9297
Nan::Callback callback;
93-
int result;
98+
int result = 0;
9499
char errorString[ERROR_STRING_SIZE];
95-
bool rts;
96-
bool cts;
97-
bool dtr;
98-
bool dsr;
99-
bool brk;
100+
bool rts = false;
101+
bool cts = false;
102+
bool dtr = false;
103+
bool dsr = false;
104+
bool brk = false;
100105
};
101106

102-
struct GetBaton {
103-
int fd;
107+
struct GetBaton : public Nan::AsyncResource {
108+
GetBaton() : AsyncResource("node-serialport:GetBaton"), errorString() {}
109+
int fd = 0;
104110
Nan::Callback callback;
105111
char errorString[ERROR_STRING_SIZE];
106-
bool cts;
107-
bool dsr;
108-
bool dcd;
112+
bool cts = false;
113+
bool dsr = false;
114+
bool dcd = false;
109115
};
110116

111-
struct GetBaudRateBaton {
112-
int fd;
117+
struct GetBaudRateBaton : public Nan::AsyncResource {
118+
GetBaudRateBaton() :
119+
AsyncResource("node-serialport:GetBaudRateBaton"), errorString() {}
120+
int fd = 0;
113121
Nan::Callback callback;
114122
char errorString[ERROR_STRING_SIZE];
115-
int baudRate;
123+
int baudRate = 0;
116124
};
117125

118-
struct VoidBaton {
119-
int fd;
126+
struct VoidBaton : public Nan::AsyncResource {
127+
VoidBaton() : AsyncResource("node-serialport:VoidBaton"), errorString() {}
128+
int fd = 0;
120129
Nan::Callback callback;
121130
char errorString[ERROR_STRING_SIZE];
122131
};

packages/bindings/src/serialport_win.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ void EIO_AfterWrite(uv_async_t* req) {
387387
} else {
388388
argv[0] = Nan::Null();
389389
}
390-
Nan::Call(baton->callback, Nan::GetCurrentContext()->Global(), 1, argv);
390+
baton->callback.Call(1, argv, baton);
391391
baton->buffer.Reset();
392392
delete baton;
393393
}
@@ -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-
Nan::Call(baton->callback, Nan::GetCurrentContext()->Global(), 2, argv);
574+
baton->callback.Call(2, argv, baton);
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-
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 2, argv);
921+
data->callback.Call(2, argv, data);
922922

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

packages/bindings/src/serialport_win.h

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99

1010
#define ERROR_STRING_SIZE 1024
1111

12-
struct WriteBaton {
13-
int fd;
14-
char* bufferData;
15-
size_t bufferLength;
16-
size_t offset;
17-
size_t bytesWritten;
18-
void* hThread;
19-
bool complete;
12+
struct WriteBaton : public Nan::AsyncResource {
13+
WriteBaton() : AsyncResource("node-serialport:WriteBaton"), bufferData(), errorString() {}
14+
int fd = 0;
15+
char* bufferData = nullptr;
16+
size_t bufferLength = 0;
17+
size_t offset = 0;
18+
size_t bytesWritten = 0;
19+
void* hThread = nullptr;
20+
bool complete = false;
2021
Nan::Persistent<v8::Object> buffer;
2122
Nan::Callback callback;
22-
int result;
23+
int result = 0;
2324
char errorString[ERROR_STRING_SIZE];
2425
};
2526

@@ -29,15 +30,16 @@ void EIO_AfterWrite(uv_async_t* req);
2930
DWORD __stdcall WriteThread(LPVOID param);
3031

3132

32-
struct ReadBaton {
33-
int fd;
34-
char* bufferData;
35-
size_t bufferLength;
36-
size_t bytesRead;
37-
size_t bytesToRead;
38-
size_t offset;
39-
void* hThread;
40-
bool complete;
33+
struct ReadBaton : public Nan::AsyncResource {
34+
ReadBaton() : AsyncResource("node-serialport:ReadBaton"), errorString() {}
35+
int fd = 0;
36+
char* bufferData = nullptr;
37+
size_t bufferLength = 0;
38+
size_t bytesRead = 0;
39+
size_t bytesToRead = 0;
40+
size_t offset = 0;
41+
void* hThread = nullptr;
42+
bool complete = false;
4143
char errorString[ERROR_STRING_SIZE];
4244
Nan::Callback callback;
4345
};
@@ -62,10 +64,11 @@ struct ListResultItem {
6264
std::string productId;
6365
};
6466

65-
struct ListBaton {
67+
struct ListBaton : public Nan::AsyncResource {
68+
ListBaton() : AsyncResource("node-serialport:ListBaton") {}
6669
Nan::Callback callback;
6770
std::list<ListResultItem*> results;
68-
char errorString[ERROR_STRING_SIZE];
71+
char errorString[ERROR_STRING_SIZE] = "";
6972
};
7073

7174
#endif // PACKAGES_SERIALPORT_SRC_SERIALPORT_WIN_H_

0 commit comments

Comments
 (0)