From 54cba45d104ccd7f2a2872644dbea743f9444e3d Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 7 Mar 2018 13:48:52 +0100 Subject: [PATCH] src: add convenience ctor for async trigger id scope --- src/async_wrap-inl.h | 7 +++++++ src/async_wrap.cc | 3 +-- src/env-inl.h | 3 +++ src/env.h | 1 + src/pipe_wrap.cc | 3 +-- src/stream_base-inl.h | 9 +++------ src/tcp_wrap.cc | 9 +++------ src/udp_wrap.cc | 6 ++---- 8 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/async_wrap-inl.h b/src/async_wrap-inl.h index cc6377d82a0fee..21b1f9cee9f0e8 100644 --- a/src/async_wrap-inl.h +++ b/src/async_wrap-inl.h @@ -64,6 +64,13 @@ inline v8::MaybeLocal AsyncWrap::MakeCallback( return MakeCallback(cb_v.As(), argc, argv); } + +// Defined here to avoid a circular dependency with env-inl.h. +inline Environment::AsyncHooks::DefaultTriggerAsyncIdScope + ::DefaultTriggerAsyncIdScope(AsyncWrap* async_wrap) + : DefaultTriggerAsyncIdScope(async_wrap->env(), + async_wrap->get_async_id()) {} + } // namespace node #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS diff --git a/src/async_wrap.cc b/src/async_wrap.cc index 08bb73f7468707..d17eda9708ded8 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -310,8 +310,7 @@ static void PromiseHook(PromiseHookType type, Local promise, parent_wrap = PromiseWrap::New(env, parent_promise, nullptr, true); } - AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope( - env, parent_wrap->get_async_id()); + AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent_wrap); wrap = PromiseWrap::New(env, promise, parent_wrap, silent); } else { wrap = PromiseWrap::New(env, promise, nullptr, silent); diff --git a/src/env-inl.h b/src/env-inl.h index 365ace55784837..fd303339f570fe 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -179,6 +179,9 @@ inline void Environment::AsyncHooks::clear_async_id_stack() { fields_[kStackLength] = 0; } +// The DefaultTriggerAsyncIdScope(AsyncWrap*) constructor is defined in +// async_wrap-inl.h to avoid a circular dependency. + inline Environment::AsyncHooks::DefaultTriggerAsyncIdScope ::DefaultTriggerAsyncIdScope(Environment* env, double default_trigger_async_id) diff --git a/src/env.h b/src/env.h index 4df4cb13b8209e..b9e459c47ec607 100644 --- a/src/env.h +++ b/src/env.h @@ -439,6 +439,7 @@ class Environment { DefaultTriggerAsyncIdScope() = delete; explicit DefaultTriggerAsyncIdScope(Environment* env, double init_trigger_async_id); + explicit DefaultTriggerAsyncIdScope(AsyncWrap* async_wrap); ~DefaultTriggerAsyncIdScope(); private: diff --git a/src/pipe_wrap.cc b/src/pipe_wrap.cc index 016ce480b6a809..14ac671b92b5bb 100644 --- a/src/pipe_wrap.cc +++ b/src/pipe_wrap.cc @@ -54,8 +54,7 @@ Local PipeWrap::Instantiate(Environment* env, AsyncWrap* parent, PipeWrap::SocketType type) { EscapableHandleScope handle_scope(env->isolate()); - AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(env, - parent->get_async_id()); + AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent); CHECK_EQ(false, env->pipe_constructor_template().IsEmpty()); Local constructor = env->pipe_constructor_template()->GetFunction(); CHECK_EQ(false, constructor.IsEmpty()); diff --git a/src/stream_base-inl.h b/src/stream_base-inl.h index 81adf7a866b927..1534dcd1d53359 100644 --- a/src/stream_base-inl.h +++ b/src/stream_base-inl.h @@ -164,8 +164,7 @@ inline int StreamBase::Shutdown(v8::Local req_wrap_obj) { ->NewInstance(env->context()).ToLocalChecked(); } - AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope( - env, GetAsyncWrap()->get_async_id()); + AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap()); ShutdownWrap* req_wrap = CreateShutdownWrap(req_wrap_obj); int err = DoShutdown(req_wrap); @@ -202,8 +201,7 @@ inline StreamWriteResult StreamBase::Write( ->NewInstance(env->context()).ToLocalChecked(); } - AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope( - env, GetAsyncWrap()->get_async_id()); + AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap()); WriteWrap* req_wrap = CreateWriteWrap(req_wrap_obj); err = DoWrite(req_wrap, bufs, count, send_handle); @@ -383,8 +381,7 @@ void StreamBase::JSMethod(const FunctionCallbackInfo& args) { if (!wrap->IsAlive()) return args.GetReturnValue().Set(UV_EINVAL); - AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope( - handle->env(), handle->get_async_id()); + AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(handle); args.GetReturnValue().Set((wrap->*Method)(args)); } diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index 61b08217b8f129..cd6ed3cef0a5bf 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -57,8 +57,7 @@ Local TCPWrap::Instantiate(Environment* env, AsyncWrap* parent, TCPWrap::SocketType type) { EscapableHandleScope handle_scope(env->isolate()); - AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope( - env, parent->get_async_id()); + AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent); CHECK_EQ(env->tcp_constructor_template().IsEmpty(), false); Local constructor = env->tcp_constructor_template()->GetFunction(); CHECK_EQ(constructor.IsEmpty(), false); @@ -289,8 +288,7 @@ void TCPWrap::Connect(const FunctionCallbackInfo& args) { int err = uv_ip4_addr(*ip_address, port, &addr); if (err == 0) { - AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope( - env, wrap->get_async_id()); + AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap); ConnectWrap* req_wrap = new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_TCPCONNECTWRAP); err = uv_tcp_connect(req_wrap->req(), @@ -326,8 +324,7 @@ void TCPWrap::Connect6(const FunctionCallbackInfo& args) { int err = uv_ip6_addr(*ip_address, port, &addr); if (err == 0) { - AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope( - env, wrap->get_async_id()); + AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap); ConnectWrap* req_wrap = new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_TCPCONNECTWRAP); err = uv_tcp_connect(req_wrap->req(), diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index 27be93abd0e5d8..e7d97dc484250c 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -359,8 +359,7 @@ void UDPWrap::DoSend(const FunctionCallbackInfo& args, int family) { SendWrap* req_wrap; { - AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope( - env, wrap->get_async_id()); + AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(wrap); req_wrap = new SendWrap(env, req_wrap_obj, have_callback); } size_t msg_size = 0; @@ -511,8 +510,7 @@ Local UDPWrap::Instantiate(Environment* env, AsyncWrap* parent, UDPWrap::SocketType type) { EscapableHandleScope scope(env->isolate()); - AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope( - env, parent->get_async_id()); + AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent); // If this assert fires then Initialize hasn't been called yet. CHECK_EQ(env->udp_constructor_function().IsEmpty(), false);