From 5f1a6b84c0020b76d1002517d1ce2d013cb27e2e Mon Sep 17 00:00:00 2001 From: Zach Bjornson Date: Fri, 4 Jan 2019 15:33:36 -0800 Subject: [PATCH] fix: propagate async context in callbacks Fixes #1751 --- packages/bindings/src/poller.cpp | 4 +- packages/bindings/src/poller.h | 2 +- packages/bindings/src/serialport.cpp | 16 ++--- packages/bindings/src/serialport.h | 90 +++++++++++++----------- packages/bindings/src/serialport_win.cpp | 6 +- packages/bindings/src/serialport_win.h | 47 +++++++------ 6 files changed, 87 insertions(+), 78 deletions(-) diff --git a/packages/bindings/src/poller.cpp b/packages/bindings/src/poller.cpp index e9ec383f39..8b4c2a881d 100644 --- a/packages/bindings/src/poller.cpp +++ b/packages/bindings/src/poller.cpp @@ -1,7 +1,7 @@ #include #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(); @@ -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) { diff --git a/packages/bindings/src/poller.h b/packages/bindings/src/poller.h index 1bcaf63e33..67f3569161 100644 --- a/packages/bindings/src/poller.h +++ b/packages/bindings/src/poller.h @@ -3,7 +3,7 @@ #include -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); diff --git a/packages/bindings/src/serialport.cpp b/packages/bindings/src/serialport.cpp index adfff121ba..f61f961e5a 100644 --- a/packages/bindings/src/serialport.cpp +++ b/packages/bindings/src/serialport.cpp @@ -92,7 +92,7 @@ void EIO_AfterOpen(uv_work_t* req) { argv[1] = Nan::New(data->result); } - Nan::Call(data->callback, Nan::GetCurrentContext()->Global(), 2, argv); + data->callback.Call(2, argv, data); delete data; delete req; } @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/packages/bindings/src/serialport.h b/packages/bindings/src/serialport.h index e25d29c4ed..aac2e97175 100644 --- a/packages/bindings/src/serialport.h +++ b/packages/bindings/src/serialport.h @@ -57,68 +57,74 @@ enum SerialPortStopBits { SerialPortParity ToParityEnum(const v8::Local& str); SerialPortStopBits ToStopBitEnum(double stopBits); -struct OpenBaton { - char errorString[ERROR_STRING_SIZE]; +struct OpenBaton : public Nan::AsyncResource { + OpenBaton() : AsyncResource("node-serialport:OpenBaton") {} + 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; + char path[1024] = ""; + 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 { - char errorString[ERROR_STRING_SIZE]; +struct ConnectionOptionsBaton : public Nan::AsyncResource { + ConnectionOptionsBaton() : AsyncResource("node-serialport:ConnectionOptionsBaton") {} + 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") {} + int fd = 0; Nan::Callback callback; - int result; - char errorString[ERROR_STRING_SIZE]; - bool rts; - bool cts; - bool dtr; - bool dsr; - bool brk; + int result = 0; + char errorString[ERROR_STRING_SIZE] = ""; + 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") {} + int fd = 0; Nan::Callback callback; - char errorString[ERROR_STRING_SIZE]; - bool cts; - bool dsr; - bool dcd; + char errorString[ERROR_STRING_SIZE] = ""; + bool cts = false; + bool dsr = false; + bool dcd = false; }; -struct GetBaudRateBaton { - int fd; +struct GetBaudRateBaton : public Nan::AsyncResource { + GetBaudRateBaton() : AsyncResource("node-serialport:GetBaudRateBaton") {} + int fd = 0; Nan::Callback callback; - char errorString[ERROR_STRING_SIZE]; - int baudRate; + char errorString[ERROR_STRING_SIZE] = ""; + int baudRate = 0; }; -struct VoidBaton { - int fd; +struct VoidBaton : public Nan::AsyncResource { + VoidBaton() : AsyncResource("node-serialport:VoidBaton") {} + int fd = 0; Nan::Callback callback; - char errorString[ERROR_STRING_SIZE]; + char errorString[ERROR_STRING_SIZE] = ""; }; int setup(int fd, OpenBaton *data); diff --git a/packages/bindings/src/serialport_win.cpp b/packages/bindings/src/serialport_win.cpp index 9294f80c48..736dbf6cd9 100644 --- a/packages/bindings/src/serialport_win.cpp +++ b/packages/bindings/src/serialport_win.cpp @@ -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; } @@ -571,7 +571,7 @@ void EIO_AfterRead(uv_async_t* req) { argv[1] = Nan::New(static_cast(baton->bytesRead)); } - Nan::Call(baton->callback, Nan::GetCurrentContext()->Global(), 2, argv); + baton->callback.Call(2, argv, baton); delete baton; } @@ -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::iterator it = data->results.begin(); it != data->results.end(); ++it) { delete *it; diff --git a/packages/bindings/src/serialport_win.h b/packages/bindings/src/serialport_win.h index a385b77a2d..1497e97856 100644 --- a/packages/bindings/src/serialport_win.h +++ b/packages/bindings/src/serialport_win.h @@ -9,18 +9,19 @@ #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") {} + int fd = 0; + char* bufferData = ""; + size_t bufferLength = 0; + size_t offset = 0; + size_t bytesWritten = 0; + void* hThread = nullptr; + bool complete = false; Nan::Persistent buffer; Nan::Callback callback; - int result; - char errorString[ERROR_STRING_SIZE]; + int result = 0; + char errorString[ERROR_STRING_SIZE] = ""; }; NAN_METHOD(Write); @@ -29,16 +30,17 @@ 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; - char errorString[ERROR_STRING_SIZE]; +struct ReadBaton : public Nan::AsyncResource { + ReadBaton() : AsyncResource("node-serialport:ReadBaton") {} + int fd = 0; + char* bufferData = ""; + 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; }; @@ -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 results; - char errorString[ERROR_STRING_SIZE]; + char errorString[ERROR_STRING_SIZE] = ""; }; #endif // PACKAGES_SERIALPORT_SRC_SERIALPORT_WIN_H_