Skip to content

Commit 6de1220

Browse files
joyeecheungtargos
authored andcommitted
src: move AsyncCallbackScope 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 4af9ff0 commit 6de1220

7 files changed

+36
-28
lines changed

src/api/callback.cc

+2-2
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

+13-7
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

+16-14
Original file line numberDiff line numberDiff line change
@@ -607,24 +607,26 @@ class AsyncHooks {
607607
void grow_async_ids_stack();
608608
};
609609

610+
class AsyncCallbackScope {
611+
public:
612+
AsyncCallbackScope() = delete;
613+
explicit AsyncCallbackScope(Environment* env);
614+
~AsyncCallbackScope();
615+
AsyncCallbackScope(const AsyncCallbackScope&) = delete;
616+
AsyncCallbackScope& operator=(const AsyncCallbackScope&) = delete;
617+
618+
private:
619+
Environment* env_;
620+
};
621+
610622
class Environment {
611623
public:
612624
Environment(const Environment&) = delete;
613625
Environment& operator=(const Environment&) = delete;
614626

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-
627-
inline size_t makecallback_depth() const;
627+
inline size_t async_callback_scope_depth() const;
628+
inline void PushAsyncCallbackScope();
629+
inline void PopAsyncCallbackScope();
628630

629631
class ImmediateInfo {
630632
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ inline int StartNodeWithIsolate(Isolate* isolate,
807807
#endif // HAVE_INSPECTOR && NODE_USE_V8_PLATFORM
808808

809809
{
810-
Environment::AsyncCallbackScope callback_scope(&env);
810+
AsyncCallbackScope callback_scope(&env);
811811
env.async_hooks()->push_async_ids(1, 0);
812812
LoadEnvironment(&env);
813813
env.async_hooks()->pop_async_id(1);

src/node_http_parser_impl.h

+2-2
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class InternalCallbackScope {
211211
Environment* env_;
212212
async_context async_context_;
213213
v8::Local<v8::Object> object_;
214-
Environment::AsyncCallbackScope callback_scope_;
214+
AsyncCallbackScope callback_scope_;
215215
bool failed_ = false;
216216
bool pushed_ids_ = false;
217217
bool closed_ = false;

src/node_worker.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ void Worker::Run() {
288288
inspector_started = true;
289289

290290
HandleScope handle_scope(isolate_);
291-
Environment::AsyncCallbackScope callback_scope(env_.get());
291+
AsyncCallbackScope callback_scope(env_.get());
292292
env_->async_hooks()->push_async_ids(1, 0);
293293
if (!RunBootstrapping(env_.get()).IsEmpty()) {
294294
USE(StartExecution(env_.get(), "internal/main/worker_thread"));

0 commit comments

Comments
 (0)