-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.js
67 lines (64 loc) · 2.43 KB
/
load.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function DownloadError(request) {
this.status = request.status;
this.statusText = request.statusText;
this.toString = () => `${this.status}: ${this.statusText}`;
}
function download_wasm(url) {
/*
* We can't use the Fetch API when the .wasm file is a local
* file (due to mime types). So instead use XMLHttpRequest.
*/
return new Promise((resolve, reject) => {
const request = new XMLHttpRequest();
request.open("GET", url);
request.onload = _ => resolve(request.response);
request.onerror = _ => reject(new DownloadError(request));
request.responseType = "arraybuffer";
request.send();
});
}
async function load_wasm(filename, reportStatus) {
/*
* There's about three ways to load a wasm module right now. Some are
* "better" and others work in more browsers:
*
* + Streaming compilation
* + Non-streaming async
* + Non-streaming sync
*
* In the future browsers will also be able to use a <script> tag, but
* for now that's not available.
*
* Since I'm not concerned about browser compatibility I would have used
* streaming compilation as it is also the simplist, but it seems like
* it doesn't work with the fetch API when the .wasm file is a local
* file (due to mime types). So I'll use the non-streaming async
* method.
*/
try {
reportStatus("Downloading...");
const url = location.href.slice(0, location.href.lastIndexOf('/')) +
"/" + filename;
const response = await download_wasm(url);
reportStatus("Compiling");
const importObj = { imports: {} };
result = await WebAssembly.instantiate(response, importObj);
reportStatus("Ready");
return result.instance.exports;
} catch(error) {
let message;
if (error instanceof DownloadError) {
message = `Failed to download bytecode`;
} else if (error instanceof WebAssembly.CompileError) {
message = `Failed to compile module: ${error}`;
} else if (error instanceof WebAssembly.LinkError) {
message = `Failed to link module: ${error}`;
} else if (error instanceof WebAssembly.RuntimeError) {
message = `Other WebAssembly error: ${error}`;
} else {
message = `Other error: ${error}`;
}
reportStatus(message);
throw new Error(message);
};
}