Skip to content

Commit bac030c

Browse files
authored
src: simplify TCPWrap::Connect signature
Just a little piece of cleanup -- this aligns `TCPWrap::Connect` with its sibling `TCPWrap::Bind`, deduplicates code and removes an unnecessary heap allocation. Signed-off-by: Anna Henningsen <anna@addaleax.net> PR-URL: #62929 Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 9f75269 commit bac030c

2 files changed

Lines changed: 23 additions & 27 deletions

File tree

src/tcp_wrap.cc

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,11 @@ void TCPWrap::Open(const FunctionCallbackInfo<Value>& args) {
373373
}
374374

375375
template <typename T>
376-
void TCPWrap::Bind(
377-
const FunctionCallbackInfo<Value>& args,
378-
int family,
379-
std::function<int(const char* ip_address, int port, T* addr)> uv_ip_addr) {
376+
void TCPWrap::Bind(const FunctionCallbackInfo<Value>& args,
377+
int family,
378+
int (*uv_ip_addr)(const char* ip_address,
379+
int port,
380+
T* addr)) {
380381
TCPWrap* wrap;
381382
ASSIGN_OR_RETURN_UNWRAP(
382383
&wrap, args.This(), args.GetReturnValue().Set(UV_EBADF));
@@ -430,29 +431,18 @@ void TCPWrap::Listen(const FunctionCallbackInfo<Value>& args) {
430431
}
431432

432433
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args) {
433-
CHECK(args[2]->IsUint32());
434-
// explicit cast to fit to libuv's type expectation
435-
int port = static_cast<int>(args[2].As<Uint32>()->Value());
436-
Connect<sockaddr_in>(args, [port](const char* ip_address, sockaddr_in* addr) {
437-
return uv_ip4_addr(ip_address, port, addr);
438-
});
434+
Connect<sockaddr_in>(args, uv_ip4_addr);
439435
}
440436

441437
void TCPWrap::Connect6(const FunctionCallbackInfo<Value>& args) {
442-
Environment* env = Environment::GetCurrent(args);
443-
CHECK(args[2]->IsUint32());
444-
int port;
445-
if (!args[2]->Int32Value(env->context()).To(&port)) return;
446-
Connect<sockaddr_in6>(args,
447-
[port](const char* ip_address, sockaddr_in6* addr) {
448-
return uv_ip6_addr(ip_address, port, addr);
449-
});
438+
Connect<sockaddr_in6>(args, uv_ip6_addr);
450439
}
451440

452441
template <typename T>
453-
void TCPWrap::Connect(
454-
const FunctionCallbackInfo<Value>& args,
455-
std::function<int(const char* ip_address, T* addr)> uv_ip_addr) {
442+
void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args,
443+
int (*uv_ip_addr)(const char* ip_address,
444+
int port,
445+
T* addr)) {
456446
Environment* env = Environment::GetCurrent(args);
457447

458448
TCPWrap* wrap;
@@ -462,14 +452,17 @@ void TCPWrap::Connect(
462452
CHECK(args[0]->IsObject());
463453
CHECK(args[1]->IsString());
464454

455+
int port;
456+
if (!args[2]->Int32Value(env->context()).To(&port)) return;
457+
465458
Local<Object> req_wrap_obj = args[0].As<Object>();
466459
node::Utf8Value ip_address(env->isolate(), args[1]);
467460

468461
ERR_ACCESS_DENIED_IF_INSUFFICIENT_PERMISSIONS(
469462
env, permission::PermissionScope::kNet, ip_address.ToStringView(), args);
470463

471464
T addr;
472-
int err = uv_ip_addr(*ip_address, &addr);
465+
int err = uv_ip_addr(*ip_address, port, &addr);
473466

474467
if (err == 0) {
475468
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap);

src/tcp_wrap.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,16 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> {
8383
static void Connect6(const v8::FunctionCallbackInfo<v8::Value>& args);
8484
template <typename T>
8585
static void Connect(const v8::FunctionCallbackInfo<v8::Value>& args,
86-
std::function<int(const char* ip_address, T* addr)> uv_ip_addr);
86+
int (*uv_ip_addr)(const char* ip_address,
87+
int port,
88+
T* addr));
8789
static void Open(const v8::FunctionCallbackInfo<v8::Value>& args);
8890
template <typename T>
89-
static void Bind(
90-
const v8::FunctionCallbackInfo<v8::Value>& args,
91-
int family,
92-
std::function<int(const char* ip_address, int port, T* addr)> uv_ip_addr);
91+
static void Bind(const v8::FunctionCallbackInfo<v8::Value>& args,
92+
int family,
93+
int (*uv_ip_addr)(const char* ip_address,
94+
int port,
95+
T* addr));
9396
static void Reset(const v8::FunctionCallbackInfo<v8::Value>& args);
9497
int Reset(v8::Local<v8::Value> close_callback = v8::Local<v8::Value>());
9598

0 commit comments

Comments
 (0)