Skip to content

Commit 5197053

Browse files
committed
src: move AsyncHooks out of Environment
PR-URL: #26824 Refs: #26776 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent a8eac78 commit 5197053

10 files changed

+91
-112
lines changed

src/api/callback.cc

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ using v8::Object;
1717
using v8::String;
1818
using v8::Value;
1919

20-
using AsyncHooks = Environment::AsyncHooks;
21-
2220
CallbackScope::CallbackScope(Isolate* isolate,
2321
Local<Object> object,
2422
async_context asyncContext)

src/async_wrap-inl.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,13 @@ inline double AsyncWrap::get_trigger_async_id() const {
4848
inline AsyncWrap::AsyncScope::AsyncScope(AsyncWrap* wrap)
4949
: wrap_(wrap) {
5050
Environment* env = wrap->env();
51-
if (env->async_hooks()->fields()[Environment::AsyncHooks::kBefore] == 0)
52-
return;
51+
if (env->async_hooks()->fields()[AsyncHooks::kBefore] == 0) return;
5352
EmitBefore(env, wrap->get_async_id());
5453
}
5554

5655
inline AsyncWrap::AsyncScope::~AsyncScope() {
5756
Environment* env = wrap_->env();
58-
if (env->async_hooks()->fields()[Environment::AsyncHooks::kAfter] == 0)
59-
return;
57+
if (env->async_hooks()->fields()[AsyncHooks::kAfter] == 0) return;
6058
EmitAfter(env, wrap_->get_async_id());
6159
}
6260

@@ -94,8 +92,8 @@ inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback(
9492

9593

9694
// Defined here to avoid a circular dependency with env-inl.h.
97-
inline Environment::AsyncHooks::DefaultTriggerAsyncIdScope
98-
::DefaultTriggerAsyncIdScope(AsyncWrap* async_wrap)
95+
inline AsyncHooks::DefaultTriggerAsyncIdScope ::DefaultTriggerAsyncIdScope(
96+
AsyncWrap* async_wrap)
9997
: DefaultTriggerAsyncIdScope(async_wrap->env(),
10098
async_wrap->get_async_id()) {}
10199

src/async_wrap.cc

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ using v8::Value;
5656
using v8::WeakCallbackInfo;
5757
using v8::WeakCallbackType;
5858

59-
using AsyncHooks = node::Environment::AsyncHooks;
6059
using TryCatchScope = node::errors::TryCatchScope;
6160

6261
namespace node {

src/env-inl.h

+15-20
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ inline MultiIsolatePlatform* IsolateData::platform() const {
6464
return platform_;
6565
}
6666

67-
inline Environment::AsyncHooks::AsyncHooks()
67+
inline AsyncHooks::AsyncHooks()
6868
: async_ids_stack_(env()->isolate(), 16 * 2),
6969
fields_(env()->isolate(), kFieldsCount),
7070
async_id_fields_(env()->isolate(), kUidFieldsCount) {
@@ -102,36 +102,33 @@ inline Environment::AsyncHooks::AsyncHooks()
102102
#undef V
103103
}
104104

105-
inline AliasedBuffer<uint32_t, v8::Uint32Array>&
106-
Environment::AsyncHooks::fields() {
105+
inline AliasedBuffer<uint32_t, v8::Uint32Array>& AsyncHooks::fields() {
107106
return fields_;
108107
}
109108

110-
inline AliasedBuffer<double, v8::Float64Array>&
111-
Environment::AsyncHooks::async_id_fields() {
109+
inline AliasedBuffer<double, v8::Float64Array>& AsyncHooks::async_id_fields() {
112110
return async_id_fields_;
113111
}
114112

115-
inline AliasedBuffer<double, v8::Float64Array>&
116-
Environment::AsyncHooks::async_ids_stack() {
113+
inline AliasedBuffer<double, v8::Float64Array>& AsyncHooks::async_ids_stack() {
117114
return async_ids_stack_;
118115
}
119116

120-
inline v8::Local<v8::String> Environment::AsyncHooks::provider_string(int idx) {
117+
inline v8::Local<v8::String> AsyncHooks::provider_string(int idx) {
121118
return providers_[idx].Get(env()->isolate());
122119
}
123120

124-
inline void Environment::AsyncHooks::no_force_checks() {
121+
inline void AsyncHooks::no_force_checks() {
125122
fields_[kCheck] -= 1;
126123
}
127124

128-
inline Environment* Environment::AsyncHooks::env() {
125+
inline Environment* AsyncHooks::env() {
129126
return Environment::ForAsyncHooks(this);
130127
}
131128

132129
// Remember to keep this code aligned with pushAsyncIds() in JS.
133-
inline void Environment::AsyncHooks::push_async_ids(double async_id,
134-
double trigger_async_id) {
130+
inline void AsyncHooks::push_async_ids(double async_id,
131+
double trigger_async_id) {
135132
// Since async_hooks is experimental, do only perform the check
136133
// when async_hooks is enabled.
137134
if (fields_[kCheck] > 0) {
@@ -150,7 +147,7 @@ inline void Environment::AsyncHooks::push_async_ids(double async_id,
150147
}
151148

152149
// Remember to keep this code aligned with popAsyncIds() in JS.
153-
inline bool Environment::AsyncHooks::pop_async_id(double async_id) {
150+
inline bool AsyncHooks::pop_async_id(double async_id) {
154151
// In case of an exception then this may have already been reset, if the
155152
// stack was multiple MakeCallback()'s deep.
156153
if (fields_[kStackLength] == 0) return false;
@@ -183,7 +180,7 @@ inline bool Environment::AsyncHooks::pop_async_id(double async_id) {
183180
}
184181

185182
// Keep in sync with clearAsyncIdStack in lib/internal/async_hooks.js.
186-
inline void Environment::AsyncHooks::clear_async_id_stack() {
183+
inline void AsyncHooks::clear_async_id_stack() {
187184
async_id_fields_[kExecutionAsyncId] = 0;
188185
async_id_fields_[kTriggerAsyncId] = 0;
189186
fields_[kStackLength] = 0;
@@ -192,9 +189,8 @@ inline void Environment::AsyncHooks::clear_async_id_stack() {
192189
// The DefaultTriggerAsyncIdScope(AsyncWrap*) constructor is defined in
193190
// async_wrap-inl.h to avoid a circular dependency.
194191

195-
inline Environment::AsyncHooks::DefaultTriggerAsyncIdScope
196-
::DefaultTriggerAsyncIdScope(Environment* env,
197-
double default_trigger_async_id)
192+
inline AsyncHooks::DefaultTriggerAsyncIdScope ::DefaultTriggerAsyncIdScope(
193+
Environment* env, double default_trigger_async_id)
198194
: async_hooks_(env->async_hooks()) {
199195
if (env->async_hooks()->fields()[AsyncHooks::kCheck] > 0) {
200196
CHECK_GE(default_trigger_async_id, 0);
@@ -206,8 +202,7 @@ inline Environment::AsyncHooks::DefaultTriggerAsyncIdScope
206202
default_trigger_async_id;
207203
}
208204

209-
inline Environment::AsyncHooks::DefaultTriggerAsyncIdScope
210-
::~DefaultTriggerAsyncIdScope() {
205+
inline AsyncHooks::DefaultTriggerAsyncIdScope ::~DefaultTriggerAsyncIdScope() {
211206
async_hooks_->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] =
212207
old_default_trigger_async_id_;
213208
}
@@ -430,7 +425,7 @@ inline void Environment::set_is_in_inspector_console_call(bool value) {
430425
}
431426
#endif
432427

433-
inline Environment::AsyncHooks* Environment::async_hooks() {
428+
inline AsyncHooks* Environment::async_hooks() {
434429
return &async_hooks_;
435430
}
436431

src/env.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -818,8 +818,7 @@ void Environment::CollectUVExceptionInfo(Local<Value> object,
818818
syscall, message, path, dest);
819819
}
820820

821-
822-
void Environment::AsyncHooks::grow_async_ids_stack() {
821+
void AsyncHooks::grow_async_ids_stack() {
823822
async_ids_stack_.reserve(async_ids_stack_.Length() * 3);
824823

825824
env()->async_hooks_binding()->Set(

src/env.h

+71-72
Original file line numberDiff line numberDiff line change
@@ -537,87 +537,86 @@ class AsyncRequest : public MemoryRetainer {
537537
std::atomic_bool stopped_ {true};
538538
};
539539

540-
class Environment {
540+
class AsyncHooks {
541541
public:
542-
Environment(const Environment&) = delete;
543-
Environment& operator=(const Environment&) = delete;
542+
// Reason for both UidFields and Fields are that one is stored as a double*
543+
// and the other as a uint32_t*.
544+
enum Fields {
545+
kInit,
546+
kBefore,
547+
kAfter,
548+
kDestroy,
549+
kPromiseResolve,
550+
kTotals,
551+
kCheck,
552+
kStackLength,
553+
kFieldsCount,
554+
};
544555

545-
class AsyncHooks {
546-
public:
547-
// Reason for both UidFields and Fields are that one is stored as a double*
548-
// and the other as a uint32_t*.
549-
enum Fields {
550-
kInit,
551-
kBefore,
552-
kAfter,
553-
kDestroy,
554-
kPromiseResolve,
555-
kTotals,
556-
kCheck,
557-
kStackLength,
558-
kFieldsCount,
559-
};
556+
enum UidFields {
557+
kExecutionAsyncId,
558+
kTriggerAsyncId,
559+
kAsyncIdCounter,
560+
kDefaultTriggerAsyncId,
561+
kUidFieldsCount,
562+
};
560563

561-
enum UidFields {
562-
kExecutionAsyncId,
563-
kTriggerAsyncId,
564-
kAsyncIdCounter,
565-
kDefaultTriggerAsyncId,
566-
kUidFieldsCount,
567-
};
564+
inline AliasedBuffer<uint32_t, v8::Uint32Array>& fields();
565+
inline AliasedBuffer<double, v8::Float64Array>& async_id_fields();
566+
inline AliasedBuffer<double, v8::Float64Array>& async_ids_stack();
568567

569-
inline AliasedBuffer<uint32_t, v8::Uint32Array>& fields();
570-
inline AliasedBuffer<double, v8::Float64Array>& async_id_fields();
571-
inline AliasedBuffer<double, v8::Float64Array>& async_ids_stack();
572-
573-
inline v8::Local<v8::String> provider_string(int idx);
574-
575-
inline void no_force_checks();
576-
inline Environment* env();
577-
578-
inline void push_async_ids(double async_id, double trigger_async_id);
579-
inline bool pop_async_id(double async_id);
580-
inline void clear_async_id_stack(); // Used in fatal exceptions.
581-
582-
AsyncHooks(const AsyncHooks&) = delete;
583-
AsyncHooks& operator=(const AsyncHooks&) = delete;
584-
585-
// Used to set the kDefaultTriggerAsyncId in a scope. This is instead of
586-
// passing the trigger_async_id along with other constructor arguments.
587-
class DefaultTriggerAsyncIdScope {
588-
public:
589-
DefaultTriggerAsyncIdScope() = delete;
590-
explicit DefaultTriggerAsyncIdScope(Environment* env,
591-
double init_trigger_async_id);
592-
explicit DefaultTriggerAsyncIdScope(AsyncWrap* async_wrap);
593-
~DefaultTriggerAsyncIdScope();
594-
595-
DefaultTriggerAsyncIdScope(const DefaultTriggerAsyncIdScope&) = delete;
596-
DefaultTriggerAsyncIdScope& operator=(const DefaultTriggerAsyncIdScope&) =
597-
delete;
598-
599-
private:
600-
AsyncHooks* async_hooks_;
601-
double old_default_trigger_async_id_;
602-
};
568+
inline v8::Local<v8::String> provider_string(int idx);
603569

570+
inline void no_force_checks();
571+
inline Environment* env();
604572

605-
private:
606-
friend class Environment; // So we can call the constructor.
607-
inline AsyncHooks();
608-
// Keep a list of all Persistent strings used for Provider types.
609-
v8::Eternal<v8::String> providers_[AsyncWrap::PROVIDERS_LENGTH];
610-
// Stores the ids of the current execution context stack.
611-
AliasedBuffer<double, v8::Float64Array> async_ids_stack_;
612-
// Attached to a Uint32Array that tracks the number of active hooks for
613-
// each type.
614-
AliasedBuffer<uint32_t, v8::Uint32Array> fields_;
615-
// Attached to a Float64Array that tracks the state of async resources.
616-
AliasedBuffer<double, v8::Float64Array> async_id_fields_;
573+
inline void push_async_ids(double async_id, double trigger_async_id);
574+
inline bool pop_async_id(double async_id);
575+
inline void clear_async_id_stack(); // Used in fatal exceptions.
617576

618-
void grow_async_ids_stack();
577+
AsyncHooks(const AsyncHooks&) = delete;
578+
AsyncHooks& operator=(const AsyncHooks&) = delete;
579+
580+
// Used to set the kDefaultTriggerAsyncId in a scope. This is instead of
581+
// passing the trigger_async_id along with other constructor arguments.
582+
class DefaultTriggerAsyncIdScope {
583+
public:
584+
DefaultTriggerAsyncIdScope() = delete;
585+
explicit DefaultTriggerAsyncIdScope(Environment* env,
586+
double init_trigger_async_id);
587+
explicit DefaultTriggerAsyncIdScope(AsyncWrap* async_wrap);
588+
~DefaultTriggerAsyncIdScope();
589+
590+
DefaultTriggerAsyncIdScope(const DefaultTriggerAsyncIdScope&) = delete;
591+
DefaultTriggerAsyncIdScope& operator=(const DefaultTriggerAsyncIdScope&) =
592+
delete;
593+
594+
private:
595+
AsyncHooks* async_hooks_;
596+
double old_default_trigger_async_id_;
619597
};
620598

599+
private:
600+
friend class Environment; // So we can call the constructor.
601+
inline AsyncHooks();
602+
// Keep a list of all Persistent strings used for Provider types.
603+
v8::Eternal<v8::String> providers_[AsyncWrap::PROVIDERS_LENGTH];
604+
// Stores the ids of the current execution context stack.
605+
AliasedBuffer<double, v8::Float64Array> async_ids_stack_;
606+
// Attached to a Uint32Array that tracks the number of active hooks for
607+
// each type.
608+
AliasedBuffer<uint32_t, v8::Uint32Array> fields_;
609+
// Attached to a Float64Array that tracks the state of async resources.
610+
AliasedBuffer<double, v8::Float64Array> async_id_fields_;
611+
612+
void grow_async_ids_stack();
613+
};
614+
615+
class Environment {
616+
public:
617+
Environment(const Environment&) = delete;
618+
Environment& operator=(const Environment&) = delete;
619+
621620
class AsyncCallbackScope {
622621
public:
623622
AsyncCallbackScope() = delete;

src/pipe_wrap.cc

-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ using v8::Object;
4747
using v8::String;
4848
using v8::Value;
4949

50-
using AsyncHooks = Environment::AsyncHooks;
51-
5250
MaybeLocal<Object> PipeWrap::Instantiate(Environment* env,
5351
AsyncWrap* parent,
5452
PipeWrap::SocketType type) {

src/stream_base-inl.h

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ using v8::PropertyCallbackInfo;
2222
using v8::String;
2323
using v8::Value;
2424

25-
using AsyncHooks = Environment::AsyncHooks;
26-
2725
inline void StreamReq::AttachToObject(v8::Local<v8::Object> req_wrap_obj) {
2826
CHECK_EQ(req_wrap_obj->GetAlignedPointerFromInternalField(kStreamReqField),
2927
nullptr);

src/tcp_wrap.cc

-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ using v8::String;
5252
using v8::Uint32;
5353
using v8::Value;
5454

55-
using AsyncHooks = Environment::AsyncHooks;
56-
5755
MaybeLocal<Object> TCPWrap::Instantiate(Environment* env,
5856
AsyncWrap* parent,
5957
TCPWrap::SocketType type) {

src/udp_wrap.cc

-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ using v8::Uint32;
4646
using v8::Undefined;
4747
using v8::Value;
4848

49-
using AsyncHooks = Environment::AsyncHooks;
50-
51-
5249
class SendWrap : public ReqWrap<uv_udp_send_t> {
5350
public:
5451
SendWrap(Environment* env, Local<Object> req_wrap_obj, bool have_callback);

0 commit comments

Comments
 (0)