Skip to content

Commit 9a1be50

Browse files
committed
src: remove Environment::GetCurrent(isolate)
1 parent 2281a04 commit 9a1be50

17 files changed

+294
-310
lines changed

src/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,6 @@ Typical ways of accessing the current `Environment` in the Node.js code are:
344344
This requires that `context` has been associated with the `Environment`
345345
instance, e.g. is the main `Context` for the `Environment` or one of its
346346
`vm.Context`s.
347-
* Given an [`Isolate`][], using `Environment::GetCurrent(isolate)`. This looks
348-
up the current [`Context`][] and then uses that.
349347
350348
<a id="realm"></a>
351349

src/api/async_resource.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ AsyncResource::AsyncResource(Isolate* isolate,
1616
Local<Object> resource,
1717
const char* name,
1818
async_id trigger_async_id)
19-
: env_(Environment::GetCurrent(isolate)),
19+
: env_(Environment::GetCurrent(isolate->GetCurrentContext())),
2020
resource_(isolate, resource),
2121
context_frame_(isolate, async_context_frame::current(isolate)) {
2222
CHECK_NOT_NULL(env_);

src/api/callback.cc

+3-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ using v8::Value;
2222
CallbackScope::CallbackScope(Isolate* isolate,
2323
Local<Object> object,
2424
async_context async_context)
25-
: CallbackScope(Environment::GetCurrent(isolate), object, async_context) {}
25+
: CallbackScope(Environment::GetCurrent(isolate->GetCurrentContext()),
26+
object,
27+
async_context) {}
2628

2729
CallbackScope::CallbackScope(Environment* env,
2830
Local<Object> object,
@@ -69,16 +71,6 @@ InternalCallbackScope::InternalCallbackScope(Environment* env,
6971
Isolate* isolate = env->isolate();
7072

7173
HandleScope handle_scope(isolate);
72-
Local<Context> current_context = isolate->GetCurrentContext();
73-
// If you hit this assertion, the caller forgot to enter the right Node.js
74-
// Environment's v8::Context first.
75-
// We first check `env->context() != current_context` because the contexts
76-
// likely *are* the same, in which case we can skip the slightly more
77-
// expensive Environment::GetCurrent() call.
78-
if (env->context() != current_context) [[unlikely]] {
79-
CHECK_EQ(Environment::GetCurrent(isolate), env);
80-
}
81-
8274
isolate->SetIdle(false);
8375

8476
prior_context_frame_.Reset(

src/api/environment.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ bool AllowWasmCodeGenerationCallback(Local<Context> context,
5858

5959
bool ShouldAbortOnUncaughtException(Isolate* isolate) {
6060
DebugSealHandleScope scope(isolate);
61-
Environment* env = Environment::GetCurrent(isolate);
62-
return env != nullptr &&
63-
(env->is_main_thread() || !env->is_stopping()) &&
61+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
62+
return env != nullptr && (env->is_main_thread() || !env->is_stopping()) &&
6463
env->abort_on_uncaught_exception() &&
6564
env->should_abort_on_uncaught_toggle()[0] &&
6665
!env->inside_should_not_abort_on_uncaught_scope();

src/api/exceptions.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Local<Value> ErrnoException(Isolate* isolate,
2525
const char* syscall,
2626
const char* msg,
2727
const char* path) {
28-
Environment* env = Environment::GetCurrent(isolate);
28+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
2929
CHECK_NOT_NULL(env);
3030

3131
Local<Value> e;
@@ -94,7 +94,7 @@ Local<Value> UVException(Isolate* isolate,
9494
const char* msg,
9595
const char* path,
9696
const char* dest) {
97-
Environment* env = Environment::GetCurrent(isolate);
97+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
9898
CHECK_NOT_NULL(env);
9999

100100
if (!msg || !msg[0])

src/api/hooks.cc

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
#include "async_wrap.h"
12
#include "env-inl.h"
23
#include "node_internals.h"
34
#include "node_process-inl.h"
4-
#include "async_wrap.h"
55

66
namespace node {
77

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

130-
void AddEnvironmentCleanupHook(Isolate* isolate,
131-
CleanupHook fun,
132-
void* arg) {
133-
Environment* env = Environment::GetCurrent(isolate);
130+
void AddEnvironmentCleanupHook(Isolate* isolate, CleanupHook fun, void* arg) {
131+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
134132
CHECK_NOT_NULL(env);
135133
env->AddCleanupHook(fun, arg);
136134
}
137135

138136
void RemoveEnvironmentCleanupHook(Isolate* isolate,
139137
CleanupHook fun,
140138
void* arg) {
141-
Environment* env = Environment::GetCurrent(isolate);
139+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
142140
CHECK_NOT_NULL(env);
143141
env->RemoveCleanupHook(fun, arg);
144142
}
@@ -158,11 +156,10 @@ static void RunAsyncCleanupHook(void* arg) {
158156
info->fun(info->arg, FinishAsyncCleanupHook, info);
159157
}
160158

161-
ACHHandle* AddEnvironmentCleanupHookInternal(
162-
Isolate* isolate,
163-
AsyncCleanupHook fun,
164-
void* arg) {
165-
Environment* env = Environment::GetCurrent(isolate);
159+
ACHHandle* AddEnvironmentCleanupHookInternal(Isolate* isolate,
160+
AsyncCleanupHook fun,
161+
void* arg) {
162+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
166163
CHECK_NOT_NULL(env);
167164
auto info = std::make_shared<AsyncCleanupHookInfo>();
168165
info->env = env;
@@ -191,7 +188,7 @@ void RequestInterrupt(Environment* env, void (*fun)(void* arg), void* arg) {
191188
}
192189

193190
async_id AsyncHooksGetExecutionAsyncId(Isolate* isolate) {
194-
Environment* env = Environment::GetCurrent(isolate);
191+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
195192
if (env == nullptr) return -1;
196193
return env->execution_async_id();
197194
}
@@ -203,12 +200,11 @@ async_id AsyncHooksGetExecutionAsyncId(Local<Context> context) {
203200
}
204201

205202
async_id AsyncHooksGetTriggerAsyncId(Isolate* isolate) {
206-
Environment* env = Environment::GetCurrent(isolate);
203+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
207204
if (env == nullptr) return -1;
208205
return env->trigger_async_id();
209206
}
210207

211-
212208
async_context EmitAsyncInit(Isolate* isolate,
213209
Local<Object> resource,
214210
const char* name,
@@ -225,7 +221,7 @@ async_context EmitAsyncInit(Isolate* isolate,
225221
Local<String> name,
226222
async_id trigger_async_id) {
227223
DebugSealHandleScope handle_scope(isolate);
228-
Environment* env = Environment::GetCurrent(isolate);
224+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
229225
CHECK_NOT_NULL(env);
230226

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

247243
void EmitAsyncDestroy(Isolate* isolate, async_context asyncContext) {
248-
EmitAsyncDestroy(Environment::GetCurrent(isolate), asyncContext);
244+
EmitAsyncDestroy(Environment::GetCurrent(isolate->GetCurrentContext()),
245+
asyncContext);
249246
}
250247

251248
void EmitAsyncDestroy(Environment* env, async_context asyncContext) {

src/async_context_frame.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Local<Value> current(Isolate* isolate) {
3838
}
3939

4040
void set(Isolate* isolate, Local<Value> value) {
41-
auto env = Environment::GetCurrent(isolate);
41+
auto env = Environment::GetCurrent(isolate->GetCurrentContext());
4242
if (!env->options()->async_context_frame) {
4343
return;
4444
}

src/env-inl.h

-7
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,6 @@ inline bool TickInfo::has_rejection_to_warn() const {
174174
return fields_[kHasRejectionToWarn] == 1;
175175
}
176176

177-
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
178-
if (!isolate->InContext()) [[unlikely]]
179-
return nullptr;
180-
v8::HandleScope handle_scope(isolate);
181-
return GetCurrent(isolate->GetCurrentContext());
182-
}
183-
184177
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
185178
if (!ContextEmbedderTag::IsNodeContext(context)) [[unlikely]] {
186179
return nullptr;

src/env.h

-1
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,6 @@ class Environment final : public MemoryRetainer {
643643
inline void PushAsyncCallbackScope();
644644
inline void PopAsyncCallbackScope();
645645

646-
static inline Environment* GetCurrent(v8::Isolate* isolate);
647646
static inline Environment* GetCurrent(v8::Local<v8::Context> context);
648647
static inline Environment* GetCurrent(
649648
const v8::FunctionCallbackInfo<v8::Value>& info);

src/node_buffer.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ MaybeLocal<Uint8Array> New(Isolate* isolate,
290290
Local<ArrayBuffer> ab,
291291
size_t byte_offset,
292292
size_t length) {
293-
Environment* env = Environment::GetCurrent(isolate);
293+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
294294
if (env == nullptr) {
295295
THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate);
296296
return MaybeLocal<Uint8Array>();
@@ -348,7 +348,7 @@ MaybeLocal<Object> New(Isolate* isolate,
348348
MaybeLocal<Object> New(Isolate* isolate, size_t length) {
349349
EscapableHandleScope handle_scope(isolate);
350350
Local<Object> obj;
351-
Environment* env = Environment::GetCurrent(isolate);
351+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
352352
if (env == nullptr) {
353353
THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate);
354354
return MaybeLocal<Object>();
@@ -389,7 +389,7 @@ MaybeLocal<Object> New(Environment* env, size_t length) {
389389

390390
MaybeLocal<Object> Copy(Isolate* isolate, const char* data, size_t length) {
391391
EscapableHandleScope handle_scope(isolate);
392-
Environment* env = Environment::GetCurrent(isolate);
392+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
393393
if (env == nullptr) {
394394
THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate);
395395
return MaybeLocal<Object>();
@@ -434,7 +434,7 @@ MaybeLocal<Object> New(Isolate* isolate,
434434
FreeCallback callback,
435435
void* hint) {
436436
EscapableHandleScope handle_scope(isolate);
437-
Environment* env = Environment::GetCurrent(isolate);
437+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
438438
if (env == nullptr) {
439439
callback(data, hint);
440440
THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate);
@@ -478,7 +478,7 @@ MaybeLocal<Object> New(Environment* env,
478478
// necessarily isolate's ArrayBuffer::Allocator.
479479
MaybeLocal<Object> New(Isolate* isolate, char* data, size_t length) {
480480
EscapableHandleScope handle_scope(isolate);
481-
Environment* env = Environment::GetCurrent(isolate);
481+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
482482
if (env == nullptr) {
483483
free(data);
484484
THROW_ERR_BUFFER_CONTEXT_NOT_AVAILABLE(isolate);

src/node_errors.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static std::string GetErrorSource(Isolate* isolate,
101101

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

10181018
void PerIsolateMessageListener(Local<Message> message, Local<Value> error) {
10191019
Isolate* isolate = message->GetIsolate();
1020+
auto context = isolate->GetCurrentContext();
10201021
switch (message->ErrorLevel()) {
10211022
case Isolate::MessageErrorLevel::kMessageWarning: {
1022-
Environment* env = Environment::GetCurrent(isolate);
1023+
Environment* env = Environment::GetCurrent(context);
10231024
if (!env) {
10241025
break;
10251026
}
@@ -1028,7 +1029,7 @@ void PerIsolateMessageListener(Local<Message> message, Local<Value> error) {
10281029
std::stringstream warning;
10291030
warning << *filename;
10301031
warning << ":";
1031-
warning << message->GetLineNumber(env->context()).FromMaybe(-1);
1032+
warning << message->GetLineNumber(context).FromMaybe(-1);
10321033
warning << " ";
10331034
v8::String::Utf8Value msg(isolate, message->Get());
10341035
warning << *msg;
@@ -1086,7 +1087,7 @@ static void NoSideEffectsToString(const FunctionCallbackInfo<Value>& args) {
10861087

10871088
static void TriggerUncaughtException(const FunctionCallbackInfo<Value>& args) {
10881089
Isolate* isolate = args.GetIsolate();
1089-
Environment* env = Environment::GetCurrent(isolate);
1090+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
10901091
Local<Value> exception = args[0];
10911092
Local<Message> message = Exception::CreateMessage(isolate, exception);
10921093
if (env != nullptr && env->abort_on_uncaught_exception()) {

src/node_platform.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,9 @@ int NodePlatform::NumberOfWorkerThreads() {
539539

540540
void PerIsolatePlatformData::RunForegroundTask(std::unique_ptr<Task> task) {
541541
if (isolate_->IsExecutionTerminating()) return;
542-
DebugSealHandleScope scope(isolate_);
543-
Environment* env = Environment::GetCurrent(isolate_);
542+
v8::HandleScope scope(isolate_);
543+
Environment* env = Environment::GetCurrent(isolate_->GetCurrentContext());
544544
if (env != nullptr) {
545-
v8::HandleScope scope(isolate_);
546545
InternalCallbackScope cb_scope(env, Object::New(isolate_), { 0, 0 },
547546
InternalCallbackScope::kNoFlags);
548547
task->Run();

src/node_report.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#include <Windows.h>
1515
#else // !_WIN32
1616
#include <cxxabi.h>
17-
#include <sys/resource.h>
1817
#include <dlfcn.h>
18+
#include <sys/resource.h>
1919
#endif
2020

2121
#include <cstring>
@@ -965,7 +965,7 @@ std::string TriggerNodeReport(Isolate* isolate,
965965
Local<Value> error) {
966966
Environment* env = nullptr;
967967
if (isolate != nullptr) {
968-
env = Environment::GetCurrent(isolate);
968+
env = Environment::GetCurrent(isolate->GetCurrentContext());
969969
}
970970
return TriggerNodeReport(isolate, env, message, trigger, name, error);
971971
}
@@ -992,7 +992,7 @@ void GetNodeReport(Isolate* isolate,
992992
std::ostream& out) {
993993
Environment* env = nullptr;
994994
if (isolate != nullptr) {
995-
env = Environment::GetCurrent(isolate);
995+
env = Environment::GetCurrent(isolate->GetCurrentContext());
996996
}
997997
bool exclude_network = env != nullptr ? env->options()->report_exclude_network
998998
: per_process::cli_options->per_isolate

src/node_sqlite.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate,
118118
const char* message) {
119119
Local<String> js_msg;
120120
Local<Object> e;
121-
Environment* env = Environment::GetCurrent(isolate);
121+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
122122
if (!String::NewFromUtf8(isolate, message).ToLocal(&js_msg) ||
123123
!Exception::Error(js_msg)
124124
->ToObject(isolate->GetCurrentContext())
@@ -136,7 +136,7 @@ inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate, int errcode) {
136136
const char* errstr = sqlite3_errstr(errcode);
137137
Local<String> js_errmsg;
138138
Local<Object> e;
139-
Environment* env = Environment::GetCurrent(isolate);
139+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
140140
if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errmsg) ||
141141
!CreateSQLiteError(isolate, errstr).ToLocal(&e) ||
142142
e->Set(env->context(),
@@ -155,7 +155,7 @@ inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate, sqlite3* db) {
155155
const char* errmsg = sqlite3_errmsg(db);
156156
Local<String> js_errmsg;
157157
Local<Object> e;
158-
Environment* env = Environment::GetCurrent(isolate);
158+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
159159
if (!String::NewFromUtf8(isolate, errstr).ToLocal(&js_errmsg) ||
160160
!CreateSQLiteError(isolate, errmsg).ToLocal(&e) ||
161161
e->Set(isolate->GetCurrentContext(),
@@ -225,7 +225,7 @@ inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, const char* message) {
225225
inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, int errcode) {
226226
const char* errstr = sqlite3_errstr(errcode);
227227

228-
Environment* env = Environment::GetCurrent(isolate);
228+
Environment* env = Environment::GetCurrent(isolate->GetCurrentContext());
229229
Local<Object> error;
230230
if (CreateSQLiteError(isolate, errstr).ToLocal(&error) &&
231231
error

src/node_task_queue.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void PromiseRejectCallback(PromiseRejectMessage message) {
5151
Isolate* isolate = promise->GetIsolate();
5252
PromiseRejectEvent event = message.GetEvent();
5353

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

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

src/node_url_pattern.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ std::optional<URLPatternRegexProvider::regex_type>
7676
URLPatternRegexProvider::create_instance(std::string_view pattern,
7777
bool ignore_case) {
7878
auto isolate = Isolate::GetCurrent();
79-
auto env = Environment::GetCurrent(isolate);
79+
auto env = Environment::GetCurrent(isolate->GetCurrentContext());
8080
int flags = RegExp::Flags::kUnicodeSets | RegExp::Flags::kDotAll;
8181
if (ignore_case) {
8282
flags |= static_cast<int>(RegExp::Flags::kIgnoreCase);
@@ -100,7 +100,7 @@ URLPatternRegexProvider::create_instance(std::string_view pattern,
100100
bool URLPatternRegexProvider::regex_match(std::string_view input,
101101
const regex_type& pattern) {
102102
auto isolate = Isolate::GetCurrent();
103-
auto env = Environment::GetCurrent(isolate);
103+
auto env = Environment::GetCurrent(isolate->GetCurrentContext());
104104
Local<String> local_input;
105105
if (!String::NewFromUtf8(
106106
isolate, input.data(), NewStringType::kNormal, input.size())
@@ -121,7 +121,7 @@ std::optional<std::vector<std::optional<std::string>>>
121121
URLPatternRegexProvider::regex_search(std::string_view input,
122122
const regex_type& global_pattern) {
123123
auto isolate = Isolate::GetCurrent();
124-
auto env = Environment::GetCurrent(isolate);
124+
auto env = Environment::GetCurrent(isolate->GetCurrentContext());
125125
Local<String> local_input;
126126
if (!String::NewFromUtf8(
127127
isolate, input.data(), NewStringType::kNormal, input.size())

0 commit comments

Comments
 (0)