Skip to content

Commit 2070d5b

Browse files
committed
src: use correct microtask queue for checkpoints
I missed in c6c8337 that we should not just use that queue for enqueuing microtasks, but also for running them. Refs: #36482 PR-URL: #36581 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
1 parent 82dd23f commit 2070d5b

3 files changed

Lines changed: 34 additions & 17 deletions

File tree

src/api/callback.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ using v8::HandleScope;
1212
using v8::Isolate;
1313
using v8::Local;
1414
using v8::MaybeLocal;
15-
using v8::MicrotasksScope;
1615
using v8::Object;
1716
using v8::String;
1817
using v8::Value;
@@ -111,7 +110,7 @@ void InternalCallbackScope::Close() {
111110
auto weakref_cleanup = OnScopeLeave([&]() { env_->RunWeakRefCleanup(); });
112111

113112
if (!tick_info->has_tick_scheduled()) {
114-
MicrotasksScope::PerformCheckpoint(env_->isolate());
113+
env_->context()->GetMicrotaskQueue()->PerformCheckpoint(env_->isolate());
115114

116115
perform_stopping_check();
117116
}

src/node_task_queue.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ using v8::kPromiseRejectAfterResolved;
2121
using v8::kPromiseRejectWithNoHandler;
2222
using v8::kPromiseResolveAfterResolved;
2323
using v8::Local;
24-
using v8::MicrotasksScope;
2524
using v8::Number;
2625
using v8::Object;
2726
using v8::Promise;
@@ -101,7 +100,8 @@ static void EnqueueMicrotask(const FunctionCallbackInfo<Value>& args) {
101100
}
102101

103102
static void RunMicrotasks(const FunctionCallbackInfo<Value>& args) {
104-
MicrotasksScope::PerformCheckpoint(args.GetIsolate());
103+
Environment* env = Environment::GetCurrent(args);
104+
env->context()->GetMicrotaskQueue()->PerformCheckpoint(env->isolate());
105105
}
106106

107107
static void SetTickCallback(const FunctionCallbackInfo<Value>& args) {

test/cctest/test_environment.cc

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -622,13 +622,14 @@ TEST_F(EnvironmentTest, NestedMicrotaskQueue) {
622622
node::InitializeContext(context);
623623
v8::Context::Scope context_scope(context);
624624

625-
int callback_calls = 0;
625+
using IntVec = std::vector<int>;
626+
IntVec callback_calls;
626627
v8::Local<v8::Function> must_call = v8::Function::New(
627628
context,
628629
[](const v8::FunctionCallbackInfo<v8::Value>& info) {
629-
int* callback_calls =
630-
static_cast<int*>(info.Data().As<v8::External>()->Value());
631-
*callback_calls |= info[0].As<v8::Int32>()->Value();
630+
IntVec* callback_calls = static_cast<IntVec*>(
631+
info.Data().As<v8::External>()->Value());
632+
callback_calls->push_back(info[0].As<v8::Int32>()->Value());
632633
},
633634
v8::External::New(isolate_, static_cast<void*>(&callback_calls)))
634635
.ToLocalChecked();
@@ -645,23 +646,40 @@ TEST_F(EnvironmentTest, NestedMicrotaskQueue) {
645646
isolate_data, context, {}, {});
646647
CHECK_NE(nullptr, env);
647648

648-
node::LoadEnvironment(
649+
v8::Local<v8::Function> eval_in_env = node::LoadEnvironment(
649650
env,
650-
"Promise.resolve().then(() => mustCall(1 << 0));\n"
651+
"mustCall(1);\n"
652+
"Promise.resolve().then(() => mustCall(2));\n"
651653
"require('vm').runInNewContext("
652-
" 'Promise.resolve().then(() => mustCall(1 << 1))',"
654+
" 'Promise.resolve().then(() => mustCall(3))',"
653655
" { mustCall },"
654656
" { microtaskMode: 'afterEvaluate' }"
655-
");"
657+
");\n"
656658
"require('vm').runInNewContext("
657-
" 'Promise.resolve().then(() => mustCall(1 << 2))',"
659+
" 'Promise.resolve().then(() => mustCall(4))',"
658660
" { mustCall }"
659-
");").ToLocalChecked();
660-
EXPECT_EQ(callback_calls, 1 << 1);
661+
");\n"
662+
"setTimeout(() => {"
663+
" Promise.resolve().then(() => mustCall(5));"
664+
"}, 10);\n"
665+
"mustCall(6);\n"
666+
"return eval;\n").ToLocalChecked().As<v8::Function>();
667+
EXPECT_EQ(callback_calls, (IntVec { 1, 3, 6, 2, 4 }));
668+
v8::Local<v8::Value> queue_microtask_code = v8::String::NewFromUtf8Literal(
669+
isolate_, "queueMicrotask(() => mustCall(7));");
670+
eval_in_env->Call(context,
671+
v8::Null(isolate_),
672+
1,
673+
&queue_microtask_code).ToLocalChecked();
674+
EXPECT_EQ(callback_calls, (IntVec { 1, 3, 6, 2, 4 }));
661675
isolate_->PerformMicrotaskCheckpoint();
662-
EXPECT_EQ(callback_calls, 1 << 1);
676+
EXPECT_EQ(callback_calls, (IntVec { 1, 3, 6, 2, 4 }));
663677
queue->PerformCheckpoint(isolate_);
664-
EXPECT_EQ(callback_calls, (1 << 0) | (1 << 1) | (1 << 2));
678+
EXPECT_EQ(callback_calls, (IntVec { 1, 3, 6, 2, 4, 7 }));
679+
680+
int exit_code = SpinEventLoop(env).FromJust();
681+
EXPECT_EQ(exit_code, 0);
682+
EXPECT_EQ(callback_calls, (IntVec { 1, 3, 6, 2, 4, 7, 5 }));
665683

666684
node::FreeEnvironment(env);
667685
node::FreeIsolateData(isolate_data);

0 commit comments

Comments
 (0)