Skip to content

Commit dc051d2

Browse files
authored
Improve console (#131)
- Do not override existing console implementations - Provide basic console.assert for shells that don't have it - Use console.assert - Provide console.trace for better debugging in shells
1 parent c6524e8 commit dc051d2

File tree

3 files changed

+37
-39
lines changed

3 files changed

+37
-39
lines changed

Dart/benchmark.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ function addTaskQueue(self) {
6262
ms = Math.max(0, ms);
6363
var id = timerIdCounter++;
6464
// A callback can be scheduled at most once.
65-
// (console.assert is only available on D8)
66-
// if (isD8) console.assert(f.$timerId === undefined);
65+
console.assert(f.$timerId === undefined);
6766
f.$timerId = id;
6867
timerIds[id] = f;
6968
if (ms == 0 && !isNextTimerDue()) {

JetStreamDriver.js

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -139,28 +139,22 @@ if (isInBrowser) {
139139
};
140140
}
141141

142-
function assert(b, m = "") {
143-
if (!b)
144-
throw new Error(`Bad assertion: ${m}`);
145-
}
146-
147-
148142
function mean(values) {
149-
assert(values instanceof Array);
143+
console.assert(values instanceof Array);
150144
let sum = 0;
151145
for (let x of values)
152146
sum += x;
153147
return sum / values.length;
154148
}
155149

156150
function geomeanScore(values) {
157-
assert(values instanceof Array);
151+
console.assert(values instanceof Array);
158152
let product = 1;
159153
for (let x of values)
160154
product *= x;
161155
const score = product ** (1 / values.length);
162156
// Allow 0 for uninitialized subScores().
163-
assert(score >= 0, `Got invalid score: ${score}`)
157+
console.assert(score >= 0, `Got invalid score: ${score}`)
164158
return score;
165159
}
166160

@@ -206,7 +200,7 @@ class ShellFileLoader {
206200
// Cache / memoize previously read files, because some workloads
207201
// share common code.
208202
load(url) {
209-
assert(!isInBrowser);
203+
console.assert(!isInBrowser);
210204
if (!globalThis.prefetchResources)
211205
return `load("${url}");`
212206

@@ -230,7 +224,7 @@ class Driver {
230224
// Make benchmark list unique and sort it.
231225
this.benchmarks = Array.from(new Set(benchmarks));
232226
this.benchmarks.sort((a, b) => a.plan.name.toLowerCase() < b.plan.name.toLowerCase() ? 1 : -1);
233-
assert(this.benchmarks.length, "No benchmarks selected");
227+
console.assert(this.benchmarks.length, "No benchmarks selected");
234228
// TODO: Cleanup / remove / merge `blobDataCache` and `loadCache` vs.
235229
// the global `fileLoader` cache.
236230
this.blobDataCache = { };
@@ -290,7 +284,7 @@ class Driver {
290284
const allScores = [];
291285
for (const benchmark of this.benchmarks) {
292286
const score = benchmark.score;
293-
assert(score > 0, `Invalid ${benchmark.name} score: ${score}`);
287+
console.assert(score > 0, `Invalid ${benchmark.name} score: ${score}`);
294288
allScores.push(score);
295289
}
296290

@@ -303,13 +297,13 @@ class Driver {
303297
for (const benchmark of this.benchmarks) {
304298
for (let [category, value] of Object.entries(benchmark.subScores())) {
305299
const arr = categoryScores.get(category);
306-
assert(value > 0, `Invalid ${benchmark.name} ${category} score: ${value}`);
300+
console.assert(value > 0, `Invalid ${benchmark.name} ${category} score: ${value}`);
307301
arr.push(value);
308302
}
309303
}
310304

311305
const totalScore = geomeanScore(allScores);
312-
assert(totalScore > 0, `Invalid total score: ${totalScore}`);
306+
console.assert(totalScore > 0, `Invalid total score: ${totalScore}`);
313307

314308
if (isInBrowser) {
315309
const summaryElement = document.getElementById("result-summary");
@@ -635,13 +629,7 @@ class ShellScripts extends Scripts {
635629
} else
636630
globalObject = runString("");
637631

638-
globalObject.console = {
639-
log: globalObject.print,
640-
warn: (e) => { print("Warn: " + e); },
641-
error: (e) => { print("Error: " + e); },
642-
debug: (e) => { print("Debug: " + e); },
643-
};
644-
632+
globalObject.console = console;
645633
globalObject.self = globalObject;
646634
globalObject.top = {
647635
currentResolve,
@@ -660,7 +648,7 @@ class ShellScripts extends Scripts {
660648
}
661649

662650
addWithURL(url) {
663-
assert(false, "Should not reach here in CLI");
651+
console.assert(false, "Should not reach here in CLI");
664652
}
665653
}
666654

@@ -821,7 +809,7 @@ class Benchmark {
821809
scripts.add(prerunCode);
822810

823811
if (!isInBrowser) {
824-
assert(this.scripts && this.scripts.length === this.plan.files.length);
812+
console.assert(this.scripts && this.scripts.length === this.plan.files.length);
825813
for (const text of this.scripts)
826814
scripts.add(text);
827815
} else {
@@ -937,7 +925,7 @@ class Benchmark {
937925
}
938926

939927
prefetchResourcesForBrowser() {
940-
assert(isInBrowser);
928+
console.assert(isInBrowser);
941929

942930
const promises = this.plan.files.map((file) => this.loadBlob("file", null, file).then((blobData) => {
943931
if (!globalThis.allIsGood)
@@ -970,7 +958,7 @@ class Benchmark {
970958
}
971959

972960
async retryPrefetchResource(type, prop, file) {
973-
assert(isInBrowser);
961+
console.assert(isInBrowser);
974962

975963
const counter = JetStream.counter;
976964
const blobData = JetStream.blobDataCache[file];
@@ -1006,7 +994,7 @@ class Benchmark {
1006994
}
1007995

1008996
async retryPrefetchResourcesForBrowser() {
1009-
assert(isInBrowser);
997+
console.assert(isInBrowser);
1010998

1011999
const counter = JetStream.counter;
10121000
for (const resource of this.plan.files) {
@@ -1027,12 +1015,12 @@ class Benchmark {
10271015
}
10281016

10291017
prefetchResourcesForShell() {
1030-
assert(!isInBrowser);
1018+
console.assert(!isInBrowser);
10311019

1032-
assert(this.scripts === null, "This initialization should be called only once.");
1020+
console.assert(this.scripts === null, "This initialization should be called only once.");
10331021
this.scripts = this.plan.files.map(file => shellFileLoader.load(file));
10341022

1035-
assert(this.preloads === null, "This initialization should be called only once.");
1023+
console.assert(this.preloads === null, "This initialization should be called only once.");
10361024
this.preloads = Object.entries(this.plan.preload ?? {});
10371025
}
10381026

@@ -1145,7 +1133,7 @@ class DefaultBenchmark extends Benchmark {
11451133
this.averageTime = null;
11461134
this.averageScore = null;
11471135

1148-
assert(this.iterations > this.worstCaseCount);
1136+
console.assert(this.iterations > this.worstCaseCount);
11491137
}
11501138

11511139
processResults(results) {
@@ -1157,7 +1145,7 @@ class DefaultBenchmark extends Benchmark {
11571145
results = results.slice(1);
11581146
results.sort((a, b) => a < b ? 1 : -1);
11591147
for (let i = 0; i + 1 < results.length; ++i)
1160-
assert(results[i] >= results[i + 1]);
1148+
console.assert(results[i] >= results[i + 1]);
11611149

11621150
const worstCase = [];
11631151
for (let i = 0; i < this.worstCaseCount; ++i)

shell-config.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,22 @@
2424
*/
2525

2626
const isInBrowser = false;
27-
console = {
28-
log: globalThis?.console?.log ?? print,
29-
error: globalThis?.console?.error ?? print,
30-
warn: globalThis?.console?.warn ?? print,
31-
}
27+
if (typeof console == "undefined")
28+
console = {};
29+
30+
console.debug ??= (...args) => console.log("Debug:", ...args);
31+
console.log ??= (...args) => print(args.join(" "));
32+
console.warn ??= (...args) => console.log("Warn:", ...args);
33+
console.error ??= (...args) => console.log("Error:", ...args);
34+
console.assert ??= (condition, message) => {
35+
if (!condition)
36+
throw new Error(`Assertion failed: ${message}`);
37+
};
38+
console.trace ??= () => {
39+
const targetObject = {};
40+
Error.captureStackTrace(targetObject);
41+
console.log(targetObject.stack);
42+
};
3243

3344
const isD8 = typeof Realm !== "undefined";
3445
if (isD8)
@@ -43,4 +54,4 @@ if (typeof performance == "undefined")
4354
performance = {};
4455

4556
performance.mark ??= function(){};
46-
performance.measure ??= function(){};
57+
performance.measure ??= function(){};

0 commit comments

Comments
 (0)