From 59e23b502f4ce1dafafdb2e1c2a2a72923dac788 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 22 Jun 2021 04:04:30 +0800 Subject: [PATCH] inspector: move inspector async hooks to environment Since async hooks are per-environment and putting them in the environment allows us to serialize them for the snapshot automatically. --- src/env.h | 2 ++ src/inspector_agent.cc | 21 ++++++++++++--------- src/inspector_agent.h | 5 +---- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/env.h b/src/env.h index 1dae1f710f8377f..dc03fcf55b2b07e 100644 --- a/src/env.h +++ b/src/env.h @@ -519,6 +519,8 @@ constexpr size_t kFsStatsBufferLength = V(internal_binding_loader, v8::Function) \ V(immediate_callback_function, v8::Function) \ V(inspector_console_extension_installer, v8::Function) \ + V(inspector_disable_async_hooks, v8::Function) \ + V(inspector_enable_async_hooks, v8::Function) \ V(messaging_deserialize_create_object, v8::Function) \ V(message_port, v8::Object) \ V(native_module_require, v8::Function) \ diff --git a/src/inspector_agent.cc b/src/inspector_agent.cc index a3c901dca2eafcb..eb6bb160f0631fa 100644 --- a/src/inspector_agent.cc +++ b/src/inspector_agent.cc @@ -802,8 +802,8 @@ void Agent::PauseOnNextJavascriptStatement(const std::string& reason) { void Agent::RegisterAsyncHook(Isolate* isolate, Local enable_function, Local disable_function) { - enable_async_hook_function_.Reset(isolate, enable_function); - disable_async_hook_function_.Reset(isolate, disable_function); + parent_env_->set_inspector_enable_async_hooks(enable_function); + parent_env_->set_inspector_disable_async_hooks(disable_function); if (pending_enable_async_hook_) { CHECK(!pending_disable_async_hook_); pending_enable_async_hook_ = false; @@ -816,8 +816,10 @@ void Agent::RegisterAsyncHook(Isolate* isolate, } void Agent::EnableAsyncHook() { - if (!enable_async_hook_function_.IsEmpty()) { - ToggleAsyncHook(parent_env_->isolate(), enable_async_hook_function_); + HandleScope scope(parent_env_->isolate()); + Local enable = parent_env_->inspector_enable_async_hooks(); + if (!enable.IsEmpty()) { + ToggleAsyncHook(parent_env_->isolate(), enable); } else if (pending_disable_async_hook_) { CHECK(!pending_enable_async_hook_); pending_disable_async_hook_ = false; @@ -827,8 +829,10 @@ void Agent::EnableAsyncHook() { } void Agent::DisableAsyncHook() { - if (!disable_async_hook_function_.IsEmpty()) { - ToggleAsyncHook(parent_env_->isolate(), disable_async_hook_function_); + HandleScope scope(parent_env_->isolate()); + Local disable = parent_env_->inspector_enable_async_hooks(); + if (!disable.IsEmpty()) { + ToggleAsyncHook(parent_env_->isolate(), disable); } else if (pending_enable_async_hook_) { CHECK(!pending_disable_async_hook_); pending_enable_async_hook_ = false; @@ -837,8 +841,7 @@ void Agent::DisableAsyncHook() { } } -void Agent::ToggleAsyncHook(Isolate* isolate, - const Global& fn) { +void Agent::ToggleAsyncHook(Isolate* isolate, const Local& fn) { // Guard against running this during cleanup -- no async events will be // emitted anyway at that point anymore, and calling into JS is not possible. // This should probably not be something we're attempting in the first place, @@ -849,7 +852,7 @@ void Agent::ToggleAsyncHook(Isolate* isolate, CHECK(!fn.IsEmpty()); auto context = parent_env_->context(); v8::TryCatch try_catch(isolate); - USE(fn.Get(isolate)->Call(context, Undefined(isolate), 0, nullptr)); + USE(fn->Call(context, Undefined(isolate), 0, nullptr)); if (try_catch.HasCaught() && !try_catch.HasTerminated()) { PrintCaughtException(isolate, context, try_catch); FatalError("\nnode::inspector::Agent::ToggleAsyncHook", diff --git a/src/inspector_agent.h b/src/inspector_agent.h index 1c8d496ba27a9e5..5b9c157dabb0f1e 100644 --- a/src/inspector_agent.h +++ b/src/inspector_agent.h @@ -117,8 +117,7 @@ class Agent { inline Environment* env() const { return parent_env_; } private: - void ToggleAsyncHook(v8::Isolate* isolate, - const v8::Global& fn); + void ToggleAsyncHook(v8::Isolate* isolate, const v8::Local& fn); node::Environment* parent_env_; // Encapsulates majority of the Inspector functionality @@ -137,8 +136,6 @@ class Agent { bool pending_enable_async_hook_ = false; bool pending_disable_async_hook_ = false; - v8::Global enable_async_hook_function_; - v8::Global disable_async_hook_function_; }; } // namespace inspector