src: move AsyncCallbackScope out of Environment · nodejs/node@6de1220 (original) (raw)

7 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -92,7 +92,7 @@ void InternalCallbackScope::Close() {
92 92 AsyncWrap::EmitAfter(env_, async_context_.async_id);
93 93 }
94 94
95 -if (env_->makecallback_depth() > 1) {
95 +if (env_->async_callback_scope_depth() > 1) {
96 96 return;
97 97 }
98 98
@@ -217,7 +217,7 @@ MaybeLocal MakeCallback(Isolate* isolate,
217 217 Context::Scope context_scope(env->context());
218 218 MaybeLocal ret =
219 219 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) {
221 221 // This is only for legacy compatibility and we may want to look into
222 222 // removing/adjusting it.
223 223 return Undefined(env->isolate());
Original file line number Diff line number Diff line change
@@ -212,18 +212,24 @@ Environment* Environment::ForAsyncHooks(AsyncHooks* hooks) {
212 212 return ContainerOf(&Environment::async_hooks_, hooks);
213 213 }
214 214
215 +inline AsyncCallbackScope::AsyncCallbackScope(Environment* env) : env_(env) {
216 + env_->PushAsyncCallbackScope();
217 +}
215 218
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_;
219 225 }
220 226
221 -inline Environment::AsyncCallbackScope::~AsyncCallbackScope() {
222 -env_->makecallback_cntr_--;
227 +inline void Environment::PushAsyncCallbackScope() {
228 +async_callback_scope_depth_++;
223 229 }
224 230
225 -inline size_t Environment::makecallback_depth() const {
226 -return makecallback_cntr_;
231 +inline void Environment::PopAsyncCallbackScope() {
232 +async_callback_scope_depth_--;
227 233 }
228 234
229 235 inline Environment::ImmediateInfo::ImmediateInfo(v8::Isolate* isolate)
Original file line number Diff line number Diff line change
@@ -607,24 +607,26 @@ class AsyncHooks {
607 607 void grow_async_ids_stack();
608 608 };
609 609
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 +
610 622 class Environment {
611 623 public:
612 624 Environment(const Environment&) = delete;
613 625 Environment& operator=(const Environment&) = delete;
614 626
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();
628 630
629 631 class ImmediateInfo {
630 632 public:
@@ -1082,7 +1084,7 @@ class Environment {
1082 1084 bool printed_error_ = false;
1083 1085 bool emit_env_nonstring_warning_ = true;
1084 1086 bool emit_err_name_warning_ = true;
1085 -size_t makecallback_cntr_ = 0;
1087 +size_t async_callback_scope_depth_ = 0;
1086 1088 std::vector<double> destroy_async_id_list_;
1087 1089
1088 1090 std::shared_ptr options_;
Original file line number Diff line number Diff line change
@@ -807,7 +807,7 @@ inline int StartNodeWithIsolate(Isolate* isolate,
807 807 #endif // HAVE_INSPECTOR && NODE_USE_V8_PLATFORM
808 808
809 809 {
810 -Environment::AsyncCallbackScope callback_scope(&env);
810 + AsyncCallbackScope callback_scope(&env);
811 811 env.async_hooks()->push_async_ids(1, 0);
812 812 LoadEnvironment(&env);
813 813 env.async_hooks()->pop_async_id(1);
Original file line number Diff line number Diff line change
@@ -323,7 +323,7 @@ class Parser : public AsyncWrap, public StreamListener {
323 323
324 324 argv[A_UPGRADE] = Boolean::New(env()->isolate(), parser_.upgrade);
325 325
326 -Environment::AsyncCallbackScope callback_scope(env());
326 + AsyncCallbackScope callback_scope(env());
327 327
328 328 MaybeLocal head_response =
329 329 MakeCallback(cb.As(), arraysize(argv), argv);
@@ -394,7 +394,7 @@ class Parser : public AsyncWrap, public StreamListener {
394 394 if (!cb->IsFunction())
395 395 return 0;
396 396
397 -Environment::AsyncCallbackScope callback_scope(env());
397 + AsyncCallbackScope callback_scope(env());
398 398
399 399 MaybeLocal r = MakeCallback(cb.As(), 0, nullptr);
400 400
Original file line number Diff line number Diff line change
@@ -211,7 +211,7 @@ class InternalCallbackScope {
211 211 Environment* env_;
212 212 async_context async_context_;
213 213 v8::Localv8::Object object_;
214 -Environment::AsyncCallbackScope callback_scope_;
214 + AsyncCallbackScope callback_scope_;
215 215 bool failed_ = false;
216 216 bool pushed_ids_ = false;
217 217 bool closed_ = false;
Original file line number Diff line number Diff line change
@@ -288,7 +288,7 @@ void Worker::Run() {
288 288 inspector_started = true;
289 289
290 290 HandleScope handle_scope(isolate_);
291 -Environment::AsyncCallbackScope callback_scope(env_.get());
291 + AsyncCallbackScope callback_scope(env_.get());
292 292 env_->async_hooks()->push_async_ids(1, 0);
293 293 if (!RunBootstrapping(env_.get()).IsEmpty()) {
294 294 USE(StartExecution(env_.get(), "internal/main/worker_thread"));