Skip to content

src: remove Environment::GetCurrent(isolate) #58311

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,6 @@ Typical ways of accessing the current `Environment` in the Node.js code are:
This requires that `context` has been associated with the `Environment`
instance, e.g. is the main `Context` for the `Environment` or one of its
`vm.Context`s.
* Given an [`Isolate`][], using `Environment::GetCurrent(isolate)`. This looks
up the current [`Context`][] and then uses that.

<a id="realm"></a>

Expand Down
2 changes: 1 addition & 1 deletion src/api/async_resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ AsyncResource::AsyncResource(Isolate* isolate,
Local<Object> resource,
const char* name,
async_id trigger_async_id)
: env_(Environment::GetCurrent(isolate)),
: env_(Environment::GetCurrent(isolate->GetCurrentContext())),
resource_(isolate, resource),
context_frame_(isolate, async_context_frame::current(isolate)) {
CHECK_NOT_NULL(env_);
Expand Down
7 changes: 4 additions & 3 deletions src/api/callback.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ using v8::Value;
CallbackScope::CallbackScope(Isolate* isolate,
Local<Object> object,
async_context async_context)
: CallbackScope(Environment::GetCurrent(isolate), object, async_context) {}
: CallbackScope(Environment::GetCurrent(isolate->GetCurrentContext()),
object,
async_context) {}

CallbackScope::CallbackScope(Environment* env,
Local<Object> object,
Expand Down Expand Up @@ -76,9 +78,8 @@ InternalCallbackScope::InternalCallbackScope(Environment* env,
// likely *are* the same, in which case we can skip the slightly more
// expensive Environment::GetCurrent() call.
if (env->context() != current_context) [[unlikely]] {
CHECK_EQ(Environment::GetCurrent(isolate), env);
CHECK_EQ(Environment::GetCurrent(current_context), env);
}

isolate->SetIdle(false);

prior_context_frame_.Reset(
Expand Down
5 changes: 2 additions & 3 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ bool AllowWasmCodeGenerationCallback(Local<Context> context,

bool ShouldAbortOnUncaughtException(Isolate* isolate) {
DebugSealHandleScope scope(isolate);
Environment* env = Environment::GetCurrent(isolate);
return env != nullptr &&
(env->is_main_thread() || !env->is_stopping()) &&
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
return env != nullptr && (env->is_main_thread() || !env->is_stopping()) &&
env->abort_on_uncaught_exception() &&
env->should_abort_on_uncaught_toggle()[0] &&
!env->inside_should_not_abort_on_uncaught_scope();
Expand Down
4 changes: 2 additions & 2 deletions src/api/exceptions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Local<Value> ErrnoException(Isolate* isolate,
const char* syscall,
const char* msg,
const char* path) {
Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
CHECK_NOT_NULL(env);

Local<Value> e;
Expand Down Expand Up @@ -94,7 +94,7 @@ Local<Value> UVException(Isolate* isolate,
const char* msg,
const char* path,
const char* dest) {
Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
CHECK_NOT_NULL(env);

if (!msg || !msg[0])
Expand Down
29 changes: 13 additions & 16 deletions src/api/hooks.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "async_wrap.h"
#include "env-inl.h"
#include "node_internals.h"
#include "node_process-inl.h"
#include "async_wrap.h"

namespace node {

Expand Down Expand Up @@ -127,18 +127,16 @@ struct ACHHandle final {
// this.
void DeleteACHHandle::operator ()(ACHHandle* handle) const { delete handle; }

void AddEnvironmentCleanupHook(Isolate* isolate,
CleanupHook fun,
void* arg) {
Environment* env = Environment::GetCurrent(isolate);
void AddEnvironmentCleanupHook(Isolate* isolate, CleanupHook fun, void* arg) {
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
CHECK_NOT_NULL(env);
env->AddCleanupHook(fun, arg);
}

void RemoveEnvironmentCleanupHook(Isolate* isolate,
CleanupHook fun,
void* arg) {
Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
CHECK_NOT_NULL(env);
env->RemoveCleanupHook(fun, arg);
}
Expand All @@ -158,11 +156,10 @@ static void RunAsyncCleanupHook(void* arg) {
info->fun(info->arg, FinishAsyncCleanupHook, info);
}

ACHHandle* AddEnvironmentCleanupHookInternal(
Isolate* isolate,
AsyncCleanupHook fun,
void* arg) {
Environment* env = Environment::GetCurrent(isolate);
ACHHandle* AddEnvironmentCleanupHookInternal(Isolate* isolate,
AsyncCleanupHook fun,
void* arg) {
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
CHECK_NOT_NULL(env);
auto info = std::make_shared<AsyncCleanupHookInfo>();
info->env = env;
Expand Down Expand Up @@ -191,7 +188,7 @@ void RequestInterrupt(Environment* env, void (*fun)(void* arg), void* arg) {
}

async_id AsyncHooksGetExecutionAsyncId(Isolate* isolate) {
Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
if (env == nullptr) return -1;
return env->execution_async_id();
}
Expand All @@ -203,12 +200,11 @@ async_id AsyncHooksGetExecutionAsyncId(Local<Context> context) {
}

async_id AsyncHooksGetTriggerAsyncId(Isolate* isolate) {
Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
if (env == nullptr) return -1;
return env->trigger_async_id();
}


async_context EmitAsyncInit(Isolate* isolate,
Local<Object> resource,
const char* name,
Expand All @@ -225,7 +221,7 @@ async_context EmitAsyncInit(Isolate* isolate,
Local<String> name,
async_id trigger_async_id) {
DebugSealHandleScope handle_scope(isolate);
Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
CHECK_NOT_NULL(env);

// Initialize async context struct
Expand All @@ -245,7 +241,8 @@ async_context EmitAsyncInit(Isolate* isolate,
}

void EmitAsyncDestroy(Isolate* isolate, async_context asyncContext) {
EmitAsyncDestroy(Environment::GetCurrent(isolate), asyncContext);
EmitAsyncDestroy(Environment::GetCurrent(isolate->GetCurrentContext()),
asyncContext);
}

void EmitAsyncDestroy(Environment* env, async_context asyncContext) {
Expand Down
2 changes: 1 addition & 1 deletion src/async_context_frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Local<Value> current(Isolate* isolate) {
}

void set(Isolate* isolate, Local<Value> value) {
auto env = Environment::GetCurrent(isolate);
auto env = Environment::GetCurrent(isolate->GetCurrentContext());
if (!env->options()->async_context_frame) {
return;
}
Expand Down
7 changes: 0 additions & 7 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,6 @@ inline bool TickInfo::has_rejection_to_warn() const {
return fields_[kHasRejectionToWarn] == 1;
}

inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
if (!isolate->InContext()) [[unlikely]]
return nullptr;
v8::HandleScope handle_scope(isolate);
return GetCurrent(isolate->GetCurrentContext());
}

inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
if (!ContextEmbedderTag::IsNodeContext(context)) [[unlikely]] {
return nullptr;
Expand Down
1 change: 0 additions & 1 deletion src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,6 @@ class Environment final : public MemoryRetainer {
inline void PushAsyncCallbackScope();
inline void PopAsyncCallbackScope();

static inline Environment* GetCurrent(v8::Isolate* isolate);
static inline Environment* GetCurrent(v8::Local<v8::Context> context);
static inline Environment* GetCurrent(
const v8::FunctionCallbackInfo<v8::Value>& info);
Expand Down
10 changes: 5 additions & 5 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ MaybeLocal<Uint8Array> New(Isolate* isolate,
Local<ArrayBuffer> ab,
size_t byte_offset,
size_t length) {
Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
if (env == nullptr) {
THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate);
return MaybeLocal<Uint8Array>();
Expand Down Expand Up @@ -348,7 +348,7 @@ MaybeLocal<Object> New(Isolate* isolate,
MaybeLocal<Object> New(Isolate* isolate, size_t length) {
EscapableHandleScope handle_scope(isolate);
Local<Object> obj;
Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
if (env == nullptr) {
THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate);
return MaybeLocal<Object>();
Expand Down Expand Up @@ -389,7 +389,7 @@ MaybeLocal<Object> New(Environment* env, size_t length) {

MaybeLocal<Object> Copy(Isolate* isolate, const char* data, size_t length) {
EscapableHandleScope handle_scope(isolate);
Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
if (env == nullptr) {
THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate);
return MaybeLocal<Object>();
Expand Down Expand Up @@ -434,7 +434,7 @@ MaybeLocal<Object> New(Isolate* isolate,
FreeCallback callback,
void* hint) {
EscapableHandleScope handle_scope(isolate);
Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
if (env == nullptr) {
callback(data, hint);
THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate);
Expand Down Expand Up @@ -478,7 +478,7 @@ MaybeLocal<Object> New(Environment* env,
// necessarily isolate's ArrayBuffer::Allocator.
MaybeLocal<Object> New(Isolate* isolate, char* data, size_t length) {
EscapableHandleScope handle_scope(isolate);
Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
if (env == nullptr) {
free(data);
THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate);
Expand Down
9 changes: 5 additions & 4 deletions src/node_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static std::string GetErrorSource(Isolate* isolate,

// If source maps have been enabled, the exception line will instead be
// added in the JavaScript context:
Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(context);
const bool has_source_map_url =
!message->GetScriptOrigin().SourceMapUrl().IsEmpty() &&
!message->GetScriptOrigin().SourceMapUrl()->IsUndefined();
Expand Down Expand Up @@ -1017,9 +1017,10 @@ const char* errno_string(int errorno) {

void PerIsolateMessageListener(Local<Message> message, Local<Value> error) {
Isolate* isolate = message->GetIsolate();
auto context = isolate->GetCurrentContext();
switch (message->ErrorLevel()) {
case Isolate::MessageErrorLevel::kMessageWarning: {
Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(context);
if (!env) {
break;
}
Expand All @@ -1028,7 +1029,7 @@ void PerIsolateMessageListener(Local<Message> message, Local<Value> error) {
std::stringstream warning;
warning << *filename;
warning << ":";
warning << message->GetLineNumber(env->context()).FromMaybe(-1);
warning << message->GetLineNumber(context).FromMaybe(-1);
warning << " ";
v8::String::Utf8Value msg(isolate, message->Get());
warning << *msg;
Expand Down Expand Up @@ -1086,7 +1087,7 @@ static void NoSideEffectsToString(const FunctionCallbackInfo<Value>& args) {

static void TriggerUncaughtException(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
Local<Value> exception = args[0];
Local<Message> message = Exception::CreateMessage(isolate, exception);
if (env != nullptr && env->abort_on_uncaught_exception()) {
Expand Down
5 changes: 2 additions & 3 deletions src/node_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,9 @@ int NodePlatform::NumberOfWorkerThreads() {

void PerIsolatePlatformData::RunForegroundTask(std::unique_ptr<Task> task) {
if (isolate_->IsExecutionTerminating()) return;
DebugSealHandleScope scope(isolate_);
Environment* env = Environment::GetCurrent(isolate_);
v8::HandleScope scope(isolate_);
Environment* env = Environment::GetCurrent(isolate_->GetCurrentContext());
if (env != nullptr) {
v8::HandleScope scope(isolate_);
InternalCallbackScope cb_scope(env, Object::New(isolate_), { 0, 0 },
InternalCallbackScope::kNoFlags);
task->Run();
Expand Down
6 changes: 3 additions & 3 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#include <Windows.h>
#else // !_WIN32
#include <cxxabi.h>
#include <sys/resource.h>
#include <dlfcn.h>
#include <sys/resource.h>
#endif

#include <cstring>
Expand Down Expand Up @@ -965,7 +965,7 @@ std::string TriggerNodeReport(Isolate* isolate,
Local<Value> error) {
Environment* env = nullptr;
if (isolate != nullptr) {
env = Environment::GetCurrent(isolate);
env = Environment::GetCurrent(isolate->GetCurrentContext());
}
return TriggerNodeReport(isolate, env, message, trigger, name, error);
}
Expand All @@ -992,7 +992,7 @@ void GetNodeReport(Isolate* isolate,
std::ostream& out) {
Environment* env = nullptr;
if (isolate != nullptr) {
env = Environment::GetCurrent(isolate);
env = Environment::GetCurrent(isolate->GetCurrentContext());
}
bool exclude_network = env != nullptr ? env->options()->report_exclude_network
: per_process::cli_options->per_isolate
Expand Down
8 changes: 4 additions & 4 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate,
const char* message) {
Local<String> js_msg;
Local<Object> e;
Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
if (!String::NewFromUtf8(isolate, message).ToLocal(&js_msg) ||
!Exception::Error(js_msg)
->ToObject(isolate->GetCurrentContext())
Expand All @@ -136,7 +136,7 @@ inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate, int errcode) {
const char* errstr = sqlite3_errstr(errcode);
Local<String> js_errmsg;
Local<Object> e;
Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errmsg) ||
!CreateSQLiteError(isolate, errstr).ToLocal(&e) ||
e->Set(env->context(),
Expand All @@ -155,7 +155,7 @@ inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate, sqlite3* db) {
const char* errmsg = sqlite3_errmsg(db);
Local<String> js_errmsg;
Local<Object> e;
Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errmsg) ||
!CreateSQLiteError(isolate, errmsg).ToLocal(&e) ||
e->Set(isolate->GetCurrentContext(),
Expand Down Expand Up @@ -225,7 +225,7 @@ inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, const char* message) {
inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, int errcode) {
const char* errstr = sqlite3_errstr(errcode);

Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
Local<Object> error;
if (CreateSQLiteError(isolate, errstr).ToLocal(&error) &&
error
Expand Down
2 changes: 1 addition & 1 deletion src/node_task_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void PromiseRejectCallback(PromiseRejectMessage message) {
Isolate* isolate = promise->GetIsolate();
PromiseRejectEvent event = message.GetEvent();

Environment* env = Environment::GetCurrent(isolate);
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());

if (env == nullptr || !env->can_call_into_js()) return;

Expand Down
6 changes: 3 additions & 3 deletions src/node_url_pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ std::optional<URLPatternRegexProvider::regex_type>
URLPatternRegexProvider::create_instance(std::string_view pattern,
bool ignore_case) {
auto isolate = Isolate::GetCurrent();
auto env = Environment::GetCurrent(isolate);
auto env = Environment::GetCurrent(isolate->GetCurrentContext());
int flags = RegExp::Flags::kUnicodeSets | RegExp::Flags::kDotAll;
if (ignore_case) {
flags |= static_cast<int>(RegExp::Flags::kIgnoreCase);
Expand All @@ -100,7 +100,7 @@ URLPatternRegexProvider::create_instance(std::string_view pattern,
bool URLPatternRegexProvider::regex_match(std::string_view input,
const regex_type& pattern) {
auto isolate = Isolate::GetCurrent();
auto env = Environment::GetCurrent(isolate);
auto env = Environment::GetCurrent(isolate->GetCurrentContext());
Local<String> local_input;
if (!String::NewFromUtf8(
isolate, input.data(), NewStringType::kNormal, input.size())
Expand All @@ -121,7 +121,7 @@ std::optional<std::vector<std::optional<std::string>>>
URLPatternRegexProvider::regex_search(std::string_view input,
const regex_type& global_pattern) {
auto isolate = Isolate::GetCurrent();
auto env = Environment::GetCurrent(isolate);
auto env = Environment::GetCurrent(isolate->GetCurrentContext());
Local<String> local_input;
if (!String::NewFromUtf8(
isolate, input.data(), NewStringType::kNormal, input.size())
Expand Down
Loading
Loading