refactor: avoid deprecated v8::Context::GetIsolate() calls (pt 2) (#47896)

* refactor: add a v8::Isolate* arg to Constructible::GetConstructor()

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: add a v8::Isolate* arg to NodeBindings::Initialize()

This is needed for the GetConstructor() call

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: avoid v8::Context::GetIsolate() call in GetIpcObject() by taking it as an arg

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: avoid v8::Context::GetIsolate() call in ipc_native::EmitIPCEvent() by taking it as an arg

Co-authored-by: Charles Kerr <charles@charleskerr.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
trop[bot] 2025-07-29 10:34:18 -04:00 committed by GitHub
commit e1e12318e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 42 additions and 36 deletions

View file

@ -327,7 +327,7 @@ void Initialize(v8::Local<v8::Object> exports,
void* priv) {
v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate();
gin_helper::Dictionary dict{isolate, exports};
dict.Set("Menu", Menu::GetConstructor(context));
dict.Set("Menu", Menu::GetConstructor(isolate, context));
#if BUILDFLAG(IS_MAC)
dict.SetMethod("setApplicationMenu", &Menu::SetApplicationMenu);
dict.SetMethod("sendActionToFirstResponder",

View file

@ -253,7 +253,7 @@ void Initialize(v8::Local<v8::Object> exports,
void* priv) {
v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate();
gin_helper::Dictionary dict{isolate, exports};
dict.Set("Notification", Notification::GetConstructor(context));
dict.Set("Notification", Notification::GetConstructor(isolate, context));
dict.SetMethod("isSupported", &Notification::IsSupported);
}

View file

@ -362,7 +362,8 @@ void Initialize(v8::Local<v8::Object> exports,
void* priv) {
v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate();
gin_helper::Dictionary dict{isolate, exports};
dict.Set("Protocol", electron::api::Protocol::GetConstructor(context));
dict.Set("Protocol",
electron::api::Protocol::GetConstructor(isolate, context));
dict.SetMethod("registerSchemesAsPrivileged", &RegisterSchemesAsPrivileged);
dict.SetMethod("getStandardSchemes", &electron::api::GetStandardSchemes);
}

View file

@ -356,7 +356,8 @@ void Initialize(v8::Local<v8::Object> exports,
void* priv) {
v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate();
gin_helper::Dictionary dict{isolate, exports};
dict.Set("ServiceWorkerMain", ServiceWorkerMain::GetConstructor(context));
dict.Set("ServiceWorkerMain",
ServiceWorkerMain::GetConstructor(isolate, context));
}
} // namespace

View file

@ -1869,7 +1869,7 @@ void Initialize(v8::Local<v8::Object> exports,
void* priv) {
v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate();
gin_helper::Dictionary dict(isolate, exports);
dict.Set("Session", Session::GetConstructor(context));
dict.Set("Session", Session::GetConstructor(isolate, context));
dict.SetMethod("fromPartition", &FromPartition);
dict.SetMethod("fromPath", &FromPath);
}

View file

@ -447,7 +447,7 @@ void Initialize(v8::Local<v8::Object> exports,
void* priv) {
v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate();
gin::Dictionary dict{isolate, exports};
dict.Set("Tray", Tray::GetConstructor(context));
dict.Set("Tray", Tray::GetConstructor(isolate, context));
}
} // namespace

View file

@ -4662,7 +4662,7 @@ void Initialize(v8::Local<v8::Object> exports,
void* priv) {
v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate();
gin_helper::Dictionary dict{isolate, exports};
dict.Set("WebContents", WebContents::GetConstructor(context));
dict.Set("WebContents", WebContents::GetConstructor(isolate, context));
dict.SetMethod("fromId", &WebContentsFromID);
dict.SetMethod("fromFrame", &WebContentsFromFrame);
dict.SetMethod("fromDevToolsTargetId", &WebContentsFromDevToolsTargetID);

View file

@ -675,7 +675,7 @@ void Initialize(v8::Local<v8::Object> exports,
void* priv) {
v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate();
gin_helper::Dictionary dict{isolate, exports};
dict.Set("WebFrameMain", WebFrameMain::GetConstructor(context));
dict.Set("WebFrameMain", WebFrameMain::GetConstructor(isolate, context));
dict.SetMethod("fromId", &FromID);
dict.SetMethod("_fromIdIfExists", &FromIdIfExists);
dict.SetMethod("_fromFtnIdIfExists", &FromFtnIdIfExists);

View file

@ -232,13 +232,14 @@ void ElectronBrowserMainParts::PostEarlyInitialization() {
// avoid conflicts we only initialize our V8 environment after that.
js_env_ = std::make_unique<JavascriptEnvironment>(node_bindings_->uv_loop());
v8::HandleScope scope(js_env_->isolate());
v8::Isolate* const isolate = js_env_->isolate();
v8::HandleScope scope(isolate);
node_bindings_->Initialize(js_env_->isolate()->GetCurrentContext());
node_bindings_->Initialize(isolate, isolate->GetCurrentContext());
// Create the global environment.
node_env_ = node_bindings_->CreateEnvironment(
js_env_->isolate(), js_env_->isolate()->GetCurrentContext(),
js_env_->platform(), js_env_->max_young_generation_size_in_bytes());
isolate, isolate->GetCurrentContext(), js_env_->platform(),
js_env_->max_young_generation_size_in_bytes());
node_env_->set_trace_sync_io(node_env_->options()->trace_sync_io);
@ -246,7 +247,7 @@ void ElectronBrowserMainParts::PostEarlyInitialization() {
node_env_->options()->unhandled_rejections = "warn-with-error-code";
// Add Electron extended APIs.
electron_bindings_->BindTo(js_env_->isolate(), node_env_->process_object());
electron_bindings_->BindTo(isolate, node_env_->process_object());
// Create explicit microtasks runner.
js_env_->CreateMicrotasksRunner();

View file

@ -40,8 +40,8 @@ template <typename T>
class Constructible {
public:
static v8::Local<v8::Function> GetConstructor(
v8::Isolate* const isolate,
v8::Local<v8::Context> context) {
v8::Isolate* isolate = context->GetIsolate();
gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
auto* wrapper_info = &T::kWrapperInfo;
v8::Local<v8::FunctionTemplate> constructor =

View file

@ -576,7 +576,8 @@ std::vector<std::string> NodeBindings::ParseNodeCliFlags() {
return args;
}
void NodeBindings::Initialize(v8::Local<v8::Context> context) {
void NodeBindings::Initialize(v8::Isolate* const isolate,
v8::Local<v8::Context> context) {
TRACE_EVENT0("electron", "NodeBindings::Initialize");
// Open node's error reporting system for browser process.
@ -635,7 +636,7 @@ void NodeBindings::Initialize(v8::Local<v8::Context> context) {
SetErrorMode(GetErrorMode() & ~SEM_NOGPFAULTERRORBOX);
#endif
gin_helper::internal::Event::GetConstructor(context);
gin_helper::internal::Event::GetConstructor(isolate, context);
g_is_initialized = true;
}

View file

@ -125,7 +125,7 @@ class NodeBindings {
virtual ~NodeBindings();
// Setup V8, libuv.
void Initialize(v8::Local<v8::Context> context);
void Initialize(v8::Isolate* isolate, v8::Local<v8::Context> context);
std::vector<std::string> ParseNodeCliFlags();

View file

@ -98,7 +98,7 @@ void ElectronApiServiceImpl::Message(bool internal,
v8::Local<v8::Value> args = gin::ConvertToV8(isolate, arguments);
ipc_native::EmitIPCEvent(context, internal, channel, {}, args);
ipc_native::EmitIPCEvent(isolate, context, internal, channel, {}, args);
}
void ElectronApiServiceImpl::ReceivePostMessage(
@ -125,7 +125,7 @@ void ElectronApiServiceImpl::ReceivePostMessage(
std::vector<v8::Local<v8::Value>> args = {message_value};
ipc_native::EmitIPCEvent(context, false, channel, ports,
ipc_native::EmitIPCEvent(isolate, context, false, channel, ports,
gin::ConvertToV8(isolate, args));
}

View file

@ -19,8 +19,8 @@ namespace {
constexpr std::string_view kIpcKey = "ipcNative";
// Gets the private object under kIpcKey
v8::Local<v8::Object> GetIpcObject(const v8::Local<v8::Context>& context) {
auto* isolate = context->GetIsolate();
v8::Local<v8::Object> GetIpcObject(v8::Isolate* const isolate,
const v8::Local<v8::Context>& context) {
auto binding_key = gin::StringToV8(isolate, kIpcKey);
auto private_binding_key = v8::Private::ForApi(isolate, binding_key);
auto global_object = context->Global();
@ -33,13 +33,13 @@ v8::Local<v8::Object> GetIpcObject(const v8::Local<v8::Context>& context) {
return value->ToObject(context).ToLocalChecked();
}
void InvokeIpcCallback(const v8::Local<v8::Context>& context,
void InvokeIpcCallback(v8::Isolate* const isolate,
const v8::Local<v8::Context>& context,
const std::string& callback_name,
std::vector<v8::Local<v8::Value>> args) {
TRACE_EVENT0("devtools.timeline", "FunctionCall");
auto* isolate = context->GetIsolate();
auto ipcNative = GetIpcObject(context);
auto ipcNative = GetIpcObject(isolate, context);
if (ipcNative.IsEmpty())
return;
@ -62,13 +62,12 @@ void InvokeIpcCallback(const v8::Local<v8::Context>& context,
} // namespace
void EmitIPCEvent(const v8::Local<v8::Context>& context,
void EmitIPCEvent(v8::Isolate* const isolate,
const v8::Local<v8::Context>& context,
bool internal,
const std::string& channel,
std::vector<v8::Local<v8::Value>> ports,
v8::Local<v8::Value> args) {
auto* isolate = context->GetIsolate();
v8::HandleScope handle_scope(isolate);
v8::Context::Scope context_scope(context);
v8::MicrotasksScope script_scope(isolate, context->GetMicrotaskQueue(),
@ -78,7 +77,7 @@ void EmitIPCEvent(const v8::Local<v8::Context>& context,
gin::ConvertToV8(isolate, internal), gin::ConvertToV8(isolate, channel),
gin::ConvertToV8(isolate, ports), args};
InvokeIpcCallback(context, "onMessage", argv);
InvokeIpcCallback(isolate, context, "onMessage", argv);
}
} // namespace electron::ipc_native

View file

@ -11,7 +11,8 @@
namespace electron::ipc_native {
void EmitIPCEvent(const v8::Local<v8::Context>& context,
void EmitIPCEvent(v8::Isolate* isolate,
const v8::Local<v8::Context>& context,
bool internal,
const std::string& channel,
std::vector<v8::Local<v8::Value>> ports,

View file

@ -105,7 +105,7 @@ void ElectronRendererClient::DidCreateScriptContext(
if (!node_integration_initialized_) {
node_integration_initialized_ = true;
node_bindings_->Initialize(renderer_context);
node_bindings_->Initialize(isolate, renderer_context);
node_bindings_->PrepareEmbedThread();
}

View file

@ -56,7 +56,8 @@ void ServiceWorkerData::Message(bool internal,
v8::Local<v8::Value> args = gin::ConvertToV8(isolate, arguments);
ipc_native::EmitIPCEvent(preload_context, internal, channel, {}, args);
ipc_native::EmitIPCEvent(isolate, preload_context, internal, channel, {},
args);
}
void ServiceWorkerData::ReceivePostMessage(const std::string& channel,

View file

@ -122,9 +122,10 @@ void NodeService::Initialize(
js_env_ = std::make_unique<JavascriptEnvironment>(node_bindings_->uv_loop());
v8::HandleScope scope(js_env_->isolate());
v8::Isolate* const isolate = js_env_->isolate();
v8::HandleScope scope{isolate};
node_bindings_->Initialize(js_env_->isolate()->GetCurrentContext());
node_bindings_->Initialize(isolate, isolate->GetCurrentContext());
// Append program path for process.argv0
auto program = base::CommandLine::ForCurrentProcess()->GetProgram();
@ -136,9 +137,9 @@ void NodeService::Initialize(
// Create the global environment.
node_env_ = node_bindings_->CreateEnvironment(
js_env_->isolate(), js_env_->isolate()->GetCurrentContext(),
js_env_->platform(), js_env_->max_young_generation_size_in_bytes(),
params->args, params->exec_args);
isolate, isolate->GetCurrentContext(), js_env_->platform(),
js_env_->max_young_generation_size_in_bytes(), params->args,
params->exec_args);
// Override the default handler set by NodeBindings.
node_env_->isolate()->SetFatalErrorHandler(V8FatalErrorCallback);