Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use primordials for extensions/timers #11234

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 33 additions & 15 deletions extensions/timers/01_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@

((window) => {
const core = window.Deno.core;
const {
Map,
Date,
Error,
ArrayPrototypePush,
MathMax,
TypeError,
Number,
String,
ArrayPrototypeShift,
ArrayPrototypeIndexOf,
ArrayPrototypeSplice,
MapPrototypeHas,
MapPrototypeDelete,
MapPrototypeSet,
MapPrototypeGet,
FunctionPrototypeBind,
} = window.__bootstrap.primordials;

// Shamelessly cribbed from extensions/fetch/11_streams.js
class AssertionError extends Error {
Expand Down Expand Up @@ -318,7 +336,7 @@
* before next macrotask timer callback is invoked. */
function handleTimerMacrotask() {
if (pendingFireTimers.length > 0) {
fire(pendingFireTimers.shift());
fire(ArrayPrototypeShift(pendingFireTimers));
return pendingFireTimers.length === 0;
}
return true;
Expand Down Expand Up @@ -363,7 +381,7 @@
// With the list dropped, the timer is no longer scheduled.
timer.scheduled = false;
// Place the callback to pending timers to fire.
pendingFireTimers.push(timer);
ArrayPrototypePush(pendingFireTimers, timer);
}
}
setOrClearGlobalTimeout(nextDueNode && nextDueNode.due, now);
Expand All @@ -388,7 +406,7 @@
dueNode = maybeNewDueNode;
}
// Append the newly scheduled timer to the list and mark it as scheduled.
dueNode.timers.push(timer);
ArrayPrototypePush(dueNode.timers, timer);
timer.scheduled = true;
// If the new timer is scheduled to fire before any timer that existed before,
// update the global timeout to reflect this.
Expand All @@ -402,8 +420,8 @@
// If either is true, they are not in tree, and their idMap entry
// will be deleted soon. Remove it from queue.
let index = -1;
if ((index = pendingFireTimers.indexOf(timer)) >= 0) {
pendingFireTimers.splice(index);
if ((index = ArrayPrototypeIndexOf(pendingFireTimers, timer)) >= 0) {
ArrayPrototypeSplice(pendingFireTimers, index);
return;
}
// If timer is not in the 2 pending queues and is unscheduled,
Expand All @@ -430,27 +448,27 @@
} else {
// Multiple timers that are due at the same point in time.
// Remove this timer from the list.
const index = list.indexOf(timer);
const index = ArrayPrototypeIndexOf(list, timer);
assert(index > -1);
list.splice(index, 1);
ArrayPrototypeSplice(list, index, 1);
}
}

function fire(timer) {
// If the timer isn't found in the ID map, that means it has been cancelled
// between the timer firing and the promise callback (this function).
if (!idMap.has(timer.id)) {
if (!MapPrototypeHas(idMap, timer.id)) {
return;
}
// Reschedule the timer if it is a repeating one, otherwise drop it.
if (!timer.repeat) {
// One-shot timer: remove the timer from this id-to-timer map.
idMap.delete(timer.id);
MapPrototypeDelete(idMap, timer.id);
} else {
// Interval timer: compute when timer was supposed to fire next.
// However make sure to never schedule the next interval in the past.
const now = OriginalDateNow();
timer.due = Math.max(now, timer.due + timer.delay);
timer.due = MathMax(now, timer.due + timer.delay);
schedule(timer, now);
}
// Call the user callback. Intermediate assignment is to avoid leaking `this`
Expand Down Expand Up @@ -480,7 +498,7 @@
let callback;

if ("function" === typeof cb) {
callback = Function.prototype.bind.call(cb, globalThis, ...args);
callback = FunctionPrototypeBind(cb, globalThis, ...args);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lucacasonato
I'm not too familiar or sure about this, but the tests seem to pass

} else {
callback = String(cb);
args = []; // args are ignored
Expand All @@ -497,7 +515,7 @@
);
delay = 1;
}
delay = Math.max(0, delay | 0);
delay = MathMax(0, delay | 0);

// Create a new, unscheduled timer object.
const timer = {
Expand All @@ -510,7 +528,7 @@
scheduled: false,
};
// Register the timer's existence in the id-to-timer map.
idMap.set(timer.id, timer);
MapPrototypeSet(idMap, timer.id, timer);
// Schedule the timer in the due table.
schedule(timer, now);
return timer.id;
Expand Down Expand Up @@ -538,14 +556,14 @@

function clearTimer(id) {
id >>>= 0;
const timer = idMap.get(id);
const timer = MapPrototypeGet(idMap, id);
if (timer === undefined) {
// Timer doesn't exist any more or never existed. This is not an error.
return;
}
// Unschedule the timer if it is currently scheduled, and forget about it.
unschedule(timer);
idMap.delete(timer.id);
MapPrototypeDelete(idMap, timer.id);
}

function clearTimeout(id = 0) {
Expand Down
7 changes: 5 additions & 2 deletions extensions/timers/02_performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"use strict";

((window) => {
const { ArrayPrototypePush, Symbol, TypeError, JSON, Object } =
window.__bootstrap.primordials;

const { webidl, structuredClone } = window.__bootstrap;
const { opNow } = window.__bootstrap.timers;
const illegalConstructorKey = Symbol("illegalConstructorKey");
Expand Down Expand Up @@ -262,7 +265,7 @@
// throw a SyntaxError. - not implemented
const entry = new PerformanceMark(markName, options);
// 3.1.1.7 Queue entry - not implemented
performanceEntries.push(entry);
ArrayPrototypePush(performanceEntries, entry);
return entry;
}

Expand Down Expand Up @@ -343,7 +346,7 @@
: null,
illegalConstructorKey,
);
performanceEntries.push(entry);
ArrayPrototypePush(performanceEntries, entry);
return entry;
}

Expand Down