Skip to content

Commit 29194d4

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 <[email protected]>
1 parent 35b6669 commit 29194d4

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

Diff for: src/env.h

+2
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,8 @@ constexpr size_t kFsStatsBufferLength =
520520
V(internal_binding_loader, v8::Function) \
521521
V(immediate_callback_function, v8::Function) \
522522
V(inspector_console_extension_installer, v8::Function) \
523+
V(inspector_disable_async_hooks, v8::Function) \
524+
V(inspector_enable_async_hooks, v8::Function) \
523525
V(messaging_deserialize_create_object, v8::Function) \
524526
V(message_port, v8::Object) \
525527
V(native_module_require, v8::Function) \

Diff for: src/inspector_agent.cc

+12-10
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;
@@ -802,8 +801,8 @@ void Agent::PauseOnNextJavascriptStatement(const std::string& reason) {
802801
void Agent::RegisterAsyncHook(Isolate* isolate,
803802
Local<Function> enable_function,
804803
Local<Function> disable_function) {
805-
enable_async_hook_function_.Reset(isolate, enable_function);
806-
disable_async_hook_function_.Reset(isolate, disable_function);
804+
parent_env_->set_inspector_enable_async_hooks(enable_function);
805+
parent_env_->set_inspector_disable_async_hooks(disable_function);
807806
if (pending_enable_async_hook_) {
808807
CHECK(!pending_disable_async_hook_);
809808
pending_enable_async_hook_ = false;
@@ -816,8 +815,10 @@ void Agent::RegisterAsyncHook(Isolate* isolate,
816815
}
817816

818817
void Agent::EnableAsyncHook() {
819-
if (!enable_async_hook_function_.IsEmpty()) {
820-
ToggleAsyncHook(parent_env_->isolate(), enable_async_hook_function_);
818+
HandleScope scope(parent_env_->isolate());
819+
Local<Function> enable = parent_env_->inspector_enable_async_hooks();
820+
if (!enable.IsEmpty()) {
821+
ToggleAsyncHook(parent_env_->isolate(), enable);
821822
} else if (pending_disable_async_hook_) {
822823
CHECK(!pending_enable_async_hook_);
823824
pending_disable_async_hook_ = false;
@@ -827,8 +828,10 @@ void Agent::EnableAsyncHook() {
827828
}
828829

829830
void Agent::DisableAsyncHook() {
830-
if (!disable_async_hook_function_.IsEmpty()) {
831-
ToggleAsyncHook(parent_env_->isolate(), disable_async_hook_function_);
831+
HandleScope scope(parent_env_->isolate());
832+
Local<Function> disable = parent_env_->inspector_enable_async_hooks();
833+
if (!disable.IsEmpty()) {
834+
ToggleAsyncHook(parent_env_->isolate(), disable);
832835
} else if (pending_enable_async_hook_) {
833836
CHECK(!pending_disable_async_hook_);
834837
pending_enable_async_hook_ = false;
@@ -837,8 +840,7 @@ void Agent::DisableAsyncHook() {
837840
}
838841
}
839842

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

Diff for: src/inspector_agent.h

+1-4
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)