-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathloader_tpl.js
More file actions
121 lines (102 loc) · 3.09 KB
/
loader_tpl.js
File metadata and controls
121 lines (102 loc) · 3.09 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Set the default global log for use by wasm_exec.js
go_log = console.log;
var useWasm = true; //location.href.includes("?wasm");
let isWasmLoaded = false;
let currentGoInstance = null;
let wasmOverflowCallback;
// Track wasm_exec_rt.js loading state.
let wasmExecRtLoaded = false;
let wasmExecRtLoadPromise = null;
window.setIgopOverflowCallback = function (callback) {
wasmOverflowCallback = callback;
};
window.isIgopLoaded = function () {
return isWasmLoaded;
};
window.goWriteSync = function (text) {
console.log(text);
};
var script = document.createElement("script");
if (useWasm) {
script.src = "$domain/wasm_exec_rt.js";
// Create a promise to track wasm_exec_rt.js loading.
wasmExecRtLoadPromise = new Promise((resolve, reject) => {
script.onload = function () {
// polyfill
if (!WebAssembly.instantiateStreaming) {
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}
wasmExecRtLoaded = true;
resolve();
loadWasm();
// const go = new Go();
// currentGoInstance = go;
// let mod, inst;
// WebAssembly.instantiateStreaming(fetch("igop_1dd7d1c3.wasm"), go.importObject).then((result) => {
// mod = result.module;
// inst = result.instance;
// isWasmLoaded = true;
// run();
// })
// async function run() {
// await go.run(inst);
// inst = await WebAssembly.instantiate(mod, go.importObject); // reset instance
// }
};
script.onerror = function () {
reject(new Error("Failed to load wasm_exec_rt.js"));
};
});
} else {
script.src = "$igop.js";
}
document.head.appendChild(script);
function handleGlobalError(event) {
if (
event.error instanceof RangeError &&
event.message.includes("Maximum call stack size exceeded")
) {
event.preventDefault();
console.error("Stack overflow detected, reload WASM module...");
if (typeof wasmOverflowCallback === "function") {
wasmOverflowCallback(event);
}
if (isWasmLoaded) {
reloadWasm();
}
}
}
window.addEventListener("error", handleGlobalError);
async function loadWasm() {
// Ensure wasm_exec_rt.js is loaded before proceeding.
if (!wasmExecRtLoaded) {
await wasmExecRtLoadPromise;
}
const go = new Go();
currentGoInstance = go;
let mod, inst;
WebAssembly.instantiateStreaming(fetch("$domain/$igop.wasm"), go.importObject).then(
(result) => {
mod = result.module;
inst = result.instance;
isWasmLoaded = true;
console.log("Load WASM module.");
run();
},
);
async function run() {
await go.run(inst);
inst = await WebAssembly.instantiate(mod, go.importObject); // reset instance
}
}
async function reloadWasm() {
isWasmLoaded = false;
if (currentGoInstance) {
currentGoInstance.exit(0);
currentGoInstance = null;
}
await loadWasm();
}