Files
v8/test/mjsunit/queue-microtask.js
Leszek Swirski e78c1a4e05 Reland "[runtime] Implement queueMicrotask"
This is a reland of commit 1bfece1550

Fixes:
  * Change the context passed to the CallableTask to the CreationContext
    of the callable, matching HTML spec. This is observable through
    onerror (this is how WPT tests caught it) so added an mjsunit test
    matching this
  * As a drive-by, CallableTask now explicitly stores a NativeContext
    rather than a Context.
  * Made queueMicrotask enumerable.
  * Most importantly, put it behind a flag (--enable-queue-microtask) so
    that it doesn't immediately break blink if something is wrong.

Original change's description:
> [runtime] Implement queueMicrotask
>
> This change introduces the global `queueMicrotask` function, which
> enqueues a microtask. The function takes one argument, a callback, and
> throws a TypeError if the argument is not a callable object. The
> enqueued microtask is a CallableTask that will be executed during the
> next microtask checkpoint. Tests are added to verify the function's
> behavior, including error handling for non-callable arguments and the
> execution order relative to other microtasks.
>
> Bug: 481610393
> Change-Id: Ic7d883d4abbbea4ec4be78715261eef8eeafa5b7
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/7544459
> Commit-Queue: Leszek Swirski <leszeks@chromium.org>
> Auto-Submit: Leszek Swirski <leszeks@chromium.org>
> Reviewed-by: Igor Sheludko <ishell@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#105113}

Bug: 481610393
Change-Id: Idc85afe610935ca110ea973d984d4c35747750e3
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/7566483
Auto-Submit: Leszek Swirski <leszeks@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Leszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/main@{#105208}
2026-02-11 07:40:18 -08:00

43 lines
1.2 KiB
JavaScript

// Copyright 2026 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --allow-natives-syntax --enable-queue-microtask
(function TestQueueMicrotask() {
assertDoesNotThrow(() => queueMicrotask(() => {}));
})();
(function TestQueueMicrotaskNotAFunction() {
assertThrows(() => queueMicrotask(), TypeError);
assertThrows(() => queueMicrotask(undefined), TypeError);
assertThrows(() => queueMicrotask(null), TypeError);
assertThrows(() => queueMicrotask(1), TypeError);
assertThrows(() => queueMicrotask('1'), TypeError);
assertThrows(() => queueMicrotask({}), TypeError);
assertThrows(() => queueMicrotask([]), TypeError);
assertThrows(() => queueMicrotask(Symbol()), TypeError);
})();
(function TestQueueMicrotaskOrder() {
let result = '';
queueMicrotask(() => result += 'a');
result += 'b';
Promise.resolve().then(() => result += 'c');
%PerformMicrotaskCheckpoint();
assertEquals('bac', result);
})();
(function TestQueueMicrotaskNested() {
let result = '';
queueMicrotask(() => {
result += 'a';
queueMicrotask(() => result += 'c');
});
result += 'b';
%PerformMicrotaskCheckpoint();
assertEquals('bac', result);
})();