Skip to content

Commit

Permalink
fix: propagate async context in callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
zbjornson committed Jan 6, 2019
1 parent d4f5128 commit e3f03f2
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 71 deletions.
2 changes: 1 addition & 1 deletion packages/bindings/src/darwin_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ void EIO_AfterList(uv_work_t* req) {
argv[0] = Nan::Null();
argv[1] = results;
}
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 2, argv);
data->callback.Call(2, argv, data);

for (std::list<ListResultItem*>::iterator it = data->results.begin(); it != data->results.end(); ++it) {
delete *it;
Expand Down
3 changes: 2 additions & 1 deletion packages/bindings/src/darwin_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ struct ListResultItem {
std::string productId;
};

struct ListBaton {
struct ListBaton : public Nan::AsyncResource {
ListBaton() : AsyncResource("node-serialport:ListBaton"), errorString() {}
Nan::Callback callback;
std::list<ListResultItem*> results;
char errorString[ERROR_STRING_SIZE];
Expand Down
4 changes: 2 additions & 2 deletions packages/bindings/src/poller.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <nan.h>
#include "./poller.h"

Poller::Poller(int fd) {
Poller::Poller(int fd) : AsyncResource("node-serialport:poller") {
Nan::HandleScope scope;
this->fd = fd;
this->poll_handle = new uv_poll_t();
Expand Down Expand Up @@ -66,7 +66,7 @@ void Poller::onData(uv_poll_t* handle, int status, int events) {
int newEvents = obj->events & ~events;
obj->poll(newEvents);

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

NAN_MODULE_INIT(Poller::Init) {
Expand Down
2 changes: 1 addition & 1 deletion packages/bindings/src/poller.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <nan.h>

class Poller : public Nan::ObjectWrap {
class Poller : public Nan::ObjectWrap, public Nan::AsyncResource {
public:
static NAN_MODULE_INIT(Init);
static void onData(uv_poll_t* handle, int status, int events);
Expand Down
16 changes: 8 additions & 8 deletions packages/bindings/src/serialport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void EIO_AfterOpen(uv_work_t* req) {
argv[1] = Nan::New<v8::Int32>(data->result);
}

Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 2, argv);
data->callback.Call(2, argv, data);
delete data;
delete req;
}
Expand Down Expand Up @@ -147,7 +147,7 @@ void EIO_AfterUpdate(uv_work_t* req) {
argv[0] = Nan::Null();
}

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

delete data;
delete req;
Expand Down Expand Up @@ -185,7 +185,7 @@ void EIO_AfterClose(uv_work_t* req) {
} else {
argv[0] = Nan::Null();
}
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 1, argv);
data->callback.Call(1, argv, data);

delete data;
delete req;
Expand Down Expand Up @@ -228,7 +228,7 @@ void EIO_AfterFlush(uv_work_t* req) {
argv[0] = Nan::Null();
}

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

delete data;
delete req;
Expand Down Expand Up @@ -282,7 +282,7 @@ void EIO_AfterSet(uv_work_t* req) {
} else {
argv[0] = Nan::Null();
}
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 1, argv);
data->callback.Call(1, argv, data);

delete data;
delete req;
Expand Down Expand Up @@ -333,7 +333,7 @@ void EIO_AfterGet(uv_work_t* req) {
argv[0] = Nan::Null();
argv[1] = results;
}
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 2, argv);
data->callback.Call(2, argv, data);

delete data;
delete req;
Expand Down Expand Up @@ -380,7 +380,7 @@ void EIO_AfterGetBaudRate(uv_work_t* req) {
argv[0] = Nan::Null();
argv[1] = results;
}
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 2, argv);
data->callback.Call(2, argv, data);

delete data;
delete req;
Expand Down Expand Up @@ -421,7 +421,7 @@ void EIO_AfterDrain(uv_work_t* req) {
} else {
argv[0] = Nan::Null();
}
Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 1, argv);
data->callback.Call(1, argv, data);

delete data;
delete req;
Expand Down
79 changes: 44 additions & 35 deletions packages/bindings/src/serialport.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,66 +57,75 @@ enum SerialPortStopBits {
SerialPortParity ToParityEnum(const v8::Local<v8::String>& str);
SerialPortStopBits ToStopBitEnum(double stopBits);

struct OpenBaton {
struct OpenBaton : public Nan::AsyncResource {
OpenBaton() :
AsyncResource("node-serialport:OpenBaton"), errorString(), path() {}
char errorString[ERROR_STRING_SIZE];
Nan::Callback callback;
char path[1024];
int fd;
int result;
int baudRate;
int dataBits;
bool rtscts;
bool xon;
bool xoff;
bool xany;
bool dsrdtr;
bool hupcl;
bool lock;
int fd = 0;
int result = 0;
int baudRate = 0;
int dataBits = 0;
bool rtscts = false;
bool xon = false;
bool xoff = false;
bool xany = false;
bool dsrdtr = false;
bool hupcl = false;
bool lock = false;
SerialPortParity parity;
SerialPortStopBits stopBits;
#ifndef WIN32
uint8_t vmin;
uint8_t vtime;
uint8_t vmin = 0;
uint8_t vtime = 0;
#endif
};

struct ConnectionOptionsBaton {
struct ConnectionOptionsBaton : public Nan::AsyncResource {
ConnectionOptionsBaton() :
AsyncResource("node-serialport:ConnectionOptionsBaton"), errorString() {}
char errorString[ERROR_STRING_SIZE];
Nan::Callback callback;
int fd;
int baudRate;
int fd = 0;
int baudRate = 0;
};

struct SetBaton {
int fd;
struct SetBaton : public Nan::AsyncResource {
SetBaton() : AsyncResource("node-serialport:SetBaton"), errorString() {}
int fd = 0;
Nan::Callback callback;
int result;
int result = 0;
char errorString[ERROR_STRING_SIZE];
bool rts;
bool cts;
bool dtr;
bool dsr;
bool brk;
bool rts = false;
bool cts = false;
bool dtr = false;
bool dsr = false;
bool brk = false;
};

struct GetBaton {
int fd;
struct GetBaton : public Nan::AsyncResource {
GetBaton() : AsyncResource("node-serialport:GetBaton"), errorString() {}
int fd = 0;
Nan::Callback callback;
char errorString[ERROR_STRING_SIZE];
bool cts;
bool dsr;
bool dcd;
bool cts = false;
bool dsr = false;
bool dcd = false;
};

struct GetBaudRateBaton {
int fd;
struct GetBaudRateBaton : public Nan::AsyncResource {
GetBaudRateBaton() :
AsyncResource("node-serialport:GetBaudRateBaton"), errorString() {}
int fd = 0;
Nan::Callback callback;
char errorString[ERROR_STRING_SIZE];
int baudRate;
int baudRate = 0;
};

struct VoidBaton {
int fd;
struct VoidBaton : public Nan::AsyncResource {
VoidBaton() : AsyncResource("node-serialport:VoidBaton"), errorString() {}
int fd = 0;
Nan::Callback callback;
char errorString[ERROR_STRING_SIZE];
};
Expand Down
6 changes: 3 additions & 3 deletions packages/bindings/src/serialport_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ void EIO_AfterWrite(uv_async_t* req) {
} else {
argv[0] = Nan::Null();
}
Nan::Call(baton->callback, Nan::GetCurrentContext()->Global(), 1, argv);
baton->callback.Call(1, argv, baton);
baton->buffer.Reset();
delete baton;
}
Expand Down Expand Up @@ -571,7 +571,7 @@ void EIO_AfterRead(uv_async_t* req) {
argv[1] = Nan::New<v8::Integer>(static_cast<int>(baton->bytesRead));
}

Nan::Call(baton->callback, Nan::GetCurrentContext()->Global(), 2, argv);
baton->callback.Call(2, argv, baton);
delete baton;
}

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

for (std::list<ListResultItem*>::iterator it = data->results.begin(); it != data->results.end(); ++it) {
delete *it;
Expand Down
43 changes: 23 additions & 20 deletions packages/bindings/src/serialport_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@

#define ERROR_STRING_SIZE 1024

struct WriteBaton {
int fd;
char* bufferData;
size_t bufferLength;
size_t offset;
size_t bytesWritten;
void* hThread;
bool complete;
struct WriteBaton : public Nan::AsyncResource {
WriteBaton() : AsyncResource("node-serialport:WriteBaton"), bufferData(), errorString() {}
int fd = 0;
char* bufferData = nullptr;
size_t bufferLength = 0;
size_t offset = 0;
size_t bytesWritten = 0;
void* hThread = nullptr;
bool complete = false;
Nan::Persistent<v8::Object> buffer;
Nan::Callback callback;
int result;
int result = 0;
char errorString[ERROR_STRING_SIZE];
};

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


struct ReadBaton {
int fd;
char* bufferData;
size_t bufferLength;
size_t bytesRead;
size_t bytesToRead;
size_t offset;
void* hThread;
bool complete;
struct ReadBaton : public Nan::AsyncResource {
ReadBaton() : AsyncResource("node-serialport:ReadBaton"), errorString() {}
int fd = 0;
char* bufferData = nullptr;
size_t bufferLength = 0;
size_t bytesRead = 0;
size_t bytesToRead = 0;
size_t offset = 0;
void* hThread = nullptr;
bool complete = false;
char errorString[ERROR_STRING_SIZE];
Nan::Callback callback;
};
Expand All @@ -62,10 +64,11 @@ struct ListResultItem {
std::string productId;
};

struct ListBaton {
struct ListBaton : public Nan::AsyncResource {
ListBaton() : AsyncResource("node-serialport:ListBaton") {}
Nan::Callback callback;
std::list<ListResultItem*> results;
char errorString[ERROR_STRING_SIZE];
char errorString[ERROR_STRING_SIZE] = "";
};

#endif // PACKAGES_SERIALPORT_SRC_SERIALPORT_WIN_H_

0 comments on commit e3f03f2

Please sign in to comment.