Skip to content

Commit 8a2ce5d

Browse files
joyeecheungtargos
authored andcommitted
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. PR-URL: #39112 Refs: #38905 Refs: #35711 Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 3ecfe9d commit 8a2ce5d

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

src/env.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,8 @@ constexpr size_t kFsStatsBufferLength =
479479
V(internal_binding_loader, v8::Function) \
480480
V(immediate_callback_function, v8::Function) \
481481
V(inspector_console_extension_installer, v8::Function) \
482+
V(inspector_disable_async_hooks, v8::Function) \
483+
V(inspector_enable_async_hooks, v8::Function) \
482484
V(messaging_deserialize_create_object, v8::Function) \
483485
V(message_port, v8::Object) \
484486
V(native_module_require, v8::Function) \

src/inspector_agent.cc

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ using node::FatalError;
4040

4141
using v8::Context;
4242
using v8::Function;
43-
using v8::Global;
4443
using v8::HandleScope;
4544
using v8::Isolate;
4645
using v8::Local;
@@ -804,8 +803,8 @@ void Agent::PauseOnNextJavascriptStatement(const std::string& reason) {
804803
void Agent::RegisterAsyncHook(Isolate* isolate,
805804
Local<Function> enable_function,
806805
Local<Function> disable_function) {
807-
enable_async_hook_function_.Reset(isolate, enable_function);
808-
disable_async_hook_function_.Reset(isolate, disable_function);
806+
parent_env_->set_inspector_enable_async_hooks(enable_function);
807+
parent_env_->set_inspector_disable_async_hooks(disable_function);
809808
if (pending_enable_async_hook_) {
810809
CHECK(!pending_disable_async_hook_);
811810
pending_enable_async_hook_ = false;
@@ -818,8 +817,10 @@ void Agent::RegisterAsyncHook(Isolate* isolate,
818817
}
819818

820819
void Agent::EnableAsyncHook() {
821-
if (!enable_async_hook_function_.IsEmpty()) {
822-
ToggleAsyncHook(parent_env_->isolate(), enable_async_hook_function_);
820+
HandleScope scope(parent_env_->isolate());
821+
Local<Function> enable = parent_env_->inspector_enable_async_hooks();
822+
if (!enable.IsEmpty()) {
823+
ToggleAsyncHook(parent_env_->isolate(), enable);
823824
} else if (pending_disable_async_hook_) {
824825
CHECK(!pending_enable_async_hook_);
825826
pending_disable_async_hook_ = false;
@@ -829,8 +830,10 @@ void Agent::EnableAsyncHook() {
829830
}
830831

831832
void Agent::DisableAsyncHook() {
832-
if (!disable_async_hook_function_.IsEmpty()) {
833-
ToggleAsyncHook(parent_env_->isolate(), disable_async_hook_function_);
833+
HandleScope scope(parent_env_->isolate());
834+
Local<Function> disable = parent_env_->inspector_enable_async_hooks();
835+
if (!disable.IsEmpty()) {
836+
ToggleAsyncHook(parent_env_->isolate(), disable);
834837
} else if (pending_enable_async_hook_) {
835838
CHECK(!pending_disable_async_hook_);
836839
pending_enable_async_hook_ = false;
@@ -839,8 +842,7 @@ void Agent::DisableAsyncHook() {
839842
}
840843
}
841844

842-
void Agent::ToggleAsyncHook(Isolate* isolate,
843-
const Global<Function>& fn) {
845+
void Agent::ToggleAsyncHook(Isolate* isolate, Local<Function> fn) {
844846
// Guard against running this during cleanup -- no async events will be
845847
// emitted anyway at that point anymore, and calling into JS is not possible.
846848
// This should probably not be something we're attempting in the first place,
@@ -851,7 +853,7 @@ void Agent::ToggleAsyncHook(Isolate* isolate,
851853
CHECK(!fn.IsEmpty());
852854
auto context = parent_env_->context();
853855
v8::TryCatch try_catch(isolate);
854-
USE(fn.Get(isolate)->Call(context, Undefined(isolate), 0, nullptr));
856+
USE(fn->Call(context, Undefined(isolate), 0, nullptr));
855857
if (try_catch.HasCaught() && !try_catch.HasTerminated()) {
856858
PrintCaughtException(isolate, context, try_catch);
857859
FatalError("\nnode::inspector::Agent::ToggleAsyncHook",

src/inspector_agent.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ class Agent {
117117
inline Environment* env() const { return parent_env_; }
118118

119119
private:
120-
void ToggleAsyncHook(v8::Isolate* isolate,
121-
const v8::Global<v8::Function>& fn);
120+
void ToggleAsyncHook(v8::Isolate* isolate, v8::Local<v8::Function> fn);
122121

123122
node::Environment* parent_env_;
124123
// Encapsulates majority of the Inspector functionality
@@ -137,8 +136,6 @@ class Agent {
137136

138137
bool pending_enable_async_hook_ = false;
139138
bool pending_disable_async_hook_ = false;
140-
v8::Global<v8::Function> enable_async_hook_function_;
141-
v8::Global<v8::Function> disable_async_hook_function_;
142139
};
143140

144141
} // namespace inspector

0 commit comments

Comments
 (0)