Skip to content

Commit cc7bb85

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

File tree

7 files changed

+36
-28
lines changed

7 files changed

+36
-28
lines changed

src/api/callback.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void InternalCallbackScope::Close() {
9292
AsyncWrap::EmitAfter(env_, async_context_.async_id);
9393
}
9494

95-
if (env_->makecallback_depth() > 1) {
95+
if (env_->async_callback_scope_depth() > 1) {
9696
return;
9797
}
9898

@@ -217,7 +217,7 @@ MaybeLocal<Value> MakeCallback(Isolate* isolate,
217217
Context::Scope context_scope(env->context());
218218
MaybeLocal<Value> ret =
219219
InternalMakeCallback(env, recv, callback, argc, argv, asyncContext);
220-
if (ret.IsEmpty() && env->makecallback_depth() == 0) {
220+
if (ret.IsEmpty() && env->async_callback_scope_depth() == 0) {
221221
// This is only for legacy compatibility and we may want to look into
222222
// removing/adjusting it.
223223
return Undefined(env->isolate());

src/env-inl.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,24 @@ Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
212212
return ContainerOf(&Environment::async_hooks_, hooks);
213213
}
214214

215+
inline AsyncCallbackScope::AsyncCallbackScope(Environment* env) : env_(env) {
216+
env_->PushAsyncCallbackScope();
217+
}
215218

216-
inline Environment::AsyncCallbackScope::AsyncCallbackScope(Environment* env)
217-
: env_(env) {
218-
env_->makecallback_cntr_++;
219+
inline AsyncCallbackScope::~AsyncCallbackScope() {
220+
env_->PopAsyncCallbackScope();
221+
}
222+
223+
inline size_t Environment::async_callback_scope_depth() const {
224+
return async_callback_scope_depth_;
219225
}
220226

221-
inline Environment::AsyncCallbackScope::~AsyncCallbackScope() {
222-
env_->makecallback_cntr_--;
227+
inline void Environment::PushAsyncCallbackScope() {
228+
async_callback_scope_depth_++;
223229
}
224230

225-
inline size_t Environment::makecallback_depth() const {
226-
return makecallback_cntr_;
231+
inline void Environment::PopAsyncCallbackScope() {
232+
async_callback_scope_depth_--;
227233
}
228234

229235
inline Environment::ImmediateInfo::ImmediateInfo(v8::Isolate* isolate)

src/env.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -612,24 +612,26 @@ class AsyncHooks {
612612
void grow_async_ids_stack();
613613
};
614614

615+
class AsyncCallbackScope {
616+
public:
617+
AsyncCallbackScope() = delete;
618+
explicit AsyncCallbackScope(Environment* env);
619+
~AsyncCallbackScope();
620+
AsyncCallbackScope(const AsyncCallbackScope&) = delete;
621+
AsyncCallbackScope& operator=(const AsyncCallbackScope&) = delete;
622+
623+
private:
624+
Environment* env_;
625+
};
626+
615627
class Environment {
616628
public:
617629
Environment(const Environment&) = delete;
618630
Environment& operator=(const Environment&) = delete;
619631

620-
class AsyncCallbackScope {
621-
public:
622-
AsyncCallbackScope() = delete;
623-
explicit AsyncCallbackScope(Environment* env);
624-
~AsyncCallbackScope();
625-
AsyncCallbackScope(const AsyncCallbackScope&) = delete;
626-
AsyncCallbackScope& operator=(const AsyncCallbackScope&) = delete;
627-
628-
private:
629-
Environment* env_;
630-
};
631-
632-
inline size_t makecallback_depth() const;
632+
inline size_t async_callback_scope_depth() const;
633+
inline void PushAsyncCallbackScope();
634+
inline void PopAsyncCallbackScope();
633635

634636
class ImmediateInfo {
635637
public:
@@ -1082,7 +1084,7 @@ class Environment {
10821084
bool printed_error_ = false;
10831085
bool emit_env_nonstring_warning_ = true;
10841086
bool emit_err_name_warning_ = true;
1085-
size_t makecallback_cntr_ = 0;
1087+
size_t async_callback_scope_depth_ = 0;
10861088
std::vector<double> destroy_async_id_list_;
10871089

10881090
std::shared_ptr<EnvironmentOptions> options_;

src/node.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ inline int StartNodeWithIsolate(Isolate* isolate,
818818
#endif // HAVE_INSPECTOR && NODE_USE_V8_PLATFORM
819819

820820
{
821-
Environment::AsyncCallbackScope callback_scope(&env);
821+
AsyncCallbackScope callback_scope(&env);
822822
env.async_hooks()->push_async_ids(1, 0);
823823
LoadEnvironment(&env);
824824
env.async_hooks()->pop_async_id(1);

src/node_http_parser_impl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ class Parser : public AsyncWrap, public StreamListener {
323323

324324
argv[A_UPGRADE] = Boolean::New(env()->isolate(), parser_.upgrade);
325325

326-
Environment::AsyncCallbackScope callback_scope(env());
326+
AsyncCallbackScope callback_scope(env());
327327

328328
MaybeLocal<Value> head_response =
329329
MakeCallback(cb.As<Function>(), arraysize(argv), argv);
@@ -394,7 +394,7 @@ class Parser : public AsyncWrap, public StreamListener {
394394
if (!cb->IsFunction())
395395
return 0;
396396

397-
Environment::AsyncCallbackScope callback_scope(env());
397+
AsyncCallbackScope callback_scope(env());
398398

399399
MaybeLocal<Value> r = MakeCallback(cb.As<Function>(), 0, nullptr);
400400

src/node_internals.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ class InternalCallbackScope {
213213
Environment* env_;
214214
async_context async_context_;
215215
v8::Local<v8::Object> object_;
216-
Environment::AsyncCallbackScope callback_scope_;
216+
AsyncCallbackScope callback_scope_;
217217
bool failed_ = false;
218218
bool pushed_ids_ = false;
219219
bool closed_ = false;

src/node_worker.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ void Worker::Run() {
277277
inspector_started = true;
278278

279279
HandleScope handle_scope(isolate_);
280-
Environment::AsyncCallbackScope callback_scope(env_.get());
280+
AsyncCallbackScope callback_scope(env_.get());
281281
env_->async_hooks()->push_async_ids(1, 0);
282282
if (!RunBootstrapping(env_.get()).IsEmpty()) {
283283
CreateEnvMessagePort(env_.get());

0 commit comments

Comments
 (0)