Advertisement
gabeisdumblol

Untitled

Jun 17th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.53 KB | None | 0 0
  1. local Select = select;
  2. local Byte = string.byte;
  3. local Sub = string.sub;
  4. local Opcode = { -- Opcode types.
  5. 'ABC', 'ABx', 'ABC', 'ABC';
  6. 'ABC', 'ABx', 'ABC', 'ABx';
  7. 'ABC', 'ABC', 'ABC', 'ABC';
  8. 'ABC', 'ABC', 'ABC', 'ABC';
  9. 'ABC', 'ABC', 'ABC', 'ABC';
  10. 'ABC', 'ABC', 'AsBx', 'ABC';
  11. 'ABC', 'ABC', 'ABC', 'ABC';
  12. 'ABC', 'ABC', 'ABC', 'AsBx';
  13. 'AsBx', 'ABC', 'ABC', 'ABC';
  14. 'ABx', 'ABC';
  15. };
  16.  
  17. -- rlbi author -> Rerumu
  18. -- special thanks;
  19. -- @cntkillme for providing faster bit extraction
  20. -- @Eternal for being #1 bug finder and providing better float decoder
  21. -- @stravant for contributing to the original project this is derived from
  22.  
  23. -- rerubi is an upgrade to the original Lua VM in Lua
  24. -- the prime goal of rerubi is to be the fastest:tm: alternative
  25. -- to a Lua in Lua bytecode execution
  26.  
  27. local function gBit(Bit, Start, End) -- No tail-calls, yay.
  28. if End then -- Thanks to cntkillme for giving input on this shorter, better approach.
  29. local Res = (Bit / 2 ^ (Start - 1)) % 2 ^ ((End - 1) - (Start - 1) + 1);
  30.  
  31. return Res - Res % 1;
  32. else
  33. local Plc = 2 ^ (Start - 1);
  34.  
  35. if (Bit % (Plc + Plc) >= Plc) then
  36. return 1;
  37. else
  38. return 0;
  39. end;
  40. end;
  41. end;
  42.  
  43. local function GetMeaning(ByteString)
  44. local Pos = 1;
  45. local gSizet;
  46. local gInt;
  47.  
  48. local function gBits8() -- Get the next byte in the stream.
  49. local F = Byte(ByteString, Pos, Pos);
  50.  
  51. Pos = Pos + 1;
  52.  
  53. return F;
  54. end;
  55.  
  56. local function gBits32()
  57. local W, X, Y, Z = Byte(ByteString, Pos, Pos + 3);
  58.  
  59. Pos = Pos + 4;
  60.  
  61. return (Z * 16777216) + (Y * 65536) + (X * 256) + W;
  62. end;
  63.  
  64. local function gBits64()
  65. return gBits32() * 4294967296 + gBits32();
  66. end;
  67.  
  68. local function gFloat()
  69. -- thanks @Eternal for giving me this so I could mangle it in here and have it work
  70. local Left = gBits32();
  71. local Right = gBits32();
  72. local IsNormal = 1
  73. local Mantissa = (gBit(Right, 1, 20) * (2 ^ 32))
  74. + Left;
  75.  
  76. local Exponent = gBit(Right, 21, 31);
  77. local Sign = ((-1) ^ gBit(Right, 32));
  78.  
  79. if (Exponent == 0) then
  80. if (Mantissa == 0) then
  81. return Sign * 0 -- +-0
  82. else
  83. Exponent = 1
  84. IsNormal = 0
  85. end
  86. elseif (Exponent == 2047) then
  87. if (Mantissa == 0) then
  88. return Sign * (1 / 0) -- +-Inf
  89. else
  90. return Sign * (0 / 0) -- +-Q/Nan
  91. end
  92. end
  93.  
  94. -- sign * 2**e-1023 * isNormal.mantissa
  95. return math.ldexp(Sign, Exponent - 1023) * (IsNormal + (Mantissa / (2 ^ 52)))
  96. end;
  97.  
  98. local function gString(Len)
  99. local Str;
  100.  
  101. if Len then
  102. Str = Sub(ByteString, Pos, Pos + Len - 1);
  103.  
  104. Pos = Pos + Len;
  105. else
  106. Len = gSizet();
  107.  
  108. if (Len == 0) then return; end;
  109.  
  110. Str = Sub(ByteString, Pos, Pos + Len - 1);
  111.  
  112. Pos = Pos + Len;
  113. end;
  114.  
  115. return Str;
  116. end;
  117.  
  118. local function ChunkDecode()
  119. local Instr = {};
  120. local Const = {};
  121. local Proto = {};
  122. local Chunk = {
  123. Instr = Instr; -- Instructions
  124. Const = Const; -- Constants
  125. Proto = Proto; -- Prototypes
  126. Lines = {}; -- Lines
  127. Name = gString(); -- Grab name string.
  128. FirstL = gInt(); -- First line.
  129. LastL = gInt(); -- Last line.
  130. Upvals = gBits8(); -- Upvalue count.
  131. Args = gBits8(); -- Arg count.
  132. Vargs = gBits8(); -- Vararg type.
  133. Stack = gBits8(); -- Stack.
  134. };
  135.  
  136. if Chunk.Name then
  137. Chunk.Name = Sub(Chunk.Name, 1, -2);
  138. end;
  139.  
  140. for Idx = 1, gInt() do -- Loading instructions to the chunk.
  141. local Data = gBits32();
  142. local Opco = gBit(Data, 1, 6);
  143. local Type = Opcode[Opco + 1];
  144. local Inst;
  145.  
  146. if Type then
  147. Inst = {
  148. Enum = Opco;
  149. gBit(Data, 7, 14); -- Register A.
  150. };
  151.  
  152. if (Type == 'ABC') then -- Most common, basic instruction type.
  153. Inst[2] = gBit(Data, 24, 32);
  154. Inst[3] = gBit(Data, 15, 23);
  155. elseif (Type == 'ABx') then
  156. Inst[2] = gBit(Data, 15, 32);
  157. elseif (Type == 'AsBx') then
  158. Inst[2] = gBit(Data, 15, 32) - 131071;
  159. end;
  160. else
  161. Inst = Data; -- Extended SETLIST
  162. end;
  163.  
  164. Instr[Idx] = Inst;
  165. end;
  166.  
  167. for Idx = 1, gInt() do -- Load constants.
  168. local Type = gBits8();
  169. local Cons;
  170.  
  171. if (Type == 1) then -- Boolean
  172. Cons = (gBits8() ~= 0);
  173. elseif (Type == 3) then -- Float/Double
  174. Cons = gFloat();
  175. elseif (Type == 4) then
  176. Cons = Sub(gString(), 1, -2);
  177. end;
  178.  
  179. Const[Idx - 1] = Cons;
  180. end;
  181.  
  182. for Idx = 1, gInt() do -- Nested function prototypes.
  183. Proto[Idx - 1] = ChunkDecode();
  184. end;
  185.  
  186. do -- Debugging
  187. local Lines = Chunk.Lines;
  188.  
  189. for Idx = 1, gInt() do
  190. Lines[Idx] = gBits32();
  191. end;
  192.  
  193. for _ = 1, gInt() do -- Locals in stack.
  194. gString(); -- Name of local.
  195. gBits32(); -- Starting point.
  196. gBits32(); -- End point.
  197. end;
  198.  
  199. for _ = 1, gInt() do -- Upvalues.
  200. gString(); -- Name of upvalue.
  201. end;
  202. end;
  203.  
  204. return Chunk; -- Finished chunk.
  205. end;
  206.  
  207. do -- Most of this chunk I was too lazy to reformat or change
  208. assert(gString(4) == "\27Lua", "Lua bytecode expected.");
  209. assert(gBits8() == 0x51, "Only Lua 5.1 is supported.");
  210.  
  211. gBits8(); -- Probably version control.
  212. gBits8(); -- Is small endians.
  213.  
  214. local IntSize = gBits8(); -- Int size
  215. local Sizet = gBits8(); -- size_t
  216.  
  217. if (IntSize == 4) then
  218. gInt = gBits32;
  219. elseif (IntSize == 8) then
  220. gInt = gBits64;
  221. else
  222. error('Integer size not supported', 2);
  223. end;
  224.  
  225. if (Sizet == 4) then
  226. gSizet = gBits32;
  227. elseif (Sizet == 8) then
  228. gSizet = gBits64;
  229. else
  230. error('Sizet size not supported', 2);
  231. end;
  232.  
  233. assert(gString(3) == "\4\8\0", "Unsupported bytecode target platform");
  234. end;
  235.  
  236. return ChunkDecode();
  237. end;
  238.  
  239. local function _Returns(...)
  240. return Select('#', ...), {...};
  241. end;
  242.  
  243. local function Wrap(Chunk, Env, Upvalues)
  244. local Instr = Chunk.Instr;
  245. local Const = Chunk.Const;
  246. local Proto = Chunk.Proto;
  247.  
  248. local function OnError(Err, Position) -- Handle your errors in whatever way.
  249. local Name = Chunk.Name or 'Code';
  250. local Line = Chunk.Lines[Position] or '?';
  251.  
  252. Err = tostring(Err):match'^.+:%s*(.+)' or Err;
  253.  
  254. error(string.format('%s (%s): %s', Name, Line, Err), 0);
  255. end;
  256.  
  257. return function(...)
  258. -- Returned function to run bytecode chunk (Don't be stupid, you can't setfenv this to work your way).
  259. local InstrPoint, Top = 1, -1;
  260. local Vararg, Varargsz = {}, Select('#', ...) - 1;
  261.  
  262. local GStack = {};
  263. local Lupvals = {};
  264. local Stack = setmetatable({}, {
  265. __index = GStack;
  266. __newindex = function(_, Key, Value)
  267. if (Key > Top) then
  268. Top = Key;
  269. end;
  270.  
  271. GStack[Key] = Value;
  272. end;
  273. });
  274.  
  275. local function Loop()
  276. local Inst, Enum;
  277.  
  278. while true do
  279. Inst = Instr[InstrPoint];
  280. Enum = Inst.Enum;
  281. InstrPoint = InstrPoint + 1;
  282.  
  283. if (Enum == 0) then -- MOVE
  284. Stack[Inst[1]] = Stack[Inst[2]];
  285. elseif (Enum == 1) then -- LOADK
  286. Stack[Inst[1]] = Const[Inst[2]];
  287. elseif (Enum == 2) then -- LOADBOOL
  288. Stack[Inst[1]] = (Inst[2] ~= 0);
  289.  
  290. if (Inst[3] ~= 0) then
  291. InstrPoint = InstrPoint + 1;
  292. end;
  293. elseif (Enum == 3) then -- LOADNIL
  294. local Stk = Stack;
  295.  
  296. for Idx = Inst[1], Inst[2] do
  297. Stk[Idx] = nil;
  298. end;
  299. elseif (Enum == 4) then -- GETUPVAL
  300. Stack[Inst[1]] = Upvalues[Inst[2]];
  301. elseif (Enum == 5) then -- GETGLOBAL
  302. Stack[Inst[1]] = Env[Const[Inst[2]]];
  303. elseif (Enum == 6) then -- GETTABLE
  304. local C = Inst[3];
  305. local Stk = Stack;
  306.  
  307. if (C > 255) then
  308. C = Const[C - 256];
  309. else
  310. C = Stk[C];
  311. end;
  312.  
  313. Stk[Inst[1]] = Stk[Inst[2]][C];
  314. elseif (Enum == 7) then -- SETGLOBAL
  315. Env[Const[Inst[2]]] = Stack[Inst[1]];
  316. elseif (Enum == 8) then -- SETUPVAL
  317. Upvalues[Inst[2]] = Stack[Inst[1]];
  318. elseif (Enum == 9) then -- SETTABLE
  319. local B, C = Inst[2], Inst[3];
  320. local Stk = Stack;
  321.  
  322. if (B > 255) then
  323. B = Const[B - 256];
  324. else
  325. B = Stk[B];
  326. end;
  327.  
  328. if (C > 255) then
  329. C = Const[C - 256];
  330. else
  331. C = Stk[C];
  332. end;
  333.  
  334. Stk[Inst[1]][B] = C;
  335. elseif (Enum == 10) then -- NEWTABLE
  336. Stack[Inst[1]] = {};
  337. elseif (Enum == 11) then -- SELF
  338. local A = Inst[1];
  339. local B = Inst[2];
  340. local C = Inst[3];
  341. local Stk = Stack;
  342.  
  343. B = Stk[B];
  344.  
  345. if (C > 255) then
  346. C = Const[C - 256];
  347. else
  348. C = Stk[C];
  349. end;
  350.  
  351. Stk[A + 1] = B;
  352. Stk[A] = B[C];
  353. elseif (Enum == 12) then -- ADD
  354. local B = Inst[2];
  355. local C = Inst[3];
  356. local Stk = Stack;
  357.  
  358. if (B > 255) then
  359. B = Const[B - 256];
  360. else
  361. B = Stk[B];
  362. end;
  363.  
  364. if (C > 255) then
  365. C = Const[C - 256];
  366. else
  367. C = Stk[C];
  368. end;
  369.  
  370. Stk[Inst[1]] = B + C;
  371. elseif (Enum == 13) then -- SUB
  372. local B = Inst[2];
  373. local C = Inst[3];
  374. local Stk = Stack;
  375.  
  376. if (B > 255) then
  377. B = Const[B - 256];
  378. else
  379. B = Stk[B];
  380. end;
  381.  
  382. if (C > 255) then
  383. C = Const[C - 256];
  384. else
  385. C = Stk[C];
  386. end;
  387.  
  388. Stk[Inst[1]] = B - C;
  389. elseif (Enum == 14) then -- MUL
  390. local B = Inst[2];
  391. local C = Inst[3];
  392. local Stk = Stack;
  393.  
  394. if (B > 255) then
  395. B = Const[B - 256];
  396. else
  397. B = Stk[B];
  398. end;
  399.  
  400. if (C > 255) then
  401. C = Const[C - 256];
  402. else
  403. C = Stk[C];
  404. end;
  405.  
  406. Stk[Inst[1]] = B * C;
  407. elseif (Enum == 15) then -- DIV
  408. local B = Inst[2];
  409. local C = Inst[3];
  410. local Stk = Stack;
  411.  
  412. if (B > 255) then
  413. B = Const[B - 256];
  414. else
  415. B = Stk[B];
  416. end;
  417.  
  418. if (C > 255) then
  419. C = Const[C - 256];
  420. else
  421. C = Stk[C];
  422. end;
  423.  
  424. Stk[Inst[1]] = B / C;
  425. elseif (Enum == 16) then -- MOD
  426. local B = Inst[2];
  427. local C = Inst[3];
  428. local Stk = Stack;
  429.  
  430. if (B > 255) then
  431. B = Const[B - 256];
  432. else
  433. B = Stk[B];
  434. end;
  435.  
  436. if (C > 255) then
  437. C = Const[C - 256];
  438. else
  439. C = Stk[C];
  440. end;
  441.  
  442. Stk[Inst[1]] = B % C;
  443. elseif (Enum == 17) then -- POW
  444. local B = Inst[2];
  445. local C = Inst[3];
  446. local Stk = Stack;
  447.  
  448. if (B > 255) then
  449. B = Const[B - 256];
  450. else
  451. B = Stk[B];
  452. end;
  453.  
  454. if (C > 255) then
  455. C = Const[C - 256];
  456. else
  457. C = Stk[C];
  458. end;
  459.  
  460. Stk[Inst[1]] = B ^ C;
  461. elseif (Enum == 18) then -- UNM
  462. Stack[Inst[1]] = -Stack[Inst[2]];
  463. elseif (Enum == 19) then -- NOT
  464. Stack[Inst[1]] = (not Stack[Inst[2]]);
  465. elseif (Enum == 20) then -- LEN
  466. Stack[Inst[1]] = #Stack[Inst[2]];
  467. elseif (Enum == 21) then -- CONCAT
  468. local Stk = Stack;
  469. local B = Inst[2];
  470. local K = Stk[B];
  471.  
  472. for Idx = B + 1, Inst[3] do
  473. K = K .. Stk[Idx];
  474. end;
  475.  
  476. Stack[Inst[1]] = K;
  477. elseif (Enum == 22) then -- JMP
  478. InstrPoint = InstrPoint + Inst[2];
  479. elseif (Enum == 23) then -- EQ
  480. local A = Inst[1] ~= 0;
  481. local B = Inst[2];
  482. local C = Inst[3];
  483. local Stk = Stack;
  484.  
  485. if (B > 255) then
  486. B = Const[B - 256];
  487. else
  488. B = Stk[B];
  489. end;
  490.  
  491. if (C > 255) then
  492. C = Const[C - 256];
  493. else
  494. C = Stk[C];
  495. end;
  496.  
  497. if (B == C) ~= A then
  498. InstrPoint = InstrPoint + 1;
  499. end;
  500. elseif (Enum == 24) then -- LT
  501. local A = Inst[1] ~= 0;
  502. local B = Inst[2];
  503. local C = Inst[3];
  504. local Stk = Stack;
  505.  
  506. if (B > 255) then
  507. B = Const[B - 256];
  508. else
  509. B = Stk[B];
  510. end;
  511.  
  512. if (C > 255) then
  513. C = Const[C - 256];
  514. else
  515. C = Stk[C];
  516. end;
  517.  
  518. if (B < C) ~= A then
  519. InstrPoint = InstrPoint + 1;
  520. end;
  521. elseif (Enum == 25) then -- LE
  522. local A = Inst[1] ~= 0;
  523. local B = Inst[2];
  524. local C = Inst[3];
  525. local Stk = Stack;
  526.  
  527. if (B > 255) then
  528. B = Const[B - 256];
  529. else
  530. B = Stk[B];
  531. end;
  532.  
  533. if (C > 255) then
  534. C = Const[C - 256];
  535. else
  536. C = Stk[C];
  537. end;
  538.  
  539. if (B <= C) ~= A then
  540. InstrPoint = InstrPoint + 1;
  541. end;
  542. elseif (Enum == 26) then -- TEST
  543. if (not not Stack[Inst[1]]) == (Inst[3] == 0) then
  544. InstrPoint = InstrPoint + 1;
  545. end;
  546. elseif (Enum == 27) then -- TESTSET
  547. local B = Stack[Inst[2]];
  548.  
  549. if (not not B) == (Inst[3] == 0) then
  550. InstrPoint = InstrPoint + 1;
  551. else
  552. Stack[Inst[1]] = B;
  553. end;
  554. elseif (Enum == 28) then -- CALL
  555. local A = Inst[1];
  556. local B = Inst[2];
  557. local C = Inst[3];
  558. local Stk = Stack;
  559. local Args, Results;
  560. local Limit, Loop;
  561.  
  562. Args = {};
  563.  
  564. if (B ~= 1) then
  565. if (B ~= 0) then
  566. Limit = A + B - 1;
  567. else
  568. Limit = Top;
  569. end;
  570.  
  571. Loop = 0;
  572.  
  573. for Idx = A + 1, Limit do
  574. Loop = Loop + 1;
  575.  
  576. Args[Loop] = Stk[Idx];
  577. end;
  578.  
  579. Limit, Results = _Returns(Stk[A](unpack(Args, 1, Limit - A)));
  580. else
  581. Limit, Results = _Returns(Stk[A]());
  582. end;
  583.  
  584. Top = A - 1;
  585.  
  586. if (C ~= 1) then
  587. if (C ~= 0) then
  588. Limit = A + C - 2;
  589. else
  590. Limit = Limit + A - 1;
  591. end;
  592.  
  593. Loop = 0;
  594.  
  595. for Idx = A, Limit do
  596. Loop = Loop + 1;
  597.  
  598. Stk[Idx] = Results[Loop];
  599. end;
  600. end;
  601. elseif (Enum == 29) then -- TAILCALL
  602. local A = Inst[1];
  603. local B = Inst[2];
  604. local Stk = Stack;
  605. local Args, Results;
  606. local Limit, Loop;
  607. local Rets = 0;
  608.  
  609. Args = {};
  610.  
  611. if (B ~= 1) then
  612. if (B ~= 0) then
  613. Limit = A + B - 1;
  614. else
  615. Limit = Top;
  616. end
  617.  
  618. Loop = 0;
  619.  
  620. for Idx = A + 1, Limit do
  621. Loop = Loop + 1;
  622.  
  623. Args[#Args + 1] = Stk[Idx];
  624. end
  625.  
  626. Results = {Stk[A](unpack(Args, 1, Limit - A))};
  627. else
  628. Results = {Stk[A]()};
  629. end;
  630.  
  631. for Index in next, Results do -- get return count
  632. if (Index > Rets) then
  633. Rets = Index;
  634. end;
  635. end;
  636.  
  637. return Results, Rets;
  638. elseif (Enum == 30) then -- RETURN
  639. local A = Inst[1];
  640. local B = Inst[2];
  641. local Stk = Stack;
  642. local Loop, Output;
  643. local Limit;
  644.  
  645. if (B == 1) then
  646. return;
  647. elseif (B == 0) then
  648. Limit = Top;
  649. else
  650. Limit = A + B - 2;
  651. end;
  652.  
  653. Output = {};
  654. Loop = 0;
  655.  
  656. for Idx = A, Limit do
  657. Loop = Loop + 1;
  658.  
  659. Output[Loop] = Stk[Idx];
  660. end;
  661.  
  662. return Output, Loop;
  663. elseif (Enum == 31) then -- FORLOOP
  664. local A = Inst[1];
  665. local Stk = Stack;
  666.  
  667. local Step = Stk[A + 2];
  668. local Index = Stk[A] + Step;
  669.  
  670. Stk[A] = Index;
  671.  
  672. if (Step > 0) then
  673. if Index <= Stk[A + 1] then
  674. InstrPoint = InstrPoint + Inst[2];
  675.  
  676. Stk[A + 3] = Index;
  677. end;
  678. else
  679. if Index >= Stk[A + 1] then
  680. InstrPoint = InstrPoint + Inst[2];
  681.  
  682. Stk[A + 3] = Index;
  683. end
  684. end
  685. elseif (Enum == 32) then -- FORPREP
  686. local A = Inst[1];
  687. local Stk = Stack;
  688.  
  689. -- As per mirroring the real vm
  690. Stk[A] = assert(tonumber(Stk[A]), '`for` initial value must be a number');
  691. Stk[A + 1] = assert(tonumber(Stk[A + 1]), '`for` limit must be a number');
  692. Stk[A + 2] = assert(tonumber(Stk[A + 2]), '`for` step must be a number');
  693.  
  694. Stk[A] = Stk[A] - Stk[A + 2];
  695.  
  696. InstrPoint = InstrPoint + Inst[2];
  697. elseif (Enum == 33) then -- TFORLOOP
  698. local A = Inst[1];
  699. local C = Inst[3];
  700. local Stk = Stack;
  701.  
  702. local Offset = A + 2;
  703. local Result = {Stk[A](Stk[A + 1], Stk[A + 2])};
  704.  
  705. for Idx = 1, C do
  706. Stack[Offset + Idx] = Result[Idx];
  707. end;
  708.  
  709. if (Stk[A + 3] ~= nil) then
  710. Stk[A + 2] = Stk[A + 3];
  711. else
  712. InstrPoint = InstrPoint + 1;
  713. end;
  714. elseif (Enum == 34) then -- SETLIST
  715. local A = Inst[1];
  716. local B = Inst[2];
  717. local C = Inst[3];
  718. local Stk = Stack;
  719.  
  720. if (C == 0) then
  721. InstrPoint = InstrPoint + 1;
  722. C = Instr[InstrPoint]; -- This implementation was ambiguous! Will eventually re-test.
  723. end;
  724.  
  725. local Offset = (C - 1) * 50;
  726. local T = Stk[A]; -- Assuming T is the newly created table.
  727.  
  728. if (B == 0) then
  729. B = Top;
  730. end;
  731.  
  732. for Idx = 1, B do
  733. T[Offset + Idx] = Stk[A + Idx];
  734. end;
  735. elseif (Enum == 35) then -- CLOSE
  736. local A = Inst[1];
  737. local Cls = {}; -- Slight doubts on any issues this may cause
  738.  
  739. for Idx = 1, #Lupvals do
  740. local List = Lupvals[Idx];
  741.  
  742. for Idz = 0, #List do
  743. local Upv = List[Idz];
  744. local Stk = Upv[1];
  745. local Pos = Upv[2];
  746.  
  747. if (Stk == Stack) and (Pos >= A) then
  748. Cls[Pos] = Stk[Pos];
  749. Upv[1] = Cls; -- @memcorrupt credit me for the spoonfeed
  750. end;
  751. end;
  752. end;
  753. elseif (Enum == 36) then -- CLOSURE
  754. local NewProto = Proto[Inst[2]];
  755. local Stk = Stack;
  756.  
  757. local Indexes;
  758. local NewUvals;
  759.  
  760. if (NewProto.Upvals ~= 0) then
  761. Indexes = {};
  762. NewUvals = setmetatable({}, {
  763. __index = function(_, Key)
  764. local Val = Indexes[Key];
  765.  
  766. return Val[1][Val[2]];
  767. end,
  768. __newindex = function(_, Key, Value)
  769. local Val = Indexes[Key];
  770.  
  771. Val[1][Val[2]] = Value;
  772. end;
  773. }
  774. );
  775.  
  776. for Idx = 1, NewProto.Upvals do
  777. local Mvm = Instr[InstrPoint];
  778.  
  779. if (Mvm.Enum == 0) then -- MOVE
  780. Indexes[Idx - 1] = {Stk, Mvm[2]};
  781. elseif (Mvm.Enum == 4) then -- GETUPVAL
  782. Indexes[Idx - 1] = {Upvalues, Mvm[2]};
  783. end;
  784.  
  785. InstrPoint = InstrPoint + 1;
  786. end;
  787.  
  788. Lupvals[#Lupvals + 1] = Indexes;
  789. end;
  790.  
  791. Stk[Inst[1]] = Wrap(NewProto, Env, NewUvals);
  792. elseif (Enum == 37) then -- VARARG
  793. local A = Inst[1];
  794. local B = Inst[2];
  795. local Stk, Vars = Stack, Vararg;
  796.  
  797. Top = A - 1;
  798.  
  799. for Idx = A, A + (B > 0 and B - 1 or Varargsz) do
  800. Stk[Idx] = Vars[Idx - A];
  801. end;
  802. end;
  803. end;
  804. end;
  805.  
  806. local Args = {...};
  807.  
  808. for Idx = 0, Varargsz do
  809. if (Idx >= Chunk.Args) then
  810. Vararg[Idx - Chunk.Args] = Args[Idx + 1];
  811. else
  812. Stack[Idx] = Args[Idx + 1];
  813. end;
  814. end;
  815.  
  816. local A, B, C = pcall(Loop); -- Pcalling to allow yielding
  817.  
  818. if A then -- We're always expecting this to come out true (because errorless code)
  819. if B and (C > 0) then -- So I flipped the conditions.
  820. return unpack(B, 1, C);
  821. end;
  822.  
  823. return;
  824. else
  825. OnError(B, InstrPoint - 1); -- Didn't get time to test the `-1` honestly, but I assume it works properly
  826. end;
  827. end;
  828. end;
  829.  
  830. return function(BCode, Env) -- lua_function LoadBytecode (string BCode, table Env)
  831. local Buffer = GetMeaning(BCode);
  832.  
  833. return Wrap(Buffer, Env or getfenv(0)), Buffer;
  834. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement