Skip to content

Commit

Permalink
dgram: convert macro to template
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
tniessen authored and danielleadams committed Jul 6, 2023
1 parent d6725b0 commit 97c32da
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
56 changes: 28 additions & 28 deletions src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ using v8::Uint32;
using v8::Undefined;
using v8::Value;

namespace {
template <int (*fn)(uv_udp_t*, int)>
void SetLibuvInt32(const FunctionCallbackInfo<Value>& args) {
UDPWrap* wrap = Unwrap<UDPWrap>(args.Holder());
if (wrap == nullptr) {
args.GetReturnValue().Set(UV_EBADF);
return;
}
Environment* env = wrap->env();
CHECK_EQ(args.Length(), 1);
int flag;
if (!args[0]->Int32Value(env->context()).To(&flag)) {
return;
}
int err = fn(wrap->GetLibuvHandle(), flag);
args.GetReturnValue().Set(err);
}
} // namespace

class SendWrap : public ReqWrap<uv_udp_send_t> {
public:
SendWrap(Environment* env, Local<Object> req_wrap_obj, bool have_callback);
Expand Down Expand Up @@ -177,10 +196,15 @@ void UDPWrap::Initialize(Local<Object> target,
SetProtoMethod(
isolate, t, "dropSourceSpecificMembership", DropSourceSpecificMembership);
SetProtoMethod(isolate, t, "setMulticastInterface", SetMulticastInterface);
SetProtoMethod(isolate, t, "setMulticastTTL", SetMulticastTTL);
SetProtoMethod(isolate, t, "setMulticastLoopback", SetMulticastLoopback);
SetProtoMethod(isolate, t, "setBroadcast", SetBroadcast);
SetProtoMethod(isolate, t, "setTTL", SetTTL);
SetProtoMethod(
isolate, t, "setMulticastTTL", SetLibuvInt32<uv_udp_set_multicast_ttl>);
SetProtoMethod(isolate,
t,
"setMulticastLoopback",
SetLibuvInt32<uv_udp_set_multicast_loop>);
SetProtoMethod(
isolate, t, "setBroadcast", SetLibuvInt32<uv_udp_set_broadcast>);
SetProtoMethod(isolate, t, "setTTL", SetLibuvInt32<uv_udp_set_ttl>);
SetProtoMethod(isolate, t, "bufferSize", BufferSize);
SetProtoMethodNoSideEffect(isolate, t, "getSendQueueSize", GetSendQueueSize);
SetProtoMethodNoSideEffect(
Expand Down Expand Up @@ -373,30 +397,6 @@ void UDPWrap::Disconnect(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(err);
}

#define X(name, fn) \
void UDPWrap::name(const FunctionCallbackInfo<Value>& args) { \
UDPWrap* wrap = Unwrap<UDPWrap>(args.Holder()); \
if (wrap == nullptr) { \
args.GetReturnValue().Set(UV_EBADF); \
return; \
} \
Environment* env = wrap->env(); \
CHECK_EQ(args.Length(), 1); \
int flag; \
if (!args[0]->Int32Value(env->context()).To(&flag)) { \
return; \
} \
int err = fn(&wrap->handle_, flag); \
args.GetReturnValue().Set(err); \
}

X(SetTTL, uv_udp_set_ttl)
X(SetBroadcast, uv_udp_set_broadcast)
X(SetMulticastTTL, uv_udp_set_multicast_ttl)
X(SetMulticastLoopback, uv_udp_set_multicast_loop)

#undef X

void UDPWrap::SetMulticastInterface(const FunctionCallbackInfo<Value>& args) {
UDPWrap* wrap;
ASSIGN_OR_RETURN_UNWRAP(&wrap,
Expand Down
7 changes: 2 additions & 5 deletions src/udp_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,6 @@ class UDPWrap final : public HandleWrap,
const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetMulticastInterface(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetMulticastTTL(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetMulticastLoopback(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetBroadcast(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetTTL(const v8::FunctionCallbackInfo<v8::Value>& args);
static void BufferSize(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetSendQueueSize(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetSendQueueCount(
Expand All @@ -175,6 +170,8 @@ class UDPWrap final : public HandleWrap,

AsyncWrap* GetAsyncWrap() override;

inline uv_udp_t* GetLibuvHandle() { return &handle_; }

static v8::MaybeLocal<v8::Object> Instantiate(Environment* env,
AsyncWrap* parent,
SocketType type);
Expand Down

0 comments on commit 97c32da

Please sign in to comment.