From edc4af0e7d0d3da8cb1c45d360ae3bcbda4a8a8c Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 5 Mar 2019 23:03:24 +0100 Subject: [PATCH] src: merge debug-only `SealHandleScope`s Instead of repeating the same `#ifdef DEBUG` + `SealHandleScope` pattern over and over, create an utility that does this for us. PR-URL: https://github.com/nodejs/node/pull/26459 Reviewed-By: Ben Noordhuis Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig Reviewed-By: Joyee Cheung --- src/api/environment.cc | 5 +---- src/api/hooks.cc | 7 ++----- src/env.cc | 4 +--- src/node_internals.h | 14 ++++++++++++++ src/node_platform.cc | 5 +---- src/stream_base-inl.h | 20 +++++--------------- 6 files changed, 24 insertions(+), 31 deletions(-) diff --git a/src/api/environment.cc b/src/api/environment.cc index 27e580133505a2..2e2bf9027804a2 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -23,7 +23,6 @@ using v8::MaybeLocal; using v8::Message; using v8::MicrotasksPolicy; using v8::ObjectTemplate; -using v8::SealHandleScope; using v8::String; using v8::Value; @@ -35,9 +34,7 @@ static bool AllowWasmCodeGenerationCallback(Local context, } static bool ShouldAbortOnUncaughtException(Isolate* isolate) { -#ifdef DEBUG - SealHandleScope scope(isolate); -#endif + DebugSealHandleScope scope(isolate); Environment* env = Environment::GetCurrent(isolate); return env != nullptr && (env->is_main_thread() || !env->is_stopping_worker()) && diff --git a/src/api/hooks.cc b/src/api/hooks.cc index aa647778d51537..4663df43a659af 100644 --- a/src/api/hooks.cc +++ b/src/api/hooks.cc @@ -1,5 +1,5 @@ #include "env-inl.h" -#include "node.h" +#include "node_internals.h" #include "node_process.h" #include "async_wrap.h" @@ -11,7 +11,6 @@ using v8::Integer; using v8::Isolate; using v8::Local; using v8::Object; -using v8::SealHandleScope; using v8::String; using v8::Value; using v8::NewStringType; @@ -116,9 +115,7 @@ async_context EmitAsyncInit(Isolate* isolate, Local resource, Local name, async_id trigger_async_id) { -#ifdef DEBUG - SealHandleScope handle_scope(isolate); -#endif + DebugSealHandleScope handle_scope(isolate); Environment* env = Environment::GetCurrent(isolate); CHECK_NOT_NULL(env); diff --git a/src/env.cc b/src/env.cc index bd456691799114..ac4266ab15ecab 100644 --- a/src/env.cc +++ b/src/env.cc @@ -644,9 +644,7 @@ void Environment::RunAndClearNativeImmediates() { auto drain_list = [&]() { TryCatchScope try_catch(this); for (auto it = list.begin(); it != list.end(); ++it) { -#ifdef DEBUG - v8::SealHandleScope seal_handle_scope(isolate()); -#endif + DebugSealHandleScope seal_handle_scope(isolate()); it->cb_(this, it->data_); if (it->refed_) ref_count++; diff --git a/src/node_internals.h b/src/node_internals.h index 166cfd9ea23615..212da7af28107c 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -217,6 +217,20 @@ class InternalCallbackScope { bool closed_ = false; }; +class DebugSealHandleScope { + public: + explicit inline DebugSealHandleScope(v8::Isolate* isolate) +#ifdef DEBUG + : actual_scope_(isolate) +#endif + {} + + private: +#ifdef DEBUG + v8::SealHandleScope actual_scope_; +#endif +}; + class ThreadPoolWork { public: explicit inline ThreadPoolWork(Environment* env) : env_(env) { diff --git a/src/node_platform.cc b/src/node_platform.cc index 115b59e356cf46..b96d5d3a1a8d70 100644 --- a/src/node_platform.cc +++ b/src/node_platform.cc @@ -12,7 +12,6 @@ using v8::Isolate; using v8::Local; using v8::Object; using v8::Platform; -using v8::SealHandleScope; using v8::Task; using node::tracing::TracingController; @@ -332,9 +331,7 @@ int NodePlatform::NumberOfWorkerThreads() { void PerIsolatePlatformData::RunForegroundTask(std::unique_ptr task) { Isolate* isolate = Isolate::GetCurrent(); -#ifdef DEBUG - SealHandleScope scope(isolate); -#endif + DebugSealHandleScope scope(isolate); Environment* env = Environment::GetCurrent(isolate); if (env != nullptr) { InternalCallbackScope cb_scope(env, Local(), { 0, 0 }, diff --git a/src/stream_base-inl.h b/src/stream_base-inl.h index f9a67872f90d77..368dcafe1c3291 100644 --- a/src/stream_base-inl.h +++ b/src/stream_base-inl.h @@ -113,39 +113,29 @@ inline void StreamResource::RemoveStreamListener(StreamListener* listener) { } inline uv_buf_t StreamResource::EmitAlloc(size_t suggested_size) { -#ifdef DEBUG - v8::SealHandleScope handle_scope(v8::Isolate::GetCurrent()); -#endif + DebugSealHandleScope handle_scope(v8::Isolate::GetCurrent()); return listener_->OnStreamAlloc(suggested_size); } inline void StreamResource::EmitRead(ssize_t nread, const uv_buf_t& buf) { -#ifdef DEBUG - v8::SealHandleScope handle_scope(v8::Isolate::GetCurrent()); -#endif + DebugSealHandleScope handle_scope(v8::Isolate::GetCurrent()); if (nread > 0) bytes_read_ += static_cast(nread); listener_->OnStreamRead(nread, buf); } inline void StreamResource::EmitAfterWrite(WriteWrap* w, int status) { -#ifdef DEBUG - v8::SealHandleScope handle_scope(v8::Isolate::GetCurrent()); -#endif + DebugSealHandleScope handle_scope(v8::Isolate::GetCurrent()); listener_->OnStreamAfterWrite(w, status); } inline void StreamResource::EmitAfterShutdown(ShutdownWrap* w, int status) { -#ifdef DEBUG - v8::SealHandleScope handle_scope(v8::Isolate::GetCurrent()); -#endif + DebugSealHandleScope handle_scope(v8::Isolate::GetCurrent()); listener_->OnStreamAfterShutdown(w, status); } inline void StreamResource::EmitWantsWrite(size_t suggested_size) { -#ifdef DEBUG - v8::SealHandleScope handle_scope(v8::Isolate::GetCurrent()); -#endif + DebugSealHandleScope handle_scope(v8::Isolate::GetCurrent()); listener_->OnStreamWantsWrite(suggested_size); }