From c46dd776dfd28d3e1b18bda98a78a1d4e1fcad5a Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 11 Dec 2020 17:15:54 +0100 Subject: [PATCH] deps: V8: backport 4bf051d536a1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Original commit message: [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 Commit-Queue: Toon Verwaest Cr-Commit-Position: refs/heads/master@{#71710} Refs: https://github.com/v8/v8/commit/4bf051d536a172e7932845d82f918ad7280c7873 PR-URL: https://github.com/nodejs/node/pull/36482 Reviewed-By: Gus Caplan Reviewed-By: James M Snell Reviewed-By: Rich Trott --- common.gypi | 2 +- deps/v8/include/v8.h | 5 ++++- deps/v8/src/api/api.cc | 6 ++++++ deps/v8/test/cctest/test-api.cc | 10 ++++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/common.gypi b/common.gypi index 8fdb6d36df0b74..bc7a232129701a 100644 --- a/common.gypi +++ b/common.gypi @@ -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.67', + 'v8_embedder_string': '-node.68', ##### V8 defaults for Node.js ##### diff --git a/deps/v8/include/v8.h b/deps/v8/include/v8.h index fe31e4cac2823b..92dda10f494a84 100644 --- a/deps/v8/include/v8.h +++ b/deps/v8/include/v8.h @@ -10381,9 +10381,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. diff --git a/deps/v8/src/api/api.cc b/deps/v8/src/api/api.cc index 3b6226b0f438d3..aa87a92ca6cc08 100644 --- a/deps/v8/src/api/api.cc +++ b/deps/v8/src/api/api.cc @@ -6000,6 +6000,12 @@ v8::Isolate* Context::GetIsolate() { return reinterpret_cast(env->GetIsolate()); } +v8::MicrotaskQueue* Context::GetMicrotaskQueue() { + i::Handle env = Utils::OpenHandle(this); + CHECK(env->IsNativeContext()); + return i::Handle::cast(env)->microtask_queue(); +} + v8::Local Context::Global() { i::Handle context = Utils::OpenHandle(this); i::Isolate* isolate = context->GetIsolate(); diff --git a/deps/v8/test/cctest/test-api.cc b/deps/v8/test/cctest/test-api.cc index 18f7738033f8db..8038e9942eefc3 100644 --- a/deps/v8/test/cctest/test-api.cc +++ b/deps/v8/test/cctest/test-api.cc @@ -27402,3 +27402,13 @@ TEST(FastApiCalls) { // are compared against the slow optimized calls. #endif // V8_LITE_MODE } + +THREADED_TEST(MicrotaskQueueOfContext) { + auto microtask_queue = v8::MicrotaskQueue::New(CcTest::isolate()); + v8::HandleScope scope(CcTest::isolate()); + v8::Local context = Context::New( + CcTest::isolate(), nullptr, v8::MaybeLocal(), + v8::MaybeLocal(), v8::DeserializeInternalFieldsCallback(), + microtask_queue.get()); + CHECK_EQ(context->GetMicrotaskQueue(), microtask_queue.get()); +}