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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 11 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 13 additions & 16 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 7 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 5 additions & 5 deletions
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);

0 commit comments

Comments
 (0)