Skip to content

Commit

Permalink
src: refactor Environment::GetCurrent(isolate) usage
Browse files Browse the repository at this point in the history
Do not require an explicit `HandleScope`, or the ability to create
one, when using `Environment::GetCurrent()`.

`isolate->InContext()` is used as an indicator that it is probably
okay to create a `HandleScope`, see also the short discussion in
nodejs#25775 (review).
  • Loading branch information
addaleax committed Mar 1, 2019
1 parent 410eb97 commit 3509d74
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
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);
return GetCurrent(isolate->GetCurrentContext());
}

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

0 comments on commit 3509d74

Please sign in to comment.