2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2021-07-05 13:39:33 -04:00
|
|
|
const {
|
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeShift,
|
|
|
|
Error,
|
2021-12-07 07:39:58 -05:00
|
|
|
FunctionPrototypeCall,
|
2021-07-05 13:39:33 -04:00
|
|
|
Map,
|
|
|
|
MapPrototypeDelete,
|
|
|
|
MapPrototypeGet,
|
|
|
|
MapPrototypeHas,
|
|
|
|
MapPrototypeSet,
|
2021-12-07 07:39:58 -05:00
|
|
|
// deno-lint-ignore camelcase
|
|
|
|
NumberPOSITIVE_INFINITY,
|
|
|
|
PromisePrototypeThen,
|
2022-02-01 12:06:11 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2022-02-07 07:54:32 -05:00
|
|
|
SafeArrayIterator,
|
2021-12-09 03:00:55 -05:00
|
|
|
SymbolFor,
|
2021-07-05 13:39:33 -04:00
|
|
|
TypeError,
|
|
|
|
} = window.__bootstrap.primordials;
|
2021-12-07 07:39:58 -05:00
|
|
|
const { webidl } = window.__bootstrap;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-04-30 15:51:48 -04:00
|
|
|
// Shamelessly cribbed from extensions/fetch/11_streams.js
|
2021-04-14 15:10:48 -04:00
|
|
|
class AssertionError extends Error {
|
|
|
|
constructor(msg) {
|
|
|
|
super(msg);
|
|
|
|
this.name = "AssertionError";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {unknown} cond
|
|
|
|
* @param {string=} msg
|
|
|
|
* @returns {asserts cond}
|
|
|
|
*/
|
|
|
|
function assert(cond, msg = "Assertion failed.") {
|
|
|
|
if (!cond) {
|
|
|
|
throw new AssertionError(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
function opNow() {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_now");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2020-10-15 21:06:31 -04:00
|
|
|
function sleepSync(millis = 0) {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_sleep_sync", millis);
|
2020-10-15 21:06:31 -04:00
|
|
|
}
|
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
/**
|
|
|
|
* The task queue corresponding to the timer task source.
|
|
|
|
*
|
|
|
|
* @type { {action: () => void, nestingLevel: number}[] }
|
|
|
|
*/
|
|
|
|
const timerTasks = [];
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
/**
|
|
|
|
* 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;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
function handleTimerMacrotask() {
|
|
|
|
if (timerTasks.length === 0) {
|
|
|
|
return true;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
const task = ArrayPrototypeShift(timerTasks);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
timerNestingLevel = task.nestingLevel;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
try {
|
|
|
|
task.action();
|
|
|
|
} finally {
|
|
|
|
timerNestingLevel = 0;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-12-07 07:39:58 -05:00
|
|
|
return timerTasks.length === 0;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2021-12-09 03:00:55 -05:00
|
|
|
* @type {Map<number, { cancelRid: number, isRef: boolean, promiseId: number }>}
|
2021-12-07 07:39:58 -05:00
|
|
|
*/
|
|
|
|
const activeTimers = new Map();
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
let nextId = 1;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
/**
|
|
|
|
* @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,
|
|
|
|
) {
|
|
|
|
// 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;
|
2021-12-09 03:00:55 -05:00
|
|
|
let timerInfo;
|
2021-12-07 07:39:58 -05:00
|
|
|
if (prevId !== undefined) {
|
|
|
|
// `prevId` is only passed for follow-up calls on intervals
|
|
|
|
assert(repeat);
|
|
|
|
id = prevId;
|
2021-12-09 03:00:55 -05:00
|
|
|
timerInfo = MapPrototypeGet(activeTimers, id);
|
2021-12-07 07:39:58 -05:00
|
|
|
} else {
|
|
|
|
// TODO(@andreubotella): Deal with overflow.
|
|
|
|
// https://github.com/whatwg/html/issues/7358
|
|
|
|
id = nextId++;
|
2021-12-09 03:00:55 -05:00
|
|
|
const cancelRid = core.opSync("op_timer_handle");
|
|
|
|
timerInfo = { cancelRid, isRef: true, promiseId: -1 };
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
// Step 4 in "run steps after a timeout".
|
2021-12-09 03:00:55 -05:00
|
|
|
MapPrototypeSet(activeTimers, id, timerInfo);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-12-07 07:39:58 -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;
|
|
|
|
if (timerNestingLevel > 5 && timeout < 4) timeout = 4;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
// 2.
|
|
|
|
// 3.
|
|
|
|
// TODO(@andreubotella): Error handling.
|
|
|
|
if (typeof callback === "function") {
|
2022-02-07 07:54:32 -05:00
|
|
|
FunctionPrototypeCall(
|
|
|
|
callback,
|
|
|
|
globalThis,
|
|
|
|
...new SafeArrayIterator(args),
|
|
|
|
);
|
2021-12-07 07:39:58 -05:00
|
|
|
} else {
|
|
|
|
// TODO(@andreubotella): eval doesn't seem to have a primordial, but
|
|
|
|
// it can be redefined in the global scope.
|
|
|
|
(0, eval)(callback);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
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);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-12-07 07:39:58 -05:00
|
|
|
} else {
|
|
|
|
// 6. Otherwise, remove global's map of active timers[id].
|
2021-12-09 03:00:55 -05:00
|
|
|
core.tryClose(timerInfo.cancelRid);
|
2021-12-07 07:39:58 -05:00
|
|
|
MapPrototypeDelete(activeTimers, id);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-12-07 07:39:58 -05:00
|
|
|
},
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
// 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
|
|
|
|
2021-12-07 07:39:58 -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(
|
|
|
|
() => ArrayPrototypePush(timerTasks, task),
|
|
|
|
timeout,
|
2021-12-09 03:00:55 -05:00
|
|
|
timerInfo,
|
2021-12-07 07:39:58 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
return id;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
/**
|
|
|
|
* @typedef ScheduledTimer
|
|
|
|
* @property {number} millis
|
|
|
|
* @property {() => void} cb
|
|
|
|
* @property {boolean} resolved
|
|
|
|
* @property {ScheduledTimer | null} prev
|
|
|
|
* @property {ScheduledTimer | null} next
|
|
|
|
*/
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
/**
|
|
|
|
* A doubly linked list of timers.
|
|
|
|
* @type { { head: ScheduledTimer | null, tail: ScheduledTimer | null } }
|
|
|
|
*/
|
|
|
|
const scheduledTimers = { head: null, tail: null };
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
/**
|
|
|
|
* @param {() => void} cb Will be run after the timeout, if it hasn't been
|
|
|
|
* cancelled.
|
|
|
|
* @param {number} millis
|
2021-12-09 03:00:55 -05:00
|
|
|
* @param {{ cancelRid: number, isRef: boolean, promiseId: number }} timerInfo
|
2021-12-07 07:39:58 -05:00
|
|
|
*/
|
2021-12-09 03:00:55 -05:00
|
|
|
function runAfterTimeout(cb, millis, timerInfo) {
|
|
|
|
const cancelRid = timerInfo.cancelRid;
|
|
|
|
const sleepPromise = core.opAsync("op_sleep", millis, cancelRid);
|
|
|
|
timerInfo.promiseId =
|
|
|
|
sleepPromise[SymbolFor("Deno.core.internalPromiseId")];
|
|
|
|
if (!timerInfo.isRef) {
|
|
|
|
core.unrefOp(timerInfo.promiseId);
|
|
|
|
}
|
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
/** @type {ScheduledTimer} */
|
|
|
|
const timerObject = {
|
|
|
|
millis,
|
|
|
|
cb,
|
|
|
|
resolved: false,
|
|
|
|
prev: scheduledTimers.tail,
|
|
|
|
next: null,
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
// Add timerObject to the end of the list.
|
|
|
|
if (scheduledTimers.tail === null) {
|
|
|
|
assert(scheduledTimers.head === null);
|
|
|
|
scheduledTimers.head = scheduledTimers.tail = timerObject;
|
2020-07-19 13:49:44 -04:00
|
|
|
} else {
|
2021-12-07 07:39:58 -05:00
|
|
|
scheduledTimers.tail.next = timerObject;
|
|
|
|
scheduledTimers.tail = timerObject;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
// 1.
|
|
|
|
PromisePrototypeThen(
|
2021-12-09 03:00:55 -05:00
|
|
|
sleepPromise,
|
2021-12-07 07:39:58 -05:00
|
|
|
() => {
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
timerObject.resolved = true;
|
|
|
|
|
|
|
|
let lowestUnresolvedTimeout = NumberPOSITIVE_INFINITY;
|
|
|
|
|
|
|
|
let currentEntry = scheduledTimers.head;
|
|
|
|
while (currentEntry !== null) {
|
|
|
|
if (currentEntry.millis < lowestUnresolvedTimeout) {
|
|
|
|
if (currentEntry.resolved) {
|
|
|
|
currentEntry.cb();
|
|
|
|
removeFromScheduledTimers(currentEntry);
|
|
|
|
} else {
|
|
|
|
lowestUnresolvedTimeout = currentEntry.millis;
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
currentEntry = currentEntry.next;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err) => {
|
2022-02-01 12:06:11 -05:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(core.InterruptedPrototype, err)) {
|
2021-12-07 07:39:58 -05:00
|
|
|
// The timer was cancelled.
|
|
|
|
removeFromScheduledTimers(timerObject);
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
/** @param {ScheduledTimer} timerObj */
|
|
|
|
function removeFromScheduledTimers(timerObj) {
|
|
|
|
if (timerObj.prev !== null) {
|
|
|
|
timerObj.prev.next = timerObj.next;
|
2020-07-19 13:49:44 -04:00
|
|
|
} else {
|
2021-12-07 07:39:58 -05:00
|
|
|
assert(scheduledTimers.head === timerObj);
|
|
|
|
scheduledTimers.head = timerObj.next;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-12-07 07:39:58 -05:00
|
|
|
if (timerObj.next !== null) {
|
|
|
|
timerObj.next.prev = timerObj.prev;
|
2021-01-06 08:53:30 -05:00
|
|
|
} else {
|
2021-12-07 07:39:58 -05:00
|
|
|
assert(scheduledTimers.tail === timerObj);
|
|
|
|
scheduledTimers.tail = timerObj.prev;
|
2021-01-06 08:53:30 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
function checkThis(thisArg) {
|
|
|
|
if (thisArg !== null && thisArg !== undefined && thisArg !== globalThis) {
|
|
|
|
throw new TypeError("Illegal invocation");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 07:39:58 -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
|
|
|
}
|
2021-12-07 07:39:58 -05:00
|
|
|
timeout = webidl.converters.long(timeout);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
return initializeTimer(callback, timeout, args, false);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
function setInterval(callback, timeout = 0, ...args) {
|
2020-07-19 13:49:44 -04:00
|
|
|
checkThis(this);
|
2021-12-07 07:39:58 -05:00
|
|
|
if (typeof callback !== "function") {
|
|
|
|
callback = webidl.converters.DOMString(callback);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-12-07 07:39:58 -05:00
|
|
|
timeout = webidl.converters.long(timeout);
|
|
|
|
|
|
|
|
return initializeTimer(callback, timeout, args, true);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function clearTimeout(id = 0) {
|
2021-12-07 07:39:58 -05:00
|
|
|
checkThis(this);
|
|
|
|
id = webidl.converters.long(id);
|
2021-12-09 03:00:55 -05:00
|
|
|
const timerInfo = MapPrototypeGet(activeTimers, id);
|
|
|
|
if (timerInfo !== undefined) {
|
|
|
|
core.tryClose(timerInfo.cancelRid);
|
2021-12-07 07:39:58 -05:00
|
|
|
MapPrototypeDelete(activeTimers, id);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearInterval(id = 0) {
|
2021-12-07 07:39:58 -05:00
|
|
|
checkThis(this);
|
|
|
|
clearTimeout(id);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-12-09 03:00:55 -05:00
|
|
|
function refTimer(id) {
|
|
|
|
const timerInfo = MapPrototypeGet(activeTimers, id);
|
|
|
|
if (timerInfo === undefined || timerInfo.isRef) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
timerInfo.isRef = true;
|
|
|
|
core.refOp(timerInfo.promiseId);
|
|
|
|
}
|
|
|
|
|
|
|
|
function unrefTimer(id) {
|
|
|
|
const timerInfo = MapPrototypeGet(activeTimers, id);
|
|
|
|
if (timerInfo === undefined || !timerInfo.isRef) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
timerInfo.isRef = false;
|
|
|
|
core.unrefOp(timerInfo.promiseId);
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
window.__bootstrap.timers = {
|
2021-12-07 07:39:58 -05:00
|
|
|
setTimeout,
|
2020-07-19 13:49:44 -04:00
|
|
|
setInterval,
|
|
|
|
clearTimeout,
|
2021-12-07 07:39:58 -05:00
|
|
|
clearInterval,
|
2020-07-19 13:49:44 -04:00
|
|
|
handleTimerMacrotask,
|
|
|
|
opNow,
|
2020-10-15 21:06:31 -04:00
|
|
|
sleepSync,
|
2021-12-09 03:00:55 -05:00
|
|
|
refTimer,
|
|
|
|
unrefTimer,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
})(this);
|