Skip to content

Commit 319032c

Browse files
authored
Merge pull request #687 from zeromq/modernize [skip ci]
2 parents 31947d8 + 2b839a7 commit 319032c

File tree

12 files changed

+24
-36
lines changed

12 files changed

+24
-36
lines changed

src/context.cc

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

1010
namespace zmq {
1111
Context::Context(const Napi::CallbackInfo& info)
12-
: Napi::ObjectWrap<Context>(info), module(*reinterpret_cast<Module*>(info.Data())) {
12+
: Napi::ObjectWrap<Context>(info), module(*static_cast<Module*>(info.Data())) {
1313
/* If this module has no global context, then create one with a process
1414
wide context pointer that is shared between threads/agents. */
1515
if (module.GlobalContext.IsEmpty()) {

src/incoming_msg.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Napi::Value IncomingMsg::IntoBuffer(const Napi::Env& env) {
2727
return env.Undefined();
2828
}
2929
}
30-
auto* data = reinterpret_cast<uint8_t*>(zmq_msg_data(ref->get()));
30+
auto* data = static_cast<uint8_t*>(zmq_msg_data(ref->get()));
3131
auto length = zmq_msg_size(ref->get());
3232

3333
if (noElectronMemoryCage) {

src/module.cc

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ Module::Global::Shared Module::Global::Instance() {
102102
return instance;
103103
}
104104

105-
Module::Module(Napi::Object exports) : MsgTrash(exports.Env()) {
106-
exports.Set("version", zmq::Version(exports.Env()));
107-
exports.Set("capability", zmq::Capabilities(exports.Env()));
108-
exports.Set("curveKeyPair", Napi::Function::New(exports.Env(), zmq::CurveKeyPair));
105+
Module::Module(Napi::Env env, Napi::Object exports) : MsgTrash(env) {
106+
exports.Set("version", zmq::Version(env));
107+
exports.Set("capability", zmq::Capabilities(env));
108+
exports.Set("curveKeyPair", Napi::Function::New(env, zmq::CurveKeyPair));
109109

110110
Context::Initialize(*this, exports);
111111
Socket::Initialize(*this, exports);
@@ -117,13 +117,5 @@ Module::Module(Napi::Object exports) : MsgTrash(exports.Env()) {
117117
}
118118
} // namespace zmq
119119

120-
/* This initializer can be called in multiple contexts, like worker threads. */
121-
NAPI_MODULE_INIT(/* env, exports */) {
122-
auto* module = new zmq::Module(Napi::Object(env, exports));
123-
auto terminate = [](void* data) { delete reinterpret_cast<zmq::Module*>(data); };
124-
125-
/* Tear down the module class when the env/agent/thread is closed.*/
126-
[[maybe_unused]] auto status = napi_add_env_cleanup_hook(env, terminate, module);
127-
assert(status == napi_ok);
128-
return exports;
129-
}
120+
using Module = zmq::Module;
121+
NODE_API_ADDON(Module)

src/module.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct Terminator {
4646
}
4747
};
4848

49-
class Module {
49+
class Module : public Napi::Addon<Module> {
5050
/* Contains shared global state that will be accessible by all
5151
agents/threads. */
5252
class Global {
@@ -67,7 +67,7 @@ class Module {
6767
};
6868

6969
public:
70-
explicit Module(Napi::Object exports);
70+
explicit Module(Napi::Env env, Napi::Object exports);
7171

7272
class Global& Global() {
7373
return *global;

src/observer.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ std::pair<const char*, const char*> ProtoError(uint32_t val) {
130130

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

266-
auto* data2 = reinterpret_cast<char*>(zmq_msg_data(&msg2));
266+
auto* data2 = static_cast<char*>(zmq_msg_data(&msg2));
267267
auto length = zmq_msg_size(&msg2);
268268

269269
auto event = Napi::Object::New(Env());

src/outgoing_msg.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ OutgoingMsg::OutgoingMsg(Napi::Value value, std::reference_wrapper<Module> modul
4848
auto length = str->size();
4949
auto* data = str->data();
5050

51-
auto release = [](void*, void* str) {
52-
delete reinterpret_cast<std::string*>(str);
53-
};
51+
auto release = [](void*, void* str) { delete static_cast<std::string*>(str); };
5452

5553
if (zmq_msg_init_data(&msg, data, length, release, str) < 0) {
5654
delete str;

src/poller.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Poller {
7575
readable_timer.get(),
7676
[](uv_timer_t* timer) {
7777
// NOLINTNEXTLINE(*-pro-type-reinterpret-cast)
78-
auto& poller = *reinterpret_cast<Poller*>(timer->data);
78+
auto& poller = *static_cast<Poller*>(timer->data);
7979
poller.Trigger(UV_READABLE);
8080
},
8181
static_cast<uint64_t>(timeout), 0);
@@ -100,7 +100,7 @@ class Poller {
100100
writable_timer.get(),
101101
[](uv_timer_t* timer) {
102102
// NOLINTNEXTLINE(*-pro-type-reinterpret-cast)
103-
auto& poller = *reinterpret_cast<Poller*>(timer->data);
103+
auto& poller = *static_cast<Poller*>(timer->data);
104104
poller.Trigger(UV_WRITABLE);
105105
},
106106
static_cast<uint64_t>(timeout), 0);
@@ -167,7 +167,7 @@ class Poller {
167167
static void Callback(uv_poll_t* poll, int32_t status, int32_t /*events*/) {
168168
if (status == 0) {
169169
// NOLINTNEXTLINE(*-pro-type-reinterpret-cast)
170-
auto& poller = *reinterpret_cast<Poller*>(poll->data);
170+
auto& poller = *static_cast<Poller*>(poll->data);
171171
poller.TriggerReadable();
172172
poller.TriggerWritable();
173173
}

src/proxy.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct ProxyContext {
2323

2424
Proxy::Proxy(const Napi::CallbackInfo& info)
2525
: Napi::ObjectWrap<Proxy>(info), async_context(Env(), "Proxy"),
26-
module(*reinterpret_cast<Module*>(info.Data())) {
26+
module(*static_cast<Module*>(info.Data())) {
2727
Arg::Validator const args{
2828
Arg::Required<Arg::Object>("Front-end must be a socket object"),
2929
Arg::Required<Arg::Object>("Back-end must be a socket object"),

src/socket.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ struct AddressContext {
6767

6868
Socket::Socket(const Napi::CallbackInfo& info)
6969
: Napi::ObjectWrap<Socket>(info), async_context(Env(), "Socket"), poller(*this),
70-
module(*reinterpret_cast<Module*>(info.Data())) {
70+
module(*static_cast<Module*>(info.Data())) {
7171
Arg::Validator const args{
7272
Arg::Required<Arg::Number>("Socket type must be a number"),
7373
Arg::Optional<Arg::Object>("Options must be an object"),
@@ -392,7 +392,7 @@ Napi::Value Socket::Bind(const Napi::CallbackInfo& info) {
392392
if (run_ctx->error != 0) {
393393
res.Reject(ErrnoException(
394394
Env(), static_cast<int32_t>(run_ctx->error), run_ctx->address)
395-
.Value());
395+
.Value());
396396
return;
397397
}
398398

@@ -448,7 +448,7 @@ Napi::Value Socket::Unbind(const Napi::CallbackInfo& info) {
448448
if (run_ctx->error != 0) {
449449
res.Reject(ErrnoException(
450450
Env(), static_cast<int32_t>(run_ctx->error), run_ctx->address)
451-
.Value());
451+
.Value());
452452
return;
453453
}
454454

src/util/trash.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ class Trash {
2525

2626
async->data = this;
2727

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

3230
[[maybe_unused]] auto err = uv_async_init(loop, async.get(), clear);
3331
assert(err == 0);

0 commit comments

Comments
 (0)