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: https://github.com/nodejs/node/pull/39112
Refs: https://github.com/nodejs/node/pull/38905
Refs: https://github.com/nodejs/node/issues/35711
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Joyee Cheung 2021-06-22 04:04:30 +08:00
parent 320339630d
commit 1544e69b93
No known key found for this signature in database
GPG key ID: 92B78A53C8303B8D
3 changed files with 15 additions and 14 deletions

View file

@ -520,6 +520,8 @@ constexpr size_t kFsStatsBufferLength =
V(internal_binding_loader, v8::Function) \ V(internal_binding_loader, v8::Function) \
V(immediate_callback_function, v8::Function) \ V(immediate_callback_function, v8::Function) \
V(inspector_console_extension_installer, 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(messaging_deserialize_create_object, v8::Function) \
V(message_port, v8::Object) \ V(message_port, v8::Object) \
V(native_module_require, v8::Function) \ V(native_module_require, v8::Function) \

View file

@ -40,7 +40,6 @@ using node::FatalError;
using v8::Context; using v8::Context;
using v8::Function; using v8::Function;
using v8::Global;
using v8::HandleScope; using v8::HandleScope;
using v8::Isolate; using v8::Isolate;
using v8::Local; using v8::Local;
@ -802,8 +801,8 @@ void Agent::PauseOnNextJavascriptStatement(const std::string& reason) {
void Agent::RegisterAsyncHook(Isolate* isolate, void Agent::RegisterAsyncHook(Isolate* isolate,
Local<Function> enable_function, Local<Function> enable_function,
Local<Function> disable_function) { Local<Function> disable_function) {
enable_async_hook_function_.Reset(isolate, enable_function); parent_env_->set_inspector_enable_async_hooks(enable_function);
disable_async_hook_function_.Reset(isolate, disable_function); parent_env_->set_inspector_disable_async_hooks(disable_function);
if (pending_enable_async_hook_) { if (pending_enable_async_hook_) {
CHECK(!pending_disable_async_hook_); CHECK(!pending_disable_async_hook_);
pending_enable_async_hook_ = false; pending_enable_async_hook_ = false;
@ -816,8 +815,10 @@ void Agent::RegisterAsyncHook(Isolate* isolate,
} }
void Agent::EnableAsyncHook() { void Agent::EnableAsyncHook() {
if (!enable_async_hook_function_.IsEmpty()) { HandleScope scope(parent_env_->isolate());
ToggleAsyncHook(parent_env_->isolate(), enable_async_hook_function_); Local<Function> enable = parent_env_->inspector_enable_async_hooks();
if (!enable.IsEmpty()) {
ToggleAsyncHook(parent_env_->isolate(), enable);
} else if (pending_disable_async_hook_) { } else if (pending_disable_async_hook_) {
CHECK(!pending_enable_async_hook_); CHECK(!pending_enable_async_hook_);
pending_disable_async_hook_ = false; pending_disable_async_hook_ = false;
@ -827,8 +828,10 @@ void Agent::EnableAsyncHook() {
} }
void Agent::DisableAsyncHook() { void Agent::DisableAsyncHook() {
if (!disable_async_hook_function_.IsEmpty()) { HandleScope scope(parent_env_->isolate());
ToggleAsyncHook(parent_env_->isolate(), disable_async_hook_function_); Local<Function> disable = parent_env_->inspector_enable_async_hooks();
if (!disable.IsEmpty()) {
ToggleAsyncHook(parent_env_->isolate(), disable);
} else if (pending_enable_async_hook_) { } else if (pending_enable_async_hook_) {
CHECK(!pending_disable_async_hook_); CHECK(!pending_disable_async_hook_);
pending_enable_async_hook_ = false; pending_enable_async_hook_ = false;
@ -837,8 +840,7 @@ void Agent::DisableAsyncHook() {
} }
} }
void Agent::ToggleAsyncHook(Isolate* isolate, void Agent::ToggleAsyncHook(Isolate* isolate, Local<Function> fn) {
const Global<Function>& fn) {
// Guard against running this during cleanup -- no async events will be // Guard against running this during cleanup -- no async events will be
// emitted anyway at that point anymore, and calling into JS is not possible. // 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, // This should probably not be something we're attempting in the first place,
@ -849,7 +851,7 @@ void Agent::ToggleAsyncHook(Isolate* isolate,
CHECK(!fn.IsEmpty()); CHECK(!fn.IsEmpty());
auto context = parent_env_->context(); auto context = parent_env_->context();
v8::TryCatch try_catch(isolate); 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()) { if (try_catch.HasCaught() && !try_catch.HasTerminated()) {
PrintCaughtException(isolate, context, try_catch); PrintCaughtException(isolate, context, try_catch);
FatalError("\nnode::inspector::Agent::ToggleAsyncHook", FatalError("\nnode::inspector::Agent::ToggleAsyncHook",

View file

@ -117,8 +117,7 @@ class Agent {
inline Environment* env() const { return parent_env_; } inline Environment* env() const { return parent_env_; }
private: private:
void ToggleAsyncHook(v8::Isolate* isolate, void ToggleAsyncHook(v8::Isolate* isolate, v8::Local<v8::Function> fn);
const v8::Global<v8::Function>& fn);
node::Environment* parent_env_; node::Environment* parent_env_;
// Encapsulates majority of the Inspector functionality // Encapsulates majority of the Inspector functionality
@ -137,8 +136,6 @@ class Agent {
bool pending_enable_async_hook_ = false; bool pending_enable_async_hook_ = false;
bool pending_disable_async_hook_ = false; bool pending_disable_async_hook_ = false;
v8::Global<v8::Function> enable_async_hook_function_;
v8::Global<v8::Function> disable_async_hook_function_;
}; };
} // namespace inspector } // namespace inspector