forked from rce-incorporated/Fiu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.lua
1399 lines (1176 loc) · 39 KB
/
Source.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
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
-- // Environment changes in the VM are not supposed to alter the behaviour of the VM so we localise globals beforehand
local type = type
local pcall = pcall
local error = error
local tonumber = tonumber
local assert = assert
local setmetatable = setmetatable
local string_format = string.format
local table_move = table.move
local table_pack = table.pack
local table_unpack = table.unpack
local table_create = table.create
local table_insert = table.insert
local table_remove = table.remove
local table_concat = table.concat
local coroutine_create = coroutine.create
local coroutine_yield = coroutine.yield
local coroutine_resume = coroutine.resume
local coroutine_close = coroutine.close
local buffer_fromstring = buffer.fromstring
local buffer_len = buffer.len
local buffer_readu8 = buffer.readu8
local buffer_readu32 = buffer.readu32
local buffer_readstring = buffer.readstring
local buffer_readf32 = buffer.readf32
local buffer_readf64 = buffer.readf64
local bit32_bor = bit32.bor
local bit32_band = bit32.band
local bit32_btest = bit32.btest
local bit32_rshift = bit32.rshift
local bit32_lshift = bit32.lshift
local bit32_extract = bit32.extract
local ttisnumber = function(v) return type(v) == "number" end
local ttisstring = function(v) return type(v) == "string" end
local ttisboolean = function(v) return type(v) == "boolean" end
local ttisfunction = function(v) return type(v) == "function" end
-- // opList contains information about the instruction, each instruction is defined in this format:
-- // {OP_NAME, OP_MODE, K_MODE, HAS_AUX}
-- // OP_MODE specifies what type of registers the instruction uses if any
-- 0 = NONE
-- 1 = A
-- 2 = AB
-- 3 = ABC
-- 4 = AD
-- 5 = AE
-- // K_MODE specifies if the instruction has a register that holds a constant table index, which will be directly converted to the constant in the 2nd pass
-- 0 = NONE
-- 1 = AUX
-- 2 = C
-- 3 = D
-- 4 = AUX import
-- 5 = AUX boolean low 1 bit
-- 6 = AUX number low 24 bits
-- // HAS_AUX boolean specifies whether the instruction is followed up with an AUX word, which may be used to execute the instruction.
local opList = {
{ "NOP", 0, 0, false },
{ "BREAK", 0, 0, false },
{ "LOADNIL", 1, 0, false },
{ "LOADB", 3, 0, false },
{ "LOADN", 4, 0, false },
{ "LOADK", 4, 3, false },
{ "MOVE", 2, 0, false },
{ "GETGLOBAL", 1, 1, true },
{ "SETGLOBAL", 1, 1, true },
{ "GETUPVAL", 2, 0, false },
{ "SETUPVAL", 2, 0, false },
{ "CLOSEUPVALS", 1, 0, false },
{ "GETIMPORT", 4, 4, true },
{ "GETTABLE", 3, 0, false },
{ "SETTABLE", 3, 0, false },
{ "GETTABLEKS", 3, 1, true },
{ "SETTABLEKS", 3, 1, true },
{ "GETTABLEN", 3, 0, false },
{ "SETTABLEN", 3, 0, false },
{ "NEWCLOSURE", 4, 0, false },
{ "NAMECALL", 3, 1, true },
{ "CALL", 3, 0, false },
{ "RETURN", 2, 0, false },
{ "JUMP", 4, 0, false },
{ "JUMPBACK", 4, 0, false },
{ "JUMPIF", 4, 0, false },
{ "JUMPIFNOT", 4, 0, false },
{ "JUMPIFEQ", 4, 0, true },
{ "JUMPIFLE", 4, 0, true },
{ "JUMPIFLT", 4, 0, true },
{ "JUMPIFNOTEQ", 4, 0, true },
{ "JUMPIFNOTLE", 4, 0, true },
{ "JUMPIFNOTLT", 4, 0, true },
{ "ADD", 3, 0, false },
{ "SUB", 3, 0, false },
{ "MUL", 3, 0, false },
{ "DIV", 3, 0, false },
{ "MOD", 3, 0, false },
{ "POW", 3, 0, false },
{ "ADDK", 3, 2, false },
{ "SUBK", 3, 2, false },
{ "MULK", 3, 2, false },
{ "DIVK", 3, 2, false },
{ "MODK", 3, 2, false },
{ "POWK", 3, 2, false },
{ "AND", 3, 0, false },
{ "OR", 3, 0, false },
{ "ANDK", 3, 2, false },
{ "ORK", 3, 2, false },
{ "CONCAT", 3, 0, false },
{ "NOT", 2, 0, false },
{ "MINUS", 2, 0, false },
{ "LENGTH", 2, 0, false },
{ "NEWTABLE", 2, 0, true },
{ "DUPTABLE", 4, 3, false },
{ "SETLIST", 3, 0, true },
{ "FORNPREP", 4, 0, false },
{ "FORNLOOP", 4, 0, false },
{ "FORGLOOP", 4, 8, true },
{ "FORGPREP_INEXT", 4, 0, false },
{ "FASTCALL3", 3, 1, true },
{ "FORGPREP_NEXT", 4, 0, false },
{ "DEP_FORGLOOP_NEXT", 0, 0, false },
{ "GETVARARGS", 2, 0, false },
{ "DUPCLOSURE", 4, 3, false },
{ "PREPVARARGS", 1, 0, false },
{ "LOADKX", 1, 1, true },
{ "JUMPX", 5, 0, false },
{ "FASTCALL", 3, 0, false },
{ "COVERAGE", 5, 0, false },
{ "CAPTURE", 2, 0, false },
{ "SUBRK", 3, 7, false },
{ "DIVRK", 3, 7, false },
{ "FASTCALL1", 3, 0, false },
{ "FASTCALL2", 3, 0, true },
{ "FASTCALL2K", 3, 1, true },
{ "FORGPREP", 4, 0, false },
{ "JUMPXEQKNIL", 4, 5, true },
{ "JUMPXEQKB", 4, 5, true },
{ "JUMPXEQKN", 4, 6, true },
{ "JUMPXEQKS", 4, 6, true },
{ "IDIV", 3, 0, false },
{ "IDIVK", 3, 2, false },
}
local LUA_MULTRET = -1
local LUA_GENERALIZED_TERMINATOR = -2
local function luau_newsettings()
return {
vectorCtor = function() error("vectorCtor was not provided") end,
vectorSize = 4,
useNativeNamecall = false,
namecallHandler = function() error("Native __namecall handler was not provided") end,
extensions = {},
callHooks = {},
errorHandling = true,
generalizedIteration = true,
allowProxyErrors = false,
useImportConstants = false,
staticEnvironment = {},
decodeOp = function(op) return op end
}
end
local function luau_validatesettings(luau_settings)
assert(type(luau_settings) == "table", "luau_settings should be a table")
assert(type(luau_settings.vectorCtor) == "function", "luau_settings.vectorCtor should be a function")
assert(type(luau_settings.vectorSize) == "number", "luau_settings.vectorSize should be a number")
assert(type(luau_settings.useNativeNamecall) == "boolean", "luau_settings.useNativeNamecall should be a boolean")
assert(type(luau_settings.namecallHandler) == "function", "luau_settings.namecallHandler should be a function")
assert(type(luau_settings.extensions) == "table", "luau_settings.extensions should be a table of functions")
assert(type(luau_settings.callHooks) == "table", "luau_settings.callHooks should be a table of functions")
assert(type(luau_settings.errorHandling) == "boolean", "luau_settings.errorHandling should be a boolean")
assert(type(luau_settings.generalizedIteration) == "boolean", "luau_settings.generalizedIteration should be a boolean")
assert(type(luau_settings.allowProxyErrors) == "boolean", "luau_settings.allowProxyErrors should be a boolean")
assert(type(luau_settings.staticEnvironment) == "table", "luau_settings.staticEnvironment should be a table")
assert(type(luau_settings.useImportConstants) == "boolean", "luau_settings.useImportConstants should be a boolean")
assert(type(luau_settings.decodeOp) == "function", "luau_settings.decodeOp should be a function")
end
local function getmaxline(module, protoid)
local proto = if (protoid == nil) then module.mainProto else module.protoList[protoid]
local size = -1
assert(proto.lineinfoenabled, "proto must have debug enabled")
for pc = 1, proto.sizecode do
local line = proto.instructionlineinfo[pc]
size = if (line > size) then line else size
end
for i, subid in proto.protos do
local maxline = getmaxline(module, subid)
size = if (maxline > size) then maxline else size
end
return size
end
local function getcoverage(module, protoid, depth, callback, size)
local proto = if (protoid == nil) then module.mainProto else module.protoList[protoid]
assert(proto.lineinfoenabled, "proto must have debug enabled")
local buffer = {}
for pc = 1, proto.sizecode do
local inst = proto.code[pc]
local line = proto.instructionlineinfo[pc]
if (inst.opcode ~= 69) then --[[ COVERAGE ]]
continue
end
local hits = inst.E
buffer[line] = if ((buffer[line] or 0) > hits) then buffer[line] else hits
end
callback(proto.debugname, proto.linedefined, depth, buffer, size)
for i, subid in proto.protos do
getcoverage(module, subid, depth + 1, callback, size)
end
end
local function luau_getcoverage(module, protoid, callback)
assert(type(module) == "table", "module must be a table")
assert(type(protoid) == "number" or type(protoid) == "nil", "protoid must be a number or nil")
assert(type(callback) == "function", "callback must be a function")
getcoverage(module, protoid, 0, callback, getmaxline(module))
end
local function resolveImportConstant(static, count, k0, k1, k2)
local res = static[k0]
if count < 2 or res == nil then
return res
end
res = res[k1]
if count < 3 or res == nil then
return res
end
res = res[k2]
return res
end
local function luau_deserialize(bytecode, luau_settings)
if luau_settings == nil then
luau_settings = luau_newsettings()
else
luau_validatesettings(luau_settings)
end
local stream = if type(bytecode) == "string" then buffer_fromstring(bytecode) else bytecode
local cursor = 0
local function readByte()
local byte = buffer_readu8(stream, cursor)
cursor = cursor + 1
return byte
end
local function readWord()
local word = buffer_readu32(stream, cursor)
cursor = cursor + 4
return word
end
local function readFloat()
local float = buffer_readf32(stream, cursor)
cursor = cursor + 4
return float
end
local function readDouble()
local double = buffer_readf64(stream, cursor)
cursor = cursor + 8
return double
end
local function readVarInt()
local result = 0
for i = 0, 4 do
local value = readByte()
result = bit32_bor(result, bit32_lshift(bit32_band(value, 0x7F), i * 7))
if not bit32_btest(value, 0x80) then
break
end
end
return result
end
local function readString()
local size = readVarInt()
if size == 0 then
return ""
else
local str = buffer_readstring(stream, cursor, size)
cursor = cursor + size
return str
end
end
local luauVersion = readByte()
local typesVersion = 0
if luauVersion == 0 then
error("the provided bytecode is an error message",0)
elseif luauVersion < 3 or luauVersion > 6 then
error("the version of the provided bytecode is unsupported",0)
elseif luauVersion >= 4 then
typesVersion = readByte()
end
local stringCount = readVarInt()
local stringList = table_create(stringCount)
for i = 1, stringCount do
stringList[i] = readString()
end
local function readInstruction(codeList)
local value = luau_settings.decodeOp(readWord())
local opcode = bit32_band(value, 0xFF)
local opinfo = opList[opcode + 1]
local opname = opinfo[1]
local opmode = opinfo[2]
local kmode = opinfo[3]
local usesAux = opinfo[4]
local inst = {
opcode = opcode;
opname = opname;
opmode = opmode;
kmode = kmode;
usesAux = usesAux;
}
table_insert(codeList, inst)
if opmode == 1 then --[[ A ]]
inst.A = bit32_band(bit32_rshift(value, 8), 0xFF)
elseif opmode == 2 then --[[ AB ]]
inst.A = bit32_band(bit32_rshift(value, 8), 0xFF)
inst.B = bit32_band(bit32_rshift(value, 16), 0xFF)
elseif opmode == 3 then --[[ ABC ]]
inst.A = bit32_band(bit32_rshift(value, 8), 0xFF)
inst.B = bit32_band(bit32_rshift(value, 16), 0xFF)
inst.C = bit32_band(bit32_rshift(value, 24), 0xFF)
elseif opmode == 4 then --[[ AD ]]
inst.A = bit32_band(bit32_rshift(value, 8), 0xFF)
local temp = bit32_band(bit32_rshift(value, 16), 0xFFFF)
inst.D = if temp < 0x8000 then temp else temp - 0x10000
elseif opmode == 5 then --[[ AE ]]
local temp = bit32_band(bit32_rshift(value, 8), 0xFFFFFF)
inst.E = if temp < 0x800000 then temp else temp - 0x1000000
end
if usesAux then
local aux = readWord()
inst.aux = aux
table_insert(codeList, {value = aux, opname = "auxvalue" })
end
return usesAux
end
local function checkkmode(inst, k)
local kmode = inst.kmode
if kmode == 1 then --// AUX
inst.K = k[inst.aux + 1]
elseif kmode == 2 then --// C
inst.K = k[inst.C + 1]
elseif kmode == 3 then--// D
inst.K = k[inst.D + 1]
elseif kmode == 4 then --// AUX import
local extend = inst.aux
local count = bit32_rshift(extend, 30)
local id0 = bit32_band(bit32_rshift(extend, 20), 0x3FF)
inst.K0 = k[id0 + 1]
inst.KC = count
if count == 2 then
local id1 = bit32_band(bit32_rshift(extend, 10), 0x3FF)
inst.K1 = k[id1 + 1]
elseif count == 3 then
local id1 = bit32_band(bit32_rshift(extend, 10), 0x3FF)
local id2 = bit32_band(bit32_rshift(extend, 0), 0x3FF)
inst.K1 = k[id1 + 1]
inst.K2 = k[id2 + 1]
end
if luau_settings.useImportConstants then
inst.K = resolveImportConstant(
luau_settings.staticEnvironment,
count, inst.K0, inst.K1, inst.K2
)
end
elseif kmode == 5 then --// AUX boolean low 1 bit
inst.K = bit32_extract(inst.aux, 0, 1) == 1
inst.KN = bit32_extract(inst.aux, 31, 1) == 1
elseif kmode == 6 then --// AUX number low 24 bits
inst.K = k[bit32_extract(inst.aux, 0, 24) + 1]
inst.KN = bit32_extract(inst.aux, 31, 1) == 1
elseif kmode == 7 then --// B
inst.K = k[inst.B + 1]
elseif kmode == 8 then --// AUX number low 16 bits
inst.K = bit32_band(inst.aux, 0xf)
end
end
local function readProto(bytecodeid)
local maxstacksize = readByte()
local numparams = readByte()
local nups = readByte()
local isvararg = readByte() ~= 0
if luauVersion >= 4 then
readByte() --// flags
local typesize = readVarInt();
cursor = cursor + typesize;
end
local sizecode = readVarInt()
local codelist = table_create(sizecode)
local skipnext = false
for i = 1, sizecode do
if skipnext then
skipnext = false
continue
end
skipnext = readInstruction(codelist)
end
local debugcodelist = table_create(sizecode)
for i = 1, sizecode do
debugcodelist[i] = codelist[i].opcode
end
local sizek = readVarInt()
local klist = table_create(sizek)
for i = 1, sizek do
local kt = readByte()
local k
if kt == 0 then --// Nil
k = nil
elseif kt == 1 then --// Bool
k = readByte() ~= 0
elseif kt == 2 then --// Number
k = readDouble()
elseif kt == 3 then --// String
k = stringList[readVarInt()]
elseif kt == 4 then --// Import
k = readWord()
elseif kt == 5 then --// Table
local dataLength = readVarInt()
k = table_create(dataLength)
for i = 1, dataLength do
k[i] = readVarInt()
end
elseif kt == 6 then --// Closure
k = readVarInt()
elseif kt == 7 then --// Vector
local x,y,z,w = readFloat(), readFloat(), readFloat(), readFloat()
if luau_settings.vectorSize == 4 then
k = luau_settings.vectorCtor(x,y,z,w)
else
k = luau_settings.vectorCtor(x,y,z)
end
end
klist[i] = k
end
-- // 2nd pass to replace constant references in the instruction
for i = 1, sizecode do
checkkmode(codelist[i], klist)
end
local sizep = readVarInt()
local protolist = table_create(sizep)
for i = 1, sizep do
protolist[i] = readVarInt() + 1
end
local linedefined = readVarInt()
local debugnameindex = readVarInt()
local debugname
if debugnameindex ~= 0 then
debugname = stringList[debugnameindex]
else
debugname = "(??)"
end
-- // lineinfo
local lineinfoenabled = readByte() ~= 0
local instructionlineinfo = nil
if lineinfoenabled then
local linegaplog2 = readByte()
local intervals = bit32_rshift((sizecode - 1), linegaplog2) + 1
local lineinfo = table_create(sizecode)
local abslineinfo = table_create(intervals)
local lastoffset = 0
for j = 1, sizecode do
lastoffset += readByte()
lineinfo[j] = lastoffset
end
local lastline = 0
for j = 1, intervals do
lastline += readWord()
abslineinfo[j] = lastline % (2 ^ 32)
end
instructionlineinfo = table_create(sizecode)
for i = 1, sizecode do
--// p->abslineinfo[pc >> p->linegaplog2] + p->lineinfo[pc];
table_insert(instructionlineinfo, abslineinfo[bit32_rshift(i - 1, linegaplog2) + 1] + lineinfo[i])
end
end
-- // debuginfo
if readByte() ~= 0 then
local sizel = readVarInt()
for i = 1, sizel do
readVarInt()
readVarInt()
readVarInt()
readByte()
end
local sizeupvalues = readVarInt()
for i = 1, sizeupvalues do
readVarInt()
end
end
return {
maxstacksize = maxstacksize;
numparams = numparams;
nups = nups;
isvararg = isvararg;
linedefined = linedefined;
debugname = debugname;
sizecode = sizecode;
code = codelist;
debugcode = debugcodelist;
sizek = sizek;
k = klist;
sizep = sizep;
protos = protolist;
lineinfoenabled = lineinfoenabled;
instructionlineinfo = instructionlineinfo;
bytecodeid = bytecodeid;
}
end
-- userdataRemapping (not used in VM, left unused)
if typesVersion == 3 then
local index = readByte()
while index ~= 0 do
readVarInt()
index = readByte()
end
end
local protoCount = readVarInt()
local protoList = table_create(protoCount)
for i = 1, protoCount do
protoList[i] = readProto(i - 1)
end
local mainProto = protoList[readVarInt() + 1]
assert(cursor == buffer_len(stream), "deserializer cursor position mismatch")
mainProto.debugname = "(main)"
return {
stringList = stringList;
protoList = protoList;
mainProto = mainProto;
typesVersion = typesVersion;
}
end
local function luau_load(module, env, luau_settings)
if luau_settings == nil then
luau_settings = luau_newsettings()
else
luau_validatesettings(luau_settings)
end
if type(module) ~= "table" then
module = luau_deserialize(module, luau_settings)
end
local protolist = module.protoList
local mainProto = module.mainProto
local breakHook = luau_settings.callHooks.breakHook
local stepHook = luau_settings.callHooks.stepHook
local interruptHook = luau_settings.callHooks.interruptHook
local panicHook = luau_settings.callHooks.panicHook
local alive = true
local function luau_close()
alive = false
end
local function luau_wrapclosure(module, proto, upvals)
local function luau_execute(...)
local debugging, stack, protos, code, varargs
if luau_settings.errorHandling then
debugging, stack, protos, code, varargs = ...
else
--// Copied from error handling wrapper
local passed = table_pack(...)
stack = table_create(proto.maxstacksize)
varargs = {
len = 0,
list = {},
}
table_move(passed, 1, proto.numparams, 0, stack)
if proto.numparams < passed.n then
local start = proto.numparams + 1
local len = passed.n - proto.numparams
varargs.len = len
table_move(passed, start, start + len - 1, 1, varargs.list)
end
passed = nil
debugging = {pc = 0, name = "NONE"}
protos = proto.protos
code = proto.code
end
local top, pc, open_upvalues, generalized_iterators = -1, 1, setmetatable({}, {__mode = "vs"}), setmetatable({}, {__mode = "ks"})
local constants = proto.k
local debugopcodes = proto.debugcode
local extensions = luau_settings.extensions
local handlingBreak = false
local inst, op
while alive do
if not handlingBreak then
inst = code[pc]
op = inst.opcode
end
handlingBreak = false
debugging.pc = pc
debugging.top = top
debugging.name = inst.opname
pc += 1
if stepHook then
stepHook(stack, debugging, proto, module, upvals)
end
if op == 0 then --[[ NOP ]]
--// Do nothing
elseif op == 1 then --[[ BREAK ]]
if breakHook then
local results = table.pack(breakHook(stack, debugging, proto, module, upvals))
if results[1] then
return table_unpack(results, 2, #results)
end
end
pc -= 1
op = debugopcodes[pc]
handlingBreak = true
elseif op == 2 then --[[ LOADNIL ]]
stack[inst.A] = nil
elseif op == 3 then --[[ LOADB ]]
stack[inst.A] = inst.B == 1
pc += inst.C
elseif op == 4 then --[[ LOADN ]]
stack[inst.A] = inst.D
elseif op == 5 then --[[ LOADK ]]
stack[inst.A] = inst.K
elseif op == 6 then --[[ MOVE ]]
stack[inst.A] = stack[inst.B]
elseif op == 7 then --[[ GETGLOBAL ]]
local kv = inst.K
stack[inst.A] = extensions[kv] or env[kv]
pc += 1 --// adjust for aux
elseif op == 8 then --[[ SETGLOBAL ]]
local kv = inst.K
env[kv] = stack[inst.A]
pc += 1 --// adjust for aux
elseif op == 9 then --[[ GETUPVAL ]]
local uv = upvals[inst.B + 1]
stack[inst.A] = uv.store[uv.index]
elseif op == 10 then --[[ SETUPVAL ]]
local uv = upvals[inst.B + 1]
uv.store[uv.index] = stack[inst.A]
elseif op == 11 then --[[ CLOSEUPVALS ]]
for i, uv in open_upvalues do
if uv.index >= inst.A then
uv.value = uv.store[uv.index]
uv.store = uv
uv.index = "value" --// self reference
open_upvalues[i] = nil
end
end
elseif op == 12 then --[[ GETIMPORT ]]
if luau_settings.useImportConstants then
stack[inst.A] = inst.K
else
local count = inst.KC
local k0 = inst.K0
local import = extensions[k0] or env[k0]
if count == 1 then
stack[inst.A] = import
elseif count == 2 then
stack[inst.A] = import[inst.K1]
elseif count == 3 then
stack[inst.A] = import[inst.K1][inst.K2]
end
end
pc += 1 --// adjust for aux
elseif op == 13 then --[[ GETTABLE ]]
stack[inst.A] = stack[inst.B][stack[inst.C]]
elseif op == 14 then --[[ SETTABLE ]]
stack[inst.B][stack[inst.C]] = stack[inst.A]
elseif op == 15 then --[[ GETTABLEKS ]]
local index = inst.K
stack[inst.A] = stack[inst.B][index]
pc += 1 --// adjust for aux
elseif op == 16 then --[[ SETTABLEKS ]]
local index = inst.K
stack[inst.B][index] = stack[inst.A]
pc += 1 --// adjust for aux
elseif op == 17 then --[[ GETTABLEN ]]
stack[inst.A] = stack[inst.B][inst.C + 1]
elseif op == 18 then --[[ SETTABLEN ]]
stack[inst.B][inst.C + 1] = stack[inst.A]
elseif op == 19 then --[[ NEWCLOSURE ]]
local newPrototype = protolist[protos[inst.D + 1]]
local nups = newPrototype.nups
local upvalues = table_create(nups)
stack[inst.A] = luau_wrapclosure(module, newPrototype, upvalues)
for i = 1, nups do
local pseudo = code[pc]
pc += 1
local type = pseudo.A
if type == 0 then --// value
local upvalue = {
value = stack[pseudo.B],
index = "value",--// self reference
}
upvalue.store = upvalue
upvalues[i] = upvalue
elseif type == 1 then --// reference
local index = pseudo.B
local prev = open_upvalues[index]
if prev == nil then
prev = {
index = index,
store = stack,
}
open_upvalues[index] = prev
end
upvalues[i] = prev
elseif type == 2 then --// upvalue
upvalues[i] = upvals[pseudo.B + 1]
end
end
elseif op == 20 then --[[ NAMECALL ]]
local A = inst.A
local B = inst.B
local kv = inst.K
local sb = stack[B]
stack[A + 1] = sb
pc += 1 --// adjust for aux
local useFallback = true
--// Special handling for native namecall behaviour
local useNativeHandler = luau_settings.useNativeNamecall
if useNativeHandler then
local nativeNamecall = luau_settings.namecallHandler
local callInst = code[pc]
local callOp = callInst.opcode
--// Copied from the CALL handler under
local callA, callB, callC = callInst.A, callInst.B, callInst.C
if stepHook then
stepHook(stack, debugging, proto, module, upvals)
end
if interruptHook then
interruptHook(stack, debugging, proto, module, upvals)
end
local params = if callB == 0 then top - callA else callB - 1
local ret_list = table_pack(
nativeNamecall(kv, table_unpack(stack, callA + 1, callA + params))
)
if ret_list[1] == true then
useFallback = false
pc += 1 --// Skip next CALL instruction
inst = callInst
op = callOp
debugging.pc = pc
debugging.name = inst.opname
table_remove(ret_list, 1)
local ret_num = ret_list.n - 1
if callC == 0 then
top = callA + ret_num - 1
else
ret_num = callC - 1
end
table_move(ret_list, 1, ret_num, callA, stack)
end
end
if useFallback then
stack[A] = sb[kv]
end
elseif op == 21 then --[[ CALL ]]
if interruptHook then
interruptHook(stack, debugging, proto, module, upvals)
end
local A, B, C = inst.A, inst.B, inst.C
local params = if B == 0 then top - A else B - 1
local func = stack[A]
local ret_list = table_pack(
func(table_unpack(stack, A + 1, A + params))
)
local ret_num = ret_list.n
if C == 0 then
top = A + ret_num - 1
else
ret_num = C - 1
end
table_move(ret_list, 1, ret_num, A, stack)
elseif op == 22 then --[[ RETURN ]]
if interruptHook then
interruptHook(stack, debugging, proto, module, upvals)
end
local A = inst.A
local B = inst.B
local b = B - 1
local nresults
if b == LUA_MULTRET then
nresults = top - A + 1
else
nresults = B - 1
end
return table_unpack(stack, A, A + nresults - 1)
elseif op == 23 then --[[ JUMP ]]
pc += inst.D
elseif op == 24 then --[[ JUMPBACK ]]
if interruptHook then
interruptHook(stack, debugging, proto, module, upvals)
end
pc += inst.D
elseif op == 25 then --[[ JUMPIF ]]
if stack[inst.A] then
pc += inst.D
end
elseif op == 26 then --[[ JUMPIFNOT ]]
if not stack[inst.A] then
pc += inst.D
end
elseif op == 27 then --[[ JUMPIFEQ ]]
if stack[inst.A] == stack[inst.aux] then
pc += inst.D
else
pc += 1
end
elseif op == 28 then --[[ JUMPIFLE ]]
if stack[inst.A] <= stack[inst.aux] then
pc += inst.D
else
pc += 1
end
elseif op == 29 then --[[ JUMPIFLT ]]
if stack[inst.A] < stack[inst.aux] then
pc += inst.D
else
pc += 1
end
elseif op == 30 then --[[ JUMPIFNOTEQ ]]
if stack[inst.A] == stack[inst.aux] then
pc += 1
else
pc += inst.D
end
elseif op == 31 then --[[ JUMPIFNOTLE ]]
if stack[inst.A] <= stack[inst.aux] then
pc += 1
else
pc += inst.D
end
elseif op == 32 then --[[ JUMPIFNOTLT ]]
if stack[inst.A] < stack[inst.aux] then
pc += 1
else
pc += inst.D
end
elseif op == 33 then --[[ ADD ]]
stack[inst.A] = stack[inst.B] + stack[inst.C]
elseif op == 34 then --[[ SUB ]]
stack[inst.A] = stack[inst.B] - stack[inst.C]
elseif op == 35 then --[[ MUL ]]
stack[inst.A] = stack[inst.B] * stack[inst.C]
elseif op == 36 then --[[ DIV ]]
stack[inst.A] = stack[inst.B] / stack[inst.C]
elseif op == 37 then --[[ MOD ]]
stack[inst.A] = stack[inst.B] % stack[inst.C]
elseif op == 38 then --[[ POW ]]
stack[inst.A] = stack[inst.B] ^ stack[inst.C]
elseif op == 39 then --[[ ADDK ]]
stack[inst.A] = stack[inst.B] + inst.K
elseif op == 40 then --[[ SUBK ]]
stack[inst.A] = stack[inst.B] - inst.K