Skip to content

Commit 4af9ff0

Browse files
joyeecheungtargos
authored andcommitted
src: move AsyncHooks out of Environment
PR-URL: #26824 Refs: #26776 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent a545cfe commit 4af9ff0

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
@@ -918,8 +918,7 @@ void Environment::CollectUVExceptionInfo(Local<Value> object,
918918
syscall, message, path, dest);
919919
}
920920

921-
922-
void Environment::AsyncHooks::grow_async_ids_stack() {
921+
void AsyncHooks::grow_async_ids_stack() {
923922
async_ids_stack_.reserve(async_ids_stack_.Length() * 3);
924923

925924
env()->async_hooks_binding()->Set(

src/env.h

+71-72
Original file line numberDiff line numberDiff line change
@@ -532,87 +532,86 @@ class AsyncRequest : public MemoryRetainer {
532532
std::atomic_bool stopped_ {true};
533533
};
534534

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

540-
class AsyncHooks {
541-
public:
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-
};
551+
enum UidFields {
552+
kExecutionAsyncId,
553+
kTriggerAsyncId,
554+
kAsyncIdCounter,
555+
kDefaultTriggerAsyncId,
556+
kUidFieldsCount,
557+
};
555558

556-
enum UidFields {
557-
kExecutionAsyncId,
558-
kTriggerAsyncId,
559-
kAsyncIdCounter,
560-
kDefaultTriggerAsyncId,
561-
kUidFieldsCount,
562-
};
559+
inline AliasedBuffer<uint32_t, v8::Uint32Array>& fields();
560+
inline AliasedBuffer<double, v8::Float64Array>& async_id_fields();
561+
inline AliasedBuffer<double, v8::Float64Array>& async_ids_stack();
563562

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();
567-
568-
inline v8::Local<v8::String> provider_string(int idx);
569-
570-
inline void no_force_checks();
571-
inline Environment* env();
572-
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.
576-
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_;
597-
};
563+
inline v8::Local<v8::String> provider_string(int idx);
598564

565+
inline void no_force_checks();
566+
inline Environment* env();
599567

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

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

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