diff options
author | Tobias Nießen <tniessen@tnie.de> | 2023-05-08 03:17:42 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-08 01:17:42 +0000 |
commit | 29e6dd1d2d74b71a911043fe9c891215f17f267a (patch) | |
tree | 6604d8579e4f6bf8d14a01475dea00efc9772e0a | |
parent | 2545019451ff375b538b0ad4fe09e906c50cbc39 (diff) | |
download | node-new-29e6dd1d2d74b71a911043fe9c891215f17f267a.tar.gz |
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 <matteo.collina@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
-rw-r--r-- | src/udp_wrap.cc | 56 | ||||
-rw-r--r-- | 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 cad50fec40..4208a21326 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 <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); @@ -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( @@ -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, diff --git a/src/udp_wrap.h b/src/udp_wrap.h index 83aeb1ec08..3b8ca7df35 100644 --- a/src/udp_wrap.h +++ b/src/udp_wrap.h @@ -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( @@ -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); |