From b89605710935bbda3080c324e347f2e1ff03db71 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Wed, 29 Jun 2016 20:20:54 +0200 Subject: [PATCH] src: unifying PipeConnectWrap and TCPConnectWrap This commit attempts to address one of the items in #4641 which is related to src/pipe_wrap.cc and src/tcp_wrap.cc. Currently both pipe_wrap.cc and tcp_wrap.cc contain a class that are almost identical. This commit extracts these parts into a separate class that both can share. PR-URL: https://github.com/nodejs/node/pull/7501 Reviewed-By: Ben Noordhuis Reviewed-By: Anna Henningsen --- node.gyp | 2 ++ src/connect_wrap.cc | 22 ++++++++++++++++++++++ src/connect_wrap.h | 26 ++++++++++++++++++++++++++ src/pipe_wrap.cc | 34 ++++++++-------------------------- src/tcp_wrap.cc | 35 ++++++++++------------------------- 5 files changed, 68 insertions(+), 51 deletions(-) create mode 100644 src/connect_wrap.cc create mode 100644 src/connect_wrap.h diff --git a/node.gyp b/node.gyp index cd6a37f28e..d192e28807 100644 --- a/node.gyp +++ b/node.gyp @@ -142,6 +142,7 @@ 'src/fs_event_wrap.cc', 'src/cares_wrap.cc', 'src/connection_wrap.cc', + 'src/connect_wrap.cc', 'src/handle_wrap.cc', 'src/js_stream.cc', 'src/node.cc', @@ -179,6 +180,7 @@ 'src/base-object.h', 'src/base-object-inl.h', 'src/connection_wrap.h', + 'src/connect_wrap.h', 'src/debug-agent.h', 'src/env.h', 'src/env-inl.h', diff --git a/src/connect_wrap.cc b/src/connect_wrap.cc new file mode 100644 index 0000000000..df3f093e73 --- /dev/null +++ b/src/connect_wrap.cc @@ -0,0 +1,22 @@ +#include "connect_wrap.h" + +#include "env.h" +#include "env-inl.h" +#include "req-wrap.h" +#include "req-wrap-inl.h" +#include "util.h" +#include "util-inl.h" + +namespace node { + +using v8::Local; +using v8::Object; + + +ConnectWrap::ConnectWrap(Environment* env, + Local req_wrap_obj, + AsyncWrap::ProviderType provider) : ReqWrap(env, req_wrap_obj, provider) { + Wrap(req_wrap_obj, this); +} + +} // namespace node diff --git a/src/connect_wrap.h b/src/connect_wrap.h new file mode 100644 index 0000000000..28d4872d7e --- /dev/null +++ b/src/connect_wrap.h @@ -0,0 +1,26 @@ +#ifndef SRC_CONNECT_WRAP_H_ +#define SRC_CONNECT_WRAP_H_ + +#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#include "env.h" +#include "req-wrap.h" +#include "async-wrap.h" +#include "v8.h" + +namespace node { + +class ConnectWrap : public ReqWrap { + public: + ConnectWrap(Environment* env, + v8::Local req_wrap_obj, + AsyncWrap::ProviderType provider); + + size_t self_size() const override { return sizeof(*this); } +}; + +} // namespace node + +#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#endif // SRC_CONNECT_WRAP_H_ diff --git a/src/pipe_wrap.cc b/src/pipe_wrap.cc index 8baf04ba7c..e013811687 100644 --- a/src/pipe_wrap.cc +++ b/src/pipe_wrap.cc @@ -8,8 +8,7 @@ #include "node.h" #include "node_buffer.h" #include "node_wrap.h" -#include "req-wrap.h" -#include "req-wrap-inl.h" +#include "connect_wrap.h" #include "stream_wrap.h" #include "util-inl.h" #include "util.h" @@ -31,26 +30,6 @@ using v8::String; using v8::Value; -// TODO(bnoordhuis) share with TCPWrap? -class PipeConnectWrap : public ReqWrap { - public: - PipeConnectWrap(Environment* env, Local req_wrap_obj); - - size_t self_size() const override { return sizeof(*this); } -}; - - -PipeConnectWrap::PipeConnectWrap(Environment* env, Local req_wrap_obj) - : ReqWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_PIPECONNECTWRAP) { - Wrap(req_wrap_obj, this); -} - - -static void NewPipeConnectWrap(const FunctionCallbackInfo& args) { - CHECK(args.IsConstructCall()); -} - - Local PipeWrap::Instantiate(Environment* env, AsyncWrap* parent) { EscapableHandleScope handle_scope(env->isolate()); CHECK_EQ(false, env->pipe_constructor_template().IsEmpty()); @@ -92,8 +71,10 @@ void PipeWrap::Initialize(Local target, env->set_pipe_constructor_template(t); // Create FunctionTemplate for PipeConnectWrap. - Local cwt = - FunctionTemplate::New(env->isolate(), NewPipeConnectWrap); + auto constructor = [](const FunctionCallbackInfo& args) { + CHECK(args.IsConstructCall()); + }; + auto cwt = FunctionTemplate::New(env->isolate(), constructor); cwt->InstanceTemplate()->SetInternalFieldCount(1); cwt->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap")); target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap"), @@ -163,7 +144,7 @@ void PipeWrap::Listen(const FunctionCallbackInfo& args) { // TODO(bnoordhuis) Maybe share this with TCPWrap? void PipeWrap::AfterConnect(uv_connect_t* req, int status) { - PipeConnectWrap* req_wrap = static_cast(req->data); + ConnectWrap* req_wrap = static_cast(req->data); PipeWrap* wrap = static_cast(req->handle->data); CHECK_EQ(req_wrap->env(), wrap->env()); Environment* env = wrap->env(); @@ -226,7 +207,8 @@ void PipeWrap::Connect(const FunctionCallbackInfo& args) { Local req_wrap_obj = args[0].As(); node::Utf8Value name(env->isolate(), args[1]); - PipeConnectWrap* req_wrap = new PipeConnectWrap(env, req_wrap_obj); + ConnectWrap* req_wrap = + new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_PIPECONNECTWRAP); uv_pipe_connect(&req_wrap->req_, &wrap->handle_, *name, diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index 674c059254..92037d0d68 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -6,8 +6,7 @@ #include "handle_wrap.h" #include "node_buffer.h" #include "node_wrap.h" -#include "req-wrap.h" -#include "req-wrap-inl.h" +#include "connect_wrap.h" #include "stream_wrap.h" #include "util.h" #include "util-inl.h" @@ -32,24 +31,6 @@ using v8::String; using v8::Value; -class TCPConnectWrap : public ReqWrap { - public: - TCPConnectWrap(Environment* env, Local req_wrap_obj); - size_t self_size() const override { return sizeof(*this); } -}; - - -TCPConnectWrap::TCPConnectWrap(Environment* env, Local req_wrap_obj) - : ReqWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_TCPCONNECTWRAP) { - Wrap(req_wrap_obj, this); -} - - -static void NewTCPConnectWrap(const FunctionCallbackInfo& args) { - CHECK(args.IsConstructCall()); -} - - Local TCPWrap::Instantiate(Environment* env, AsyncWrap* parent) { EscapableHandleScope handle_scope(env->isolate()); CHECK_EQ(env->tcp_constructor_template().IsEmpty(), false); @@ -112,8 +93,10 @@ void TCPWrap::Initialize(Local target, env->set_tcp_constructor_template(t); // Create FunctionTemplate for TCPConnectWrap. - Local cwt = - FunctionTemplate::New(env->isolate(), NewTCPConnectWrap); + auto constructor = [](const FunctionCallbackInfo& args) { + CHECK(args.IsConstructCall()); + }; + auto cwt = FunctionTemplate::New(env->isolate(), constructor); cwt->InstanceTemplate()->SetInternalFieldCount(1); cwt->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap")); target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "TCPConnectWrap"), @@ -253,7 +236,7 @@ void TCPWrap::Listen(const FunctionCallbackInfo& args) { void TCPWrap::AfterConnect(uv_connect_t* req, int status) { - TCPConnectWrap* req_wrap = static_cast(req->data); + ConnectWrap* req_wrap = static_cast(req->data); TCPWrap* wrap = static_cast(req->handle->data); CHECK_EQ(req_wrap->env(), wrap->env()); Environment* env = wrap->env(); @@ -300,7 +283,8 @@ void TCPWrap::Connect(const FunctionCallbackInfo& args) { int err = uv_ip4_addr(*ip_address, port, &addr); if (err == 0) { - TCPConnectWrap* req_wrap = new TCPConnectWrap(env, req_wrap_obj); + ConnectWrap* req_wrap = + new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_TCPCONNECTWRAP); err = uv_tcp_connect(&req_wrap->req_, &wrap->handle_, reinterpret_cast(&addr), @@ -334,7 +318,8 @@ void TCPWrap::Connect6(const FunctionCallbackInfo& args) { int err = uv_ip6_addr(*ip_address, port, &addr); if (err == 0) { - TCPConnectWrap* req_wrap = new TCPConnectWrap(env, req_wrap_obj); + ConnectWrap* req_wrap = + new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_TCPCONNECTWRAP); err = uv_tcp_connect(&req_wrap->req_, &wrap->handle_, reinterpret_cast(&addr),