From 30a50512f644ba5ade92a717e85d3812e516567f Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Mon, 30 Dec 2024 01:28:26 -0800 Subject: [PATCH 1/2] fix: use the Napi::Addon interface for the module --- src/module.cc | 20 ++++++-------------- src/module.h | 4 ++-- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/module.cc b/src/module.cc index 554d7225..bed5f00f 100644 --- a/src/module.cc +++ b/src/module.cc @@ -102,10 +102,10 @@ Module::Global::Shared Module::Global::Instance() { return instance; } -Module::Module(Napi::Object exports) : MsgTrash(exports.Env()) { - exports.Set("version", zmq::Version(exports.Env())); - exports.Set("capability", zmq::Capabilities(exports.Env())); - exports.Set("curveKeyPair", Napi::Function::New(exports.Env(), zmq::CurveKeyPair)); +Module::Module(Napi::Env env, Napi::Object exports) : MsgTrash(env) { + exports.Set("version", zmq::Version(env)); + exports.Set("capability", zmq::Capabilities(env)); + exports.Set("curveKeyPair", Napi::Function::New(env, zmq::CurveKeyPair)); Context::Initialize(*this, exports); Socket::Initialize(*this, exports); @@ -117,13 +117,5 @@ Module::Module(Napi::Object exports) : MsgTrash(exports.Env()) { } } // namespace zmq -/* This initializer can be called in multiple contexts, like worker threads. */ -NAPI_MODULE_INIT(/* env, exports */) { - auto* module = new zmq::Module(Napi::Object(env, exports)); - auto terminate = [](void* data) { delete reinterpret_cast(data); }; - - /* Tear down the module class when the env/agent/thread is closed.*/ - [[maybe_unused]] auto status = napi_add_env_cleanup_hook(env, terminate, module); - assert(status == napi_ok); - return exports; -} +using Module = zmq::Module; +NODE_API_ADDON(Module) diff --git a/src/module.h b/src/module.h index 5e3a70a7..063e5674 100644 --- a/src/module.h +++ b/src/module.h @@ -46,7 +46,7 @@ struct Terminator { } }; -class Module { +class Module : public Napi::Addon { /* Contains shared global state that will be accessible by all agents/threads. */ class Global { @@ -67,7 +67,7 @@ class Module { }; public: - explicit Module(Napi::Object exports); + explicit Module(Napi::Env env, Napi::Object exports); class Global& Global() { return *global; From 2b839a761c9527117d6d4521e0c6415799c9ace8 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Mon, 30 Dec 2024 02:05:12 -0800 Subject: [PATCH 2/2] fix: static cast instead of reinterpret cast where possible --- src/context.cc | 2 +- src/incoming_msg.cc | 2 +- src/observer.cc | 4 ++-- src/outgoing_msg.cc | 4 +--- src/poller.h | 6 +++--- src/proxy.cc | 2 +- src/socket.cc | 6 +++--- src/util/trash.h | 4 +--- src/util/uvdelayed.h | 2 +- src/util/uvwork.h | 4 ++-- 10 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/context.cc b/src/context.cc index 91d1970a..174b14ed 100644 --- a/src/context.cc +++ b/src/context.cc @@ -9,7 +9,7 @@ namespace zmq { Context::Context(const Napi::CallbackInfo& info) - : Napi::ObjectWrap(info), module(*reinterpret_cast(info.Data())) { + : Napi::ObjectWrap(info), module(*static_cast(info.Data())) { /* If this module has no global context, then create one with a process wide context pointer that is shared between threads/agents. */ if (module.GlobalContext.IsEmpty()) { diff --git a/src/incoming_msg.cc b/src/incoming_msg.cc index 0d105405..f1ad8663 100644 --- a/src/incoming_msg.cc +++ b/src/incoming_msg.cc @@ -27,7 +27,7 @@ Napi::Value IncomingMsg::IntoBuffer(const Napi::Env& env) { return env.Undefined(); } } - auto* data = reinterpret_cast(zmq_msg_data(ref->get())); + auto* data = static_cast(zmq_msg_data(ref->get())); auto length = zmq_msg_size(ref->get()); if (noElectronMemoryCage) { diff --git a/src/observer.cc b/src/observer.cc index d615f157..882e52e3 100644 --- a/src/observer.cc +++ b/src/observer.cc @@ -130,7 +130,7 @@ std::pair ProtoError(uint32_t val) { Observer::Observer(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info), async_context(Env(), "Observer"), poller(*this), - module(*reinterpret_cast(info.Data())) { + module(*static_cast(info.Data())) { Arg::Validator const args{ Arg::Required("Socket must be a socket object"), }; @@ -263,7 +263,7 @@ void Observer::Receive(const Napi::Promise::Deferred& res) { } } - auto* data2 = reinterpret_cast(zmq_msg_data(&msg2)); + auto* data2 = static_cast(zmq_msg_data(&msg2)); auto length = zmq_msg_size(&msg2); auto event = Napi::Object::New(Env()); diff --git a/src/outgoing_msg.cc b/src/outgoing_msg.cc index 64c80fc5..3585ee38 100644 --- a/src/outgoing_msg.cc +++ b/src/outgoing_msg.cc @@ -48,9 +48,7 @@ OutgoingMsg::OutgoingMsg(Napi::Value value, std::reference_wrapper modul auto length = str->size(); auto* data = str->data(); - auto release = [](void*, void* str) { - delete reinterpret_cast(str); - }; + auto release = [](void*, void* str) { delete static_cast(str); }; if (zmq_msg_init_data(&msg, data, length, release, str) < 0) { delete str; diff --git a/src/poller.h b/src/poller.h index 132f83a9..b2063831 100644 --- a/src/poller.h +++ b/src/poller.h @@ -75,7 +75,7 @@ class Poller { readable_timer.get(), [](uv_timer_t* timer) { // NOLINTNEXTLINE(*-pro-type-reinterpret-cast) - auto& poller = *reinterpret_cast(timer->data); + auto& poller = *static_cast(timer->data); poller.Trigger(UV_READABLE); }, static_cast(timeout), 0); @@ -100,7 +100,7 @@ class Poller { writable_timer.get(), [](uv_timer_t* timer) { // NOLINTNEXTLINE(*-pro-type-reinterpret-cast) - auto& poller = *reinterpret_cast(timer->data); + auto& poller = *static_cast(timer->data); poller.Trigger(UV_WRITABLE); }, static_cast(timeout), 0); @@ -167,7 +167,7 @@ class Poller { static void Callback(uv_poll_t* poll, int32_t status, int32_t /*events*/) { if (status == 0) { // NOLINTNEXTLINE(*-pro-type-reinterpret-cast) - auto& poller = *reinterpret_cast(poll->data); + auto& poller = *static_cast(poll->data); poller.TriggerReadable(); poller.TriggerWritable(); } diff --git a/src/proxy.cc b/src/proxy.cc index 520f2519..625ecf01 100644 --- a/src/proxy.cc +++ b/src/proxy.cc @@ -23,7 +23,7 @@ struct ProxyContext { Proxy::Proxy(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info), async_context(Env(), "Proxy"), - module(*reinterpret_cast(info.Data())) { + module(*static_cast(info.Data())) { Arg::Validator const args{ Arg::Required("Front-end must be a socket object"), Arg::Required("Back-end must be a socket object"), diff --git a/src/socket.cc b/src/socket.cc index 5e43011a..df8bf45f 100644 --- a/src/socket.cc +++ b/src/socket.cc @@ -67,7 +67,7 @@ struct AddressContext { Socket::Socket(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info), async_context(Env(), "Socket"), poller(*this), - module(*reinterpret_cast(info.Data())) { + module(*static_cast(info.Data())) { Arg::Validator const args{ Arg::Required("Socket type must be a number"), Arg::Optional("Options must be an object"), @@ -392,7 +392,7 @@ Napi::Value Socket::Bind(const Napi::CallbackInfo& info) { if (run_ctx->error != 0) { res.Reject(ErrnoException( Env(), static_cast(run_ctx->error), run_ctx->address) - .Value()); + .Value()); return; } @@ -448,7 +448,7 @@ Napi::Value Socket::Unbind(const Napi::CallbackInfo& info) { if (run_ctx->error != 0) { res.Reject(ErrnoException( Env(), static_cast(run_ctx->error), run_ctx->address) - .Value()); + .Value()); return; } diff --git a/src/util/trash.h b/src/util/trash.h index eff3e80c..a835b531 100644 --- a/src/util/trash.h +++ b/src/util/trash.h @@ -25,9 +25,7 @@ class Trash { async->data = this; - auto clear = [](uv_async_t* async) { - reinterpret_cast(async->data)->Clear(); - }; + auto clear = [](uv_async_t* async) { static_cast(async->data)->Clear(); }; [[maybe_unused]] auto err = uv_async_init(loop, async.get(), clear); assert(err == 0); diff --git a/src/util/uvdelayed.h b/src/util/uvdelayed.h index f30ff376..92c0e39b 100644 --- a/src/util/uvdelayed.h +++ b/src/util/uvdelayed.h @@ -33,7 +33,7 @@ class UvDelayed { assert(err == 0); err = uv_check_start(check.get(), [](uv_check_t* check) { - auto& immediate = *reinterpret_cast(check->data); + auto& immediate = *static_cast(check->data); immediate.check.reset(); immediate.idle.reset(); immediate.delayed_callback(); diff --git a/src/util/uvwork.h b/src/util/uvwork.h index 801c2de3..f1d88913 100644 --- a/src/util/uvwork.h +++ b/src/util/uvwork.h @@ -23,11 +23,11 @@ class UvWork { auto err = uv_queue_work( loop, work.get(), [](uv_work_t* req) { - auto& work = *reinterpret_cast(req->data); + auto& work = *static_cast(req->data); work.execute_callback(); }, [](uv_work_t* req, int /*status*/) { - auto& work = *reinterpret_cast(req->data); + auto& work = *static_cast(req->data); work.complete_callback(); delete &work; });