-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlterm.lua
482 lines (389 loc) · 12.1 KB
/
lterm.lua
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
local loader = require("loader")
local scale = 1
local pretty
do
local inputColor, outputColor, errorColor = 11, 12, 8
local syntaxTheme = {
keyword = 13, -- purple
specialKeyword = 12, -- light blue
func = 12, -- light blue
string = 8, -- red
stringEscape = 10, -- yellow
primitive = 9, -- orange
comment = 6, -- dark gray
catch = 16 -- everything else is white
}
local keywords = {
[ "and" ] = true, [ "break" ] = true, [ "do" ] = true, [ "else" ] = true,
[ "elseif" ] = true, [ "end" ] = true, [ "false" ] = true, [ "for" ] = true,
[ "function" ] = true, [ "if" ] = true, [ "in" ] = true, [ "local" ] = true,
[ "nil" ] = true, [ "not" ] = true, [ "or" ] = true, [ "repeat" ] = true, [ "return" ] = true,
[ "then" ] = true, [ "true" ] = true, [ "until" ] = true, [ "while" ] = true,
}
local function prettySort(a, b)
local ta, tb = type(a), type(b)
if ta == "string" then return tb ~= "string" or a < b
elseif tb == "string" then return false end
if ta == "number" then return tb ~= "number" or a < b end
return false
end
local debugInfo = type(debug) == "table" and type(debug.getinfo) == "function" and debug.getinfo
local function getFunctionArgs(func)
if debugInfo then
local args = {}
local hook = debug.gethook()
local argHook = function()
local info = debugInfo(3)
if info.name ~= "pcall" then return end
for i = 1, math.huge do
local name = debug.getlocal(2, i)
if name == "(*temporary)" or not name then
debug.sethook(hook)
return error()
end
args[#args + 1] = name
end
end
debug.sethook(argHook, "c")
pcall(func)
return args
end
end
local function prettyFunction(fn)
if debugInfo then
local info = debugInfo(fn, "S")
if info.short_src and info.linedefined and info.linedefined >= 1 then
local args
if info.what == "Lua" then
args = getFunctionArgs(fn)
end
if args then
return "function<" .. info.short_src .. ":" .. info.linedefined .. ">(" .. table.concat(args, ", ") .. ")"
else
return "function<" .. info.short_src .. ":" .. info.linedefined .. ">"
end
end
end
return tostring(fn)
end
local function prettySize(obj, tracking, limit)
local objType = type(obj)
if objType == "string" then return #string.format("%q", obj):gsub("\\\n", "\\n")
elseif objType == "function" then return #prettyFunction(obj)
elseif objType ~= "table" or tracking[obj] then return #tostring(obj) end
local count = 2
tracking[obj] = true
for k, v in pairs(obj) do
count = count + prettySize(k, tracking, limit) + prettySize(v, tracking, limit)
if count >= limit then break end
end
tracking[obj] = nil
return count
end
local function prettyImpl(obj, tracking, width, height, indent, tupleLength)
local objType = type(obj)
if objType == "string" then
local formatted = string.format("%q", obj):gsub("\\\n", "\\n")
local limit = math.max(8, math.floor(width * height * 0.8))
if #formatted > limit then
shell.write(formatted:sub(1, limit-3), syntaxTheme.string)
shell.write("...", syntaxTheme.string)
else
shell.write(formatted, syntaxTheme.string)
end
return
elseif objType == "number" then
return shell.write(tostring(obj), syntaxTheme.primitive)
elseif objType == "boolean" then
return shell.write(tostring(obj), syntaxTheme.primitive)
elseif objType == "function" then
return shell.write(prettyFunction(obj), 7)
elseif objType ~= "table" or tracking[obj] then
return shell.write(tostring(obj), 7)
elseif (getmetatable(obj) or {}).__tostring then
return shell.write(tostring(obj), 16)
end
local open, close = "{", "}"
if tupleLength then open, close = "(", ")" end
if (tupleLength == nil or tupleLength == 0) and next(obj) == nil then
return shell.write(open .. close, 16)
elseif width <= 7 then
shell.write(open, 16) shell.write(" ... ", 6) shell.write(close, 16)
return
end
local shouldNewline = false
local length = tupleLength or #obj
local size, children, keys, kn = 2, 0, {}, 0
for k, v in pairs(obj) do
if type(k) == "number" and k >= 1 and k <= length and k % 1 == 0 then
local vs = prettySize(v, tracking, width)
size = size + vs + 2
children = children + 1
else
kn = kn + 1
keys[kn] = k
local vs, ks = prettySize(v, tracking, width), prettySize(k, tracking, width)
size = size + vs + ks + 2
children = children + 2
end
if size >= width * 0.6 then shouldNewline = true end
end
if shouldNewline and height <= 1 then
shell.write(open, 16) shell.write(" ... ", 6) shell.write(close, 16)
return
end
table.sort(keys, prettySort)
local nextNewline, subIndent, childWidth, childHeight
if shouldNewline then
nextNewline, subIndent = ",\n", indent .. " "
height = height - 2
childWidth, childHeight = width - 2, math.ceil(height / children)
if children > height then children = height - 2 end
else
nextNewline, subIndent = ", ", ""
width = width - 2
childWidth, childHeight = math.ceil(width / children), 1
end
shell.write(open .. (shouldNewline and "\n" or " "), 16)
tracking[obj] = true
local seen = {}
local first = true
for k = 1, length do
if not first then shell.write(nextNewline, 16) else first = false end
shell.write(subIndent, 16)
seen[k] = true
prettyImpl(obj[k], tracking, childWidth, childHeight, subIndent)
children = children - 1
if children < 0 then
if not first then shell.write(nextNewline, 16) else first = false end
shell.write(subIndent .. "...", 6)
break
end
end
for i = 1, kn do
local k, v = keys[i], obj[keys[i]]
if not seen[k] then
if not first then shell.write(nextNewline, 16) else first = false end
shell.write(subIndent, 16)
if type(k) == "string" and not keywords[k] and k:match("^[%a_][%a%d_]*$") then
shell.write(k .. " = ", 16)
prettyImpl(v, tracking, childWidth, childHeight, subIndent)
else
shell.write("[", 16)
prettyImpl(k, tracking, childWidth, childHeight, subIndent)
shell.write("] = ", 16)
prettyImpl(v, tracking, childWidth, childHeight, subIndent)
end
children = children - 1
if children < 0 then
if not first then shell.write(nextNewline) end
shell.write(subIndent .. "...", 6)
break
end
end
end
tracking[obj] = nil
shell.write((shouldNewline and "\n" .. indent or " ") .. (tupleLength and ")" or "}"), 16)
end
function pretty(t, n)
local width, height = gpu.width / (gpu.font.data.w + 1), gpu.height / (gpu.font.data.h + 1)
return prettyImpl(t, {}, width, 999999999, "", n)
end
end
local args = {...}
if #args ~= 1 then
print("Usage: lterm <file.wasm>", 8)
return
end
local data
do
local handle = fs.open(args[1], "rb")
data = handle:read("*a")
handle:close()
end
local function linearRead(memory, offset, len)
local ptr = memory + offset
local val = ""
for i = 1, len do
val = val .. string.char(ptr[i - 1])
end
return val
end
local instance = loader.load(data)
local width, height
instance:link("env", "setDisplayMode", function(a, b, c)
-- TODO: actually have a difference between the modes
-- debugTrace("DISPLAY: ", a, b, c)
width, height = b, c
return 1
end)
local bufferStack = {}
instance:link("env", "pushFromMemory", function(offset, length)
bufferStack[#bufferStack + 1] = linearRead(instance.exports.memory, offset, length)
end)
instance:link("env", "popToMemory", function(offset, length)
local str = bufferStack[#bufferStack]
bufferStack[#bufferStack] = nil
for i = 1, #str do
instance.exports.memory[i - 1] = str:sub(i, i):byte()
end
end)
instance:link("env", "print", function()
shell.write(bufferStack[#bufferStack])
bufferStack[#bufferStack] = nil
end)
instance:link("env", "log", function()
debugTrace(bufferStack[#bufferStack])
bufferStack[#bufferStack] = nil
end)
local requestCounter = 0
instance:link("env", "readImage", function(fn)
debugTrace(bufferStack[#bufferStack])
debugTrace(fn)
requestCounter = requestCounter + 1
return requestCounter
end)
local inputType = 1
instance:link("env", "focusInput", function(type)
debugTrace("Input type: " .. type)
inputType = type
end)
instance:link("env", "shutdown", function()
print()
os.exit()
end)
local stepInterval = -1
instance:link("env", "setStepInterval", function(ms)
stepInterval = ms/1000
end)
instance:link("env", "getNativeDisplayWidth", function()
return gpu.width
end)
instance:link("env", "getNativeDisplayHeight", function()
return gpu.height
end)
instance:link("env", "displayMemory", function(offset, length)
local ptr = instance.exports.memory + offset
for i = 1, width do
for j = 1, height do
local ind = ((j - 1)*width + (i - 1)) * 4
local char = ptr[ind] or 0
local val = 16 * char / 255
gpu.drawRectangle((i - 1)*scale, (j - 1)*scale, scale, scale, math.floor(val))
end
end
return
end)
instance:link("env", "startTone", function(a, b, c, d, e, f)
debugTrace(a, b, c, d, e, f)
-- 523
speaker.play({channel = (d + 3) % 5 + 1, frequency = b, time = 1/60, shift = 0, volume = 0.1, attack = 0, release = 0})
end)
instance:link("env", "stopTone", function() end)
local xAxis, yAxis = 0, 0
instance:link("env", "getGameAxisX", function()
return xAxis -- TODO
end)
instance:link("env", "getGameAxisY", function()
return yAxis -- TODO
end)
instance:link("env", "getGameButtonA", function()
return 0 -- TODO
end)
instance:link("env", "getGameButtonB", function()
return 0 -- TODO
end)
instance:link("env", "getGameButtonX", function()
return 0 -- TODO
end)
instance:link("env", "getGameButtonY", function()
return 0 -- TODO
end)
instance:link("env", "getMousePressed", function()
return 0 -- TODO
end)
instance:link("env", "getInputText", function()
bufferStack[#bufferStack + 1] = ""
return 0 -- TODO
end)
instance:link("env", "getInputPosition", function()
return 0 -- TODO
end)
instance:link("env", "getInputSelected", function()
return 0 -- TODO
end)
instance:link("env", "getMouseX", function()
return 0 -- TODO
end)
instance:link("env", "getMouseY", function()
return 0 -- TODO
end)
instance:link("Math", "random", function()
return math.random()
end)
if instance.startFunc then
instance.startFunc()
end
if instance.exports.init then
instance.exports.init()
end
if instance.exports.step or instance.exports.display then
local running = true
local function event(e, ...)
if e == "key" then
local k = ...
if k == "escape" then
if instance.exports["break"] then
instance.exports["break"]()
end
running = false
elseif k == "left" then
xAxis = -1
elseif k == "right" then
xAxis = 1
elseif k == "up" then
yAxis = -1
elseif k == "down" then
yAxis = 1
end
elseif e == "keyUp" then
local k = ...
if k == "left" and xAxis == -1 then
xAxis = 0
elseif k == "right" and xAxis == 1 then
xAxis = 0
elseif k == "up" and yAxis == -1 then
yAxis = 0
elseif k == "down" and yAxis == 1 then
yAxis = 0
end
end
end
local function update(dt)
if instance.exports.step then
instance.exports.step(os.clock())
end
end
local function draw()
if instance.exports.display then
instance.exports.display(os.clock())
end
gpu.swap()
end
local eq = {}
local last = os.clock()
while running do
while true do
local a = {coroutine.yield()}
if not a[1] then break end
table.insert(eq, a)
end
while #eq > 0 do
event(unpack(table.remove(eq, 1)))
end
update(os.clock() - last)
last = os.clock()
draw()
end
end
print()