From adda0c56d29f054502de96cfc2921a6b10d101b8 Mon Sep 17 00:00:00 2001 From: Alessio Attilio Date: Thu, 27 Aug 2026 12:55:28 +0200 Subject: [PATCH] vm: support shared microtask queues Add `vm.MicrotaskQueue` and `options.microtaskQueue` to allow multiple contexts to share an explicit V8 microtask queue and let users control synchronous checkpoint draining. Fixes: https://github.com/nodejs/node/issues/65555 Signed-off-by: Alessio Attilio Assisted-by: Antigravity --- doc/api/vm.md | 58 +++++++ lib/vm.js | 36 ++++- src/env_properties.h | 1 + src/node_contextify.cc | 100 ++++++++++++- src/node_contextify.h | 36 ++++- .../test-vm-shared-microtask-queue.js | 141 ++++++++++++++++++ 6 files changed, 363 insertions(+), 9 deletions(-) create mode 100644 test/parallel/test-vm-shared-microtask-queue.js diff --git a/doc/api/vm.md b/doc/api/vm.md index 14cd1f269b12..4152a3024113 100644 --- a/doc/api/vm.md +++ b/doc/api/vm.md @@ -1318,6 +1318,31 @@ added: A `ModuleRequest` represents the request to import a module with given import attributes and phase. +## Class: `vm.MicrotaskQueue` + + + +Represents an explicit microtask queue that can be shared across multiple +`vm.Context` instances and synchronously drained by the embedder. + +### `new vm.MicrotaskQueue()` + + + +Creates a new `vm.MicrotaskQueue` instance. + +### `microtaskQueue.runMicrotasks()` + + + +Synchronously runs all microtasks currently queued in this microtask queue. + ## `vm.compileFunction(code[, params[, options]])` + +* Returns: {vm.MicrotaskQueue} + +Creates a new [`vm.MicrotaskQueue`][] instance. Shortcut to +`new vm.MicrotaskQueue()`. + +## `vm.isMicrotaskQueue(object)` + + + +* `object` {any} +* Returns: {boolean} + +Returns `true` if the given `object` is an instance of [`vm.MicrotaskQueue`][]. + ## `vm.measureMemory([options])`