-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
68 lines (57 loc) · 2.43 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"
>
<script src="wasm_exec.js"></script>
<title>Go Fmt WASM</title>
</head>
<body>
<h1 class="mt-5 mx-5">Go fmt in the browser using WebAssembly</h1>
<textarea style="height: 400px; width: 600px;" class="form-control my-5 mx-5" id="source-code">
package main
func main () {
some := "test"
}
</textarea>
<button type="button" class="btn btn-primary mx-5" id="format-btn" disabled>Format</button>
<h3 class="mx-5 my-3">Performance</h3>
<p class="mx-5">Loading time: <span id="loading">...</span>ms</p>
<p class="mx-5">Formatting time: <span id="formatting">...</span>ms</p>
<script>
const start1 = performance.now();
// polyfill if instantiateStreaming is not available
if (!WebAssembly.instantiateStreaming) {
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer()
return await WebAssembly.instantiate(source, importObject)
}
}
const go = new Go();
WebAssembly.instantiateStreaming(fetch("format-go-code.wasm"), go.importObject)
.then((result) => {
go.run(result.instance);
const end1 = performance.now();
document.getElementById("loading").innerText = `${Math.ceil(end1 - start1)}`
const btn = document.getElementById("format-btn")
btn.disabled = false
btn.addEventListener("click", (e) => {
e.preventDefault()
let node = document.getElementById("source-code")
const start2 = performance.now();
let result = formatGoCode(node.value)
const end2 = performance.now();
document.getElementById("formatting").innerText = `${Math.ceil(end2 - start2)}`
if (result !== "") {
node.value = result
}
})
});
</script>
</body>
</html>