@@ -561,18 +561,28 @@ const BenchmarkState = Object.freeze({
561
561
class Scripts {
562
562
constructor ( ) {
563
563
this . scripts = [ ] ;
564
+ // Expose a globalThis.JetStream object to the workload. We use
565
+ // a proxy to prevent prototype access and throw on unknown properties.
564
566
this . add ( `
565
- const isInBrowser = ${ isInBrowser } ;
566
- const isD8 = ${ isD8 } ;
567
- if (typeof performance.mark === 'undefined') {
568
- performance.mark = function(name) { return { name }};
569
- }
570
- if (typeof performance.measure === 'undefined') {
571
- performance.measure = function() {};
572
- }
567
+ const throwOnAccess = (name) => new Proxy({}, {
568
+ get(target, property, receiver) {
569
+ throw new Error(name + "." + property + " is not defined.");
570
+ }
571
+ });
572
+ globalThis.JetStream = {
573
+ __proto__: throwOnAccess("JetStream"),
574
+ preload: {
575
+ __proto__: throwOnAccess("JetStream.preload"),
576
+ },
577
+ };
578
+ ` ) ;
579
+ this . add ( `
580
+ performance.mark ??= function(name) { return { name }};
581
+ performance.measure ??= function() {};
573
582
` ) ;
574
583
}
575
584
585
+
576
586
run ( ) {
577
587
throw new Error ( "Subclasses need to implement this" ) ;
578
588
}
@@ -585,6 +595,13 @@ class Scripts {
585
595
throw new Error ( "addWithURL not supported" ) ;
586
596
}
587
597
598
+ addBrowserTest ( ) {
599
+ this . add ( `
600
+ globalThis.JetStream.isInBrowser = ${ isInBrowser } ;
601
+ globalThis.JetStream.isD8 = ${ isD8 } ;
602
+ ` ) ;
603
+ }
604
+
588
605
addDeterministicRandom ( ) {
589
606
this . add ( `(() => {
590
607
const initialSeed = 49734321;
@@ -799,11 +816,13 @@ class Benchmark {
799
816
800
817
if ( ! ! this . plan . deterministicRandom )
801
818
scripts . addDeterministicRandom ( )
819
+ if ( ! ! this . plan . exposeBrowserTest )
820
+ scripts . addBrowserTest ( ) ;
802
821
803
822
if ( this . plan . preload ) {
804
823
let preloadCode = "" ;
805
824
for ( let [ variableName , blobURLOrPath ] of this . preloads )
806
- preloadCode += `const ${ variableName } = "${ blobURLOrPath } ";\n` ;
825
+ preloadCode += `JetStream.preload. ${ variableName } = "${ blobURLOrPath } ";\n` ;
807
826
scripts . add ( preloadCode ) ;
808
827
}
809
828
@@ -1259,39 +1278,39 @@ class AsyncBenchmark extends DefaultBenchmark {
1259
1278
// with this class and make all benchmarks async.
1260
1279
if ( isInBrowser ) {
1261
1280
str += `
1262
- async function getBinary (blobURL) {
1281
+ JetStream.getBinary = async function(blobURL) {
1263
1282
const response = await fetch(blobURL);
1264
1283
return new Int8Array(await response.arrayBuffer());
1265
- }
1284
+ };
1266
1285
1267
- async function getString (blobURL) {
1286
+ JetStream.getString = async function(blobURL) {
1268
1287
const response = await fetch(blobURL);
1269
1288
return response.text();
1270
- }
1289
+ };
1271
1290
1272
- async function dynamicImport (blobURL) {
1291
+ JetStream.dynamicImport = async function(blobURL) {
1273
1292
return await import(blobURL);
1274
- }
1293
+ };
1275
1294
` ;
1276
1295
} else {
1277
1296
str += `
1278
- async function getBinary (path) {
1297
+ JetStream.getBinary = async function(path) {
1279
1298
return new Int8Array(read(path, "binary"));
1280
- }
1299
+ };
1281
1300
1282
- async function getString (path) {
1301
+ JetStream.getString = async function(path) {
1283
1302
return read(path);
1284
- }
1303
+ };
1285
1304
1286
- async function dynamicImport (path) {
1305
+ JetStream.dynamicImport = async function(path) {
1287
1306
try {
1288
1307
return await import(path);
1289
1308
} catch (e) {
1290
1309
// In shells, relative imports require different paths, so try with and
1291
1310
// without the "./" prefix (e.g., JSC requires it).
1292
1311
return await import(path.slice("./".length))
1293
1312
}
1294
- }
1313
+ };
1295
1314
` ;
1296
1315
}
1297
1316
return str ;
@@ -1323,7 +1342,7 @@ class AsyncBenchmark extends DefaultBenchmark {
1323
1342
}
1324
1343
benchmark.validate?.(${ this . iterations } );
1325
1344
top.currentResolve(results);
1326
- }
1345
+ };
1327
1346
doRun().catch((error) => { top.currentReject(error); });`
1328
1347
}
1329
1348
} ;
@@ -1342,7 +1361,7 @@ class WasmEMCCBenchmark extends AsyncBenchmark {
1342
1361
console.log('Intercepted quit/abort');
1343
1362
};
1344
1363
1345
- oldPrint = globalObject.print;
1364
+ const oldPrint = globalObject.print;
1346
1365
globalObject.print = globalObject.printErr = (...args) => {
1347
1366
if (verbose)
1348
1367
console.log('Intercepted print: ', ...args);
@@ -1409,7 +1428,6 @@ class WSLBenchmark extends Benchmark {
1409
1428
1410
1429
performance.measure(markLabel, markLabel);
1411
1430
}
1412
-
1413
1431
top.currentResolve(results);
1414
1432
}` ;
1415
1433
}
@@ -1469,8 +1487,7 @@ class WasmLegacyBenchmark extends Benchmark {
1469
1487
console.log('Intercepted quit/abort');
1470
1488
};
1471
1489
1472
- oldPrint = globalObject.print;
1473
- oldConsoleLog = globalObject.console.log;
1490
+ const oldConsoleLog = globalObject.console.log;
1474
1491
globalObject.print = globalObject.printErr = (...args) => {
1475
1492
if (verbose)
1476
1493
oldConsoleLog('Intercepted print: ', ...args);
@@ -1483,12 +1500,12 @@ class WasmLegacyBenchmark extends Benchmark {
1483
1500
printErr: globalObject.print
1484
1501
};
1485
1502
globalObject.Module = Module;
1486
- ` ;
1503
+ ` ;
1487
1504
return str ;
1488
1505
}
1489
1506
1490
1507
get runnerCode ( ) {
1491
- let str = `function loadBlob(key, path, andThen) {` ;
1508
+ let str = `JetStream. loadBlob = function (key, path, andThen) {` ;
1492
1509
1493
1510
if ( isInBrowser ) {
1494
1511
str += `
@@ -1519,15 +1536,15 @@ class WasmLegacyBenchmark extends Benchmark {
1519
1536
console.log(e.stack);
1520
1537
throw e;
1521
1538
}
1522
- })
1539
+ });
1523
1540
` ;
1524
1541
}
1525
1542
1526
- str += "}" ;
1543
+ str += "};\n " ;
1527
1544
1528
1545
const keys = Object . keys ( this . plan . preload ) ;
1529
1546
for ( let i = 0 ; i < keys . length ; ++ i ) {
1530
- str += `loadBlob("${ keys [ i ] } ", "${ this . plan . preload [ keys [ i ] ] } ", () => {\n` ;
1547
+ str += `JetStream. loadBlob("${ keys [ i ] } ", "${ this . plan . preload [ keys [ i ] ] } ", () => {\n` ;
1531
1548
}
1532
1549
if ( this . plan . async ) {
1533
1550
str += `doRun().catch((e) => {
@@ -1845,6 +1862,8 @@ let BENCHMARKS = [
1845
1862
"./RexBench/UniPoker/benchmark.js" ,
1846
1863
] ,
1847
1864
deterministicRandom : true ,
1865
+ // FIXME: UniPoker should not access isInBrowser.
1866
+ exposeBrowserTest : true ,
1848
1867
tags : [ "Default" , "RexBench" ] ,
1849
1868
} ) ,
1850
1869
// Simple
@@ -2209,6 +2228,7 @@ let BENCHMARKS = [
2209
2228
} ,
2210
2229
async : true ,
2211
2230
deterministicRandom : true ,
2231
+ exposeBrowserTest : true ,
2212
2232
tags : [ "Wasm" ] ,
2213
2233
} ) ,
2214
2234
new WasmLegacyBenchmark ( {
@@ -2229,6 +2249,7 @@ let BENCHMARKS = [
2229
2249
} ,
2230
2250
async : true ,
2231
2251
deterministicRandom : true ,
2252
+ exposeBrowserTest : true ,
2232
2253
tags : [ "Wasm" ] ,
2233
2254
} ) ,
2234
2255
new WasmEMCCBenchmark ( {
@@ -2251,6 +2272,7 @@ let BENCHMARKS = [
2251
2272
files : [
2252
2273
"./worker/bomb.js" ,
2253
2274
] ,
2275
+ exposeBrowserTest : true ,
2254
2276
iterations : 80 ,
2255
2277
preload : {
2256
2278
rayTrace3D : "./worker/bomb-subtests/3d-raytrace.js" ,
0 commit comments