Skip to content

Commit 8869c79

Browse files
committed
Move blob fetching to runnerCode since it needs to do an async operation to get the blob as an array buffer in modern browsers.
1 parent a6bd526 commit 8869c79

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

JetStreamDriver.js

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,8 @@ class Benchmark {
775775

776776
if (this.plan.preload) {
777777
let str = "";
778-
for (let [ variableName, blobUrlOrPath ] of this.preloads)
779-
str += `const ${variableName} = "${blobUrlOrPath}";\n`;
778+
for (let [ variableName, blobURLOrPath ] of this.preloads)
779+
str += `const ${variableName} = "${blobURLOrPath}";\n`;
780780
addScript(str);
781781
}
782782

@@ -1174,7 +1174,7 @@ class AsyncBenchmark extends DefaultBenchmark {
11741174
// part of a larger project's build system or a wasm benchmark compiled from a language that doesn't compile with emcc.
11751175
class WasmEMCCBenchmark extends AsyncBenchmark {
11761176
get prerunCode() {
1177-
let str = `
1177+
return `
11781178
let verbose = false;
11791179
11801180
let globalObject = this;
@@ -1205,31 +1205,38 @@ class WasmEMCCBenchmark extends AsyncBenchmark {
12051205
},
12061206
};
12071207
globalObject.Module = Module;
1208+
`;
1209+
}
1210+
1211+
// This is in runnerCode rather than prerunCode because prerunCode isn't currently structured to be async by default.
1212+
get runnerCode() {
1213+
let str = `(async function doRunWrapper() {`
1214+
if (isInBrowser) {
1215+
str += `
1216+
async function getBinary(key, blobURL) {
1217+
const response = await fetch(blobURL);
1218+
Module[key] = new Int8Array(await response.arrayBuffer());
1219+
}
1220+
`;
1221+
} else
1222+
str += `
1223+
// Needed because SpiderMonkey shell doesn't have a setTimeout.
1224+
Module.setStatus = null;
1225+
Module.monitorRunDependencies = null;
1226+
function getBinary(key, path) {
1227+
Module[key] = new Int8Array(read(path, "binary"));
1228+
}
12081229
`;
12091230

1210-
if (isInBrowser) {
1211-
str += `
1212-
function getBinary(key, blobUrl) {
1213-
var xhr = new XMLHttpRequest();
1214-
xhr.open('GET', blobUrl, false);
1215-
xhr.responseType = 'arraybuffer';
1216-
xhr.send(null);
1217-
Module[key] = new Int8Array(xhr.response);
1218-
}
1219-
`;
1220-
} else
1221-
str += `
1222-
Module.setStatus = null;
1223-
Module.monitorRunDependencies = null;
1224-
function getBinary(key, path) {
1225-
Module[key] = new Int8Array(read(path, "binary"));
1226-
}
1227-
`;
1228-
1229-
for (let [ preloadKey, blobUrlOrPath ] of this.preloads)
1230-
str += `getBinary("${preloadKey}", "${blobUrlOrPath}");\n`
1231-
1232-
return str;
1231+
for (let [ preloadKey, blobURLOrPath ] of this.preloads)
1232+
str += `await getBinary("${preloadKey}", "${blobURLOrPath}");\n`
1233+
1234+
str += super.runnerCode;
1235+
1236+
str += "\n})().catch((error) => { top.currentReject(error); });"
1237+
1238+
return str;
1239+
12331240
}
12341241
};
12351242

0 commit comments

Comments
 (0)