From e1e582a67432f8d24981f9c37559a8f97863dd90 Mon Sep 17 00:00:00 2001 From: Simon Rask Date: Fri, 2 Jul 2021 22:43:25 +0200 Subject: [PATCH 1/3] refactor: use primordials for extensions/timers --- extensions/timers/01_timers.js | 9 +++++---- extensions/timers/02_performance.js | 10 ++++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/extensions/timers/01_timers.js b/extensions/timers/01_timers.js index 66faf85fd7ca79..20b196d69a1f1e 100644 --- a/extensions/timers/01_timers.js +++ b/extensions/timers/01_timers.js @@ -3,6 +3,7 @@ ((window) => { const core = window.Deno.core; + const { Map, Date, Error, ArrayPrototypePush, MathMax, TypeError, Number, String } = window.__bootstrap.primordials; // Shamelessly cribbed from extensions/fetch/11_streams.js class AssertionError extends Error { @@ -363,7 +364,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); @@ -388,7 +389,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. @@ -450,7 +451,7 @@ // 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` @@ -497,7 +498,7 @@ ); delay = 1; } - delay = Math.max(0, delay | 0); + delay = MathMax(0, delay | 0); // Create a new, unscheduled timer object. const timer = { diff --git a/extensions/timers/02_performance.js b/extensions/timers/02_performance.js index bca98fdbd1bc19..ead520499472c0 100644 --- a/extensions/timers/02_performance.js +++ b/extensions/timers/02_performance.js @@ -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"); @@ -153,8 +156,7 @@ [customInspect]() { return this.detail - ? `${this.constructor.name} {\n detail: ${ - JSON.stringify(this.detail, null, 2) + ? `${this.constructor.name} {\n detail: ${JSON.stringify(this.detail, null, 2) },\n name: "${this.name}",\n entryType: "${this.entryType}",\n startTime: ${this.startTime},\n duration: ${this.duration}\n}` : `${this.constructor.name} { detail: ${this.detail}, name: "${this.name}", entryType: "${this.entryType}", startTime: ${this.startTime}, duration: ${this.duration} }`; } @@ -262,7 +264,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; } @@ -343,7 +345,7 @@ : null, illegalConstructorKey, ); - performanceEntries.push(entry); + ArrayPrototypePush(performanceEntries, entry); return entry; } From b81c990319440af034021442727df621921e091b Mon Sep 17 00:00:00 2001 From: Simon Rask Date: Fri, 2 Jul 2021 22:48:12 +0200 Subject: [PATCH 2/3] fmt --- extensions/timers/01_timers.js | 11 ++++++++++- extensions/timers/02_performance.js | 3 ++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/extensions/timers/01_timers.js b/extensions/timers/01_timers.js index 20b196d69a1f1e..cc8d61efdd8408 100644 --- a/extensions/timers/01_timers.js +++ b/extensions/timers/01_timers.js @@ -3,7 +3,16 @@ ((window) => { const core = window.Deno.core; - const { Map, Date, Error, ArrayPrototypePush, MathMax, TypeError, Number, String } = window.__bootstrap.primordials; + const { + Map, + Date, + Error, + ArrayPrototypePush, + MathMax, + TypeError, + Number, + String, + } = window.__bootstrap.primordials; // Shamelessly cribbed from extensions/fetch/11_streams.js class AssertionError extends Error { diff --git a/extensions/timers/02_performance.js b/extensions/timers/02_performance.js index ead520499472c0..ab1b79770c8fa8 100644 --- a/extensions/timers/02_performance.js +++ b/extensions/timers/02_performance.js @@ -156,7 +156,8 @@ [customInspect]() { return this.detail - ? `${this.constructor.name} {\n detail: ${JSON.stringify(this.detail, null, 2) + ? `${this.constructor.name} {\n detail: ${ + JSON.stringify(this.detail, null, 2) },\n name: "${this.name}",\n entryType: "${this.entryType}",\n startTime: ${this.startTime},\n duration: ${this.duration}\n}` : `${this.constructor.name} { detail: ${this.detail}, name: "${this.name}", entryType: "${this.entryType}", startTime: ${this.startTime}, duration: ${this.duration} }`; } From 73a345b164efdf15af2f3ba061a723110389b542 Mon Sep 17 00:00:00 2001 From: Simon Rask Date: Sat, 3 Jul 2021 13:13:52 +0200 Subject: [PATCH 3/3] More primordials --- extensions/timers/01_timers.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/extensions/timers/01_timers.js b/extensions/timers/01_timers.js index cc8d61efdd8408..c836d6c1b916ed 100644 --- a/extensions/timers/01_timers.js +++ b/extensions/timers/01_timers.js @@ -12,6 +12,14 @@ TypeError, Number, String, + ArrayPrototypeShift, + ArrayPrototypeIndexOf, + ArrayPrototypeSplice, + MapPrototypeHas, + MapPrototypeDelete, + MapPrototypeSet, + MapPrototypeGet, + FunctionPrototypeBind, } = window.__bootstrap.primordials; // Shamelessly cribbed from extensions/fetch/11_streams.js @@ -328,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; @@ -412,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, @@ -440,22 +448,22 @@ } 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. @@ -490,7 +498,7 @@ let callback; if ("function" === typeof cb) { - callback = Function.prototype.bind.call(cb, globalThis, ...args); + callback = FunctionPrototypeBind(cb, globalThis, ...args); } else { callback = String(cb); args = []; // args are ignored @@ -520,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; @@ -548,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) {