@@ -560,20 +560,35 @@ const BenchmarkState = Object.freeze({
560560
561561
562562class Scripts {
563- constructor ( ) {
563+ constructor ( preloads ) {
564564 this . scripts = [ ] ;
565+
566+ let preloadsCode = "" ;
567+ let resourcesCode = "" ;
568+ for ( let { name, resource, blobURLOrPath } of preloads ) {
569+ console . assert ( name ?. length > 0 , "Invalid preload name." ) ;
570+ console . assert ( resource ?. length > 0 , "Invalid preload resource." ) ;
571+ console . assert ( blobURLOrPath ?. length > 0 , "Invalid preload data." ) ;
572+ preloadsCode += `${ JSON . stringify ( name ) } : "${ blobURLOrPath } ",\n` ;
573+ resourcesCode += `${ JSON . stringify ( resource ) } : "${ blobURLOrPath } ",\n` ;
574+ }
565575 // Expose a globalThis.JetStream object to the workload. We use
566576 // a proxy to prevent prototype access and throw on unknown properties.
567577 this . add ( `
568578 const throwOnAccess = (name) => new Proxy({}, {
569579 get(target, property, receiver) {
570580 throw new Error(name + "." + property + " is not defined.");
571581 }
572- });
582+ });
573583 globalThis.JetStream = {
574584 __proto__: throwOnAccess("JetStream"),
575585 preload: {
576586 __proto__: throwOnAccess("JetStream.preload"),
587+ ${ preloadsCode }
588+ },
589+ resources: {
590+ __proto__: throwOnAccess("JetStream.preload"),
591+ ${ resourcesCode }
577592 },
578593 };
579594 ` ) ;
@@ -631,8 +646,8 @@ class Scripts {
631646}
632647
633648class ShellScripts extends Scripts {
634- constructor ( ) {
635- super ( ) ;
649+ constructor ( preloads ) {
650+ super ( preloads ) ;
636651 this . prefetchedResources = Object . create ( null ) ; ;
637652 }
638653
@@ -698,8 +713,8 @@ class ShellScripts extends Scripts {
698713}
699714
700715class BrowserScripts extends Scripts {
701- constructor ( ) {
702- super ( ) ;
716+ constructor ( preloads ) {
717+ super ( preloads ) ;
703718 this . add ( "window.onerror = top.currentReject;" ) ;
704719 }
705720
@@ -898,7 +913,7 @@ class Benchmark {
898913 if ( this . isDone )
899914 throw new Error ( `Cannot run Benchmark ${ this . name } twice` ) ;
900915 this . _state = BenchmarkState . PREPARE ;
901- const scripts = isInBrowser ? new BrowserScripts ( ) : new ShellScripts ( ) ;
916+ const scripts = isInBrowser ? new BrowserScripts ( this . preloads ) : new ShellScripts ( this . preloads ) ;
902917
903918 if ( ! ! this . plan . deterministicRandom )
904919 scripts . addDeterministicRandom ( )
@@ -908,15 +923,6 @@ class Benchmark {
908923 if ( this . shellPrefetchedResources ) {
909924 scripts . addPrefetchedResources ( this . shellPrefetchedResources ) ;
910925 }
911- if ( this . plan . preload ) {
912- let preloadCode = "" ;
913- for ( let [ variableName , blobURLOrPath ] of this . preloads ) {
914- console . assert ( variableName ?. length > 0 , "Invalid preload name." ) ;
915- console . assert ( blobURLOrPath ?. length > 0 , "Invalid preload data." ) ;
916- preloadCode += `JetStream.preload[${ JSON . stringify ( variableName ) } ] = "${ blobURLOrPath } ";\n` ;
917- }
918- scripts . add ( preloadCode ) ;
919- }
920926
921927 const prerunCode = this . prerunCode ;
922928 if ( prerunCode )
@@ -1072,7 +1078,7 @@ class Benchmark {
10721078 promises . push ( this . loadBlob ( "preload" , name , resource ) . then ( ( blobData ) => {
10731079 if ( ! globalThis . allIsGood )
10741080 return ;
1075- this . preloads . push ( [ blobData . prop , blobData . blobURL ] ) ;
1081+ this . preloads . push ( { name : blobData . prop , resource : blobData . resource , blobURLOrPath : blobData . blobURL } ) ;
10761082 this . updateCounter ( ) ;
10771083 } ) . catch ( ( error ) => {
10781084 // We'll try again later in retryPrefetchResourceForBrowser(). Don't throw an error.
@@ -1099,7 +1105,7 @@ class Benchmark {
10991105 if ( type == "preload" ) {
11001106 if ( this . failedPreloads && this . failedPreloads [ blobData . prop ] ) {
11011107 this . failedPreloads [ blobData . prop ] = false ;
1102- this . preloads . push ( [ blobData . prop , blobData . blobURL ] ) ;
1108+ this . preloads . push ( { name : blobData . prop , resource : blobData . resource , blobURLOrPath : blobData . blobURL } ) ;
11031109 counter . failedPreloadResources -- ;
11041110 }
11051111 }
@@ -1112,7 +1118,7 @@ class Benchmark {
11121118 if ( ! globalThis . allIsGood )
11131119 return ;
11141120 if ( blobData . type == "preload" )
1115- this . preloads . push ( [ blobData . prop , blobData . blobURL ] ) ;
1121+ this . preloads . push ( { name : blobData . prop , resource : blobData . resource , blobURLOrPath : blobData . blobURL } ) ;
11161122 this . updateCounter ( ) ;
11171123 } ) ;
11181124
@@ -1170,7 +1176,7 @@ class Benchmark {
11701176 this . shellPrefetchedResources [ resource ] = bytes ;
11711177 }
11721178
1173- this . preloads . push ( [ name , resource ] ) ;
1179+ this . preloads . push ( { name, resource, blobURLOrPath : resource } ) ;
11741180 }
11751181 }
11761182
0 commit comments