Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: refactor Environment::GetCurrent(isolate) usage #26376

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ using v8::MaybeLocal;
using v8::Message;
using v8::MicrotasksPolicy;
using v8::ObjectTemplate;
using v8::SealHandleScope;
using v8::String;
using v8::Value;

Expand All @@ -34,7 +35,9 @@ static bool AllowWasmCodeGenerationCallback(Local<Context> context,
}

static bool ShouldAbortOnUncaughtException(Isolate* isolate) {
HandleScope scope(isolate);
#ifdef DEBUG
SealHandleScope scope(isolate);
#endif
Environment* env = Environment::GetCurrent(isolate);
return env != nullptr &&
(env->is_main_thread() || !env->is_stopping_worker()) &&
Expand Down
3 changes: 3 additions & 0 deletions src/api/exceptions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Local<Value> ErrnoException(Isolate* isolate,
const char* msg,
const char* path) {
Environment* env = Environment::GetCurrent(isolate);
CHECK_NOT_NULL(env);

Local<Value> e;
Local<String> estring = OneByteString(isolate, errors::errno_string(errorno));
Expand Down Expand Up @@ -99,6 +100,7 @@ Local<Value> UVException(Isolate* isolate,
const char* path,
const char* dest) {
Environment* env = Environment::GetCurrent(isolate);
CHECK_NOT_NULL(env);

if (!msg || !msg[0])
msg = uv_strerror(errorno);
Expand Down Expand Up @@ -187,6 +189,7 @@ Local<Value> WinapiErrnoException(Isolate* isolate,
const char* msg,
const char* path) {
Environment* env = Environment::GetCurrent(isolate);
CHECK_NOT_NULL(env);
Local<Value> e;
bool must_free = false;
if (!msg || !msg[0]) {
Expand Down
11 changes: 4 additions & 7 deletions src/api/hooks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ using v8::Integer;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::SealHandleScope;
using v8::String;
using v8::Value;
using v8::NewStringType;
Expand Down Expand Up @@ -88,16 +89,12 @@ void RemoveEnvironmentCleanupHook(Isolate* isolate,
}

async_id AsyncHooksGetExecutionAsyncId(Isolate* isolate) {
// Environment::GetCurrent() allocates a Local<> handle.
HandleScope handle_scope(isolate);
Environment* env = Environment::GetCurrent(isolate);
if (env == nullptr) return -1;
return env->execution_async_id();
}

async_id AsyncHooksGetTriggerAsyncId(Isolate* isolate) {
// Environment::GetCurrent() allocates a Local<> handle.
HandleScope handle_scope(isolate);
Environment* env = Environment::GetCurrent(isolate);
if (env == nullptr) return -1;
return env->trigger_async_id();
Expand All @@ -119,7 +116,9 @@ async_context EmitAsyncInit(Isolate* isolate,
Local<Object> resource,
Local<String> name,
async_id trigger_async_id) {
HandleScope handle_scope(isolate);
#ifdef DEBUG
SealHandleScope handle_scope(isolate);
#endif
Environment* env = Environment::GetCurrent(isolate);
CHECK_NOT_NULL(env);

Expand All @@ -140,8 +139,6 @@ async_context EmitAsyncInit(Isolate* isolate,
}

void EmitAsyncDestroy(Isolate* isolate, async_context asyncContext) {
// Environment::GetCurrent() allocates a Local<> handle.
HandleScope handle_scope(isolate);
AsyncWrap::EmitDestroy(
Environment::GetCurrent(isolate), asyncContext.async_id);
}
Expand Down
2 changes: 2 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ inline void Environment::AssignToContext(v8::Local<v8::Context> context,
}

inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
if (UNLIKELY(!isolate->InContext())) return nullptr;
v8::HandleScope handle_scope(isolate);
addaleax marked this conversation as resolved.
Show resolved Hide resolved
return GetCurrent(isolate->GetCurrentContext());
}

Expand Down
8 changes: 8 additions & 0 deletions src/inspector/main_thread_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ void MainThreadInterface::DispatchMessages() {
MessageQueue::value_type task;
std::swap(dispatching_message_queue_.front(), task);
dispatching_message_queue_.pop_front();

// TODO(addaleax): The V8 inspector code currently sometimes allocates
// handles that leak to the outside scope, rendering a HandleScope here
// necessary. This handle scope can be removed/turned into a
// SealHandleScope once/if
// https://chromium-review.googlesource.com/c/v8/v8/+/1484304 makes it
// into our copy of V8, maybe guarded with #ifdef DEBUG if we want.
v8::HandleScope handle_scope(isolate_);
task->Call(this);
}
} while (had_messages);
Expand Down
1 change: 0 additions & 1 deletion src/node_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ void OnFatalError(const char* location, const char* message) {
}
#ifdef NODE_REPORT
Isolate* isolate = Isolate::GetCurrent();
HandleScope handle_scope(isolate);
Environment* env = Environment::GetCurrent(isolate);
if (env == nullptr || env->isolate_data()->options()->report_on_fatalerror) {
report::TriggerNodeReport(
Expand Down
5 changes: 2 additions & 3 deletions src/node_perf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,9 @@ inline Local<Value> GetName(Local<Function> fn) {
// execution.
void TimerFunctionCall(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
Environment* env = Environment::GetCurrent(isolate);
Local<Context> context = isolate->GetCurrentContext();
Environment* env = Environment::GetCurrent(context);
CHECK_NOT_NULL(env);
Local<Context> context = env->context();
Local<Function> fn = args.Data().As<Function>();
size_t count = args.Length();
size_t idx;
Expand Down
16 changes: 11 additions & 5 deletions src/node_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

namespace node {

using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::Platform;
using v8::SealHandleScope;
using v8::Task;
using node::tracing::TracingController;

Expand Down Expand Up @@ -332,11 +332,17 @@ int NodePlatform::NumberOfWorkerThreads() {

void PerIsolatePlatformData::RunForegroundTask(std::unique_ptr<Task> task) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
#ifdef DEBUG
SealHandleScope scope(isolate);
#endif
Environment* env = Environment::GetCurrent(isolate);
InternalCallbackScope cb_scope(env, Local<Object>(), { 0, 0 },
InternalCallbackScope::kAllowEmptyResource);
task->Run();
if (env != nullptr) {
InternalCallbackScope cb_scope(env, Local<Object>(), { 0, 0 },
InternalCallbackScope::kAllowEmptyResource);
task->Run();
} else {
task->Run();
addaleax marked this conversation as resolved.
Show resolved Hide resolved
}
}

void PerIsolatePlatformData::DeleteFromScheduledTasks(DelayedTask* task) {
Expand Down