refactor: only pass v8::Context to gin_helper::MicrotasksScope constructor (#45484)

refactor: forward v8::Context to v8::MicrotasksScope constructor
This commit is contained in:
Milan Burda 2025-02-07 02:44:19 +01:00 committed by GitHub
parent 326957009a
commit 517935cd55
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 23 additions and 37 deletions

View file

@ -240,8 +240,7 @@ void ElectronBindings::DidReceiveMemoryDump(
v8::Local<v8::Context> local_context =
v8::Local<v8::Context>::New(isolate, context);
gin_helper::MicrotasksScope microtasks_scope{
isolate, local_context->GetMicrotaskQueue(), true,
v8::MicrotasksScope::kRunMicrotasks};
local_context, true, v8::MicrotasksScope::kRunMicrotasks};
v8::Context::Scope context_scope(local_context);
if (!success) {

View file

@ -51,8 +51,7 @@ struct V8FunctionInvoker<v8::Local<v8::Value>(ArgTypes...)> {
v8::Local<v8::Function> holder = function.NewHandle(isolate);
v8::Local<v8::Context> context = holder->GetCreationContextChecked();
gin_helper::MicrotasksScope microtasks_scope{
isolate, context->GetMicrotaskQueue(), true,
v8::MicrotasksScope::kRunMicrotasks};
context, true, v8::MicrotasksScope::kRunMicrotasks};
v8::Context::Scope context_scope(context);
std::array<v8::Local<v8::Value>, sizeof...(raw)> args{
gin::ConvertToV8(isolate, std::forward<ArgTypes>(raw))...};
@ -77,8 +76,7 @@ struct V8FunctionInvoker<void(ArgTypes...)> {
v8::Local<v8::Function> holder = function.NewHandle(isolate);
v8::Local<v8::Context> context = holder->GetCreationContextChecked();
gin_helper::MicrotasksScope microtasks_scope{
isolate, context->GetMicrotaskQueue(), true,
v8::MicrotasksScope::kRunMicrotasks};
context, true, v8::MicrotasksScope::kRunMicrotasks};
v8::Context::Scope context_scope(context);
std::array<v8::Local<v8::Value>, sizeof...(raw)> args{
gin::ConvertToV8(isolate, std::forward<ArgTypes>(raw))...};
@ -102,8 +100,7 @@ struct V8FunctionInvoker<ReturnType(ArgTypes...)> {
v8::Local<v8::Function> holder = function.NewHandle(isolate);
v8::Local<v8::Context> context = holder->GetCreationContextChecked();
gin_helper::MicrotasksScope microtasks_scope{
isolate, context->GetMicrotaskQueue(), true,
v8::MicrotasksScope::kRunMicrotasks};
context, true, v8::MicrotasksScope::kRunMicrotasks};
v8::Context::Scope context_scope(context);
std::array<v8::Local<v8::Value>, sizeof...(raw)> args{
gin::ConvertToV8(isolate, std::forward<ArgTypes>(raw))...};

View file

@ -25,7 +25,7 @@ v8::Local<v8::Value> CallMethodWithArgs(
// Perform microtask checkpoint after running JavaScript.
gin_helper::MicrotasksScope microtasks_scope{
isolate, obj->GetCreationContextChecked()->GetMicrotaskQueue(), true,
obj->GetCreationContextChecked(), true,
v8::MicrotasksScope::kRunMicrotasks};
// node::MakeCallback will also run pending tasks in Node.js.

View file

@ -268,8 +268,7 @@ class Invoker<std::index_sequence<indices...>, ArgTypes...>
void DispatchToCallback(
base::RepeatingCallback<ReturnType(ArgTypes...)> callback) {
gin_helper::MicrotasksScope microtasks_scope{
args_->isolate(),
args_->GetHolderCreationContext()->GetMicrotaskQueue(), true,
args_->GetHolderCreationContext(), true,
v8::MicrotasksScope::kRunMicrotasks};
args_->Return(
callback.Run(std::move(ArgumentHolder<indices, ArgTypes>::value)...));
@ -280,8 +279,7 @@ class Invoker<std::index_sequence<indices...>, ArgTypes...>
// that have the void return type.
void DispatchToCallback(base::RepeatingCallback<void(ArgTypes...)> callback) {
gin_helper::MicrotasksScope microtasks_scope{
args_->isolate(),
args_->GetHolderCreationContext()->GetMicrotaskQueue(), true,
args_->GetHolderCreationContext(), true,
v8::MicrotasksScope::kRunMicrotasks};
callback.Run(std::move(ArgumentHolder<indices, ArgTypes>::value)...);
}

View file

@ -5,13 +5,15 @@
#include "shell/common/gin_helper/microtasks_scope.h"
#include "shell/common/process_util.h"
#include "v8/include/v8-context.h"
namespace gin_helper {
MicrotasksScope::MicrotasksScope(v8::Isolate* isolate,
v8::MicrotaskQueue* microtask_queue,
MicrotasksScope::MicrotasksScope(v8::Local<v8::Context> context,
bool ignore_browser_checkpoint,
v8::MicrotasksScope::Type scope_type) {
auto* isolate = context->GetIsolate();
auto* microtask_queue = context->GetMicrotaskQueue();
if (electron::IsBrowserProcess()) {
if (!ignore_browser_checkpoint)
v8::MicrotasksScope::PerformCheckpoint(isolate);

View file

@ -16,8 +16,7 @@ namespace gin_helper {
// In the render process creates a v8::MicrotasksScope.
class MicrotasksScope {
public:
MicrotasksScope(v8::Isolate* isolate,
v8::MicrotaskQueue* microtask_queue,
MicrotasksScope(v8::Local<v8::Context> context,
bool ignore_browser_checkpoint,
v8::MicrotasksScope::Type scope_type);
~MicrotasksScope();

View file

@ -17,8 +17,7 @@ namespace gin_helper {
PromiseBase::SettleScope::SettleScope(const PromiseBase& base)
: handle_scope_{base.isolate()},
context_{base.GetContext()},
microtasks_scope_{base.isolate(), context_->GetMicrotaskQueue(), false,
v8::MicrotasksScope::kRunMicrotasks},
microtasks_scope_{context_, false, v8::MicrotasksScope::kRunMicrotasks},
context_scope_{context_} {}
PromiseBase::SettleScope::~SettleScope() = default;

View file

@ -288,8 +288,7 @@ void ErrorMessageListener(v8::Local<v8::Message> message,
node::Environment* env = node::Environment::GetCurrent(isolate);
if (env) {
gin_helper::MicrotasksScope microtasks_scope(
isolate, env->context()->GetMicrotaskQueue(), false,
v8::MicrotasksScope::kDoNotRunMicrotasks);
env->context(), false, v8::MicrotasksScope::kDoNotRunMicrotasks);
// Emit the after() hooks now that the exception has been handled.
// Analogous to node/lib/internal/process/execution.js#L176-L180
if (env->async_hooks()->fields()[node::AsyncHooks::kAfter]) {

View file

@ -36,7 +36,7 @@ class V8Serializer : public v8::ValueSerializer::Delegate {
bool Serialize(v8::Local<v8::Value> value, blink::CloneableMessage* out) {
gin_helper::MicrotasksScope microtasks_scope{
isolate_, isolate_->GetCurrentContext()->GetMicrotaskQueue(), false,
isolate_->GetCurrentContext(), false,
v8::MicrotasksScope::kDoNotRunMicrotasks};
WriteBlinkEnvelope(19);