Skip to content

Commit 72340c9

Browse files
tniessentargos
authored andcommitted
dgram: convert macro to template
It's not pretty either way, but a template is still preferable over a macro. PR-URL: #47891 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent e457d89 commit 72340c9

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

src/udp_wrap.cc

+28-28
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ using v8::Uint32;
5252
using v8::Undefined;
5353
using v8::Value;
5454

55+
namespace {
56+
template <int (*fn)(uv_udp_t*, int)>
57+
void SetLibuvInt32(const FunctionCallbackInfo<Value>& args) {
58+
UDPWrap* wrap = Unwrap<UDPWrap>(args.Holder());
59+
if (wrap == nullptr) {
60+
args.GetReturnValue().Set(UV_EBADF);
61+
return;
62+
}
63+
Environment* env = wrap->env();
64+
CHECK_EQ(args.Length(), 1);
65+
int flag;
66+
if (!args[0]->Int32Value(env->context()).To(&flag)) {
67+
return;
68+
}
69+
int err = fn(wrap->GetLibuvHandle(), flag);
70+
args.GetReturnValue().Set(err);
71+
}
72+
} // namespace
73+
5574
class SendWrap : public ReqWrap<uv_udp_send_t> {
5675
public:
5776
SendWrap(Environment* env, Local<Object> req_wrap_obj, bool have_callback);
@@ -177,10 +196,15 @@ void UDPWrap::Initialize(Local<Object> target,
177196
SetProtoMethod(
178197
isolate, t, "dropSourceSpecificMembership", DropSourceSpecificMembership);
179198
SetProtoMethod(isolate, t, "setMulticastInterface", SetMulticastInterface);
180-
SetProtoMethod(isolate, t, "setMulticastTTL", SetMulticastTTL);
181-
SetProtoMethod(isolate, t, "setMulticastLoopback", SetMulticastLoopback);
182-
SetProtoMethod(isolate, t, "setBroadcast", SetBroadcast);
183-
SetProtoMethod(isolate, t, "setTTL", SetTTL);
199+
SetProtoMethod(
200+
isolate, t, "setMulticastTTL", SetLibuvInt32<uv_udp_set_multicast_ttl>);
201+
SetProtoMethod(isolate,
202+
t,
203+
"setMulticastLoopback",
204+
SetLibuvInt32<uv_udp_set_multicast_loop>);
205+
SetProtoMethod(
206+
isolate, t, "setBroadcast", SetLibuvInt32<uv_udp_set_broadcast>);
207+
SetProtoMethod(isolate, t, "setTTL", SetLibuvInt32<uv_udp_set_ttl>);
184208
SetProtoMethod(isolate, t, "bufferSize", BufferSize);
185209
SetProtoMethodNoSideEffect(isolate, t, "getSendQueueSize", GetSendQueueSize);
186210
SetProtoMethodNoSideEffect(
@@ -373,30 +397,6 @@ void UDPWrap::Disconnect(const FunctionCallbackInfo<Value>& args) {
373397
args.GetReturnValue().Set(err);
374398
}
375399

376-
#define X(name, fn) \
377-
void UDPWrap::name(const FunctionCallbackInfo<Value>& args) { \
378-
UDPWrap* wrap = Unwrap<UDPWrap>(args.Holder()); \
379-
if (wrap == nullptr) { \
380-
args.GetReturnValue().Set(UV_EBADF); \
381-
return; \
382-
} \
383-
Environment* env = wrap->env(); \
384-
CHECK_EQ(args.Length(), 1); \
385-
int flag; \
386-
if (!args[0]->Int32Value(env->context()).To(&flag)) { \
387-
return; \
388-
} \
389-
int err = fn(&wrap->handle_, flag); \
390-
args.GetReturnValue().Set(err); \
391-
}
392-
393-
X(SetTTL, uv_udp_set_ttl)
394-
X(SetBroadcast, uv_udp_set_broadcast)
395-
X(SetMulticastTTL, uv_udp_set_multicast_ttl)
396-
X(SetMulticastLoopback, uv_udp_set_multicast_loop)
397-
398-
#undef X
399-
400400
void UDPWrap::SetMulticastInterface(const FunctionCallbackInfo<Value>& args) {
401401
UDPWrap* wrap;
402402
ASSIGN_OR_RETURN_UNWRAP(&wrap,

src/udp_wrap.h

+2-5
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,6 @@ class UDPWrap final : public HandleWrap,
144144
const v8::FunctionCallbackInfo<v8::Value>& args);
145145
static void SetMulticastInterface(
146146
const v8::FunctionCallbackInfo<v8::Value>& args);
147-
static void SetMulticastTTL(const v8::FunctionCallbackInfo<v8::Value>& args);
148-
static void SetMulticastLoopback(
149-
const v8::FunctionCallbackInfo<v8::Value>& args);
150-
static void SetBroadcast(const v8::FunctionCallbackInfo<v8::Value>& args);
151-
static void SetTTL(const v8::FunctionCallbackInfo<v8::Value>& args);
152147
static void BufferSize(const v8::FunctionCallbackInfo<v8::Value>& args);
153148
static void GetSendQueueSize(const v8::FunctionCallbackInfo<v8::Value>& args);
154149
static void GetSendQueueCount(
@@ -175,6 +170,8 @@ class UDPWrap final : public HandleWrap,
175170

176171
AsyncWrap* GetAsyncWrap() override;
177172

173+
inline uv_udp_t* GetLibuvHandle() { return &handle_; }
174+
178175
static v8::MaybeLocal<v8::Object> Instantiate(Environment* env,
179176
AsyncWrap* parent,
180177
SocketType type);

0 commit comments

Comments
 (0)