-
Notifications
You must be signed in to change notification settings - Fork 3
/
ui.js
117 lines (117 loc) · 3.86 KB
/
ui.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
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
(function(){"use strict";
const taBoard = document.getElementById("taBoard");
const prOut = document.getElementById("prOut");
const assert = {
equal: (x, y) => {
if (x != y) {
throw ["assert.equal", x, y];
} else {
console.log("assert.equal", x);
}
}
};
const util = require("./util");
document.getElementById("btnRt").addEventListener("click", (s, e) => {
require("./rt")().then(rt => {
console.log(window.mod = rt);
let newt = rt.newtbl();
let news = rt.newstr("asdf");
let news2 = rt.newstr("asdf");
assert.equal(util.fromUtf8(rt.strbytes(news)), "asdf");
let newf = rt.newf64(4.2);
let nil = rt.mkref(rt.mod.tblget(newt.val, news.val));
rt.mod.tblset(newt.val, news.val, newf.val);
assert.equal(nil.val, rt.nil.val);
assert.equal(rt.mod.tblget(newt.val, news.val), newf.val);
assert.equal(rt.mod.tblget(newt.val, news2.val), newf.val);
let newf2 = rt.newf64(5.4);
let newf3 = rt.newf64(6.6);
let newf4 = rt.newf64(7.8);
rt.mod.tblset(newt.val, newf2.val, newf3.val);
rt.mod.tblset(newt.val, newf3.val, newf4.val);
rt.mod.tblset(newt.val, newf4.val, newf2.val);
console.log("newt", newt.val, "news", news.val, news2.val, "newf", newf.val,
newf2.val, newf3.val, newf4.val);
assert.equal(rt.mod.tblget(newt.val, newf2.val), newf3.val);
assert.equal(rt.mod.tblget(newt.val, newf3.val), newf4.val);
assert.equal(rt.mod.tblget(newt.val, newf4.val), newf2.val);
assert.equal(rt.mod.tblget(newt.val, newf.val), nil.val);
console.log("Tested newtbl/tblget/tblset. Next: string gc pressure");
let s = "0123456789", ss = [];
for (let i=0; i<10; i++) s += s;
for (let j=0; j<2; j++) {
for (let i=0; i<8; i++) {
ss.push(rt.newstr(s));
console.log(rt.mem.buffer.byteLength, ss.map(x => x.val), Array.from(rt.handles).map(x => x.val));
}
while (ss.length) {
rt.free(ss.pop());
}
console.log("Freed" + (j?". Next: lex":". Now to do it again"));
}
let codestr = rt.newstr("local x = 3 * 5 + 21 + 2.1 + 0x2.1 + 0xa + 0xap2 + 1.25e2 + 1.25e-2;" +
"local y = 'x' + 'z' + 'z' + [[z]] + [=[x]=]\n" +
"return 'a\\na'");
rt.mod.lex(codestr.val);
rt.free(codestr);
let lexstr = rt.mkref(rt.mod.nthtmp(12));
let svec = rt.mkref(rt.mod.nthtmp(8));
let nvec = rt.mkref(rt.mod.nthtmp(4));
rt.mod.tmppop();
rt.mod.tmppop();
rt.mod.tmppop();
console.log(rt.strbytes(lexstr));
console.log(rt.obj2js(svec));
console.log(rt.obj2js(nvec));
rt.free(lexstr);
rt.free(svec);
rt.free(nvec);
console.log("Next: vm");
let main = rt.mkref(rt.mod.newfunc());
let mainbc = rt.newstr(new Uint8Array([2, 12, 0, 0, 0]));
let mainconsts = rt.mkref(rt.mod.newvec(0));
let mainfrees = rt.mkref(rt.mod.newvec(0));
let mem = new Uint8Array(rt.mem.buffer);
util.writeuint32(mem, main.val + 10, mainbc.val);
util.writeuint32(mem, main.val + 14, mainconsts.val);
util.writeuint32(mem, main.val + 18, mainfrees.val);
rt.mod.init(main.val);
console.log(rt.mod.eval());
console.log(rt.mod.eval());
});
});
document.getElementById("btnGo").addEventListener("click", (s, e) => {
require("./rt")().then(rt => {
let codestr = rt.newstr(taBoard.value);
console.log(rt.strbytes(codestr));
rt.mod.lex(codestr.val);
rt.free(codestr);
let lexstr = rt.mkref(rt.mod.nthtmp(12));
let svec = rt.mkref(rt.mod.nthtmp(8));
let nvec = rt.mkref(rt.mod.nthtmp(4));
rt.mod.tmppop();
rt.mod.tmppop();
rt.mod.tmppop();
console.log(rt.strbytes(lexstr));
console.log(rt.obj2js(svec));
console.log(rt.obj2js(nvec));
rt.free(lexstr);
rt.free(svec);
rt.free(nvec);
});
/*
const lua = require("./luwa");
const imp = {
"": {
p: x => prOut.textContent += x + " ",
q: x => prOut.textContent += String.fromCharCode(x),
i: () => prompt("Number", "")|0,
c: () => prompt("Character", "").charCodeAt(0)|0,
m: new WebAssembly.Memory({ initial: 1 }),
}
};
prOut.textContent = "";
lua.runSource(taBoard.value, imp);
*/
});
})();