-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.html
91 lines (73 loc) · 3.08 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>zig fmt</title>
<style>
textarea {
font-family: monospace;
}
</style>
</head>
<body>
<h1>zig fmt <button disabled>Format</button></h1>
<textarea rows="42"
cols="120">const std = @import("std"); pub fn main() void { std.debug.warn("Hello World\n"); }</textarea>
<p>Source: <a href="https://github.com/shritesh/zigfmt-web">https://github.com/shritesh/zigfmt-web</a></p>
<script>
const format = function (source) {
// convert source to Uint8Array
const textEncoder = new TextEncoder();
const sourceArray = textEncoder.encode(source);
// get memory from wasm
// assume first memory locations are for return values, the rest are for params
const return_len = (32 / 8) + (32 / 8); // u32, u32
const source_len = sourceArray.length;
const ptr = this._wasm_alloc(return_len + source_len);
if (ptr === 0) {
throw "Cannot allocate memory";
}
// copy sourceArray to wasm
var memoryu8 = new Uint8Array(this.memory.buffer);
for (let i = 0; i < source_len; ++i) {
memoryu8[ptr + return_len + i] = sourceArray[i];
}
// call function
const succeed = this.format_export(ptr + return_len, source_len, ptr, ptr + (32 / 8));
// read result into Uint32Array()
const return_slice = new Uint32Array(this.memory.buffer.slice(ptr, ptr + return_len));
const return_val_ptr = return_slice[0];
const return_val_len = return_slice[1];
// dealloc function params
this._wasm_dealloc(ptr, return_len + source_len);
// throw if function returned error
if (!succeed) {
throw "WASM Call returned error"
}
// get the result
const result = new Uint8Array(this.memory.buffer.slice(return_val_ptr, return_val_ptr + return_val_len));
// dealloc result
this._wasm_dealloc(return_val_ptr, return_val_len);
// decode result
const textDecoder = new TextDecoder();
// return result
return textDecoder.decode(result);
};
fetch('fmt.wasm')
.then(response => response.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes, {}))
.then(result => {
const fmt = format.bind(result.instance.exports);
const button = document.querySelector('button');
button.disabled = false;
button.onclick = function (e) {
const textArea = document.querySelector('textarea');
const result = fmt(textArea.value);
textArea.value = result;
};
});
</script>
</body>
</html>