Skip to content

Commit 4bf051d

Browse files
addaleaxCommit Bot
authored andcommitted
[api] Add Context::GetMicrotaskQueue method
Add a method that returns the microtask queue that is being used by the `v8::Context`. This is helpful in non-monolithic embedders like Node.js, which accept Contexts created by its own embedders like Electron, or for native Node.js addons. In particular, it enables: 1. Making sure that “nested” `Context`s use the correct microtask queue, i.e. the one from the outer Context. 2. Enqueueing microtasks into the correct microtask queue. Previously, these things only worked when the microtask queue for a given Context was the Isolate’s default queue. As an alternative, I considered adding a way to make new `Context`s inherit the queue from the `Context` that was entered at the time of their creation, but that seemed a bit more “magic”, less flexible, and didn’t take care of concern 2 listed above. Change-Id: I15ed796df90f23c97a545a8e1b30a3bf4a5c4320 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2579914 Reviewed-by: Toon Verwaest <verwaest@chromium.org> Commit-Queue: Toon Verwaest <verwaest@chromium.org> Cr-Commit-Position: refs/heads/master@{#71710}
1 parent 8ff422a commit 4bf051d

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

include/v8.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10554,9 +10554,12 @@ class V8_EXPORT Context {
1055410554
*/
1055510555
void Exit();
1055610556

10557-
/** Returns an isolate associated with a current context. */
10557+
/** Returns the isolate associated with a current context. */
1055810558
Isolate* GetIsolate();
1055910559

10560+
/** Returns the microtask queue associated with a current context. */
10561+
MicrotaskQueue* GetMicrotaskQueue();
10562+
1056010563
/**
1056110564
* The field at kDebugIdIndex used to be reserved for the inspector.
1056210565
* It now serves no purpose.

src/api/api.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6261,6 +6261,12 @@ v8::Isolate* Context::GetIsolate() {
62616261
return reinterpret_cast<Isolate*>(env->GetIsolate());
62626262
}
62636263

6264+
v8::MicrotaskQueue* Context::GetMicrotaskQueue() {
6265+
i::Handle<i::Context> env = Utils::OpenHandle(this);
6266+
CHECK(env->IsNativeContext());
6267+
return i::Handle<i::NativeContext>::cast(env)->microtask_queue();
6268+
}
6269+
62646270
v8::Local<v8::Object> Context::Global() {
62656271
i::Handle<i::Context> context = Utils::OpenHandle(this);
62666272
i::Isolate* isolate = context->GetIsolate();

test/cctest/test-api.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28801,3 +28801,13 @@ UNINITIALIZED_TEST(SingleThreadedDefaultPlatform) {
2880128801
isolate->Dispose();
2880228802
i::V8::SetPlatformForTesting(old_platform);
2880328803
}
28804+
28805+
THREADED_TEST(MicrotaskQueueOfContext) {
28806+
auto microtask_queue = v8::MicrotaskQueue::New(CcTest::isolate());
28807+
v8::HandleScope scope(CcTest::isolate());
28808+
v8::Local<Context> context = Context::New(
28809+
CcTest::isolate(), nullptr, v8::MaybeLocal<ObjectTemplate>(),
28810+
v8::MaybeLocal<Value>(), v8::DeserializeInternalFieldsCallback(),
28811+
microtask_queue.get());
28812+
CHECK_EQ(context->GetMicrotaskQueue(), microtask_queue.get());
28813+
}

0 commit comments

Comments
 (0)