Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use the Napi::Addon interface for the module #687

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace zmq {
Context::Context(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<Context>(info), module(*reinterpret_cast<Module*>(info.Data())) {
: Napi::ObjectWrap<Context>(info), module(*static_cast<Module*>(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()) {
Expand Down
2 changes: 1 addition & 1 deletion src/incoming_msg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Napi::Value IncomingMsg::IntoBuffer(const Napi::Env& env) {
return env.Undefined();
}
}
auto* data = reinterpret_cast<uint8_t*>(zmq_msg_data(ref->get()));
auto* data = static_cast<uint8_t*>(zmq_msg_data(ref->get()));
auto length = zmq_msg_size(ref->get());

if (noElectronMemoryCage) {
Expand Down
20 changes: 6 additions & 14 deletions src/module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<zmq::Module*>(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)
4 changes: 2 additions & 2 deletions src/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct Terminator {
}
};

class Module {
class Module : public Napi::Addon<Module> {
/* Contains shared global state that will be accessible by all
agents/threads. */
class Global {
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ std::pair<const char*, const char*> ProtoError(uint32_t val) {

Observer::Observer(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<Observer>(info), async_context(Env(), "Observer"), poller(*this),
module(*reinterpret_cast<Module*>(info.Data())) {
module(*static_cast<Module*>(info.Data())) {
Arg::Validator const args{
Arg::Required<Arg::Object>("Socket must be a socket object"),
};
Expand Down Expand Up @@ -263,7 +263,7 @@ void Observer::Receive(const Napi::Promise::Deferred& res) {
}
}

auto* data2 = reinterpret_cast<char*>(zmq_msg_data(&msg2));
auto* data2 = static_cast<char*>(zmq_msg_data(&msg2));
auto length = zmq_msg_size(&msg2);

auto event = Napi::Object::New(Env());
Expand Down
4 changes: 1 addition & 3 deletions src/outgoing_msg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ OutgoingMsg::OutgoingMsg(Napi::Value value, std::reference_wrapper<Module> modul
auto length = str->size();
auto* data = str->data();

auto release = [](void*, void* str) {
delete reinterpret_cast<std::string*>(str);
};
auto release = [](void*, void* str) { delete static_cast<std::string*>(str); };

if (zmq_msg_init_data(&msg, data, length, release, str) < 0) {
delete str;
Expand Down
6 changes: 3 additions & 3 deletions src/poller.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Poller {
readable_timer.get(),
[](uv_timer_t* timer) {
// NOLINTNEXTLINE(*-pro-type-reinterpret-cast)
auto& poller = *reinterpret_cast<Poller*>(timer->data);
auto& poller = *static_cast<Poller*>(timer->data);
poller.Trigger(UV_READABLE);
},
static_cast<uint64_t>(timeout), 0);
Expand All @@ -100,7 +100,7 @@ class Poller {
writable_timer.get(),
[](uv_timer_t* timer) {
// NOLINTNEXTLINE(*-pro-type-reinterpret-cast)
auto& poller = *reinterpret_cast<Poller*>(timer->data);
auto& poller = *static_cast<Poller*>(timer->data);
poller.Trigger(UV_WRITABLE);
},
static_cast<uint64_t>(timeout), 0);
Expand Down Expand Up @@ -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<Poller*>(poll->data);
auto& poller = *static_cast<Poller*>(poll->data);
poller.TriggerReadable();
poller.TriggerWritable();
}
Expand Down
2 changes: 1 addition & 1 deletion src/proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct ProxyContext {

Proxy::Proxy(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<Proxy>(info), async_context(Env(), "Proxy"),
module(*reinterpret_cast<Module*>(info.Data())) {
module(*static_cast<Module*>(info.Data())) {
Arg::Validator const args{
Arg::Required<Arg::Object>("Front-end must be a socket object"),
Arg::Required<Arg::Object>("Back-end must be a socket object"),
Expand Down
6 changes: 3 additions & 3 deletions src/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct AddressContext {

Socket::Socket(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<Socket>(info), async_context(Env(), "Socket"), poller(*this),
module(*reinterpret_cast<Module*>(info.Data())) {
module(*static_cast<Module*>(info.Data())) {
Arg::Validator const args{
Arg::Required<Arg::Number>("Socket type must be a number"),
Arg::Optional<Arg::Object>("Options must be an object"),
Expand Down Expand Up @@ -392,7 +392,7 @@ Napi::Value Socket::Bind(const Napi::CallbackInfo& info) {
if (run_ctx->error != 0) {
res.Reject(ErrnoException(
Env(), static_cast<int32_t>(run_ctx->error), run_ctx->address)
.Value());
.Value());
return;
}

Expand Down Expand Up @@ -448,7 +448,7 @@ Napi::Value Socket::Unbind(const Napi::CallbackInfo& info) {
if (run_ctx->error != 0) {
res.Reject(ErrnoException(
Env(), static_cast<int32_t>(run_ctx->error), run_ctx->address)
.Value());
.Value());
return;
}

Expand Down
4 changes: 1 addition & 3 deletions src/util/trash.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ class Trash {

async->data = this;

auto clear = [](uv_async_t* async) {
reinterpret_cast<Trash*>(async->data)->Clear();
};
auto clear = [](uv_async_t* async) { static_cast<Trash*>(async->data)->Clear(); };

[[maybe_unused]] auto err = uv_async_init(loop, async.get(), clear);
assert(err == 0);
Expand Down
2 changes: 1 addition & 1 deletion src/util/uvdelayed.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class UvDelayed {
assert(err == 0);

err = uv_check_start(check.get(), [](uv_check_t* check) {
auto& immediate = *reinterpret_cast<UvDelayed*>(check->data);
auto& immediate = *static_cast<UvDelayed*>(check->data);
immediate.check.reset();
immediate.idle.reset();
immediate.delayed_callback();
Expand Down
4 changes: 2 additions & 2 deletions src/util/uvwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class UvWork {
auto err = uv_queue_work(
loop, work.get(),
[](uv_work_t* req) {
auto& work = *reinterpret_cast<UvWork*>(req->data);
auto& work = *static_cast<UvWork*>(req->data);
work.execute_callback();
},
[](uv_work_t* req, int /*status*/) {
auto& work = *reinterpret_cast<UvWork*>(req->data);
auto& work = *static_cast<UvWork*>(req->data);
work.complete_callback();
delete &work;
});
Expand Down
Loading