diff --git a/Dart/benchmark.js b/Dart/benchmark.js index ae7154c0..2ad4d3ab 100644 --- a/Dart/benchmark.js +++ b/Dart/benchmark.js @@ -62,8 +62,7 @@ function addTaskQueue(self) { ms = Math.max(0, ms); var id = timerIdCounter++; // A callback can be scheduled at most once. - // (console.assert is only available on D8) - // if (isD8) console.assert(f.$timerId === undefined); + console.assert(f.$timerId === undefined); f.$timerId = id; timerIds[id] = f; if (ms == 0 && !isNextTimerDue()) { diff --git a/JetStreamDriver.js b/JetStreamDriver.js index d1ef7964..2a72a794 100644 --- a/JetStreamDriver.js +++ b/JetStreamDriver.js @@ -139,14 +139,8 @@ if (isInBrowser) { }; } -function assert(b, m = "") { - if (!b) - throw new Error(`Bad assertion: ${m}`); -} - - function mean(values) { - assert(values instanceof Array); + console.assert(values instanceof Array); let sum = 0; for (let x of values) sum += x; @@ -154,13 +148,13 @@ function mean(values) { } function geomeanScore(values) { - assert(values instanceof Array); + console.assert(values instanceof Array); let product = 1; for (let x of values) product *= x; const score = product ** (1 / values.length); // Allow 0 for uninitialized subScores(). - assert(score >= 0, `Got invalid score: ${score}`) + console.assert(score >= 0, `Got invalid score: ${score}`) return score; } @@ -206,7 +200,7 @@ class ShellFileLoader { // Cache / memoize previously read files, because some workloads // share common code. load(url) { - assert(!isInBrowser); + console.assert(!isInBrowser); if (!globalThis.prefetchResources) return `load("${url}");` @@ -230,7 +224,7 @@ class Driver { // Make benchmark list unique and sort it. this.benchmarks = Array.from(new Set(benchmarks)); this.benchmarks.sort((a, b) => a.plan.name.toLowerCase() < b.plan.name.toLowerCase() ? 1 : -1); - assert(this.benchmarks.length, "No benchmarks selected"); + console.assert(this.benchmarks.length, "No benchmarks selected"); // TODO: Cleanup / remove / merge `blobDataCache` and `loadCache` vs. // the global `fileLoader` cache. this.blobDataCache = { }; @@ -290,7 +284,7 @@ class Driver { const allScores = []; for (const benchmark of this.benchmarks) { const score = benchmark.score; - assert(score > 0, `Invalid ${benchmark.name} score: ${score}`); + console.assert(score > 0, `Invalid ${benchmark.name} score: ${score}`); allScores.push(score); } @@ -303,13 +297,13 @@ class Driver { for (const benchmark of this.benchmarks) { for (let [category, value] of Object.entries(benchmark.subScores())) { const arr = categoryScores.get(category); - assert(value > 0, `Invalid ${benchmark.name} ${category} score: ${value}`); + console.assert(value > 0, `Invalid ${benchmark.name} ${category} score: ${value}`); arr.push(value); } } const totalScore = geomeanScore(allScores); - assert(totalScore > 0, `Invalid total score: ${totalScore}`); + console.assert(totalScore > 0, `Invalid total score: ${totalScore}`); if (isInBrowser) { const summaryElement = document.getElementById("result-summary"); @@ -628,13 +622,7 @@ class ShellScripts extends Scripts { } else globalObject = runString(""); - globalObject.console = { - log: globalObject.print, - warn: (e) => { print("Warn: " + e); }, - error: (e) => { print("Error: " + e); }, - debug: (e) => { print("Debug: " + e); }, - }; - + globalObject.console = console; globalObject.self = globalObject; globalObject.top = { currentResolve, @@ -653,7 +641,7 @@ class ShellScripts extends Scripts { } addWithURL(url) { - assert(false, "Should not reach here in CLI"); + console.assert(false, "Should not reach here in CLI"); } } @@ -813,7 +801,7 @@ class Benchmark { scripts.add(prerunCode); if (!isInBrowser) { - assert(this.scripts && this.scripts.length === this.plan.files.length); + console.assert(this.scripts && this.scripts.length === this.plan.files.length); for (const text of this.scripts) scripts.add(text); } else { @@ -929,7 +917,7 @@ class Benchmark { } prefetchResourcesForBrowser() { - assert(isInBrowser); + console.assert(isInBrowser); const promises = this.plan.files.map((file) => this.loadBlob("file", null, file).then((blobData) => { if (!globalThis.allIsGood) @@ -962,7 +950,7 @@ class Benchmark { } async retryPrefetchResource(type, prop, file) { - assert(isInBrowser); + console.assert(isInBrowser); const counter = JetStream.counter; const blobData = JetStream.blobDataCache[file]; @@ -998,7 +986,7 @@ class Benchmark { } async retryPrefetchResourcesForBrowser() { - assert(isInBrowser); + console.assert(isInBrowser); const counter = JetStream.counter; for (const resource of this.plan.files) { @@ -1019,12 +1007,12 @@ class Benchmark { } prefetchResourcesForShell() { - assert(!isInBrowser); + console.assert(!isInBrowser); - assert(this.scripts === null, "This initialization should be called only once."); + console.assert(this.scripts === null, "This initialization should be called only once."); this.scripts = this.plan.files.map(file => shellFileLoader.load(file)); - assert(this.preloads === null, "This initialization should be called only once."); + console.assert(this.preloads === null, "This initialization should be called only once."); this.preloads = Object.entries(this.plan.preload ?? {}); } @@ -1137,7 +1125,7 @@ class DefaultBenchmark extends Benchmark { this.averageTime = null; this.averageScore = null; - assert(this.iterations > this.worstCaseCount); + console.assert(this.iterations > this.worstCaseCount); } processResults(results) { @@ -1149,7 +1137,7 @@ class DefaultBenchmark extends Benchmark { results = results.slice(1); results.sort((a, b) => a < b ? 1 : -1); for (let i = 0; i + 1 < results.length; ++i) - assert(results[i] >= results[i + 1]); + console.assert(results[i] >= results[i + 1]); const worstCase = []; for (let i = 0; i < this.worstCaseCount; ++i) diff --git a/shell-config.js b/shell-config.js index 8b29c7bf..8b2c6da9 100644 --- a/shell-config.js +++ b/shell-config.js @@ -24,11 +24,22 @@ */ const isInBrowser = false; -console = { - log: globalThis?.console?.log ?? print, - error: globalThis?.console?.error ?? print, - warn: globalThis?.console?.warn ?? print, -} +if (typeof console == "undefined") + console = {}; + +console.debug ??= (...args) => console.log("Debug:", ...args); +console.log ??= (...args) => print(args.join(" ")); +console.warn ??= (...args) => console.log("Warn:", ...args); +console.error ??= (...args) => console.log("Error:", ...args); +console.assert ??= (condition, message) => { + if (!condition) + throw new Error(`Assertion failed: ${message}`); +}; +console.trace ??= () => { + const targetObject = {}; + Error.captureStackTrace(targetObject); + console.log(targetObject.stack); +}; const isD8 = typeof Realm !== "undefined"; if (isD8) @@ -43,4 +54,4 @@ if (typeof performance == "undefined") performance = {}; performance.mark ??= function(){}; -performance.measure ??= function(){}; \ No newline at end of file +performance.measure ??= function(){};