Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: use correct outer Context’s microtask queue #36482

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

# Reset this number to 0 on major V8 upgrades.
# Increment by one for each non-official patch applied to deps/v8.
'v8_embedder_string': '-node.20',
'v8_embedder_string': '-node.21',

##### V8 defaults for Node.js #####

Expand Down
5 changes: 4 additions & 1 deletion deps/v8/include/v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -10417,9 +10417,12 @@ class V8_EXPORT Context {
*/
void Exit();

/** Returns an isolate associated with a current context. */
/** Returns the isolate associated with a current context. */
Isolate* GetIsolate();

/** Returns the microtask queue associated with a current context. */
MicrotaskQueue* GetMicrotaskQueue();

/**
* The field at kDebugIdIndex used to be reserved for the inspector.
* It now serves no purpose.
Expand Down
6 changes: 6 additions & 0 deletions deps/v8/src/api/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6118,6 +6118,12 @@ v8::Isolate* Context::GetIsolate() {
return reinterpret_cast<Isolate*>(env->GetIsolate());
}

v8::MicrotaskQueue* Context::GetMicrotaskQueue() {
i::Handle<i::Context> env = Utils::OpenHandle(this);
CHECK(env->IsNativeContext());
return i::Handle<i::NativeContext>::cast(env)->microtask_queue();
}

v8::Local<v8::Object> Context::Global() {
i::Handle<i::Context> context = Utils::OpenHandle(this);
i::Isolate* isolate = context->GetIsolate();
Expand Down
10 changes: 10 additions & 0 deletions deps/v8/test/cctest/test-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28348,3 +28348,13 @@ TEST(TriggerThreadSafeMetricsEvent) {
CHECK_EQ(recorder->count_, 1); // Increased.
CHECK_EQ(recorder->module_count_, 42);
}

THREADED_TEST(MicrotaskQueueOfContext) {
auto microtask_queue = v8::MicrotaskQueue::New(CcTest::isolate());
v8::HandleScope scope(CcTest::isolate());
v8::Local<Context> context = Context::New(
CcTest::isolate(), nullptr, v8::MaybeLocal<ObjectTemplate>(),
v8::MaybeLocal<Value>(), v8::DeserializeInternalFieldsCallback(),
microtask_queue.get());
CHECK_EQ(context->GetMicrotaskQueue(), microtask_queue.get());
}
3 changes: 2 additions & 1 deletion src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,8 @@ void AsyncWrap::EmitDestroy(Environment* env, double async_id) {
// interrupt to get this Microtask scheduled as fast as possible.
if (env->destroy_async_id_list()->size() == 16384) {
env->RequestInterrupt([](Environment* env) {
env->isolate()->EnqueueMicrotask(
env->context()->GetMicrotaskQueue()->EnqueueMicrotask(
env->isolate(),
[](void* arg) {
DestroyAsyncIdsCallback(static_cast<Environment*>(arg));
}, env);
Expand Down
4 changes: 3 additions & 1 deletion src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context(
object_template,
{}, // global object
{}, // deserialization callback
microtask_queue() ? microtask_queue().get() : nullptr);
microtask_queue() ?
microtask_queue().get() :
env->isolate()->GetCurrentContext()->GetMicrotaskQueue());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

env->context()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's equivalent right now, yes, but I'd like to write this in a more future-proof way if you don't mind :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There will be situations where the active environment doesn't match the active context?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, we could extend Node.js to supporting multiple different Contexts per Environment, which would be nice, and we’re not that far from supporting it

if (ctx.IsEmpty()) return MaybeLocal<Context>();
// Only partially initialize the context - the primordials are left out
// and only initialized when necessary.
Expand Down
3 changes: 2 additions & 1 deletion src/node_task_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ static void EnqueueMicrotask(const FunctionCallbackInfo<Value>& args) {

CHECK(args[0]->IsFunction());

isolate->EnqueueMicrotask(args[0].As<Function>());
isolate->GetCurrentContext()->GetMicrotaskQueue()
->EnqueueMicrotask(isolate, args[0].As<Function>());
}

static void RunMicrotasks(const FunctionCallbackInfo<Value>& args) {
Expand Down
55 changes: 55 additions & 0 deletions test/cctest/test_environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -608,3 +608,58 @@ TEST_F(NodeZeroIsolateTestFixture, CtrlCWithOnlySafeTerminationTest) {
isolate->Dispose();
}
#endif // _WIN32

TEST_F(EnvironmentTest, NestedMicrotaskQueue) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;

std::unique_ptr<v8::MicrotaskQueue> queue = v8::MicrotaskQueue::New(isolate_);
v8::Local<v8::Context> context = v8::Context::New(
isolate_, nullptr, {}, {}, {}, queue.get());
node::InitializeContext(context);
v8::Context::Scope context_scope(context);

int callback_calls = 0;
v8::Local<v8::Function> must_call = v8::Function::New(
context,
[](const v8::FunctionCallbackInfo<v8::Value>& info) {
int* callback_calls =
static_cast<int*>(info.Data().As<v8::External>()->Value());
*callback_calls |= info[0].As<v8::Int32>()->Value();
},
v8::External::New(isolate_, static_cast<void*>(&callback_calls)))
.ToLocalChecked();
context->Global()->Set(
context,
v8::String::NewFromUtf8Literal(isolate_, "mustCall"),
must_call).Check();

node::IsolateData* isolate_data = node::CreateIsolateData(
isolate_, &NodeTestFixture::current_loop, platform.get());
CHECK_NE(nullptr, isolate_data);

node::Environment* env = node::CreateEnvironment(
isolate_data, context, {}, {});
CHECK_NE(nullptr, env);

node::LoadEnvironment(
env,
"Promise.resolve().then(() => mustCall(1 << 0));\n"
"require('vm').runInNewContext("
" 'Promise.resolve().then(() => mustCall(1 << 1))',"
" { mustCall },"
" { microtaskMode: 'afterEvaluate' }"
");"
"require('vm').runInNewContext("
" 'Promise.resolve().then(() => mustCall(1 << 2))',"
" { mustCall }"
");").ToLocalChecked();
EXPECT_EQ(callback_calls, 1 << 1);
isolate_->PerformMicrotaskCheckpoint();
EXPECT_EQ(callback_calls, 1 << 1);
queue->PerformCheckpoint(isolate_);
EXPECT_EQ(callback_calls, (1 << 0) | (1 << 1) | (1 << 2));

node::FreeEnvironment(env);
node::FreeIsolateData(isolate_data);
}