-
Notifications
You must be signed in to change notification settings - Fork 0
/
webstart.ts
87 lines (80 loc) · 2.71 KB
/
webstart.ts
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
import { panic } from "./panic";
import { run } from "./runner";
document.addEventListener("DOMContentLoaded", async () => {
function display(arg: string) {
const output = document.getElementById("output");
output.textContent += arg + "\n";
}
const importObject = {
imports: {
print_num: (arg: any) => {
console.log("Logging num from WASM: ", arg);
display(String(arg));
return arg;
},
print_bool: (arg: any) => {
const val = arg === 1 ? "True" : "False";
console.log("Logging bool from WASM: ", val);
display(val);
return arg;
},
print_none: (arg: any) => {
console.log("Logging NONE from WASM");
display("None");
return arg;
},
panic: panic,
abs: Math.abs,
min: Math.min,
max: Math.max,
pow: Math.pow,
},
};
// stop the form from any default behaviors
document
.getElementById("code-form")
.addEventListener("submit", (e) => e.preventDefault());
const generatedCodeBox = document.getElementById("generated-code");
const output = document.getElementById("output");
const generatedCodeLabels = Array.from(
document.getElementsByClassName(
"output label"
) as HTMLCollectionOf<HTMLElement>
);
const lines = Array.from(
document.getElementsByClassName("line") as HTMLCollectionOf<HTMLElement>
);
const memTable = document.getElementById("memory");
const memValues = document.getElementById("mem-values");
document.getElementById("run").addEventListener("click", function () {
const program = (
document.getElementById("user-code") as HTMLTextAreaElement
).value;
// reset output so it can be printed to - generatedCodeBox is dealt with separately
output.innerHTML = "";
run(program, { importObject })
.then((r) => {
lines.forEach((l) => (l.hidden = false));
memTable.hidden = false;
generatedCodeLabels.forEach((label) => (label.hidden = false));
generatedCodeBox.innerText = r.source;
output.textContent += `Final return value: ${r.ans}`;
output.setAttribute("style", "color:black");
for (let i = 1; i < 11; i++) {
(
memValues.childNodes[2 * i + 1].childNodes[0] as HTMLOutputElement
).innerText = String(r.mem[i]);
}
console.log("run finished");
})
.catch((e) => {
lines.forEach((l) => (l.hidden = true));
memTable.hidden = true;
generatedCodeLabels.forEach((label) => (label.hidden = true));
generatedCodeBox.innerHTML = "";
output.textContent = String(e);
output.setAttribute("style", "color:red");
console.log("run failed", e);
});
});
});