diff --git a/src/api/callback.cc b/src/api/callback.cc index 72bfae2605adc7..911b1160eba342 100644 --- a/src/api/callback.cc +++ b/src/api/callback.cc @@ -56,9 +56,11 @@ InternalCallbackScope::InternalCallbackScope(Environment* env, return; } - HandleScope handle_scope(env->isolate()); + Isolate* isolate = env->isolate(); + + HandleScope handle_scope(isolate); // If you hit this assertion, you forgot to enter the v8::Context first. - CHECK_EQ(Environment::GetCurrent(env->isolate()), env); + CHECK_EQ(Environment::GetCurrent(isolate), env); env->isolate()->SetIdle(false); @@ -83,7 +85,8 @@ void InternalCallbackScope::Close() { if (closed_) return; closed_ = true; - auto idle = OnScopeLeave([&]() { env_->isolate()->SetIdle(true); }); + Isolate* isolate = env_->isolate(); + auto idle = OnScopeLeave([&]() { isolate->SetIdle(true); }); if (!env_->can_call_into_js()) return; auto perform_stopping_check = [&]() { @@ -113,8 +116,9 @@ void InternalCallbackScope::Close() { auto weakref_cleanup = OnScopeLeave([&]() { env_->RunWeakRefCleanup(); }); + Local context = env_->context(); if (!tick_info->has_tick_scheduled()) { - env_->context()->GetMicrotaskQueue()->PerformCheckpoint(env_->isolate()); + context->GetMicrotaskQueue()->PerformCheckpoint(isolate); perform_stopping_check(); } @@ -130,7 +134,7 @@ void InternalCallbackScope::Close() { return; } - HandleScope handle_scope(env_->isolate()); + HandleScope handle_scope(isolate); Local process = env_->process_object(); if (!env_->can_call_into_js()) return; @@ -141,7 +145,7 @@ void InternalCallbackScope::Close() { // to initializes the tick callback during bootstrap. CHECK(!tick_callback.IsEmpty()); - if (tick_callback->Call(env_->context(), process, 0, nullptr).IsEmpty()) { + if (tick_callback->Call(context, process, 0, nullptr).IsEmpty()) { failed_ = true; } perform_stopping_check(); @@ -181,6 +185,7 @@ MaybeLocal InternalMakeCallback(Environment* env, MaybeLocal ret; + Local context = env->context(); if (use_async_hooks_trampoline) { MaybeStackBuffer, 16> args(3 + argc); args[0] = v8::Number::New(env->isolate(), asyncContext.async_id); @@ -189,9 +194,9 @@ MaybeLocal InternalMakeCallback(Environment* env, for (int i = 0; i < argc; i++) { args[i + 3] = argv[i]; } - ret = hook_cb->Call(env->context(), recv, args.length(), &args[0]); + ret = hook_cb->Call(context, recv, args.length(), &args[0]); } else { - ret = callback->Call(env->context(), recv, argc, argv); + ret = callback->Call(context, recv, argc, argv); } if (ret.IsEmpty()) { @@ -266,7 +271,7 @@ MaybeLocal MakeCallback(Isolate* isolate, if (ret.IsEmpty() && env->async_callback_scope_depth() == 0) { // This is only for legacy compatibility and we may want to look into // removing/adjusting it. - return Undefined(env->isolate()); + return Undefined(isolate); } return ret; } @@ -285,11 +290,12 @@ MaybeLocal MakeSyncCallback(Isolate* isolate, CHECK_NOT_NULL(env); if (!env->can_call_into_js()) return Local(); - Context::Scope context_scope(env->context()); + Local context = env->context(); + Context::Scope context_scope(context); if (env->async_callback_scope_depth()) { // There's another MakeCallback() on the stack, piggy back on it. // In particular, retain the current async_context. - return callback->Call(env->context(), recv, argc, argv); + return callback->Call(context, recv, argc, argv); } // This is a toplevel invocation and the caller (intentionally) diff --git a/src/api/embed_helpers.cc b/src/api/embed_helpers.cc index ff0f8d7f723447..8e2fc67695b875 100644 --- a/src/api/embed_helpers.cc +++ b/src/api/embed_helpers.cc @@ -19,9 +19,10 @@ Maybe SpinEventLoop(Environment* env) { MultiIsolatePlatform* platform = GetMultiIsolatePlatform(env); CHECK_NOT_NULL(platform); - HandleScope handle_scope(env->isolate()); + Isolate* isolate = env->isolate(); + HandleScope handle_scope(isolate); Context::Scope context_scope(env->context()); - SealHandleScope seal(env->isolate()); + SealHandleScope seal(isolate); if (env->is_stopping()) return Nothing(); @@ -35,7 +36,7 @@ Maybe SpinEventLoop(Environment* env) { uv_run(env->event_loop(), UV_RUN_DEFAULT); if (env->is_stopping()) break; - platform->DrainTasks(env->isolate()); + platform->DrainTasks(isolate); more = uv_loop_alive(env->event_loop()); if (more && !env->is_stopping()) continue; diff --git a/src/api/environment.cc b/src/api/environment.cc index 3d061d38a5717a..baf3065471244a 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -359,12 +359,13 @@ Environment* CreateEnvironment( } void FreeEnvironment(Environment* env) { - Isolate::DisallowJavascriptExecutionScope disallow_js(env->isolate(), + Isolate* isolate = env->isolate(); + Isolate::DisallowJavascriptExecutionScope disallow_js(isolate, Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE); { - HandleScope handle_scope(env->isolate()); // For env->context(). + HandleScope handle_scope(isolate); // For env->context(). Context::Scope context_scope(env->context()); - SealHandleScope seal_handle_scope(env->isolate()); + SealHandleScope seal_handle_scope(isolate); env->set_stopping(true); env->stop_sub_worker_contexts(); @@ -377,7 +378,7 @@ void FreeEnvironment(Environment* env) { // NodePlatform implementation. MultiIsolatePlatform* platform = env->isolate_data()->platform(); if (platform != nullptr) - platform->DrainTasks(env->isolate()); + platform->DrainTasks(isolate); delete env; } @@ -409,14 +410,15 @@ MaybeLocal LoadEnvironment( Environment* env, const char* main_script_source_utf8) { CHECK_NOT_NULL(main_script_source_utf8); + Isolate* isolate = env->isolate(); return LoadEnvironment( env, [&](const StartExecutionCallbackInfo& info) -> MaybeLocal { // This is a slightly hacky way to convert UTF-8 to UTF-16. Local str = - String::NewFromUtf8(env->isolate(), + String::NewFromUtf8(isolate, main_script_source_utf8).ToLocalChecked(); - auto main_utf16 = std::make_unique(env->isolate(), str); + auto main_utf16 = std::make_unique(isolate, str); // TODO(addaleax): Avoid having a global table for all scripts. std::string name = "embedder_main_" + std::to_string(env->thread_id()); diff --git a/src/api/exceptions.cc b/src/api/exceptions.cc index 310b2acc4073d6..493e0e806f76f3 100644 --- a/src/api/exceptions.cc +++ b/src/api/exceptions.cc @@ -11,6 +11,7 @@ namespace node { +using v8::Context; using v8::Exception; using v8::Integer; using v8::Isolate; @@ -51,18 +52,19 @@ Local ErrnoException(Isolate* isolate, } e = Exception::Error(cons); + Local context = env->context(); Local obj = e.As(); - obj->Set(env->context(), + obj->Set(context, env->errno_string(), Integer::New(isolate, errorno)).Check(); - obj->Set(env->context(), env->code_string(), estring).Check(); + obj->Set(context, env->code_string(), estring).Check(); if (path_string.IsEmpty() == false) { - obj->Set(env->context(), env->path_string(), path_string).Check(); + obj->Set(context, env->path_string(), path_string).Check(); } if (syscall != nullptr) { - obj->Set(env->context(), + obj->Set(context, env->syscall_string(), OneByteString(isolate, syscall)).Check(); } @@ -135,15 +137,16 @@ Local UVException(Isolate* isolate, Exception::Error(js_msg)->ToObject(isolate->GetCurrentContext()) .ToLocalChecked(); - e->Set(env->context(), + Local context = env->context(); + e->Set(context, env->errno_string(), Integer::New(isolate, errorno)).Check(); - e->Set(env->context(), env->code_string(), js_code).Check(); - e->Set(env->context(), env->syscall_string(), js_syscall).Check(); + e->Set(context, env->code_string(), js_code).Check(); + e->Set(context, env->syscall_string(), js_syscall).Check(); if (!js_path.IsEmpty()) - e->Set(env->context(), env->path_string(), js_path).Check(); + e->Set(context, env->path_string(), js_path).Check(); if (!js_dest.IsEmpty()) - e->Set(env->context(), env->dest_string(), js_dest).Check(); + e->Set(context, env->dest_string(), js_dest).Check(); return e; } @@ -209,19 +212,20 @@ Local WinapiErrnoException(Isolate* isolate, e = Exception::Error(message); } + Local context = env->context(); Local obj = e.As(); - obj->Set(env->context(), env->errno_string(), Integer::New(isolate, errorno)) + obj->Set(context, env->errno_string(), Integer::New(isolate, errorno)) .Check(); if (path != nullptr) { - obj->Set(env->context(), + obj->Set(context, env->path_string(), String::NewFromUtf8(isolate, path).ToLocalChecked()) .Check(); } if (syscall != nullptr) { - obj->Set(env->context(), + obj->Set(context, env->syscall_string(), OneByteString(isolate, syscall)) .Check(); diff --git a/src/api/hooks.cc b/src/api/hooks.cc index c19d8c5b9106f4..4076a5523e0f34 100644 --- a/src/api/hooks.cc +++ b/src/api/hooks.cc @@ -38,14 +38,15 @@ Maybe EmitProcessBeforeExit(Environment* env) { AsyncWrap::DestroyAsyncIdsCallback(env); HandleScope handle_scope(env->isolate()); - Context::Scope context_scope(env->context()); + Local context = env->context(); + Context::Scope context_scope(context); Local exit_code_v; - if (!env->process_object()->Get(env->context(), env->exit_code_string()) + if (!env->process_object()->Get(context, env->exit_code_string()) .ToLocal(&exit_code_v)) return Nothing(); Local exit_code; - if (!exit_code_v->ToInteger(env->context()).ToLocal(&exit_code)) { + if (!exit_code_v->ToInteger(context).ToLocal(&exit_code)) { return Nothing(); } @@ -59,8 +60,10 @@ int EmitExit(Environment* env) { Maybe EmitProcessExit(Environment* env) { // process.emit('exit') - HandleScope handle_scope(env->isolate()); - Context::Scope context_scope(env->context()); + Isolate* isolate = env->isolate(); + HandleScope handle_scope(isolate); + Local context = env->context(); + Context::Scope context_scope(context); Local process_object = env->process_object(); // TODO(addaleax): It might be nice to share process._exiting and @@ -68,19 +71,19 @@ Maybe EmitProcessExit(Environment* env) { // native side, so that we don't manually have to read and write JS properties // here. These getters could use e.g. a typed array for performance. if (process_object - ->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "_exiting"), - True(env->isolate())).IsNothing()) return Nothing(); + ->Set(context, + FIXED_ONE_BYTE_STRING(isolate, "_exiting"), + True(isolate)).IsNothing()) return Nothing(); Local exit_code = env->exit_code_string(); Local code_v; int code; - if (!process_object->Get(env->context(), exit_code).ToLocal(&code_v) || - !code_v->Int32Value(env->context()).To(&code) || - ProcessEmit(env, "exit", Integer::New(env->isolate(), code)).IsEmpty() || + if (!process_object->Get(context, exit_code).ToLocal(&code_v) || + !code_v->Int32Value(context).To(&code) || + ProcessEmit(env, "exit", Integer::New(isolate, code)).IsEmpty() || // Reload exit code, it may be changed by `emit('exit')` - !process_object->Get(env->context(), exit_code).ToLocal(&code_v) || - !code_v->Int32Value(env->context()).To(&code)) { + !process_object->Get(context, exit_code).ToLocal(&code_v) || + !code_v->Int32Value(context).To(&code)) { return Nothing(); }