-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path00-init.lua
More file actions
1403 lines (1332 loc) · 62.1 KB
/
Copy path00-init.lua
File metadata and controls
1403 lines (1332 loc) · 62.1 KB
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--- The expect module provides error checking functions for other parts of the kernel.
expect = {}
local native_types = {["nil"] = true, boolean = true, number = true, string = true, table = true, ["function"] = true, userdata = true, thread = true}
local function funclike(v) return (type(v) == "table" and ((getmetatable(v) or {}).__call)) or type(v) == "function" end
local function check_type(msg, value, ...)
local vt = type(value)
local vmt
if vt == "table" then
local mt = getmetatable(value)
if mt then vmt = mt.__name end
end
local args = table.pack(...)
for _, typ in ipairs(args) do
if native_types[typ] then if vt == typ then return value end
elseif vmt == typ then return value
elseif funclike(typ) and typ(value) then return value end
end
local info = debug.getinfo(2, "n")
if info and info.name and info.name ~= "" then msg = msg .. " to '" .. info.name .. "'" end
local types
if #args == 1 and funclike(args[1]) then
local _, err = args[1](value)
error(msg .. " (" .. err .. ")", 3)
else
for i, v in ipairs(args) do args[i] = tostring(v) end
if args.n == 1 then types = args[1]
elseif args.n == 2 then types = args[1] .. " or " .. args[2]
else types = table.concat(args, ", ", 1, args.n - 1) .. ", or " .. args[args.n] end
error(msg .. " (expected " .. types .. ", got " .. vt .. ")", 3)
end
end
--- Check that a numbered argument matches the expected type(s). If the type
-- doesn't match, throw an error.
-- This function supports custom types by checking the __name metaproperty.
-- Passing the result of @{expect.struct}, @{expect.array}, or @{expect.match}
-- as a type parameter will use that function as a validator.
-- @tparam number index The index of the argument to check
-- @tparam any value The value to check
-- @tparam string|function(v):boolean ... The types to check for
-- @treturn any `value`
function expect.expect(index, value, ...)
return check_type("bad argument #" .. index, value, ...)
end
--- Check that a key in a table matches the expected type(s). If the type
-- doesn't match, throw an error.
-- This function supports custom types by checking the __name metaproperty.
-- Passing the result of @{expect.struct}, @{expect.array}, or @{expect.match}
-- as a type parameter will use that function as a validator.
-- @tparam any tbl The table (or other indexable value) to search through
-- @tparam any key The key of the table to check
-- @tparam string|function(v):boolean ... The types to check for
-- @treturn any The indexed value in the table
function expect.field(tbl, key, ...)
local ok, str = pcall(string.format, "%q", key)
if not ok then str = tostring(key) end
return check_type("bad field " .. str, tbl[key], ...)
end
--- Check that a number is between the specified minimum and maximum values. If
-- the number is out of bounds, throw an error.
-- @tparam number num The number to check
-- @tparam[opt=-math.huge] number min The minimum value of the number (inclusive)
-- @tparam[opt=math.huge] number max The maximum value of the number (inclusive)
-- @treturn number `num`
function expect.range(num, min, max)
expect.expect(1, num, "number")
expect.expect(2, min, "number", "nil")
expect.expect(3, max, "number", "nil")
if max and min and max < min then error("bad argument #3 (min must be less than or equal to max)", 2) end
if num ~= num or num < (min or -math.huge) or num > (max or math.huge) then error(("number outside of range (expected %s to be within %s and %s)"):format(num, min or -math.huge, max or math.huge), 3) end
return num
end
setmetatable(expect, {__call = function(self, ...) return expect.expect(...) end})
--- serialization.lua
local keywords = {
["and"] = true,
["break"] = true,
["do"] = true,
["else"] = true,
["elseif"] = true,
["end"] = true,
["false"] = true,
["for"] = true,
["function"] = true,
["goto"] = 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 lua_serialize(val, stack, opts, level)
if stack[val] then error("Cannot serialize recursive value", 0) end
local tt = type(val)
if tt == "table" then
if not next(val) then return "{}" end
stack[val] = true
local res = opts.minified and "{" or "{\n"
local num = {}
for i, v in ipairs(val) do
if not opts.minified then res = res .. (" "):rep(level) end
num[i] = true
res = res .. lua_serialize(v, stack, opts, level + 1) .. (opts.minified and "," or ",\n")
end
for k, v in pairs(val) do if not num[k] then
if not opts.minified then res = res .. (" "):rep(level) end
if type(k) == "string" and k:match "^[A-Za-z_][A-Za-z0-9_]*$" and not keywords[k] then res = res .. k
else res = res .. "[" .. lua_serialize(k, stack, opts, level + 1) .. "]" end
res = res .. (opts.minified and "=" or " = ") .. lua_serialize(v, stack, opts, level + 1) .. (opts.minified and "," or ",\n")
end end
if opts.minified then res = res:gsub(",$", "")
else res = res .. (" "):rep(level - 1) end
stack[val] = nil
return res .. "}"
elseif tt == "nil" or tt == "number" or tt == "boolean" or tt == "string" then
return ("%q"):format(val):gsub("\\\n", "\\n"):gsub("\\?[%z\1-\31\127-\255]", function(c) return ("\\%03d"):format(string.byte(c)) end)
else
error("Cannot serialize type " .. tt, 0)
end
end
--- Serializes an arbitrary Lua object into a serialized Lua string.
-- @tparam any val The value to encode
-- @tparam[opt] {minified=boolean} opts Any options to specify while encoding
-- @treturn string The serialized Lua representation of the object
function serialize(val, opts)
expect(2, opts, "table", "nil")
return lua_serialize(val, {}, opts or {}, 1)
end
--- Parses a serialized Lua string and returns a Lua value represented by the string.
-- @tparam string str The serialized Lua string to decode
-- @treturn any The Lua value from the serialized Lua
function unserialize(str)
expect(1, str, "string")
return assert(load("return " .. str, "=unserialize", "t", {}))()
end
-- We need the keys API from CraftOS to be able to meaningfully decipher key constants.
do
local file = fs.open("/rom/apis/keys.lua", "r")
local env = setmetatable({dofile = function() return expect end}, {__index = _G})
if _VERSION < "Lua 5.2" then env._ENV = env end
local fn
if loadstring and setfenv then
fn = loadstring(file.readAll(), "@/rom/apis/keys.lua")
setfenv(fn, env)
else
fn = load(file.readAll(), "@/rom/apis/keys.lua", "t", env)
end
file.close()
fn()
keys = {}
for k,v in pairs(env) do keys[k] = v end
end
-- load is the de facto loader - loadstring will no longer be available. Since load for strings isn't available on old versions of Lua/Cobalt, we shim it if necessary.
local Cload = load
if not pcall(load, "return", "=test", "t", {}) then
local old_load, old_loadstring, expect, setfenv = load, loadstring, expect, setfenv
function load(chunk, name, mode, env)
expect(1, chunk, "string", "function")
expect(2, name, "string", "nil")
expect(3, mode, "string", "nil")
expect(4, env, "table", "nil")
if type(chunk) == "string" then
if chunk:sub(1, 4) == "\27Lua" then
if mode == nil or mode:find "b" then
local fn, err = old_loadstring(chunk, name)
if fn and env then setfenv(fn, env) end
return fn, err
else return nil, "attempt to load a binary chunk (mode is '" .. (mode or "bt") .. "')" end
else
if mode == nil or mode:find "t" then
local fn, err = old_loadstring(chunk, name)
if fn and env then setfenv(fn, env) end
return fn, err
else return nil, "attempt to load a text chunk (mode is '" .. (mode or "bt") .. "')" end
end
else
local fn, err = old_load(chunk, name)
if fn then setfenv(fn, env) end
return fn, err
end
end
end
loadstring = nil -- Make sure loadstring is always gone
-- Remove bit and apply bit32, as this is a Lua 5.2 environment.
if bit then
if not bit32 then
local bit = bit
bit32 = {
bnot = bit.bnot,
lshift = bit.blshift,
rshift = bit.blogic_rshift,
arshift = bit.brshift
}
function bit32.band(x, y, ...)
expect(1, x, "number")
expect(2, y, "number", "nil")
if not y then return x end
return bit32.band(bit.band(x, y), ...)
end
function bit32.bor(x, y, ...)
expect(1, x, "number")
expect(2, y, "number", "nil")
if not y then return x end
return bit32.bor(bit.bor(x, y), ...)
end
function bit32.bxor(x, y, ...)
expect(1, x, "number")
expect(2, y, "number", "nil")
if not y then return x end
return bit32.bxor(bit.bxor(x, y), ...)
end
function bit32.btest(...)
return bit32.band(...) ~= 0
end
function bit32.extract(n, field, width)
expect(1, n, "number")
expect(2, field, "number")
expect(3, width, "number", "nil");
(expect.range or function() end)(field, 0, 31);
(expect.range or function() end)(field + width - 1, 0, 31)
width = width or 1
local res = 0
for i = field + width - 1, field, -1 do
res = res * 2 + (bit.band(n, 2^i) / 2^i)
end
return res
end
function bit32.replace(n, v, field, width)
expect(1, n, "number")
expect(2, v, "number")
expect(3, field, "number")
expect(4, width, "number", "nil");
(expect.range or function() end)(field, 0, 31);
(expect.range or function() end)(field + width - 1, 0, 31)
width = width or 1
local mask = 2^width - 1
return bit.bor(bit.band(n, bit.bnot(bit.blshift(mask, field))), bit.blshift(bit.band(v, mask), field))
end
function bit32.lrotate(x, disp)
return bit.bor(bit.blshift(x, disp), bit.blogic_rshift(x, 32-disp))
end
function bit32.rrotate(x, disp)
return bit.bor(bit.blogic_rshift(x, disp), bit.blshift(x, 32-disp))
end
end
bit = nil
end
-- Check whether the _VERSION number is correct, and fix it if it's not
if _VERSION == "Lua 5.1" and load("::a:: goto a") then
_VERSION = "Lua 5.2"
if load("return 1 >> 2 & 3") then
_VERSION = "Lua 5.3"
if load("local <const> a = 2") then _VERSION = "Lua 5.4" end
end
end
-- Implement miscellaneous Lua 5.2 functionality if on 5.1
if _VERSION == "Lua 5.1" then
if not table.pack then table.pack = function(...)
local t = {...}
t.n = select("#", ...)
return t
end end
if not table.unpack then table.unpack, unpack = unpack, nil end
local _, v = xpcall(function(m) return m end, function() end, true)
if not v then
local old_xpcall = xpcall
xpcall = function(f, errh, ...)
if select("#", ...) > 0 then
local args = table.pack(...)
return old_xpcall(function() return f(table.unpack(args, 1, args.n)) end, errh)
else return old_xpcall(f, errh) end
end
end
end
-- Fix fs.combine on older versions to allow variable arguments
if tonumber(_HOST:match "ComputerCraft 1.(%d+)") < 95 then
-- The verbosity here fixes some issues with the sumneko Lua LSP.
---@param base string
---@param extra string
---@return string
local oldc = fs.combine
---@param p string
---@param ... string
---@return string
function fs.combine(p, ...)
if (...) ~= nil then
return oldc(p, fs.combine(...))
else
return p
end
end
end
-- Add string.pack if it's not present
if not string.pack then
local expect = expect.expect
local ByteOrder = {BIG_ENDIAN = 1, LITTLE_ENDIAN = 2}
local isint = {b = 1, B = 1, h = 1, H = 1, l = 1, L = 1, j = 1, J = 1, T = 1}
local packoptsize_tbl = {b = 1, B = 1, x = 1, h = 2, H = 2, f = 4, j = 4, J = 4, l = 8, L = 8, T = 8, d = 8, n = 8}
local function round(n) if n % 1 >= 0.5 then return math.ceil(n) else return math.floor(n) end end
local function floatToRawIntBits(f)
if f == 0 then return 0
elseif f == -0 then return 0x80000000
elseif f == math.huge then return 0x7F800000
elseif f == -math.huge then return 0xFF800000 end
local m, e = math.frexp(f)
if e > 127 or e < -126 then error("number out of range", 3) end
e, m = e + 126, round((math.abs(m) - 0.5) * 0x1000000)
if m > 0x7FFFFF then e = e + 1 end
return bit32.bor(f < 0 and 0x80000000 or 0, bit32.lshift(bit32.band(e, 0xFF), 23), bit32.band(m, 0x7FFFFF))
end
local function doubleToRawLongBits(f)
if f == 0 then return 0, 0
elseif f == -0 then return 0x80000000, 0
elseif f == math.huge then return 0x7FF00000, 0
elseif f == -math.huge then return 0xFFF00000, 0 end
local m, e = math.frexp(f)
if e > 1023 or e < -1022 then error("number out of range", 3) end
e, m = e + 1022, round((math.abs(m) - 0.5) * 0x20000000000000)
if m > 0xFFFFFFFFFFFFF then e = e + 1 end
return bit32.bor(f < 0 and 0x80000000 or 0, bit32.lshift(bit32.band(e, 0x7FF), 20), bit32.band(m / 0x100000000, 0xFFFFF)), bit32.band(m, 0xFFFFFFFF)
end
local function intBitsToFloat(l)
if l == 0 then return 0
elseif l == 0x80000000 then return -0
elseif l == 0x7F800000 then return math.huge
elseif l == 0xFF800000 then return -math.huge end
local m, e = bit32.band(l, 0x7FFFFF), bit32.band(bit32.rshift(l, 23), 0xFF)
e, m = e - 126, m / 0x1000000 + 0.5
local n = math.ldexp(m, e)
return bit32.btest(l, 0x80000000) and -n or n
end
local function longBitsToDouble(lh, ll)
if lh == 0 and ll == 0 then return 0
elseif lh == 0x80000000 and ll == 0 then return -0
elseif lh == 0x7FF00000 and ll == 0 then return math.huge
elseif lh == 0xFFF00000 and ll == 0 then return -math.huge end
local m, e = bit32.band(lh, 0xFFFFF) * 0x100000000 + bit32.band(ll, 0xFFFFFFFF), bit32.band(bit32.rshift(lh, 20), 0x7FF)
e, m = e - 1022, m / 0x20000000000000 + 0.5
local n = math.ldexp(m, e)
return bit32.btest(lh, 0x80000000) and -n or n
end
local function packint(num, size, output, offset, alignment, endianness, signed)
local total_size = 0
if offset % math.min(size, alignment) ~= 0 and alignment > 1 then
local i = 0
while offset % math.min(size, alignment) ~= 0 and i < alignment do
output[offset] = 0
offset = offset + 1
total_size = total_size + 1
i = i + 1
end
end
if endianness == ByteOrder.BIG_ENDIAN then
local added_padding = 0
if size > 8 then for i = 0, size - 9 do
output[offset + i] = (signed and num >= 2^(size * 8 - 1) ~= 0) and 0xFF or 0
added_padding = added_padding + 1
total_size = total_size + 1
end end
for i = added_padding, size - 1 do
output[offset + i] = bit32.band(bit32.rshift(num, ((size - i - 1) * 8)), 0xFF)
total_size = total_size + 1
end
else
for i = 0, math.min(size, 8) - 1 do
output[offset + i] = num / 2^(i * 8) % 256
total_size = total_size + 1
end
for i = 8, size - 1 do
output[offset + i] = (signed and num >= 2^(size * 8 - 1) ~= 0) and 0xFF or 0
total_size = total_size + 1
end
end
return total_size
end
local function unpackint(str, offset, size, endianness, alignment, signed)
local result, rsize = 0, 0
if offset % math.min(size, alignment) ~= 0 and alignment > 1 then
for i = 0, alignment - 1 do
if offset % math.min(size, alignment) == 0 then break end
offset = offset + 1
rsize = rsize + 1
end
end
for i = 0, size - 1 do
result = result + str:byte(offset + i) * 2^((endianness == ByteOrder.BIG_ENDIAN and size - i - 1 or i) * 8)
rsize = rsize + 1
end
if (signed and result >= 2^(size * 8 - 1)) then result = result - 2^(size * 8) end
return result, rsize
end
local function packoptsize(opt, alignment)
local retval = packoptsize_tbl[opt] or 0
if (alignment > 1 and retval % alignment ~= 0) then retval = retval + (alignment - (retval % alignment)) end
return retval
end
--[[
* string.pack (fmt, v1, v2, ...)
*
* Returns a binary string containing the values v1, v2, etc.
* serialized in binary form (packed) according to the format string fmt.
]]
function string.pack(...)
local fmt = expect(1, ..., "string")
local endianness = ByteOrder.LITTLE_ENDIAN
local alignment = 1
local pos = 1
local argnum = 2
local output = {}
local i = 1
while i <= #fmt do
local c = fmt:sub(i, i)
i = i + 1
if c == '=' or c == '<' then
endianness = ByteOrder.LITTLE_ENDIAN
elseif c == '>' then
endianness = ByteOrder.BIG_ENDIAN
elseif c == '!' then
local size = -1
while (i <= #fmt and fmt:sub(i, i):match("%d")) do
if (size >= 0xFFFFFFFF / 10) then error("bad argument #1 to 'pack' (invalid format)", 2) end
size = (math.max(size, 0) * 10) + tonumber(fmt:sub(i, i))
i = i + 1
end
if (size > 16 or size == 0) then error(string.format("integral size (%d) out of limits [1,16]", size), 2)
elseif (size == -1) then alignment = 4
else alignment = size end
elseif isint[c] then
local num = expect(argnum, select(argnum, ...), "number")
argnum = argnum + 1
if (num >= math.pow(2, (packoptsize(c, 0) * 8 - (c:match("%l") and 1 or 0))) or
num < (c:match("%l") and -math.pow(2, (packoptsize(c, 0) * 8 - 1)) or 0)) then
error(string.format("bad argument #%d to 'pack' (integer overflow)", argnum - 1), 2)
end
pos = pos + packint(num, packoptsize(c, 0), output, pos, alignment, endianness, false)
elseif c:lower() == 'i' then
local signed = c == 'i'
local size = -1
while i <= #fmt and fmt:sub(i, i):match("%d") do
if (size >= 0xFFFFFFFF / 10) then error("bad argument #1 to 'pack' (invalid format)", 2) end
size = (math.max(size, 0) * 10) + tonumber(fmt:sub(i, i))
i = i + 1
end
if (size > 16 or size == 0) then
error(string.format("integral size (%d) out of limits [1,16]", size), 2)
elseif (alignment > 1 and (size ~= 1 and size ~= 2 and size ~= 4 and size ~= 8 and size ~= 16)) then
error("bad argument #1 to 'pack' (format asks for alignment not power of 2)", 2)
elseif (size == -1) then size = 4 end
local num = expect(argnum, select(argnum, ...), "number")
argnum = argnum + 1
if (num >= math.pow(2, (size * 8 - (c:match("%l") and 1 or 0))) or
num < (c:match("%l") and -math.pow(2, (size * 8 - 1)) or 0)) then
error(string.format("bad argument #%d to 'pack' (integer overflow)", argnum - 1), 2)
end
pos = pos + packint(num, size, output, pos, alignment, endianness, signed)
elseif c == 'f' then
local f = expect(argnum, select(argnum, ...), "number")
argnum = argnum + 1
local l = floatToRawIntBits(f)
if (pos % math.min(4, alignment) ~= 0 and alignment > 1) then
for j = 0, alignment - 1 do
if pos % math.min(4, alignment) == 0 then break end
output[pos] = 0
pos = pos + 1
end
end
for j = 0, 3 do output[pos + (endianness == ByteOrder.BIG_ENDIAN and 3 - j or j)] = bit32.band(bit32.rshift(l, (j * 8)), 0xFF) end
pos = pos + 4
elseif c == 'd' or c == 'n' then
local f = expect(argnum, select(argnum, ...), "number")
argnum = argnum + 1
local lh, ll = doubleToRawLongBits(f)
if (pos % math.min(8, alignment) ~= 0 and alignment > 1) then
for j = 0, alignment - 1 do
if pos % math.min(8, alignment) == 0 then break end
output[pos] = 0
pos = pos + 1
end
end
for j = 0, 3 do output[pos + (endianness == ByteOrder.BIG_ENDIAN and 7 - j or j)] = bit32.band(bit32.rshift(ll, (j * 8)), 0xFF) end
for j = 4, 7 do output[pos + (endianness == ByteOrder.BIG_ENDIAN and 7 - j or j)] = bit32.band(bit32.rshift(lh, ((j - 4) * 8)), 0xFF) end
pos = pos + 8
elseif c == 'c' then
local size = 0
if (i > #fmt or not fmt:sub(i, i):match("%d")) then
error("missing size for format option 'c'", 2)
end
while (i <= #fmt and fmt:sub(i, i):match("%d")) do
if (size >= 0xFFFFFFFF / 10) then error("bad argument #1 to 'pack' (invalid format)", 2) end
size = (size * 10) + tonumber(fmt:sub(i, i))
i = i + 1
end
if (pos + size < pos or pos + size > 0xFFFFFFFF) then error("bad argument #1 to 'pack' (format result too large)", 2) end
local str = expect(argnum, select(argnum, ...), "string")
argnum = argnum + 1
if (#str > size) then error(string.format("bad argument #%d to 'pack' (string longer than given size)", argnum - 1), 2) end
if size > 0 then
for j = 0, size - 1 do output[pos+j] = str:byte(j + 1) or 0 end
pos = pos + size
end
elseif c == 'z' then
local str = expect(argnum, select(argnum, ...), "string")
argnum = argnum + 1
for b in str:gmatch "." do if (b == '\0') then error(string.format("bad argument #%d to 'pack' (string contains zeros)", argnum - 1), 2) end end
for j = 0, #str - 1 do output[pos+j] = str:byte(j + 1) end
output[pos + #str] = 0
pos = pos + #str + 1
elseif c == 's' then
local size = 0
while (i <= #fmt and fmt:sub(i, i):match("%d")) do
if (size >= 0xFFFFFFFF / 10) then error("bad argument #1 to 'pack' (invalid format)", 2) end
size = (size * 10) + tonumber(fmt:sub(i, i))
i = i + 1
end
if (size > 16) then
error(string.format("integral size (%d) out of limits [1,16]", size), 2)
elseif (size == 0) then size = 4 end
local str = expect(argnum, select(argnum, ...), "string")
argnum = argnum + 1
if (#str >= math.pow(2, (size * 8))) then
error(string.format("bad argument #%d to 'pack' (string length does not fit in given size)", argnum - 1), 2)
end
packint(#str, size, output, pos, 1, endianness, false)
for j = size, #str + size - 1 do output[pos+j] = str:byte(j - size + 1) or 0 end
pos = pos + #str + size
elseif c == 'x' then
output[pos] = 0
pos = pos + 1
elseif c == 'X' then
if (i >= #fmt) then error("invalid next option for option 'X'", 2) end
local size = 0
local c = fmt:sub(i, i)
i = i + 1
if c:lower() == 'i' then
while i <= #fmt and fmt:sub(i, i):match("%d") do
if (size >= 0xFFFFFFFF / 10) then error("bad argument #1 to 'pack' (invalid format)", 2) end
size = (size * 10) + tonumber(fmt:sub(i, i))
i = i + 1
end
if (size > 16 or size == 0) then
error(string.format("integral size (%d) out of limits [1,16]", size), 2)
end
else size = packoptsize(c, 0) end
if (size < 1) then error("invalid next option for option 'X'", 2) end
if (pos % math.min(size, alignment) ~= 0 and alignment > 1) then
for j = 1, alignment do
if pos % math.min(size, alignment) == 0 then break end
output[pos] = 0
pos = pos + 1
end
end
elseif c ~= ' ' then error(string.format("invalid format option '%s'", c), 2) end
end
return string.char(table.unpack(output))
end
--[[
* string.packsize (fmt)
*
* Returns the size of a string resulting from string.pack with the given format.
* The format string cannot have the variable-length options 's' or 'z'.
]]
function string.packsize(fmt)
local pos = 0
local alignment = 1
local i = 1
while i <= #fmt do
local c = fmt:sub(i, i)
i = i + 1
if c == '!' then
local size = 0
while i <= #fmt and fmt:sub(i, i):match("%d") do
if (size >= 0xFFFFFFFF / 10) then error("bad argument #1 to 'pack' (invalid format)", 2) end
size = (size * 10) + tonumber(fmt:sub(i, i))
i = i + 1
end
if (size > 16) then error(string.format("integral size (%d) out of limits [1,16]", size), 2)
elseif (size == 0) then alignment = 4
else alignment = size end
elseif isint[c] then
local size = packoptsize(c, 0)
if (pos % math.min(size, alignment) ~= 0 and alignment > 1) then
for j = 1, alignment do
if pos % math.min(size, alignment) == 0 then break end
pos = pos + 1
end
end
pos = pos + size
elseif c:lower() == 'i' then
local size = 0
while i <= #fmt and fmt:sub(i, i):match("%d") do
if (size >= 0xFFFFFFFF / 10) then error("bad argument #1 to 'pack' (invalid format)", 2) end
size = (size * 10) + tonumber(fmt:sub(i, i))
i = i + 1
end
if (size > 16) then
error(string.format("integral size (%d) out of limits [1,16]", size))
elseif (alignment > 1 and (size ~= 1 and size ~= 2 and size ~= 4 and size ~= 8 and size ~= 16)) then
error("bad argument #1 to 'pack' (format asks for alignment not power of 2)", 2)
elseif (size == 0) then size = 4 end
if (pos % math.min(size, alignment) ~= 0 and alignment > 1) then
for j = 1, alignment do
if pos % math.min(size, alignment) == 0 then break end
pos = pos + 1
end
end
pos = pos + size
elseif c == 'f' then
if (pos % math.min(4, alignment) ~= 0 and alignment > 1) then
for j = 1, alignment do
if pos % math.min(4, alignment) == 0 then break end
pos = pos + 1
end
end
pos = pos + 4
elseif c == 'd' or c == 'n' then
if (pos % math.min(8, alignment) ~= 0 and alignment > 1) then
for j = 1, alignment do
if pos % math.min(8, alignment) == 0 then break end
pos = pos + 1
end
end
pos = pos + 8
elseif c == 'c' then
local size = 0
if (i > #fmt or not fmt:sub(i, i):match("%d")) then
error("missing size for format option 'c'", 2)
end
while i <= #fmt and fmt:sub(i, i):match("%d") do
if (size >= 0xFFFFFFFF / 10) then error("bad argument #1 to 'pack' (invalid format)", 2) end
size = (size * 10) + tonumber(fmt:sub(i, i))
i = i + 1
end
if (pos + size < pos or pos + size > 0x7FFFFFFF) then error("bad argument #1 to 'packsize' (format result too large)", 2) end
pos = pos + size
elseif c == 'x' then
pos = pos + 1
elseif c == 'X' then
if (i >= #fmt) then error("invalid next option for option 'X'", 2) end
local size = 0
local c = fmt:sub(i, i)
i = i + 1
if c:lower() == 'i' then
while i <= #fmt and fmt:sub(i, i):match("%d") do
if (size >= 0xFFFFFFFF / 10) then error("bad argument #1 to 'pack' (invalid format)", 2) end
size = (size * 10) + tonumber(fmt:sub(i, i))
i = i + 1
end
if (size > 16 or size == 0) then
error(string.format("integral size (%d) out of limits [1,16]", size), 2)
end
else size = packoptsize(c, 0) end
if (size < 1) then error("invalid next option for option 'X'", 2) end
if (pos % math.min(size, alignment) ~= 0 and alignment > 1) then
for j = 1, alignment do
if pos % math.min(size, alignment) == 0 then break end
pos = pos + 1
end
end
elseif c == 's' or c == 'z' then error("bad argument #1 to 'packsize' (variable-length format)", 2)
elseif c ~= ' ' and c ~= '<' and c ~= '>' and c ~= '=' then error(string.format("invalid format option '%s'", c), 2) end
end
return pos
end
--[[
* string.unpack (fmt, s [, pos])
*
* Returns the values packed in string s (see string.pack) according to the format string fmt.
* An optional pos marks where to start reading in s (default is 1).
* After the read values, this function also returns the index of the first unread byte in s.
]]
function string.unpack(fmt, str, pos)
expect(1, fmt, "string")
expect(2, str, "string")
expect(3, pos, "number", "nil")
if pos then
if (pos < 0) then pos = #str + pos
elseif (pos == 0) then error("bad argument #3 to 'unpack' (initial position out of string)", 2) end
if (pos > #str or pos < 0) then error("bad argument #3 to 'unpack' (initial position out of string)", 2) end
else pos = 1 end
local endianness = ByteOrder.LITTLE_ENDIAN
local alignment = 1
local retval = {}
local i = 1
while i <= #fmt do
local c = fmt:sub(i, i)
i = i + 1
if c == '<' or c == '=' then
endianness = ByteOrder.LITTLE_ENDIAN
elseif c == '>' then
endianness = ByteOrder.BIG_ENDIAN
elseif c == '!' then
local size = 0
while i <= #fmt and fmt:sub(i, i):match("%d") do
if (size >= 0xFFFFFFFF / 10) then error("bad argument #1 to 'pack' (invalid format)", 2) end
size = (size * 10) + tonumber(fmt:sub(i, i))
i = i + 1
end
if (size > 16) then
error(string.format("integral size (%d) out of limits [1,16]", size))
elseif (size == 0) then alignment = 4
else alignment = size end
elseif isint[c] then
if (pos + packoptsize(c, 0) > #str + 1) then error("data string too short", 2) end
local res, ressz = unpackint(str, pos, packoptsize(c, 0), endianness, alignment, c:match("%l") ~= nil)
retval[#retval+1] = res
pos = pos + ressz
elseif c:lower() == 'i' then
local signed = c == 'i'
local size = 0
while (i <= #fmt and fmt:sub(i, i):match("%d")) do
if (size >= 0xFFFFFFFF / 10) then error("bad argument #1 to 'pack' (invalid format)", 2) end
size = (size * 10) + tonumber(fmt:sub(i, i))
i = i + 1
end
if (size > 16) then
error(string.format("integral size (%d) out of limits [1,16]", size), 2)
elseif (size > 8) then
error(string.format("%d-byte integer does not fit into Lua Integer", size), 2)
elseif (size == 0) then size = 4 end
if (pos + size > #str + 1) then error("data string too short", 2) end
local res, ressz = unpackint(str, pos, size, endianness, alignment, signed)
retval[#retval+1] = res
pos = pos + ressz
elseif c == 'f' then
if (pos % math.min(4, alignment) ~= 0 and alignment > 1) then
for j = 1, alignment do
if pos % math.min(4, alignment) == 0 then break end
pos = pos + 1
end
end
if (pos + 4 > #str + 1) then error("data string too short", 2) end
local res = unpackint(str, pos, 4, endianness, alignment, false)
retval[#retval+1] = intBitsToFloat(res)
pos = pos + 4
elseif c == 'd' or c == 'n' then
if (pos % math.min(8, alignment) ~= 0 and alignment > 1) then
for j = 1, alignment do
if pos % math.min(8, alignment) == 0 then break end
pos = pos + 1
end
end
if (pos + 8 > #str + 1) then error("data string too short", 2) end
local lh, ll = 0, 0
for j = 0, 3 do lh = bit32.bor(lh, bit32.lshift((str:byte(pos + j)), ((endianness == ByteOrder.BIG_ENDIAN and 3 - j or j) * 8))) end
for j = 0, 3 do ll = bit32.bor(ll, bit32.lshift((str:byte(pos + j + 4)), ((endianness == ByteOrder.BIG_ENDIAN and 3 - j or j) * 8))) end
if endianness == ByteOrder.LITTLE_ENDIAN then lh, ll = ll, lh end
retval[#retval+1] = longBitsToDouble(lh, ll)
pos = pos + 8
elseif c == 'c' then
local size = 0
if (i > #fmt or not fmt:sub(i, i):match("%d")) then
error("missing size for format option 'c'", 2)
end
while i <= #fmt and fmt:sub(i, i):match("%d") do
if (size >= 0xFFFFFFFF / 10) then error("bad argument #1 to 'pack' (invalid format)") end
size = (size * 10) + tonumber(fmt:sub(i, i))
i = i + 1
end
if (pos + size > #str + 1) then error("data string too short", 2) end
retval[#retval+1] = str:sub(pos, pos + size - 1)
pos = pos + size
elseif c == 'z' then
local size = 0
while (str:byte(pos + size) ~= 0) do
size = size + 1
if (pos + size > #str) then error("unfinished string for format 'z'", 2) end
end
retval[#retval+1] = str:sub(pos, pos + size - 1)
pos = pos + size + 1
elseif c == 's' then
local size = 0
while i <= #fmt and fmt:sub(i, i):match("%d") do
if (size >= 0xFFFFFFFF / 10) then error("bad argument #1 to 'pack' (invalid format)", 2) end
size = (size * 10) + tonumber(fmt:sub(i, i))
i = i + 1
end
if (size > 16) then
error(string.format("integral size (%d) out of limits [1,16]", size), 2)
elseif (size == 0) then size = 4 end
if (pos + size > #str + 1) then error("data string too short", 2) end
local num, numsz = unpackint(str, pos, size, endianness, alignment, false)
pos = pos + numsz
if (pos + num > #str + 1) then error("data string too short", 2) end
retval[#retval+1] = str:sub(pos, pos + num - 1)
pos = pos + num
elseif c == 'x' then
pos = pos + 1
elseif c == 'X' then
if (i >= #fmt) then error("invalid next option for option 'X'", 2) end
local size = 0
local c = fmt:sub(i, i)
i = i + 1
if c:lower() == 'i' then
while i <= #fmt and fmt:sub(i, i):match("%d") do
if (size >= 0xFFFFFFFF / 10) then error("bad argument #1 to 'pack' (invalid format)", 2) end
size = (size * 10) + tonumber(fmt:sub(i, i))
i = i + 1
end
if (size > 16 or size == 0) then
error(string.format("integral size (%d) out of limits [1,16]", size), 2)
elseif (size == -1) then size = 4 end
else size = packoptsize(c, 0) end
if (size < 1) then error("invalid next option for option 'X'", 2) end
if (pos % math.min(size, alignment) ~= 0 and alignment > 1) then
for j = 1, alignment do
if pos % math.min(size, alignment) == 0 then break end
pos = pos + 1
end
end
elseif c ~= ' ' then error(string.format("invalid format option '%s'", c), 2) end
end
retval[#retval+1] = pos
return table.unpack(retval)
end
end
-- Early version of panic function, before log initialization finishes (this is redefined later to use syslog)
function panic(message)
term.setBackgroundColor(32768)
term.setTextColor(16384)
term.setCursorBlink(false)
local x, y = term.getCursorPos()
x = 1
local w, h = term.getSize()
message = "panic: " .. (message or "unknown")
for word in message:gmatch "%S+" do
if x + #word >= w then
x, y = 1, y + 1
if y > h then
term.scroll(1)
y = y - 1
end
end
term.setCursorPos(x, y)
if x == 1 then term.clearLine() end
term.write(word .. " ")
x = x + #word + 1
end
x, y = 1, y + 1
if y > h then
term.scroll(1)
y = y - 1
end
if debug then
local traceback = debug.traceback(nil, 2)
for line in traceback:gmatch "[^\n]+" do
term.setCursorPos(1, y)
term.write(line)
y = y + 1
if y > h then
term.scroll(1)
y = y - 1
end
end
end
term.setCursorPos(1, y)
term.setTextColor(2)
term.write("panic: We are hanging here...")
mainThread = nil
if _HEADLESS then os.shutdown(1) end
while true do coroutine.yield() end
end
--- Small function to execute a syscall and error if it fails.
-- @tparam string call The syscall to execute
-- @tparam any ... The arguments to pass to the syscall
-- @treturn any... The values returned from the syscall
function do_syscall(call, ...)
local res = table.pack(coroutine.yield("syscall", call, ...))
if res[1] then return table.unpack(res, 2, res.n)
else error(res[2], 3) end
end
--- Copies a value. If the value is a table, copies all of its contents too.
-- @tparam any tab The value to copy
-- @treturn any The new copied value
function deepcopy(tab)
if type(tab) == "table" then
local retval = setmetatable({}, deepcopy(getmetatable(tab)))
for k,v in pairs(tab) do retval[deepcopy(k)] = deepcopy(v) end
return retval
else return tab end
end
--- Splits a string by a separator.
-- @tparam string str The string to split
-- @tparam[opt="%s"] string sep The separator pattern to split by
-- @treturn {string} A list of items in the string
function split(str, sep)
local t = {}
for match in str:gmatch("[^" .. (sep or "%s") .. "]+") do t[#t+1] = match end
return t
end
local procTime = pcall(os.epoch, "nano") and function() return os.epoch "nano" / 1000000 end or (ccemux and function() return ccemux.nanoTime() / 1000000 end or function() return os.epoch "utc" end)
local currentThread
function getCurrentThread() return currentThread end
local empty_packed_table = {n = 0}
--- Resumes a thread's coroutine, handling different yield types.
-- @tparam Process process The process that owns the thread
-- @tparam Thread thread The thread to resume
-- @tparam table ev An event to pass to the thread, if present
-- @tparam boolean dead Whether a thread in the current run cycle has died
-- @tparam boolean allWaiting Whether all previous threads were waiting for an event
-- @treturn boolean Whether this thread or a previous thread has died
-- @treturn boolean Whether all threads (including this one) are waiting for an event
function executeThread(process, thread, ev, dead, allWaiting)
local args
if thread.paused then return false, allWaiting end
if thread.status == "starting" then args = thread.args
elseif thread.status == "syscall" then args = table.pack(table.unpack(thread.syscall_return, 3, thread.syscall_return.n))
elseif thread.status == "preempt" then args = empty_packed_table
elseif thread.status == "suspended" then args = {ev[1], {}} for k, v in pairs(ev[2]) do args[2][k] = v end
elseif thread.status == "paused" then return false, allWaiting end
if thread.status ~= "dead" and (not thread.filter or thread.filter(process, thread, ev)) then
local old_dead = dead
dead = false
thread.filter = nil
local params
if thread.yielding then
--syslog.debug("Resuming yielded syscall")
params = {n = thread.syscall_return.n, true, "syscall", thread.yielding, table.unpack(thread.syscall_return, 4, thread.syscall_return.n)}
thread.yielding = nil
else
--syslog.debug("Resuming thread", process.id, thread.id)
assert(process.globalMetatables, "Process " .. process.id .. " has no global metatables")
currentThread = thread
local old = globalMetatables
globalMetatables = process.globalMetatables
updateGlobalMetatables()
local start = procTime()
params = table.pack(coroutine.resume(thread.coro, table.unpack(args, 1, args.n)))
--syslog.debug("Yield", params.n, table.unpack(params, 1, params.n))
process.cputime = process.cputime + (procTime() - start) / 1000
globalMetatables = old
updateGlobalMetatables()
currentThread = nil
end
if params[2] == "secure_syscall" then params[2] = "syscall"
elseif params[2] == "secure_event" then params[2] = nil end
if params[2] == "syscall" then
--syslog.debug("Calling syscall", params[3])
thread.status = "syscall"
local oldAllWaiting = allWaiting
allWaiting = false
if params[3] and syscalls[params[3]] then
local start = procTime()
thread.syscall_return = table.pack(coroutine.resume(thread.syscall, params[3], process, thread, table.unpack(params, 4, params.n)))
process.systime = process.systime + (procTime() - start) / 1000
if thread.syscall_return[2] == kSyscallComplete then
if not thread.syscall_return[3] and type(thread.syscall_return[4]) == "string" then
syslog.log({level = "debug", category = "Syscall Failure", process = 0, module = params[3]}, thread.syscall_return[4])
thread.syscall_return[4] = thread.syscall_return[4]:gsub("kernel:%d+: ", "")
end
if thread.syscall_return[4] == kSyscallYield then
thread.yielding = thread.syscall_return[5]
allWaiting = oldAllWaiting
end
else