Skip to content

Commit bbc0d14

Browse files
addaleaxtargos
authored andcommitted
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 7c903ec commit bbc0d14

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

src/api/callback.cc

+1-2
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

+2-2
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

+31-13
Original file line numberDiff line numberDiff line change
@@ -629,13 +629,14 @@ TEST_F(EnvironmentTest, NestedMicrotaskQueue) {
629629
node::InitializeContext(context);
630630
v8::Context::Scope context_scope(context);
631631

632-
int callback_calls = 0;
632+
using IntVec = std::vector<int>;
633+
IntVec callback_calls;
633634
v8::Local<v8::Function> must_call = v8::Function::New(
634635
context,
635636
[](const v8::FunctionCallbackInfo<v8::Value>& info) {
636-
int* callback_calls =
637-
static_cast<int*>(info.Data().As<v8::External>()->Value());
638-
*callback_calls |= info[0].As<v8::Int32>()->Value();
637+
IntVec* callback_calls = static_cast<IntVec*>(
638+
info.Data().As<v8::External>()->Value());
639+
callback_calls->push_back(info[0].As<v8::Int32>()->Value());
639640
},
640641
v8::External::New(isolate_, static_cast<void*>(&callback_calls)))
641642
.ToLocalChecked();
@@ -652,23 +653,40 @@ TEST_F(EnvironmentTest, NestedMicrotaskQueue) {
652653
isolate_data, context, {}, {});
653654
CHECK_NE(nullptr, env);
654655

655-
node::LoadEnvironment(
656+
v8::Local<v8::Function> eval_in_env = node::LoadEnvironment(
656657
env,
657-
"Promise.resolve().then(() => mustCall(1 << 0));\n"
658+
"mustCall(1);\n"
659+
"Promise.resolve().then(() => mustCall(2));\n"
658660
"require('vm').runInNewContext("
659-
" 'Promise.resolve().then(() => mustCall(1 << 1))',"
661+
" 'Promise.resolve().then(() => mustCall(3))',"
660662
" { mustCall },"
661663
" { microtaskMode: 'afterEvaluate' }"
662-
");"
664+
");\n"
663665
"require('vm').runInNewContext("
664-
" 'Promise.resolve().then(() => mustCall(1 << 2))',"
666+
" 'Promise.resolve().then(() => mustCall(4))',"
665667
" { mustCall }"
666-
");").ToLocalChecked();
667-
EXPECT_EQ(callback_calls, 1 << 1);
668+
");\n"
669+
"setTimeout(() => {"
670+
" Promise.resolve().then(() => mustCall(5));"
671+
"}, 10);\n"
672+
"mustCall(6);\n"
673+
"return eval;\n").ToLocalChecked().As<v8::Function>();
674+
EXPECT_EQ(callback_calls, (IntVec { 1, 3, 6, 2, 4 }));
675+
v8::Local<v8::Value> queue_microtask_code = v8::String::NewFromUtf8Literal(
676+
isolate_, "queueMicrotask(() => mustCall(7));");
677+
eval_in_env->Call(context,
678+
v8::Null(isolate_),
679+
1,
680+
&queue_microtask_code).ToLocalChecked();
681+
EXPECT_EQ(callback_calls, (IntVec { 1, 3, 6, 2, 4 }));
668682
isolate_->PerformMicrotaskCheckpoint();
669-
EXPECT_EQ(callback_calls, 1 << 1);
683+
EXPECT_EQ(callback_calls, (IntVec { 1, 3, 6, 2, 4 }));
670684
queue->PerformCheckpoint(isolate_);
671-
EXPECT_EQ(callback_calls, (1 << 0) | (1 << 1) | (1 << 2));
685+
EXPECT_EQ(callback_calls, (IntVec { 1, 3, 6, 2, 4, 7 }));
686+
687+
int exit_code = SpinEventLoop(env).FromJust();
688+
EXPECT_EQ(exit_code, 0);
689+
EXPECT_EQ(callback_calls, (IntVec { 1, 3, 6, 2, 4, 7, 5 }));
672690

673691
node::FreeEnvironment(env);
674692
node::FreeIsolateData(isolate_data);

0 commit comments

Comments
 (0)