diff --git a/src/api/callback.cc b/src/api/callback.cc index 47a1549e93d943..7c6e8d8697ebf3 100644 --- a/src/api/callback.cc +++ b/src/api/callback.cc @@ -17,8 +17,6 @@ using v8::Object; using v8::String; using v8::Value; -using AsyncHooks = Environment::AsyncHooks; - CallbackScope::CallbackScope(Isolate* isolate, Local object, async_context asyncContext) diff --git a/src/async_wrap-inl.h b/src/async_wrap-inl.h index 4405bb3a9baa52..6ef968933b8c1f 100644 --- a/src/async_wrap-inl.h +++ b/src/async_wrap-inl.h @@ -48,15 +48,13 @@ inline double AsyncWrap::get_trigger_async_id() const { inline AsyncWrap::AsyncScope::AsyncScope(AsyncWrap* wrap) : wrap_(wrap) { Environment* env = wrap->env(); - if (env->async_hooks()->fields()[Environment::AsyncHooks::kBefore] == 0) - return; + if (env->async_hooks()->fields()[AsyncHooks::kBefore] == 0) return; EmitBefore(env, wrap->get_async_id()); } inline AsyncWrap::AsyncScope::~AsyncScope() { Environment* env = wrap_->env(); - if (env->async_hooks()->fields()[Environment::AsyncHooks::kAfter] == 0) - return; + if (env->async_hooks()->fields()[AsyncHooks::kAfter] == 0) return; EmitAfter(env, wrap_->get_async_id()); } @@ -94,8 +92,8 @@ inline v8::MaybeLocal AsyncWrap::MakeCallback( // Defined here to avoid a circular dependency with env-inl.h. -inline Environment::AsyncHooks::DefaultTriggerAsyncIdScope - ::DefaultTriggerAsyncIdScope(AsyncWrap* async_wrap) +inline AsyncHooks::DefaultTriggerAsyncIdScope ::DefaultTriggerAsyncIdScope( + AsyncWrap* async_wrap) : DefaultTriggerAsyncIdScope(async_wrap->env(), async_wrap->get_async_id()) {} diff --git a/src/async_wrap.cc b/src/async_wrap.cc index 22d5d91eb096f9..fffce469d50d55 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -56,7 +56,6 @@ using v8::Value; using v8::WeakCallbackInfo; using v8::WeakCallbackType; -using AsyncHooks = node::Environment::AsyncHooks; using TryCatchScope = node::errors::TryCatchScope; namespace node { diff --git a/src/env-inl.h b/src/env-inl.h index c794226d2c44a7..d211d46997cd41 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -64,7 +64,7 @@ inline MultiIsolatePlatform* IsolateData::platform() const { return platform_; } -inline Environment::AsyncHooks::AsyncHooks() +inline AsyncHooks::AsyncHooks() : async_ids_stack_(env()->isolate(), 16 * 2), fields_(env()->isolate(), kFieldsCount), async_id_fields_(env()->isolate(), kUidFieldsCount) { @@ -102,36 +102,33 @@ inline Environment::AsyncHooks::AsyncHooks() #undef V } -inline AliasedBuffer& -Environment::AsyncHooks::fields() { +inline AliasedBuffer& AsyncHooks::fields() { return fields_; } -inline AliasedBuffer& -Environment::AsyncHooks::async_id_fields() { +inline AliasedBuffer& AsyncHooks::async_id_fields() { return async_id_fields_; } -inline AliasedBuffer& -Environment::AsyncHooks::async_ids_stack() { +inline AliasedBuffer& AsyncHooks::async_ids_stack() { return async_ids_stack_; } -inline v8::Local Environment::AsyncHooks::provider_string(int idx) { +inline v8::Local AsyncHooks::provider_string(int idx) { return providers_[idx].Get(env()->isolate()); } -inline void Environment::AsyncHooks::no_force_checks() { +inline void AsyncHooks::no_force_checks() { fields_[kCheck] -= 1; } -inline Environment* Environment::AsyncHooks::env() { +inline Environment* AsyncHooks::env() { return Environment::ForAsyncHooks(this); } // Remember to keep this code aligned with pushAsyncIds() in JS. -inline void Environment::AsyncHooks::push_async_ids(double async_id, - double trigger_async_id) { +inline void AsyncHooks::push_async_ids(double async_id, + double trigger_async_id) { // Since async_hooks is experimental, do only perform the check // when async_hooks is enabled. if (fields_[kCheck] > 0) { @@ -150,7 +147,7 @@ inline void Environment::AsyncHooks::push_async_ids(double async_id, } // Remember to keep this code aligned with popAsyncIds() in JS. -inline bool Environment::AsyncHooks::pop_async_id(double async_id) { +inline bool AsyncHooks::pop_async_id(double async_id) { // In case of an exception then this may have already been reset, if the // stack was multiple MakeCallback()'s deep. if (fields_[kStackLength] == 0) return false; @@ -183,7 +180,7 @@ inline bool Environment::AsyncHooks::pop_async_id(double async_id) { } // Keep in sync with clearAsyncIdStack in lib/internal/async_hooks.js. -inline void Environment::AsyncHooks::clear_async_id_stack() { +inline void AsyncHooks::clear_async_id_stack() { async_id_fields_[kExecutionAsyncId] = 0; async_id_fields_[kTriggerAsyncId] = 0; fields_[kStackLength] = 0; @@ -192,9 +189,8 @@ inline void Environment::AsyncHooks::clear_async_id_stack() { // 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) +inline AsyncHooks::DefaultTriggerAsyncIdScope ::DefaultTriggerAsyncIdScope( + Environment* env, double default_trigger_async_id) : async_hooks_(env->async_hooks()) { if (env->async_hooks()->fields()[AsyncHooks::kCheck] > 0) { CHECK_GE(default_trigger_async_id, 0); @@ -206,8 +202,7 @@ inline Environment::AsyncHooks::DefaultTriggerAsyncIdScope default_trigger_async_id; } -inline Environment::AsyncHooks::DefaultTriggerAsyncIdScope - ::~DefaultTriggerAsyncIdScope() { +inline AsyncHooks::DefaultTriggerAsyncIdScope ::~DefaultTriggerAsyncIdScope() { async_hooks_->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] = old_default_trigger_async_id_; } @@ -430,7 +425,7 @@ inline void Environment::set_is_in_inspector_console_call(bool value) { } #endif -inline Environment::AsyncHooks* Environment::async_hooks() { +inline AsyncHooks* Environment::async_hooks() { return &async_hooks_; } diff --git a/src/env.cc b/src/env.cc index fea03b70db07da..73a8e6e773f58c 100644 --- a/src/env.cc +++ b/src/env.cc @@ -918,8 +918,7 @@ void Environment::CollectUVExceptionInfo(Local object, syscall, message, path, dest); } - -void Environment::AsyncHooks::grow_async_ids_stack() { +void AsyncHooks::grow_async_ids_stack() { async_ids_stack_.reserve(async_ids_stack_.Length() * 3); env()->async_hooks_binding()->Set( diff --git a/src/env.h b/src/env.h index d883e37c8108f0..ec126e6d2a0599 100644 --- a/src/env.h +++ b/src/env.h @@ -532,87 +532,86 @@ class AsyncRequest : public MemoryRetainer { std::atomic_bool stopped_ {true}; }; -class Environment { +class AsyncHooks { public: - Environment(const Environment&) = delete; - Environment& operator=(const Environment&) = delete; + // Reason for both UidFields and Fields are that one is stored as a double* + // and the other as a uint32_t*. + enum Fields { + kInit, + kBefore, + kAfter, + kDestroy, + kPromiseResolve, + kTotals, + kCheck, + kStackLength, + kFieldsCount, + }; - class AsyncHooks { - public: - // Reason for both UidFields and Fields are that one is stored as a double* - // and the other as a uint32_t*. - enum Fields { - kInit, - kBefore, - kAfter, - kDestroy, - kPromiseResolve, - kTotals, - kCheck, - kStackLength, - kFieldsCount, - }; + enum UidFields { + kExecutionAsyncId, + kTriggerAsyncId, + kAsyncIdCounter, + kDefaultTriggerAsyncId, + kUidFieldsCount, + }; - enum UidFields { - kExecutionAsyncId, - kTriggerAsyncId, - kAsyncIdCounter, - kDefaultTriggerAsyncId, - kUidFieldsCount, - }; + inline AliasedBuffer& fields(); + inline AliasedBuffer& async_id_fields(); + inline AliasedBuffer& async_ids_stack(); - inline AliasedBuffer& fields(); - inline AliasedBuffer& async_id_fields(); - inline AliasedBuffer& async_ids_stack(); - - inline v8::Local provider_string(int idx); - - inline void no_force_checks(); - inline Environment* env(); - - inline void push_async_ids(double async_id, double trigger_async_id); - inline bool pop_async_id(double async_id); - inline void clear_async_id_stack(); // Used in fatal exceptions. - - AsyncHooks(const AsyncHooks&) = delete; - AsyncHooks& operator=(const AsyncHooks&) = delete; - - // Used to set the kDefaultTriggerAsyncId in a scope. This is instead of - // passing the trigger_async_id along with other constructor arguments. - class DefaultTriggerAsyncIdScope { - public: - DefaultTriggerAsyncIdScope() = delete; - explicit DefaultTriggerAsyncIdScope(Environment* env, - double init_trigger_async_id); - explicit DefaultTriggerAsyncIdScope(AsyncWrap* async_wrap); - ~DefaultTriggerAsyncIdScope(); - - DefaultTriggerAsyncIdScope(const DefaultTriggerAsyncIdScope&) = delete; - DefaultTriggerAsyncIdScope& operator=(const DefaultTriggerAsyncIdScope&) = - delete; - - private: - AsyncHooks* async_hooks_; - double old_default_trigger_async_id_; - }; + inline v8::Local provider_string(int idx); + inline void no_force_checks(); + inline Environment* env(); - private: - friend class Environment; // So we can call the constructor. - inline AsyncHooks(); - // Keep a list of all Persistent strings used for Provider types. - v8::Eternal providers_[AsyncWrap::PROVIDERS_LENGTH]; - // Stores the ids of the current execution context stack. - AliasedBuffer async_ids_stack_; - // Attached to a Uint32Array that tracks the number of active hooks for - // each type. - AliasedBuffer fields_; - // Attached to a Float64Array that tracks the state of async resources. - AliasedBuffer async_id_fields_; + inline void push_async_ids(double async_id, double trigger_async_id); + inline bool pop_async_id(double async_id); + inline void clear_async_id_stack(); // Used in fatal exceptions. - void grow_async_ids_stack(); + AsyncHooks(const AsyncHooks&) = delete; + AsyncHooks& operator=(const AsyncHooks&) = delete; + + // Used to set the kDefaultTriggerAsyncId in a scope. This is instead of + // passing the trigger_async_id along with other constructor arguments. + class DefaultTriggerAsyncIdScope { + public: + DefaultTriggerAsyncIdScope() = delete; + explicit DefaultTriggerAsyncIdScope(Environment* env, + double init_trigger_async_id); + explicit DefaultTriggerAsyncIdScope(AsyncWrap* async_wrap); + ~DefaultTriggerAsyncIdScope(); + + DefaultTriggerAsyncIdScope(const DefaultTriggerAsyncIdScope&) = delete; + DefaultTriggerAsyncIdScope& operator=(const DefaultTriggerAsyncIdScope&) = + delete; + + private: + AsyncHooks* async_hooks_; + double old_default_trigger_async_id_; }; + private: + friend class Environment; // So we can call the constructor. + inline AsyncHooks(); + // Keep a list of all Persistent strings used for Provider types. + v8::Eternal providers_[AsyncWrap::PROVIDERS_LENGTH]; + // Stores the ids of the current execution context stack. + AliasedBuffer async_ids_stack_; + // Attached to a Uint32Array that tracks the number of active hooks for + // each type. + AliasedBuffer fields_; + // Attached to a Float64Array that tracks the state of async resources. + AliasedBuffer async_id_fields_; + + void grow_async_ids_stack(); +}; + +class Environment { + public: + Environment(const Environment&) = delete; + Environment& operator=(const Environment&) = delete; + class AsyncCallbackScope { public: AsyncCallbackScope() = delete; diff --git a/src/pipe_wrap.cc b/src/pipe_wrap.cc index a6f6f7bb375419..cafa33f9af14bf 100644 --- a/src/pipe_wrap.cc +++ b/src/pipe_wrap.cc @@ -47,8 +47,6 @@ using v8::Object; using v8::String; using v8::Value; -using AsyncHooks = Environment::AsyncHooks; - MaybeLocal PipeWrap::Instantiate(Environment* env, AsyncWrap* parent, PipeWrap::SocketType type) { diff --git a/src/stream_base-inl.h b/src/stream_base-inl.h index dbf3c07b920cf8..08a02e186cfe73 100644 --- a/src/stream_base-inl.h +++ b/src/stream_base-inl.h @@ -22,8 +22,6 @@ using v8::PropertyCallbackInfo; using v8::String; using v8::Value; -using AsyncHooks = Environment::AsyncHooks; - inline void StreamReq::AttachToObject(v8::Local req_wrap_obj) { CHECK_EQ(req_wrap_obj->GetAlignedPointerFromInternalField(kStreamReqField), nullptr); diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index 4d16fa41413753..aedb079ccffc23 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -52,8 +52,6 @@ using v8::String; using v8::Uint32; using v8::Value; -using AsyncHooks = Environment::AsyncHooks; - MaybeLocal TCPWrap::Instantiate(Environment* env, AsyncWrap* parent, TCPWrap::SocketType type) { diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index f1693aa39b9735..e568bb66a6682d 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -46,9 +46,6 @@ using v8::Uint32; using v8::Undefined; using v8::Value; -using AsyncHooks = Environment::AsyncHooks; - - class SendWrap : public ReqWrap { public: SendWrap(Environment* env, Local req_wrap_obj, bool have_callback);