forked from agnivade/wasmbrowsertest
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
99 lines (94 loc) · 3.11 KB
/
index.html
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
<!doctype html>
<!--
Copyright 2018 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<html>
<head>
<meta charset="utf-8">
<title>Go wasm</title>
</head>
<body>
<!--
Add the following polyfill for Microsoft Edge 17/18 support:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/encoding.min.js"></script>
(see https://caniuse.com/#feat=textencoder)
-->
<script src="wasm_exec.js"></script>
<script>
if (!WebAssembly.instantiateStreaming) { // polyfill
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}
let exitCode = 0;
function goExit(code) {
exitCode = code;
}
function enosys() {
const err = new Error("not implemented");
err.code = "ENOSYS";
return err;
}
let coverageProfileContents = "";
function overrideFS(fs) {
// A typical runtime opens fd's in sequence above the standard descriptors (0-2).
// Choose an arbitrarily high fd for the custom coverage file to avoid conflict with the actual runtime fd's.
const coverFileDescriptor = Number.MAX_SAFE_INTEGER;
const coverFilePath = {{.CoverageFile}};
// Wraps the default operations with bind() to ensure internal usage of 'this' continues to work.
const defaultOpen = fs.open.bind(fs);
fs.open = (path, flags, mode, callback) => {
if (path === coverFilePath) {
callback(null, coverFileDescriptor);
return;
}
defaultOpen(path, flags, mode, callback);
};
const defaultClose = fs.close.bind(fs);
fs.close = (fd, callback) => {
if (fd === coverFileDescriptor) {
callback(null);
return;
}
defaultClose(fd, callback);
};
if (!globalThis.TextDecoder) {
throw new Error("globalThis.TextDecoder is not available, polyfill required");
}
const decoder = new TextDecoder("utf-8");
const defaultWrite = fs.write.bind(fs);
fs.write = (fd, buf, offset, length, position, callback) => {
if (fd === coverFileDescriptor) {
coverageProfileContents += decoder.decode(buf);
callback(null, buf.length);
return;
}
defaultWrite(fd, buf, offset, length, position, callback);
};
}
(async() => {
const go = new Go();
overrideFS(globalThis.fs)
go.argv = [{{range $i, $item := .Args}} {{if $i}}, {{end}} "{{$item}}" {{end}}];
// The notFirst variable sets itself to true after first iteration. This is to put commas in between.
go.env = { {{ $notFirst := false }}
{{range $key, $val := .EnvMap}} {{if $notFirst}}, {{end}} {{$key}}: "{{$val}}" {{ $notFirst = true }}
{{end}} };
go.exit = goExit;
let mod, inst;
await WebAssembly.instantiateStreaming(fetch("{{.WASMFile}}"), go.importObject).then((result) => {
mod = result.module;
inst = result.instance;
}).catch((err) => {
console.error(err);
});
await go.run(inst);
document.getElementById("doneButton").disabled = false;
})();
</script>
<button id="doneButton" style="display: none;" disabled>Done</button>
</body>
</html>