-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
217 lines (194 loc) · 9.5 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="An example of the library bottom zig">
<meta name="keywords" content="library, bottom zig, zig library">
<meta name="author" content="Deecellar">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Example of bottom-zig online</title>
<!--- We preload our wasm-->
<link rel="preload" href="wasm/bottom-zig.wasm">
<link rel="stylesheet" href="css/output.css">
</head>
<body class="dark:bg-gray-700 bg-gray-50 dark:text-gray-200">
<div class="container mx-auto">
<div class="flex flex-col items-center">
<div class="flex-1">
<h1 class="text-center text-3xl font-bold ">Example of bottom-zig online</h1>
<h2 class="text-center text-xl">
This is a simple example on the bottom-zig library.
</h2>
</div>
<!-- We make a form with 2 text boxes and a button in between -->
<div class="flex-1">
<div class="flex flex-col items-center">
<div class="flex-1 align-middle justify-center">
<!-- Input box for encoded/decoded text-->
<textarea type="text" required
class="border border-gray-400 p-2 w-full m-2 dark:border-blue-500 dark:bg-gray-600 dark:text-gray-300"
id="text" placeholder="Enter text here"></textarea>
<!-- Buttons for Bottomify, regress and swap result and input boxes-->
<div class="flex flex-row items-stretch ml-4 ">
<button class="border border-gray-400 p-2 m-2 w-32 text-md dark:border-blue-500"
id="bottomify">Bottomify</button>
<button class="border border-gray-400 p-2 m-2 w-32 text-md dark:border-blue-500"
id="regress">Regress</button>
<button class="border border-gray-400 p-2 m-2 w-32 text-md max-w-full dark:border-blue-500"
id="swap">Swap</button>
</div>
<!-- Result box for encoded/decoded text-->
<textarea type="text"
class="border border-gray-400 m-3 dark:border-blue-500 dark:bg-slate-600 dark:text-gray-300 p-2 w-full"
id="result" placeholder="Result" disabled></textarea>
</div>
</div>
</div>
</div>
<!-- Just a normal alert notification when things go wrong-->
<div class="flex flex-col items-center">
<div class="flex-1">
<div id="alert" aria-live="assertive">
<div class="bg-red-500 dark:bg-red-800 text-white dark:text-graw-100 font-bold p-2 m-2 rounded-lg">
<p class="text-center">Something went wrong!</p>
<p class="sr-only">You have written something incorrectly in the input box with the following
message</p>
<noscript>This page does not work without wasm or javascript activated</noscript>
<div id="extra">
</div>
</div>
</div>
</div>
</div>
</div>
</body>
<script>
const alert = document.getElementById("alert");
alert.classList.add("hidden");
const extra = document.getElementById("extra");
// We hide our alert box at the beginning
// We define the buttons and the input boxes
const text = document.getElementById("text");
const result = document.getElementById("result");
const bottomify = document.getElementById("bottomify");
const regress = document.getElementById("regress");
const swap = document.getElementById("swap");
// On unhandled exceptions we show the alert box with the error message
window.addEventListener("unhandledrejection", function (event) {
alert.classList.remove("hidden");
extra.appendChild(document.createTextNode(event.reason));
});
if (typeof WebAssembly === "undefined") {
alert.classList.remove("hidden");
extra.innerHTML = "Your browser does not support or have disabled WebAssembly. See if your Browser support wasm at: https://caniuse.com/wasm";
}
else {
(async () => {
// We load the wasm module at wasm/bottom-zig.wasm with the promise/then method
var obj = null;
var response = await fetch("wasm/bottom-zig.wasm");
var wasm = await response.arrayBuffer();
function readString(addr, len) {
let utf8Decoder = new TextDecoder();
let view = new Uint8Array(obj.instance.exports.memory.buffer);
return utf8Decoder.decode(view.slice(addr, addr + len));
}
function startWasm() {
const {
encode,
decode,
_start
} = obj.instance.exports;
_start();
// we set encode and decode to the buttons
bottomify.addEventListener("click", () => {
encode();
});
regress.addEventListener("click", () => {
decode();
}
);
swap.addEventListener("click", () => {
// We swap the input and result boxes
let temp = text.value;
text.value = result.value;
result.value = temp;
});
}
var env = {
env: {
memoryBase: 0,
tableBase: 0,
memory: new WebAssembly.Memory({
initial: 2,
maximum: 10
}),
setResult: (ptr, len) => {
document.getElementById("result").value = readString(ptr, len);
},
appendResult: (ptr, len) => {
document.getElementById("result").value += readString(ptr, len);
},
getText: () => {
// We get the text from the input box
let text = document.getElementById("text").value;
// We convert the text to a Uint8Array
let textArray = new Uint8Array(obj.instance.exports.memory.buffer);
let textLen = new TextEncoder().encode(text);
textArray.set(textLen);
// We return the Uint8Array
return textArray.byteOffset;
},
getTextLen: () => {
let text = document.getElementById("text").value;
let textLen = new TextEncoder().encode(text);
return textLen.length;
},
appendException: (ptr, len) => {
let result = new Uint8Array(obj.instance.exports.memory.buffer, ptr, len);
// We append the result as a new paragraph in the alert box
extra.appendChild(document.createElement("p")).innerHTML = readString(ptr, len);
// we show the alert box
alert.classList.remove("hidden");
},
hideException: () => {
alert.classList.add("hidden");
// we clear the alert box
extra.innerHTML = "";
},
logus: (ptr, len) => {
let result = new Uint8Array(obj.instance.exports.memory.buffer, ptr, len);
console.log(readString(ptr, len));
},
restart: (state) => {
(async () => {
obj = await WebAssembly.instantiate(wasm, env);
startWasm();
if (state === 1) {
extra.appendChild(document.createElement("p")).innerHTML = "The text is too large to encode";
}
else if (state === 2) {
extra.appendChild(document.createElement("p")).innerHTML = "The text is too large to decode";
}
else {
extra.appendChild(document.createElement("p")).innerHTML = "Error when erroring, no memory!";
}
alert.classList.remove("hidden");
})().catch(e => {
alert.classList.remove("hidden");
extra.innerHTML = e;
});
}
}
};
obj = await WebAssembly.instantiate(wasm, env);
// We restart our wasm module on uncaught runtime errors
startWasm();
})().catch(e => {
alert.classList.remove("hidden");
extra.innerHTML = e;
});
}
</script>
</html>