11#include " ./socket.h"
22
3+ #include < array>
34#include < cmath>
45#include < cstdint>
56#include < limits>
67#include < unordered_set>
7- #include < array>
88
99#include " ./context.h"
1010#include " ./incoming_msg.h"
@@ -105,13 +105,20 @@ Socket::Socket(const Napi::CallbackInfo& info)
105105 uv_os_sock_t file_descriptor = 0 ;
106106 std::function<void ()> const finalize = nullptr ;
107107
108+ const auto error = [this ]() {
109+ [[maybe_unused]] auto err = zmq_close (socket);
110+ assert (err == 0 );
111+
112+ socket = nullptr ;
113+ };
114+
108115#ifdef ZMQ_THREAD_SAFE
109116 {
110117 int value = 0 ;
111118 size_t length = sizeof (value);
112119 if (zmq_getsockopt (socket, ZMQ_THREAD_SAFE, &value, &length) < 0 ) {
113120 ErrnoException (Env (), zmq_errno ()).ThrowAsJavaScriptException ();
114- goto error;
121+ error () ;
115122 }
116123
117124 thread_safe = (value != 0 );
@@ -126,7 +133,7 @@ Socket::Socket(const Napi::CallbackInfo& info)
126133 auto poll = zmq_poller_new ();
127134 if (poll == nullptr ) {
128135 ErrnoException (Env (), zmq_errno ()).ThrowAsJavaScriptException ();
129- goto error;
136+ error () ;
130137 }
131138
132139 /* Callback to free the underlying poller. Move the poller to transfer
@@ -139,31 +146,31 @@ Socket::Socket(const Napi::CallbackInfo& info)
139146 if (zmq_poller_add (poll, socket, nullptr , ZMQ_POLLIN | ZMQ_POLLOUT) < 0 ) {
140147 ErrnoException (Env (), zmq_errno ()).ThrowAsJavaScriptException ();
141148 finalize ();
142- goto error;
149+ error () ;
143150 }
144151
145152 if (zmq_poller_fd (poll, &fd) < 0 ) {
146153 ErrnoException (Env (), zmq_errno ()).ThrowAsJavaScriptException ();
147154 finalize ();
148- goto error;
155+ error () ;
149156 }
150157#else
151158 /* A thread safe socket was requested, but there is no support for
152159 retrieving a poller FD, so we cannot construct them. */
153160 ErrnoException (Env (), EINVAL).ThrowAsJavaScriptException ();
154- goto error;
161+ error () ;
155162#endif
156163 } else {
157164 size_t length = sizeof (file_descriptor);
158165 if (zmq_getsockopt (socket, ZMQ_FD, &file_descriptor, &length) < 0 ) {
159166 ErrnoException (Env (), zmq_errno ()).ThrowAsJavaScriptException ();
160- goto error;
167+ error () ;
161168 }
162169 }
163170
164171 if (poller.Initialize (Env (), file_descriptor, finalize) < 0 ) {
165172 ErrnoException (Env (), errno).ThrowAsJavaScriptException ();
166- goto error;
173+ error () ;
167174 }
168175
169176 /* Initialization was successful, register the socket for cleanup. */
@@ -177,14 +184,6 @@ Socket::Socket(const Napi::CallbackInfo& info)
177184 if (info[1 ].IsObject ()) {
178185 Assign (info.This ().As <Napi::Object>(), info[1 ].As <Napi::Object>());
179186 }
180-
181- return ;
182-
183- error:
184- [[maybe_unused]] auto err = zmq_close (socket);
185- assert (err == 0 );
186-
187- socket = nullptr ;
188187}
189188
190189Socket::~Socket () {
0 commit comments