-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.html
170 lines (164 loc) · 5.83 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!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;
}
const securityToken = "{{.SecurityToken}}";
const fsPath = "/fs";
function fsHandler(name, body, onOk, onErr) {
const url = fsPath + "/" + name;
const options = {method: "POST",
body: JSON.stringify(body), headers:{"WBT-Token":securityToken}};
fetch(url, options).then(res => res.json()).then(payload => {
if (payload.error) {
const err = new Error(payload.error);
err.code = payload.code;
onErr(err);
} else {
onOk(payload);
}
}).catch((fetchError) => {
console.log("fetch error", fetchError)
const err = new Error("bad server response");
err.code = "ENOSYS";
onErr(err);
})
}
function bufferToBase64(buf) {
let binaryString = "";
let bytes = new Uint8Array(buf);
const len = bytes.length;
for (let i = 0; i < len; i++) {
binaryString += String.fromCharCode(bytes[i]);
}
return btoa(binaryString);
}
function overrideProcess(process) {
// provide non-negative pid so counter file regex matches
// https://github.com/golang/go/blob/9a49b26bdf771ecdfa2d3bc3ee5175eed5321f20/src/internal/coverage/defs.go#L327
process.pid = {{.Pid}};
process.ppid = {{.Ppid}};
process.cwd = () => { return fsPath };
}
// Prepending /fs/ prevents jsProcess.Call("cwd") for windows drive letter paths.
// https://github.com/golang/go/blob/5a9b6432ec8b9199ce9fce9387e94195138b313f/src/syscall/fs_js.go#L104
// This prefix is removed by filesys/handler.go fixPath() in each api call.
function fsp(path) {
return fsPath + "/" + path
}
function overrideFS(fs) {
// The fs.constants are read at https://github.com/golang/go/blob/561a5079057e3a660ab638e1ba957a96c4ff3fd1/src/syscall/fs_js.go#L25
// These values are pulled from https://github.com/golang/go/blob/561a5079057e3a660ab638e1ba957a96c4ff3fd1/src/syscall/syscall_js.go#L126
fs.constants = { O_WRONLY: 1, O_RDWR: 2,
O_CREAT: 0o0100, O_TRUNC: 0o01000, O_APPEND: 0o02000, O_EXCL: 0o0200 };
fs.open = (path, flags, mode, callback) => {
fsHandler("open", {path:fsp(path),flags,mode}, (resp) => callback(null, resp.fd), callback);
};
fs.close = (fd, callback) => {
fsHandler("close", {fd}, () => callback(null), callback);
};
const defaultWrite = fs.write.bind(fs);
fs.write = (fd, buf, offset, length, position, callback) => {
// stdin=0, stdout=1, stderr=2
if (fd < 3) {
defaultWrite(fd, buf, offset, length, position, callback);
return;
}
const buffer = bufferToBase64(buf)
fsHandler("write", {fd, buffer, offset, length, position}, (resp) => {
callback(null, resp.written);
}, callback);
};
fs.stat = (path, callback) => {
fsHandler("stat", {path:fsp(path)}, (resp) => callback(null, resp), callback);
}
fs.fstat = (fd, callback) => {
fsHandler("fstat", {fd}, (resp) => {
// for https://github.com/golang/go/blob/c19c4c566c63818dfd059b352e52c4710eecf14d/src/syscall/fs_js.go#L93
resp.isDirectory = () => {
return (resp.mode & (1 << 14)) > 0
}
callback(null, resp);
}, callback);
}
fs.rename = (from, to, callback) => {
fsHandler("rename", {from:fsp(from),to:fsp(to)}, () => callback(null), callback);
}
fs.readdir = (path, callback) => {
fsHandler("readdir", {path:fsp(path)}, (resp) => callback(null, resp.entries), callback);
}
fs.lstat = (path, callback) => {
fsHandler("lstat", {path:fsp(path)}, (resp) => callback(null, resp), callback);
}
fs.read = (fd, buffer, offset, length, position, callback) => {
fsHandler("read", {fd,offset,length,position}, (resp) => {
const binaryString = atob(resp.buffer);
for (let i = 0; i < binaryString.length; i++) {
buffer[i] = binaryString.charCodeAt(i);
}
callback(null, resp.read);
}, callback);
}
fs.mkdir = (path, perm, callback) => {
fsHandler("mkdir", {path:fsp(path), perm}, () => callback(null), callback);
}
fs.unlink = (path, callback) => {
fsHandler("unlink", {path:fsp(path)}, () => callback(null), callback);
}
fs.rmdir = (path, callback) => {
fsHandler("rmdir", {path:fsp(path)}, () => callback(null), callback);
}
}
(async() => {
const go = new Go();
overrideFS(globalThis.fs);
overrideProcess(globalThis.process);
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);
});
try {
await go.run(inst);
} catch(e) {
exitCode = 1
console.error(e)
}
document.getElementById("doneButton").disabled = false;
})();
</script>
<button id="doneButton" style="display: none;" disabled>Done</button>
</body>
</html>