-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
88 lines (73 loc) · 3 KB
/
script.js
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
const go = new Go()
WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then(wasmModule => {
go.run(wasmModule.instance)
const textEnc = new TextEncoder()
const textDec = new TextDecoder()
const linierMemory = new Uint8Array(wasmModule.instance.exports.memory.buffer)
function malloc(value) {
const input = textEnc.encode(value)
const ptr = wasmModule.instance.exports.lmalloc(input.length)
for (let i = 0; i < input.length; i++) {
linierMemory[ptr + i] = input[i]
}
}
function parseResult(ptr) {
const size = wasmModule.instance.exports.size()
const str = textDec.decode(linierMemory.slice(ptr, ptr + size))
return JSON.parse(str)
}
document.getElementById("explain-input").addEventListener("keydown", (ev) => {
if (ev.key === 'Enter' || ev.key === 'Return') {
document.getElementById("explain-button").click();
}
})
document.getElementById("evaluate-button").addEventListener("click", () => {
let value = document.getElementById("explain-input").value
if(!value) {
document.getElementById("result").innerHTML = "input is empty"
return
}
malloc(value)
const res = parseResult(wasmModule.instance.exports.evaluate())
if (res.err){
document.getElementById("result").innerHTML = res.err
return
}
document.getElementById("result").innerHTML = "<div>" + res.value + "</div>" + "<hr/>" + "<br/>"
});
document.getElementById("explain-button").addEventListener("click", () => {
let value = document.getElementById("explain-input").value
if(!value) {
document.getElementById("result").innerHTML = "input is empty"
return
}
malloc(value)
const res = parseResult(wasmModule.instance.exports.explain())
if (res.err){
document.getElementById("result").innerHTML = res.err
return
}
let result = ""
res.value.forEach(step => {
result += "<div>"
step.EquivalentForms?.forEach((equivalentForm, i) => {
result += equivalentForm
result += "<i>→</i>"
})
result += "<strong>" + step.Result + "</strong>"
result += "</div>"
if(step.Explaination) {
result += "<div class=\"explanation\">"+
"<p>"+
"<small style=\"font-weight:500\">Bitwise Operation Explanation:\n</small>"+
step.Explaination+
" ≈ "+
"<strong>"+step.Result+"</strong>"+
"</p>"+
"</div>"
}
result += "<hr/>"
});
document.getElementById("result").innerHTML = result
});
});