2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-12-07 08:21:01 -05:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
const ops = core.ops;
|
|
|
|
const {
|
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeShift,
|
|
|
|
FunctionPrototypeCall,
|
|
|
|
MapPrototypeDelete,
|
|
|
|
MapPrototypeGet,
|
|
|
|
MapPrototypeHas,
|
|
|
|
MapPrototypeSet,
|
|
|
|
Uint8Array,
|
|
|
|
Uint32Array,
|
|
|
|
PromisePrototypeThen,
|
|
|
|
SafeArrayIterator,
|
2023-04-14 16:23:28 -04:00
|
|
|
SafeMap,
|
2023-04-02 13:41:41 -04:00
|
|
|
TypedArrayPrototypeGetBuffer,
|
2023-02-07 14:22:46 -05:00
|
|
|
TypeError,
|
|
|
|
indirectEval,
|
|
|
|
} = primordials;
|
2023-03-08 06:44:54 -05:00
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
|
|
|
import { reportException } from "ext:deno_web/02_event.js";
|
|
|
|
import { assert } from "ext:deno_web/00_infra.js";
|
2023-06-06 05:01:28 -04:00
|
|
|
const { op_sleep, op_void_async_deferred } = core.ensureFastOps();
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
const hrU8 = new Uint8Array(8);
|
2023-04-02 13:41:41 -04:00
|
|
|
const hr = new Uint32Array(TypedArrayPrototypeGetBuffer(hrU8));
|
2023-02-07 14:22:46 -05:00
|
|
|
function opNow() {
|
|
|
|
ops.op_now(hrU8);
|
|
|
|
return (hr[0] * 1000 + hr[1] / 1e6);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The task queue corresponding to the timer task source.
|
|
|
|
*
|
|
|
|
* @type { {action: () => void, nestingLevel: number}[] }
|
|
|
|
*/
|
|
|
|
const timerTasks = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current task's timer nesting level, or zero if we're not currently
|
|
|
|
* running a timer task (since the minimum nesting level is 1).
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
let timerNestingLevel = 0;
|
|
|
|
|
|
|
|
function handleTimerMacrotask() {
|
2023-06-14 10:21:06 -04:00
|
|
|
// We have no work to do, tell the runtime that we don't
|
|
|
|
// need to perform microtask checkpoint.
|
2023-02-07 14:22:46 -05:00
|
|
|
if (timerTasks.length === 0) {
|
2023-06-14 10:21:06 -04:00
|
|
|
return undefined;
|
2021-12-07 07:39:58 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const task = ArrayPrototypeShift(timerTasks);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
timerNestingLevel = task.nestingLevel;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
try {
|
|
|
|
task.action();
|
|
|
|
} finally {
|
|
|
|
timerNestingLevel = 0;
|
|
|
|
}
|
|
|
|
return timerTasks.length === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The keys in this map correspond to the key ID's in the spec's map of active
|
|
|
|
* timers. The values are the timeout's cancel rid.
|
|
|
|
*
|
2023-11-09 15:57:26 -05:00
|
|
|
* @type {Map<number, { cancelRid: number, isRef: boolean, promise: Promise<void> }>}
|
2023-02-07 14:22:46 -05:00
|
|
|
*/
|
2023-04-14 16:23:28 -04:00
|
|
|
const activeTimers = new SafeMap();
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
let nextId = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Function | string} callback
|
|
|
|
* @param {number} timeout
|
|
|
|
* @param {Array<any>} args
|
|
|
|
* @param {boolean} repeat
|
|
|
|
* @param {number | undefined} prevId
|
|
|
|
* @returns {number} The timer ID
|
|
|
|
*/
|
|
|
|
function initializeTimer(
|
|
|
|
callback,
|
|
|
|
timeout,
|
|
|
|
args,
|
|
|
|
repeat,
|
|
|
|
prevId,
|
2023-05-22 18:19:44 -04:00
|
|
|
// TODO(bartlomieju): remove this option, once `nextTick` and `setImmediate`
|
|
|
|
// in Node compat are cleaned up
|
|
|
|
respectNesting = true,
|
2023-02-07 14:22:46 -05:00
|
|
|
) {
|
|
|
|
// 2. If previousId was given, let id be previousId; otherwise, let
|
|
|
|
// previousId be an implementation-defined integer than is greater than zero
|
|
|
|
// and does not already exist in global's map of active timers.
|
|
|
|
let id;
|
|
|
|
let timerInfo;
|
|
|
|
if (prevId !== undefined) {
|
|
|
|
// `prevId` is only passed for follow-up calls on intervals
|
|
|
|
assert(repeat);
|
|
|
|
id = prevId;
|
|
|
|
timerInfo = MapPrototypeGet(activeTimers, id);
|
|
|
|
} else {
|
|
|
|
// TODO(@andreubotella): Deal with overflow.
|
|
|
|
// https://github.com/whatwg/html/issues/7358
|
|
|
|
id = nextId++;
|
|
|
|
const cancelRid = ops.op_timer_handle();
|
2023-11-09 15:57:26 -05:00
|
|
|
timerInfo = { cancelRid, isRef: true, promise: null };
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
// Step 4 in "run steps after a timeout".
|
|
|
|
MapPrototypeSet(activeTimers, id, timerInfo);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3. If the surrounding agent's event loop's currently running task is a
|
|
|
|
// task that was created by this algorithm, then let nesting level be the
|
|
|
|
// task's timer nesting level. Otherwise, let nesting level be zero.
|
|
|
|
// 4. If timeout is less than 0, then set timeout to 0.
|
|
|
|
// 5. If nesting level is greater than 5, and timeout is less than 4, then
|
|
|
|
// set timeout to 4.
|
|
|
|
//
|
|
|
|
// The nesting level of 5 and minimum of 4 ms are spec-mandated magic
|
|
|
|
// constants.
|
|
|
|
if (timeout < 0) timeout = 0;
|
2023-05-22 18:19:44 -04:00
|
|
|
if (timerNestingLevel > 5 && timeout < 4 && respectNesting) timeout = 4;
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
// 9. Let task be a task that runs the following steps:
|
|
|
|
const task = {
|
|
|
|
action: () => {
|
|
|
|
// 1. If id does not exist in global's map of active timers, then abort
|
|
|
|
// these steps.
|
|
|
|
//
|
|
|
|
// This is relevant if the timer has been canceled after the sleep op
|
|
|
|
// resolves but before this task runs.
|
|
|
|
if (!MapPrototypeHas(activeTimers, id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2.
|
|
|
|
// 3.
|
|
|
|
if (typeof callback === "function") {
|
|
|
|
try {
|
|
|
|
FunctionPrototypeCall(
|
|
|
|
callback,
|
|
|
|
globalThis,
|
|
|
|
...new SafeArrayIterator(args),
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
reportException(error);
|
2022-10-17 07:29:16 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
|
|
|
indirectEval(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (repeat) {
|
|
|
|
if (MapPrototypeHas(activeTimers, id)) {
|
|
|
|
// 4. If id does not exist in global's map of active timers, then
|
|
|
|
// abort these steps.
|
|
|
|
// NOTE: If might have been removed via the author code in handler
|
|
|
|
// calling clearTimeout() or clearInterval().
|
|
|
|
// 5. If repeat is true, then perform the timer initialization steps
|
|
|
|
// again, given global, handler, timeout, arguments, true, and id.
|
|
|
|
initializeTimer(callback, timeout, args, true, id);
|
2021-12-07 07:39:58 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
|
|
|
// 6. Otherwise, remove global's map of active timers[id].
|
|
|
|
core.tryClose(timerInfo.cancelRid);
|
|
|
|
MapPrototypeDelete(activeTimers, id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// 10. Increment nesting level by one.
|
|
|
|
// 11. Set task's timer nesting level to nesting level.
|
|
|
|
nestingLevel: timerNestingLevel + 1,
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 12. Let completionStep be an algorithm step which queues a global task on
|
|
|
|
// the timer task source given global to run task.
|
|
|
|
// 13. Run steps after a timeout given global, "setTimeout/setInterval",
|
|
|
|
// timeout, completionStep, and id.
|
|
|
|
runAfterTimeout(
|
2023-06-14 11:04:49 -04:00
|
|
|
task,
|
2023-02-07 14:22:46 -05:00
|
|
|
timeout,
|
|
|
|
timerInfo,
|
|
|
|
);
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef ScheduledTimer
|
|
|
|
* @property {number} millis
|
2023-06-14 11:04:49 -04:00
|
|
|
* @property { {action: () => void, nestingLevel: number}[] } task
|
2023-02-07 14:22:46 -05:00
|
|
|
* @property {boolean} resolved
|
|
|
|
* @property {ScheduledTimer | null} prev
|
|
|
|
* @property {ScheduledTimer | null} next
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A doubly linked list of timers.
|
|
|
|
* @type { { head: ScheduledTimer | null, tail: ScheduledTimer | null } }
|
|
|
|
*/
|
|
|
|
const scheduledTimers = { head: null, tail: null };
|
|
|
|
|
|
|
|
/**
|
2023-06-14 11:04:49 -04:00
|
|
|
* @param { {action: () => void, nestingLevel: number}[] } task Will be run
|
|
|
|
* after the timeout, if it hasn't been cancelled.
|
2023-02-07 14:22:46 -05:00
|
|
|
* @param {number} millis
|
2023-11-09 15:57:26 -05:00
|
|
|
* @param {{ cancelRid: number, isRef: boolean, promise: Promise<void> }} timerInfo
|
2023-02-07 14:22:46 -05:00
|
|
|
*/
|
2023-06-14 11:04:49 -04:00
|
|
|
function runAfterTimeout(task, millis, timerInfo) {
|
2023-02-07 14:22:46 -05:00
|
|
|
const cancelRid = timerInfo.cancelRid;
|
2023-05-22 08:09:31 -04:00
|
|
|
let sleepPromise;
|
|
|
|
// If this timeout is scheduled for 0ms it means we want it to run at the
|
|
|
|
// end of the event loop turn. There's no point in setting up a Tokio timer,
|
|
|
|
// since its lowest resolution is 1ms. Firing of a "void async" op is better
|
|
|
|
// in this case, because the timer will take closer to 0ms instead of >1ms.
|
|
|
|
if (millis === 0) {
|
|
|
|
sleepPromise = op_void_async_deferred();
|
|
|
|
} else {
|
|
|
|
sleepPromise = op_sleep(millis, cancelRid);
|
|
|
|
}
|
2023-11-09 15:57:26 -05:00
|
|
|
timerInfo.promise = sleepPromise;
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!timerInfo.isRef) {
|
2023-11-09 15:57:26 -05:00
|
|
|
core.unrefOpPromise(timerInfo.promise);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @type {ScheduledTimer} */
|
|
|
|
const timerObject = {
|
|
|
|
millis,
|
|
|
|
resolved: false,
|
|
|
|
prev: scheduledTimers.tail,
|
|
|
|
next: null,
|
2023-08-10 00:01:35 -04:00
|
|
|
task,
|
2023-02-07 14:22:46 -05:00
|
|
|
};
|
2021-12-07 07:39:58 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Add timerObject to the end of the list.
|
|
|
|
if (scheduledTimers.tail === null) {
|
|
|
|
assert(scheduledTimers.head === null);
|
|
|
|
scheduledTimers.head = scheduledTimers.tail = timerObject;
|
|
|
|
} else {
|
|
|
|
scheduledTimers.tail.next = timerObject;
|
|
|
|
scheduledTimers.tail = timerObject;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 1.
|
|
|
|
PromisePrototypeThen(
|
|
|
|
sleepPromise,
|
|
|
|
(cancelled) => {
|
2023-06-14 11:04:49 -04:00
|
|
|
if (timerObject.resolved) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-22 08:09:31 -04:00
|
|
|
// "op_void_async_deferred" returns null
|
|
|
|
if (cancelled !== null && !cancelled) {
|
2023-02-07 14:22:46 -05:00
|
|
|
// The timer was cancelled.
|
|
|
|
removeFromScheduledTimers(timerObject);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// 2. Wait until any invocations of this algorithm that had the same
|
|
|
|
// global and orderingIdentifier, that started before this one, and
|
|
|
|
// whose milliseconds is equal to or less than this one's, have
|
|
|
|
// completed.
|
|
|
|
// 4. Perform completionSteps.
|
|
|
|
|
|
|
|
// IMPORTANT: Since the sleep ops aren't guaranteed to resolve in the
|
|
|
|
// right order, whenever one resolves, we run through the scheduled
|
|
|
|
// timers list (which is in the order in which they were scheduled), and
|
|
|
|
// we call the callback for every timer which both:
|
|
|
|
// a) has resolved, and
|
|
|
|
// b) its timeout is lower than the lowest unresolved timeout found so
|
|
|
|
// far in the list.
|
|
|
|
|
|
|
|
let currentEntry = scheduledTimers.head;
|
|
|
|
while (currentEntry !== null) {
|
2023-06-14 11:04:49 -04:00
|
|
|
if (currentEntry.millis <= timerObject.millis) {
|
|
|
|
currentEntry.resolved = true;
|
2023-08-10 00:01:35 -04:00
|
|
|
ArrayPrototypePush(timerTasks, currentEntry.task);
|
2023-06-14 11:04:49 -04:00
|
|
|
removeFromScheduledTimers(currentEntry);
|
|
|
|
|
|
|
|
if (currentEntry === timerObject) {
|
|
|
|
break;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
currentEntry = currentEntry.next;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param {ScheduledTimer} timerObj */
|
|
|
|
function removeFromScheduledTimers(timerObj) {
|
|
|
|
if (timerObj.prev !== null) {
|
|
|
|
timerObj.prev.next = timerObj.next;
|
|
|
|
} else {
|
|
|
|
assert(scheduledTimers.head === timerObj);
|
|
|
|
scheduledTimers.head = timerObj.next;
|
|
|
|
}
|
|
|
|
if (timerObj.next !== null) {
|
|
|
|
timerObj.next.prev = timerObj.prev;
|
|
|
|
} else {
|
|
|
|
assert(scheduledTimers.tail === timerObj);
|
|
|
|
scheduledTimers.tail = timerObj.prev;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2021-12-07 07:39:58 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function checkThis(thisArg) {
|
|
|
|
if (thisArg !== null && thisArg !== undefined && thisArg !== globalThis) {
|
|
|
|
throw new TypeError("Illegal invocation");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function setTimeout(callback, timeout = 0, ...args) {
|
|
|
|
checkThis(this);
|
|
|
|
if (typeof callback !== "function") {
|
|
|
|
callback = webidl.converters.DOMString(callback);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
timeout = webidl.converters.long(timeout);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return initializeTimer(callback, timeout, args, false);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function setInterval(callback, timeout = 0, ...args) {
|
|
|
|
checkThis(this);
|
|
|
|
if (typeof callback !== "function") {
|
|
|
|
callback = webidl.converters.DOMString(callback);
|
2021-12-09 03:00:55 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
timeout = webidl.converters.long(timeout);
|
|
|
|
|
|
|
|
return initializeTimer(callback, timeout, args, true);
|
|
|
|
}
|
|
|
|
|
2023-05-22 18:19:44 -04:00
|
|
|
// TODO(bartlomieju): remove this option, once `nextTick` and `setImmediate`
|
|
|
|
// in Node compat are cleaned up
|
|
|
|
function setTimeoutUnclamped(callback, timeout = 0, ...args) {
|
|
|
|
checkThis(this);
|
|
|
|
if (typeof callback !== "function") {
|
|
|
|
callback = webidl.converters.DOMString(callback);
|
|
|
|
}
|
|
|
|
timeout = webidl.converters.long(timeout);
|
|
|
|
|
|
|
|
return initializeTimer(callback, timeout, args, false, undefined, false);
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function clearTimeout(id = 0) {
|
|
|
|
checkThis(this);
|
|
|
|
id = webidl.converters.long(id);
|
|
|
|
const timerInfo = MapPrototypeGet(activeTimers, id);
|
|
|
|
if (timerInfo !== undefined) {
|
|
|
|
core.tryClose(timerInfo.cancelRid);
|
|
|
|
MapPrototypeDelete(activeTimers, id);
|
2021-12-09 03:00:55 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-12-09 03:00:55 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function clearInterval(id = 0) {
|
|
|
|
checkThis(this);
|
|
|
|
clearTimeout(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
function refTimer(id) {
|
|
|
|
const timerInfo = MapPrototypeGet(activeTimers, id);
|
|
|
|
if (timerInfo === undefined || timerInfo.isRef) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
timerInfo.isRef = true;
|
2023-11-09 15:57:26 -05:00
|
|
|
core.refOpPromise(timerInfo.promise);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function unrefTimer(id) {
|
|
|
|
const timerInfo = MapPrototypeGet(activeTimers, id);
|
|
|
|
if (timerInfo === undefined || !timerInfo.isRef) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
timerInfo.isRef = false;
|
2023-11-09 15:57:26 -05:00
|
|
|
core.unrefOpPromise(timerInfo.promise);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-12-24 08:04:32 -05:00
|
|
|
// Defer to avoid starving the event loop. Not using queueMicrotask()
|
|
|
|
// for that reason: it lets promises make forward progress but can
|
|
|
|
// still starve other parts of the event loop.
|
|
|
|
function defer(go) {
|
|
|
|
PromisePrototypeThen(op_void_async_deferred(), () => go());
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export {
|
|
|
|
clearInterval,
|
|
|
|
clearTimeout,
|
2023-12-24 08:04:32 -05:00
|
|
|
defer,
|
2023-02-07 14:22:46 -05:00
|
|
|
handleTimerMacrotask,
|
|
|
|
opNow,
|
|
|
|
refTimer,
|
|
|
|
setInterval,
|
|
|
|
setTimeout,
|
2023-05-22 18:19:44 -04:00
|
|
|
setTimeoutUnclamped,
|
2023-02-07 14:22:46 -05:00
|
|
|
unrefTimer,
|
|
|
|
};
|