From 29e6dd1d2d74b71a911043fe9c891215f17f267a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Mon, 8 May 2023 03:17:42 +0200 Subject: [PATCH] dgram: convert macro to template It's not pretty either way, but a template is still preferable over a macro. PR-URL: https://github.com/nodejs/node/pull/47891 Reviewed-By: Matteo Collina Reviewed-By: Ben Noordhuis --- src/udp_wrap.cc | 56 ++++++++++++++++++++++++------------------------- src/udp_wrap.h | 7 ++----- 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index cad50fec409730..4208a213260e26 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -52,6 +52,25 @@ using v8::Uint32; using v8::Undefined; using v8::Value; +namespace { +template +void SetLibuvInt32(const FunctionCallbackInfo& args) { + UDPWrap* wrap = Unwrap(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 { public: SendWrap(Environment* env, Local req_wrap_obj, bool have_callback); @@ -177,10 +196,15 @@ void UDPWrap::Initialize(Local 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); + SetProtoMethod(isolate, + t, + "setMulticastLoopback", + SetLibuvInt32); + SetProtoMethod( + isolate, t, "setBroadcast", SetLibuvInt32); + SetProtoMethod(isolate, t, "setTTL", SetLibuvInt32); SetProtoMethod(isolate, t, "bufferSize", BufferSize); SetProtoMethodNoSideEffect(isolate, t, "getSendQueueSize", GetSendQueueSize); SetProtoMethodNoSideEffect( @@ -373,30 +397,6 @@ void UDPWrap::Disconnect(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(err); } -#define X(name, fn) \ - void UDPWrap::name(const FunctionCallbackInfo& args) { \ - UDPWrap* wrap = Unwrap(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& args) { UDPWrap* wrap; ASSIGN_OR_RETURN_UNWRAP(&wrap, diff --git a/src/udp_wrap.h b/src/udp_wrap.h index 83aeb1ec084037..3b8ca7df351a36 100644 --- a/src/udp_wrap.h +++ b/src/udp_wrap.h @@ -144,11 +144,6 @@ class UDPWrap final : public HandleWrap, const v8::FunctionCallbackInfo& args); static void SetMulticastInterface( const v8::FunctionCallbackInfo& args); - static void SetMulticastTTL(const v8::FunctionCallbackInfo& args); - static void SetMulticastLoopback( - const v8::FunctionCallbackInfo& args); - static void SetBroadcast(const v8::FunctionCallbackInfo& args); - static void SetTTL(const v8::FunctionCallbackInfo& args); static void BufferSize(const v8::FunctionCallbackInfo& args); static void GetSendQueueSize(const v8::FunctionCallbackInfo& args); static void GetSendQueueCount( @@ -175,6 +170,8 @@ class UDPWrap final : public HandleWrap, AsyncWrap* GetAsyncWrap() override; + inline uv_udp_t* GetLibuvHandle() { return &handle_; } + static v8::MaybeLocal Instantiate(Environment* env, AsyncWrap* parent, SocketType type);