Skip to content

Commit 120849f

Browse files
XadillaXdanielleadams
authored andcommitted
src: cache necessary isolate & context in api/*
Refs: #37473 PR-URL: #38366 Reviewed-By: Joyee Cheung <[email protected]>
1 parent 08ad2f6 commit 120849f

File tree

5 files changed

+61
-45
lines changed

5 files changed

+61
-45
lines changed

src/api/callback.cc

+17-11
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ InternalCallbackScope::InternalCallbackScope(Environment* env,
5656
return;
5757
}
5858

59-
HandleScope handle_scope(env->isolate());
59+
Isolate* isolate = env->isolate();
60+
61+
HandleScope handle_scope(isolate);
6062
// If you hit this assertion, you forgot to enter the v8::Context first.
61-
CHECK_EQ(Environment::GetCurrent(env->isolate()), env);
63+
CHECK_EQ(Environment::GetCurrent(isolate), env);
6264

6365
env->isolate()->SetIdle(false);
6466

@@ -83,7 +85,8 @@ void InternalCallbackScope::Close() {
8385
if (closed_) return;
8486
closed_ = true;
8587

86-
auto idle = OnScopeLeave([&]() { env_->isolate()->SetIdle(true); });
88+
Isolate* isolate = env_->isolate();
89+
auto idle = OnScopeLeave([&]() { isolate->SetIdle(true); });
8790

8891
if (!env_->can_call_into_js()) return;
8992
auto perform_stopping_check = [&]() {
@@ -113,8 +116,9 @@ void InternalCallbackScope::Close() {
113116

114117
auto weakref_cleanup = OnScopeLeave([&]() { env_->RunWeakRefCleanup(); });
115118

119+
Local<Context> context = env_->context();
116120
if (!tick_info->has_tick_scheduled()) {
117-
env_->context()->GetMicrotaskQueue()->PerformCheckpoint(env_->isolate());
121+
context->GetMicrotaskQueue()->PerformCheckpoint(isolate);
118122

119123
perform_stopping_check();
120124
}
@@ -130,7 +134,7 @@ void InternalCallbackScope::Close() {
130134
return;
131135
}
132136

133-
HandleScope handle_scope(env_->isolate());
137+
HandleScope handle_scope(isolate);
134138
Local<Object> process = env_->process_object();
135139

136140
if (!env_->can_call_into_js()) return;
@@ -141,7 +145,7 @@ void InternalCallbackScope::Close() {
141145
// to initializes the tick callback during bootstrap.
142146
CHECK(!tick_callback.IsEmpty());
143147

144-
if (tick_callback->Call(env_->context(), process, 0, nullptr).IsEmpty()) {
148+
if (tick_callback->Call(context, process, 0, nullptr).IsEmpty()) {
145149
failed_ = true;
146150
}
147151
perform_stopping_check();
@@ -181,6 +185,7 @@ MaybeLocal<Value> InternalMakeCallback(Environment* env,
181185

182186
MaybeLocal<Value> ret;
183187

188+
Local<Context> context = env->context();
184189
if (use_async_hooks_trampoline) {
185190
MaybeStackBuffer<Local<Value>, 16> args(3 + argc);
186191
args[0] = v8::Number::New(env->isolate(), asyncContext.async_id);
@@ -189,9 +194,9 @@ MaybeLocal<Value> InternalMakeCallback(Environment* env,
189194
for (int i = 0; i < argc; i++) {
190195
args[i + 3] = argv[i];
191196
}
192-
ret = hook_cb->Call(env->context(), recv, args.length(), &args[0]);
197+
ret = hook_cb->Call(context, recv, args.length(), &args[0]);
193198
} else {
194-
ret = callback->Call(env->context(), recv, argc, argv);
199+
ret = callback->Call(context, recv, argc, argv);
195200
}
196201

197202
if (ret.IsEmpty()) {
@@ -266,7 +271,7 @@ MaybeLocal<Value> MakeCallback(Isolate* isolate,
266271
if (ret.IsEmpty() && env->async_callback_scope_depth() == 0) {
267272
// This is only for legacy compatibility and we may want to look into
268273
// removing/adjusting it.
269-
return Undefined(env->isolate());
274+
return Undefined(isolate);
270275
}
271276
return ret;
272277
}
@@ -285,11 +290,12 @@ MaybeLocal<Value> MakeSyncCallback(Isolate* isolate,
285290
CHECK_NOT_NULL(env);
286291
if (!env->can_call_into_js()) return Local<Value>();
287292

288-
Context::Scope context_scope(env->context());
293+
Local<Context> context = env->context();
294+
Context::Scope context_scope(context);
289295
if (env->async_callback_scope_depth()) {
290296
// There's another MakeCallback() on the stack, piggy back on it.
291297
// In particular, retain the current async_context.
292-
return callback->Call(env->context(), recv, argc, argv);
298+
return callback->Call(context, recv, argc, argv);
293299
}
294300

295301
// This is a toplevel invocation and the caller (intentionally)

src/api/embed_helpers.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ Maybe<int> SpinEventLoop(Environment* env) {
1919
MultiIsolatePlatform* platform = GetMultiIsolatePlatform(env);
2020
CHECK_NOT_NULL(platform);
2121

22-
HandleScope handle_scope(env->isolate());
22+
Isolate* isolate = env->isolate();
23+
HandleScope handle_scope(isolate);
2324
Context::Scope context_scope(env->context());
24-
SealHandleScope seal(env->isolate());
25+
SealHandleScope seal(isolate);
2526

2627
if (env->is_stopping()) return Nothing<int>();
2728

@@ -35,7 +36,7 @@ Maybe<int> SpinEventLoop(Environment* env) {
3536
uv_run(env->event_loop(), UV_RUN_DEFAULT);
3637
if (env->is_stopping()) break;
3738

38-
platform->DrainTasks(env->isolate());
39+
platform->DrainTasks(isolate);
3940

4041
more = uv_loop_alive(env->event_loop());
4142
if (more && !env->is_stopping()) continue;

src/api/environment.cc

+8-6
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,13 @@ Environment* CreateEnvironment(
359359
}
360360

361361
void FreeEnvironment(Environment* env) {
362-
Isolate::DisallowJavascriptExecutionScope disallow_js(env->isolate(),
362+
Isolate* isolate = env->isolate();
363+
Isolate::DisallowJavascriptExecutionScope disallow_js(isolate,
363364
Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE);
364365
{
365-
HandleScope handle_scope(env->isolate()); // For env->context().
366+
HandleScope handle_scope(isolate); // For env->context().
366367
Context::Scope context_scope(env->context());
367-
SealHandleScope seal_handle_scope(env->isolate());
368+
SealHandleScope seal_handle_scope(isolate);
368369

369370
env->set_stopping(true);
370371
env->stop_sub_worker_contexts();
@@ -377,7 +378,7 @@ void FreeEnvironment(Environment* env) {
377378
// NodePlatform implementation.
378379
MultiIsolatePlatform* platform = env->isolate_data()->platform();
379380
if (platform != nullptr)
380-
platform->DrainTasks(env->isolate());
381+
platform->DrainTasks(isolate);
381382

382383
delete env;
383384
}
@@ -409,14 +410,15 @@ MaybeLocal<Value> LoadEnvironment(
409410
Environment* env,
410411
const char* main_script_source_utf8) {
411412
CHECK_NOT_NULL(main_script_source_utf8);
413+
Isolate* isolate = env->isolate();
412414
return LoadEnvironment(
413415
env,
414416
[&](const StartExecutionCallbackInfo& info) -> MaybeLocal<Value> {
415417
// This is a slightly hacky way to convert UTF-8 to UTF-16.
416418
Local<String> str =
417-
String::NewFromUtf8(env->isolate(),
419+
String::NewFromUtf8(isolate,
418420
main_script_source_utf8).ToLocalChecked();
419-
auto main_utf16 = std::make_unique<String::Value>(env->isolate(), str);
421+
auto main_utf16 = std::make_unique<String::Value>(isolate, str);
420422

421423
// TODO(addaleax): Avoid having a global table for all scripts.
422424
std::string name = "embedder_main_" + std::to_string(env->thread_id());

src/api/exceptions.cc

+16-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace node {
1313

14+
using v8::Context;
1415
using v8::Exception;
1516
using v8::Integer;
1617
using v8::Isolate;
@@ -51,18 +52,19 @@ Local<Value> ErrnoException(Isolate* isolate,
5152
}
5253
e = Exception::Error(cons);
5354

55+
Local<Context> context = env->context();
5456
Local<Object> obj = e.As<Object>();
55-
obj->Set(env->context(),
57+
obj->Set(context,
5658
env->errno_string(),
5759
Integer::New(isolate, errorno)).Check();
58-
obj->Set(env->context(), env->code_string(), estring).Check();
60+
obj->Set(context, env->code_string(), estring).Check();
5961

6062
if (path_string.IsEmpty() == false) {
61-
obj->Set(env->context(), env->path_string(), path_string).Check();
63+
obj->Set(context, env->path_string(), path_string).Check();
6264
}
6365

6466
if (syscall != nullptr) {
65-
obj->Set(env->context(),
67+
obj->Set(context,
6668
env->syscall_string(),
6769
OneByteString(isolate, syscall)).Check();
6870
}
@@ -135,15 +137,16 @@ Local<Value> UVException(Isolate* isolate,
135137
Exception::Error(js_msg)->ToObject(isolate->GetCurrentContext())
136138
.ToLocalChecked();
137139

138-
e->Set(env->context(),
140+
Local<Context> context = env->context();
141+
e->Set(context,
139142
env->errno_string(),
140143
Integer::New(isolate, errorno)).Check();
141-
e->Set(env->context(), env->code_string(), js_code).Check();
142-
e->Set(env->context(), env->syscall_string(), js_syscall).Check();
144+
e->Set(context, env->code_string(), js_code).Check();
145+
e->Set(context, env->syscall_string(), js_syscall).Check();
143146
if (!js_path.IsEmpty())
144-
e->Set(env->context(), env->path_string(), js_path).Check();
147+
e->Set(context, env->path_string(), js_path).Check();
145148
if (!js_dest.IsEmpty())
146-
e->Set(env->context(), env->dest_string(), js_dest).Check();
149+
e->Set(context, env->dest_string(), js_dest).Check();
147150

148151
return e;
149152
}
@@ -209,19 +212,20 @@ Local<Value> WinapiErrnoException(Isolate* isolate,
209212
e = Exception::Error(message);
210213
}
211214

215+
Local<Context> context = env->context();
212216
Local<Object> obj = e.As<Object>();
213-
obj->Set(env->context(), env->errno_string(), Integer::New(isolate, errorno))
217+
obj->Set(context, env->errno_string(), Integer::New(isolate, errorno))
214218
.Check();
215219

216220
if (path != nullptr) {
217-
obj->Set(env->context(),
221+
obj->Set(context,
218222
env->path_string(),
219223
String::NewFromUtf8(isolate, path).ToLocalChecked())
220224
.Check();
221225
}
222226

223227
if (syscall != nullptr) {
224-
obj->Set(env->context(),
228+
obj->Set(context,
225229
env->syscall_string(),
226230
OneByteString(isolate, syscall))
227231
.Check();

src/api/hooks.cc

+16-13
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ Maybe<bool> EmitProcessBeforeExit(Environment* env) {
3838
AsyncWrap::DestroyAsyncIdsCallback(env);
3939

4040
HandleScope handle_scope(env->isolate());
41-
Context::Scope context_scope(env->context());
41+
Local<Context> context = env->context();
42+
Context::Scope context_scope(context);
4243

4344
Local<Value> exit_code_v;
44-
if (!env->process_object()->Get(env->context(), env->exit_code_string())
45+
if (!env->process_object()->Get(context, env->exit_code_string())
4546
.ToLocal(&exit_code_v)) return Nothing<bool>();
4647

4748
Local<Integer> exit_code;
48-
if (!exit_code_v->ToInteger(env->context()).ToLocal(&exit_code)) {
49+
if (!exit_code_v->ToInteger(context).ToLocal(&exit_code)) {
4950
return Nothing<bool>();
5051
}
5152

@@ -59,28 +60,30 @@ int EmitExit(Environment* env) {
5960

6061
Maybe<int> EmitProcessExit(Environment* env) {
6162
// process.emit('exit')
62-
HandleScope handle_scope(env->isolate());
63-
Context::Scope context_scope(env->context());
63+
Isolate* isolate = env->isolate();
64+
HandleScope handle_scope(isolate);
65+
Local<Context> context = env->context();
66+
Context::Scope context_scope(context);
6467
Local<Object> process_object = env->process_object();
6568

6669
// TODO(addaleax): It might be nice to share process._exiting and
6770
// process.exitCode via getter/setter pairs that pass data directly to the
6871
// native side, so that we don't manually have to read and write JS properties
6972
// here. These getters could use e.g. a typed array for performance.
7073
if (process_object
71-
->Set(env->context(),
72-
FIXED_ONE_BYTE_STRING(env->isolate(), "_exiting"),
73-
True(env->isolate())).IsNothing()) return Nothing<int>();
74+
->Set(context,
75+
FIXED_ONE_BYTE_STRING(isolate, "_exiting"),
76+
True(isolate)).IsNothing()) return Nothing<int>();
7477

7578
Local<String> exit_code = env->exit_code_string();
7679
Local<Value> code_v;
7780
int code;
78-
if (!process_object->Get(env->context(), exit_code).ToLocal(&code_v) ||
79-
!code_v->Int32Value(env->context()).To(&code) ||
80-
ProcessEmit(env, "exit", Integer::New(env->isolate(), code)).IsEmpty() ||
81+
if (!process_object->Get(context, exit_code).ToLocal(&code_v) ||
82+
!code_v->Int32Value(context).To(&code) ||
83+
ProcessEmit(env, "exit", Integer::New(isolate, code)).IsEmpty() ||
8184
// Reload exit code, it may be changed by `emit('exit')`
82-
!process_object->Get(env->context(), exit_code).ToLocal(&code_v) ||
83-
!code_v->Int32Value(env->context()).To(&code)) {
85+
!process_object->Get(context, exit_code).ToLocal(&code_v) ||
86+
!code_v->Int32Value(context).To(&code)) {
8487
return Nothing<int>();
8588
}
8689

0 commit comments

Comments
 (0)