Advertisement
nicopow

Untitled

Jun 27th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.18 KB | None | 0 0
  1. local data = {'\27','\76','\117','\97','\81','\0','\1','\4','\4','\4','\8','\0','\29','\0','\0','\0','\61','\87','\111','\114',
  2. '\107','\115','\112','\97','\99','\101','\46','\83','\111','\117','\114','\99','\101','\32','\69','\110','\99','\114','\121',
  3. '\112','\116','\105','\111','\110','\0','\1','\0','\0','\0','\46','\0','\0','\0','\0','\0','\0','\9','\41','\0','\0','\0',
  4. '\10','\0','\0','\1','\65','\0','\0','\0','\129','\64','\0','\0','\34','\64','\0','\1','\67','\0','\128','\0','\133','\128',
  5. '\0','\0','\139','\192','\64','\1','\1','\1','\1','\0','\156','\128','\128','\1','\134','\64','\65','\1','\198','\128','\65',
  6. '\1','\5','\193','\1','\0','\100','\1','\0','\0','\28','\193','\0','\1','\26','\1','\0','\0','\22','\64','\1','\128','\133',
  7. '\129','\0','\0','\139','\193','\64','\3','\1','\2','\2','\0','\156','\129','\128','\1','\64','\0','\0','\3','\22','\192',
  8. '\0','\128','\133','\129','\0','\0','\134','\1','\65','\3','\134','\65','\65','\3','\70','\64','\66','\3','\134','\129','\66',
  9. '\1','\134','\129','\1','\0','\154','\1','\0','\0','\22','\192','\0','\128','\133','\193','\2','\0','\193','\1','\3','\0',
  10. '\156','\65','\0','\1','\22','\128','\0','\128','\133','\193','\2','\0','\193','\65','\3','\0','\156','\65','\0','\1','\133',
  11. '\193','\2','\0','\192','\1','\128','\0','\156','\65','\0','\1','\30','\0','\128','\0','\14','\0','\0','\0','\4','\8','\0',
  12. '\0','\0','\105','\105','\65','\115','\115','\108','\121','\0','\4','\17','\0','\0','\0','\80','\108','\101','\97','\115',
  13. '\101','\68','\111','\110','\116','\80','\111','\107','\101','\77','\101','\0','\4','\5','\0','\0','\0','\103','\97','\109',
  14. '\101','\0','\4','\11','\0','\0','\0','\71','\101','\116','\83','\101','\114','\118','\105','\99','\101','\0','\4','\8','\0',
  15. '\0','\0','\80','\108','\97','\121','\101','\114','\115','\0','\4','\12','\0','\0','\0','\76','\111','\99','\97','\108','\80',
  16. '\108','\97','\121','\101','\114','\0','\4','\7','\0','\0','\0','\85','\115','\101','\114','\73','\100','\0','\4','\6','\0',
  17. '\0','\0','\112','\99','\97','\108','\108','\0','\4','\8','\0','\0','\0','\67','\111','\114','\101','\71','\117','\105','\0',
  18. '\4','\10','\0','\0','\0','\80','\108','\97','\121','\101','\114','\71','\117','\105','\0','\4','\5','\0','\0','\0','\78',
  19. '\97','\109','\101','\0','\4','\6','\0','\0','\0','\112','\114','\105','\110','\116','\0','\4','\12','\0','\0','\0','\87',
  20. '\104','\105','\116','\101','\108','\105','\115','\116','\101','\100','\0','\4','\35','\0','\0','\0','\84','\104','\105',
  21. '\115','\32','\115','\116','\117','\112','\100','\105','\100','\32','\111','\98','\115','\99','\117','\97','\116','\105',
  22. '\111','\110','\32','\115','\117','\99','\107','\115','\32','\100','\105','\99','\107','\0','\1','\0','\0','\0','\0','\0',
  23. '\0','\0','\23','\0','\0','\0','\26','\0','\0','\0','\0','\0','\0','\4','\11','\0','\0','\0','\5','\0','\0','\0','\6','\64',
  24. '\64','\0','\65','\128','\0','\0','\28','\128','\0','\1','\69','\192','\0','\0','\75','\0','\193','\0','\193','\64','\1',
  25. '\0','\92','\64','\128','\1','\75','\128','\65','\0','\92','\64','\0','\1','\30','\0','\128','\0','\7','\0','\0','\0','\4',
  26. '\9','\0','\0','\0','\73','\110','\115','\116','\97','\110','\99','\101','\0','\4','\4','\0','\0','\0','\110','\101','\119',
  27. '\0','\4','\6','\0','\0','\0','\70','\114','\97','\109','\101','\0','\4','\5','\0','\0','\0','\103','\97','\109','\101','\0',
  28. '\4','\11','\0','\0','\0','\71','\101','\116','\83','\101','\114','\118','\105','\99','\101','\0','\4','\8','\0','\0','\0',
  29. '\67','\111','\114','\101','\71','\117','\105','\0','\4','\8','\0','\0','\0','\68','\101','\115','\116','\114','\111','\121',
  30. '\0','\0','\0','\0','\0','\11','\0','\0','\0','\24','\0','\0','\0','\24','\0','\0','\0','\24','\0','\0','\0','\24','\0','\0',
  31. '\0','\24','\0','\0','\0','\24','\0','\0','\0','\24','\0','\0','\0','\24','\0','\0','\0','\25','\0','\0','\0','\25','\0',
  32. '\0','\0','\26','\0','\0','\0','\1','\0','\0','\0','\2','\0','\0','\0','\102','\0','\8','\0','\0','\0','\10','\0','\0','\0',
  33. '\0','\0','\0','\0','\41','\0','\0','\0','\13','\0','\0','\0','\14','\0','\0','\0','\16','\0','\0','\0','\16','\0','\0','\0',
  34. '\18','\0','\0','\0','\20','\0','\0','\0','\20','\0','\0','\0','\20','\0','\0','\0','\20','\0','\0','\0','\20','\0','\0',
  35. '\0','\21','\0','\0','\0','\23','\0','\0','\0','\26','\0','\0','\0','\23','\0','\0','\0','\27','\0','\0','\0','\27','\0',
  36. '\0','\0','\28','\0','\0','\0','\28','\0','\0','\0','\28','\0','\0','\0','\28','\0','\0','\0','\28','\0','\0','\0','\28',
  37. '\0','\0','\0','\30','\0','\0','\0','\30','\0','\0','\0','\30','\0','\0','\0','\30','\0','\0','\0','\33','\0','\0','\0','\33',
  38. '\0','\0','\0','\33','\0','\0','\0','\33','\0','\0','\0','\34','\0','\0','\0','\34','\0','\0','\0','\34','\0','\0','\0','\34',
  39. '\0','\0','\0','\36','\0','\0','\0','\36','\0','\0','\0','\36','\0','\0','\0','\38','\0','\0','\0','\38','\0','\0','\0','\38',
  40. '\0','\0','\0','\46','\0','\0','\0','\6','\0','\0','\0','\12','\0','\0','\0','\87','\104','\105','\116','\101','\108','\105',
  41. '\115','\116','\101','\100','\0','\4','\0','\0','\0','\40','\0','\0','\0','\5','\0','\0','\0','\84','\121','\112','\101',
  42. '\0','\5','\0','\0','\0','\40','\0','\0','\0','\4','\0','\0','\0','\80','\108','\114','\0','\10','\0','\0','\0','\40','\0',
  43. '\0','\0','\10','\0','\0','\0','\80','\108','\114','\85','\115','\101','\114','\73','\100','\0','\11','\0','\0','\0','\40',
  44. '\0','\0','\0','\2','\0','\0','\0','\97','\0','\14','\0','\0','\0','\40','\0','\0','\0','\2','\0','\0','\0','\115','\0','\14',
  45. '\0','\0','\0','\40','\0','\0','\0','\0','\0','\0','\0'}
  46.  
  47. local lua_opcode_types = {
  48. "ABC", "ABx", "ABC", "ABC",
  49. "ABC", "ABx", "ABC", "ABx",
  50. "ABC", "ABC", "ABC", "ABC",
  51. "ABC", "ABC", "ABC", "ABC",
  52. "ABC", "ABC", "ABC", "ABC",
  53. "ABC", "ABC", "AsBx", "ABC",
  54. "ABC", "ABC", "ABC", "ABC",
  55. "ABC", "ABC", "ABC", "AsBx",
  56. "AsBx", "ABC", "ABC", "ABC",
  57. "ABx", "ABC",
  58. }
  59. local lua_opcode_names = {
  60. "MOVE", "LOADK", "LOADBOOL", "LOADNIL",
  61. "GETUPVAL", "GETGLOBAL", "GETTABLE", "SETGLOBAL",
  62. "SETUPVAL", "SETTABLE", "NEWTABLE", "SELF",
  63. "ADD", "SUB", "MUL", "DIV",
  64. "MOD", "POW", "UNM", "NOT",
  65. "LEN", "CONCAT", "JMP", "EQ",
  66. "LT", "LE", "TEST", "TESTSET",
  67. "CALL", "TAILCALL", "RETURN", "FORLOOP",
  68. "FORPREP", "TFORLOOP", "SETLIST", "CLOSE",
  69. "CLOSURE", "VARARG"
  70. };
  71. local function get_bits(input, n, n2)
  72. if n2 then
  73. local total = 0
  74. local digitn = 0
  75. for i = n, n2 do
  76. total = total + 2^digitn*get_bits(input, i)
  77. digitn = digitn + 1
  78. end
  79. return total
  80. else
  81. local pn = 2^(n-1)
  82. return (input % (pn + pn) >= pn) and 1 or 0
  83. end
  84. end
  85. local function decode_bytecode(bytecode)
  86. local index = 1
  87. local big_endian = false
  88. local int_size;
  89. local size_t;
  90. local get_int, get_size_t;
  91. local get_int8, get_int32, get_int64, get_float64, get_string;
  92. do
  93. function get_int8()
  94. local a = bytecode:byte(index, index);
  95. index = index + 1
  96. return a
  97. end
  98. function get_int32()
  99. local a, b, c, d = bytecode:byte(index, index + 3);
  100. index = index + 4;
  101. return d*16777216 + c*65536 + b*256 + a
  102. end
  103. function get_int64()
  104. local a = get_int32();
  105. local b = get_int32();
  106. return b*4294967296 + a;
  107. end
  108. function get_float64()
  109. local a = get_int32()
  110. local b = get_int32()
  111. return (-2*get_bits(b, 32)+1)*(2^(get_bits(b, 21, 31)-1023))*
  112. ((get_bits(b, 1, 20)*(2^32) + a)/(2^52)+1)
  113. end
  114. function get_string(len)
  115. local str;
  116. if len then
  117. str = bytecode:sub(index, index + len - 1);
  118. index = index + len;
  119. else
  120. len = get_size_t();
  121. if len == 0 then return; end
  122. str = bytecode:sub(index, index + len - 1);
  123. index = index + len;
  124. end
  125. return str;
  126. end
  127. end
  128. local function decode_chunk()
  129. local chunk;
  130. local instructions = {};
  131. local constants = {};
  132. local prototypes = {};
  133. local debug = {
  134. lines = {};
  135. };
  136. chunk = {
  137. instructions = instructions;
  138. constants = constants;
  139. prototypes = prototypes;
  140. debug = debug;
  141. };
  142. local num;
  143. chunk.name = get_string();
  144. chunk.first_line = get_int();
  145. chunk.last_line = get_int();
  146. if chunk.name then chunk.name = chunk.name:sub(1, -2); end
  147. chunk.upvalues = get_int8();
  148. chunk.arguments = get_int8();
  149. chunk.varg = get_int8();
  150. chunk.stack = get_int8();
  151. do
  152. num = get_int();
  153. for i = 1, num do
  154. local instruction = {
  155. };
  156. local data = get_int32();
  157. local opcode = get_bits(data, 1, 6);
  158. local type = lua_opcode_types[opcode + 1];
  159. instruction.opcode = opcode;
  160. instruction.type = type;
  161. instruction.A = get_bits(data, 7, 14);
  162. if type == "ABC" then
  163. instruction.B = get_bits(data, 24, 32);
  164. instruction.C = get_bits(data, 15, 23);
  165. elseif type == "ABx" then
  166. instruction.Bx = get_bits(data, 15, 32);
  167. elseif type == "AsBx" then
  168. instruction.sBx = get_bits(data, 15, 32) - 131071;
  169. end
  170. instructions[i] = instruction;
  171. end
  172. end
  173. do
  174. num = get_int();
  175. for i = 1, num do
  176. local constant = {
  177. };
  178. local type = get_int8();
  179. constant.type = type;
  180. if type == 1 then
  181. constant.data = (get_int8() ~= 0);
  182. elseif type == 3 then
  183. constant.data = get_float64();
  184. elseif type == 4 then
  185. constant.data = get_string():sub(1, -2);
  186. end
  187. constants[i-1] = constant;
  188. end
  189. end
  190. do
  191. num = get_int();
  192. for i = 1, num do
  193. prototypes[i-1] = decode_chunk();
  194. end
  195. end
  196. do
  197. local data = debug.lines
  198. num = get_int();
  199. for i = 1, num do
  200. data[i] = get_int32();
  201. end
  202. num = get_int();
  203. for i = 1, num do
  204. get_string():sub(1, -2);
  205. get_int32();
  206. get_int32();
  207. end
  208. num = get_int();
  209. for i = 1, num do
  210. get_string();
  211. end
  212. end
  213. return chunk;
  214. end
  215. do
  216. assert(get_string(4) == "\27Lua", "Lua bytecode expected.");
  217. assert(get_int8() == 0x51, "Only Lua 5.1 is supported.");
  218. get_int8();
  219. big_endian = (get_int8() == 0);
  220. int_size = get_int8();
  221. size_t = get_int8();
  222. if int_size == 4 then
  223. get_int = get_int32;
  224. elseif int_size == 8 then
  225. get_int = get_int64;
  226. else
  227. error("Unsupported bytecode target platform");
  228. end
  229. if size_t == 4 then
  230. get_size_t = get_int32;
  231. elseif size_t == 8 then
  232. get_size_t = get_int64;
  233. else
  234. error("Unsupported bytecode target platform");
  235. end
  236. assert(get_string(3) == "\4\8\0",
  237. "Unsupported bytecode target platform");
  238. end
  239. return decode_chunk();
  240. end
  241. local function handle_return(...)
  242. local c = select("#", ...)
  243. local t = {...}
  244. return c, t
  245. end
  246. local function create_wrapper(cache, upvalues)
  247. local instructions = cache.instructions;
  248. local constants = cache.constants;
  249. local prototypes = cache.prototypes;
  250.  
  251. local stack, top
  252. local environment
  253. local IP = 1;
  254. local vararg, vararg_size
  255.  
  256. local opcode_funcs = {
  257. [0] = function(instruction)
  258. stack[instruction.A] = stack[instruction.B];
  259. end,
  260. [1] = function(instruction)
  261. stack[instruction.A] = constants[instruction.Bx].data;
  262. end,
  263. [2] = function(instruction)
  264. stack[instruction.A] = instruction.B ~= 0
  265. if instruction.C ~= 0 then
  266. IP = IP + 1
  267. end
  268. end,
  269. [3] = function(instruction)
  270. local stack = stack
  271. for i = instruction.A, instruction.B do
  272. stack[i] = nil
  273. end
  274. end,
  275. [4] = function(instruction)
  276. stack[instruction.A] = upvalues[instruction.B]
  277. end,
  278. [5] = function(instruction)
  279. local key = constants[instruction.Bx].data;
  280. stack[instruction.A] = environment[key];
  281. end,
  282. [6] = function(instruction)
  283. local C = instruction.C
  284. local stack = stack
  285. C = C > 255 and constants[C-256].data or stack[C]
  286. stack[instruction.A] = stack[instruction.B][C];
  287. end,
  288. [7] = function(instruction)
  289. local key = constants[instruction.Bx].data;
  290. environment[key] = stack[instruction.A];
  291. end,
  292. [8] = function (instruction)
  293. upvalues[instruction.B] = stack[instruction.A]
  294. end,
  295. [9] = function (instruction)
  296. local B = instruction.B;
  297. local C = instruction.C;
  298. local stack, constants = stack, constants;
  299. B = B > 255 and constants[B-256].data or stack[B];
  300. C = C > 255 and constants[C-256].data or stack[C];
  301. stack[instruction.A][B] = C
  302. end,
  303. [10] = function (instruction)
  304. stack[instruction.A] = {}
  305. end,
  306. [11] = function (instruction)
  307. local A = instruction.A
  308. local B = instruction.B
  309. local C = instruction.C
  310. local stack = stack
  311. B = stack[B]
  312. C = C > 255 and constants[C-256].data or stack[C]
  313. stack[A+1] = B
  314. stack[A] = B[C]
  315. end,
  316. [12] = function(instruction)
  317. local B = instruction.B;
  318. local C = instruction.C;
  319. local stack, constants = stack, constants;
  320. B = B > 255 and constants[B-256].data or stack[B];
  321. C = C > 255 and constants[C-256].data or stack[C];
  322. stack[instruction.A] = B+C;
  323. end,
  324. [13] = function(instruction)
  325. local B = instruction.B;
  326. local C = instruction.C;
  327. local stack, constants = stack, constants;
  328. B = B > 255 and constants[B-256].data or stack[B];
  329. C = C > 255 and constants[C-256].data or stack[C];
  330. stack[instruction.A] = B - C;
  331. end,
  332. [14] = function(instruction)
  333. local B = instruction.B;
  334. local C = instruction.C;
  335. local stack, constants = stack, constants;
  336. B = B > 255 and constants[B-256].data or stack[B];
  337. C = C > 255 and constants[C-256].data or stack[C];
  338. stack[instruction.A] = B * C;
  339. end,
  340. [15] = function(instruction)
  341. local B = instruction.B;
  342. local C = instruction.C;
  343. local stack, constants = stack, constants;
  344. B = B > 255 and constants[B-256].data or stack[B];
  345. C = C > 255 and constants[C-256].data or stack[C];
  346. stack[instruction.A] = B / C;
  347. end,
  348. [16] = function(instruction)
  349. local B = instruction.B;
  350. local C = instruction.C;
  351. local stack, constants = stack, constants;
  352. B = B > 255 and constants[B-256].data or stack[B];
  353. C = C > 255 and constants[C-256].data or stack[C];
  354. stack[instruction.A] = B % C;
  355. end,
  356. [17] = function(instruction)
  357. local B = instruction.B;
  358. local C = instruction.C;
  359. local stack, constants = stack, constants;
  360. B = B > 255 and constants[B-256].data or stack[B];
  361. C = C > 255 and constants[C-256].data or stack[C];
  362. stack[instruction.A] = B ^ C;
  363. end,
  364. [18] = function(instruction)
  365. stack[instruction.A] = -stack[instruction.B]
  366. end,
  367. [19] = function(instruction)
  368. stack[instruction.A] = not stack[instruction.B]
  369. end,
  370. [20] = function(instruction)
  371. stack[instruction.A] = #stack[instruction.B]
  372. end,
  373. [21] = function(instruction)
  374. local B = instruction.B
  375. local result = stack[B]
  376. for i = B+1, instruction.C do
  377. result = result .. stack[i]
  378. end
  379. stack[instruction.A] = result
  380. end,
  381. [22] = function(instruction)
  382. IP = IP + instruction.sBx
  383. end,
  384. [23] = function(instruction)
  385. local A = instruction.A
  386. local B = instruction.B
  387. local C = instruction.C
  388. local stack, constants = stack, constants
  389. A = A ~= 0
  390. B = B > 255 and constants[B-256].data or stack[B]
  391. C = C > 255 and constants[C-256].data or stack[C]
  392. if (B == C) ~= A then
  393. IP = IP + 1
  394. end
  395. end,
  396. [24] = function(instruction)
  397. local A = instruction.A
  398. local B = instruction.B
  399. local C = instruction.C
  400. local stack, constants = stack, constants
  401. A = A ~= 0
  402. B = B > 255 and constants[B-256].data or stack[B]
  403. C = C > 255 and constants[C-256].data or stack[C]
  404. if (B < C) ~= A then
  405. IP = IP + 1
  406. end
  407. end,
  408. [25] = function(instruction)
  409. local A = instruction.A
  410. local B = instruction.B
  411. local C = instruction.C
  412. local stack, constants = stack, constants
  413. A = A ~= 0
  414. B = B > 255 and constants[B-256].data or stack[B]
  415. C = C > 255 and constants[C-256].data or stack[C]
  416. if (B <= C) ~= A then
  417. IP = IP + 1
  418. end
  419. end,
  420. [26] = function(instruction)
  421. if stack[instruction.A] == (instruction.C ~= 0) then
  422. IP = IP + 1
  423. end
  424. end,
  425. [27] = function(instruction)
  426. local stack = stack
  427. local B = stack[instruction.B]
  428. if B == (instruction.C ~= 0) then
  429. IP = IP + 1
  430. else
  431. stack[instruction.A] = B
  432. end
  433. end,
  434. [28] = function(instruction)
  435. local A = instruction.A;
  436. local B = instruction.B;
  437. local C = instruction.C;
  438. local stack = stack;
  439. local args, results;
  440. local limit, loop
  441. args = {};
  442. if B ~= 1 then
  443. if B ~= 0 then
  444. limit = A+B-1;
  445. else
  446. limit = top
  447. end
  448. loop = 0
  449. for i = A+1, limit do
  450. loop = loop + 1
  451. args[loop] = stack[i];
  452. end
  453. limit, results = handle_return(stack[A](unpack(args, 1, limit-A)))
  454. else
  455. limit, results = handle_return(stack[A]())
  456. end
  457. top = A - 1
  458. if C ~= 1 then
  459. if C ~= 0 then
  460. limit = A+C-2;
  461. else
  462. limit = limit+A
  463. end
  464. loop = 0;
  465. for i = A, limit do
  466. loop = loop + 1;
  467. stack[i] = results[loop];
  468. end
  469. end
  470. end,
  471. [29] = function (instruction)
  472. local A = instruction.A;
  473. local B = instruction.B;
  474. local C = instruction.C;
  475. local stack = stack;
  476. local args, results;
  477. local top, limit, loop = top
  478. args = {};
  479. if B ~= 1 then
  480. if B ~= 0 then
  481. limit = A+B-1;
  482. else
  483. limit = top
  484. end
  485. loop = 0
  486. for i = A+1, limit do
  487. loop = loop + 1
  488. args[#args+1] = stack[i];
  489. end
  490. results = {stack[A](unpack(args, 1, limit-A))};
  491. else
  492. results = {stack[A]()};
  493. end
  494. return true, results
  495. end,
  496. [30] = function(instruction)
  497. local A = instruction.A;
  498. local B = instruction.B;
  499. local stack = stack;
  500. local limit;
  501. local loop, output;
  502. if B == 1 then
  503. return true;
  504. end
  505. if B == 0 then
  506. limit = top
  507. else
  508. limit = A + B - 2;
  509. end
  510. output = {};
  511. local loop = 0
  512. for i = A, limit do
  513. loop = loop + 1
  514. output[loop] = stack[i];
  515. end
  516. return true, output;
  517. end,
  518. [31] = function(instruction)
  519. local A = instruction.A
  520. local stack = stack
  521. local step = stack[A+2]
  522. local index = stack[A] + step
  523. stack[A] = index
  524. if step > 0 then
  525. if index <= stack[A+1] then
  526. IP = IP + instruction.sBx
  527. stack[A+3] = index
  528. end
  529. else
  530. if index >= stack[A+1] then
  531. IP = IP + instruction.sBx
  532. stack[A+3] = index
  533. end
  534. end
  535. end,
  536. [32] = function(instruction)
  537. local A = instruction.A
  538. local stack = stack
  539. stack[A] = stack[A] - stack[A+2]
  540. IP = IP + instruction.sBx
  541. end,
  542. [33] = function(instruction)
  543. local A = instruction.A
  544. local B = instruction.B
  545. local C = instruction.C
  546. local stack = stack
  547. local offset = A+2
  548. local result = {stack[A](stack[A+1], stack[A+2])}
  549. for i = 1, C do
  550. stack[offset+i] = result[i]
  551. end
  552. if stack[A+3] ~= nil then
  553. stack[A+2] = stack[A+3]
  554. else
  555. IP = IP + 1
  556. end
  557. end,
  558. [34] = function(instruction)
  559. local A = instruction.A
  560. local B = instruction.B
  561. local C = instruction.C
  562. local stack = stack
  563. if C == 0 then
  564. error("NYI: extended SETLIST")
  565. else
  566. local offset = (C - 1) * 50
  567. local t = stack[A]
  568. if B == 0 then
  569. B = top
  570. end
  571. for i = 1, B do
  572. t[offset+i] = stack[A+i]
  573. end
  574. end
  575. end,
  576. [35] = function(instruction)
  577. io.stderr:write("NYI: CLOSE")
  578. io.stderr:flush()
  579. end,
  580. [36] = function(instruction)
  581. local proto = prototypes[instruction.Bx]
  582. local instructions = instructions
  583. local stack = stack
  584. local indices = {}
  585. local new_upvals = setmetatable({},
  586. {
  587. __index = function(t, k)
  588. local upval = indices[k]
  589. return upval.segment[upval.offset]
  590. end,
  591. __newindex = function(t, k, v)
  592. local upval = indices[k]
  593. upval.segment[upval.offset] = v
  594. end
  595. }
  596. )
  597. for i = 1, proto.upvalues do
  598. local movement = instructions[IP]
  599. if movement.opcode == 0 then
  600. indices[i-1] = {segment = stack, offset = movement.B}
  601. elseif instructions[IP].opcode == 4 then
  602. indices[i-1] = {segment = upvalues, offset = movement.B}
  603. end
  604. IP = IP + 1
  605. end
  606. local _, func = create_wrapper(proto, new_upvals)
  607. stack[instruction.A] = func
  608. end,
  609. [37] = function(instruction)
  610. local A = instruction.A
  611. local B = instruction.B
  612. local stack, vararg = stack, vararg
  613.  
  614. for i = A, A + (B > 0 and B - 1 or vararg_size) do
  615. stack[i] = vararg[i - A]
  616. end
  617. end,
  618. }
  619. local function loop()
  620. local instructions = instructions
  621. local instruction, a, b
  622. while true do
  623. instruction = instructions[IP];
  624. IP = IP + 1
  625. a, b = opcode_funcs[instruction.opcode](instruction);
  626. if a then
  627. return b;
  628. end
  629. end
  630. end
  631. local debugging = {
  632. get_stack = function()
  633. return stack;
  634. end;
  635. get_IP = function()
  636. return IP;
  637. end
  638. };
  639. local function func(...)
  640. local local_stack = {};
  641. local ghost_stack = {};
  642. top = -1
  643. stack = setmetatable(local_stack, {
  644. __index = ghost_stack;
  645. __newindex = function(t, k, v)
  646. if k > top and v then
  647. top = k
  648. end
  649. ghost_stack[k] = v
  650. end;
  651. })
  652. local args = {...};
  653. vararg = {}
  654. vararg_size = select("#", ...) - 1
  655. for i = 0, vararg_size do
  656. local_stack[i] = args[i+1];
  657. vararg[i] = args[i+1]
  658. end
  659. environment = getfenv();
  660. IP = 1;
  661. local thread = coroutine.create(loop)
  662. local a, b = coroutine.resume(thread)
  663. if a then
  664. if b then
  665. return unpack(b);
  666. end
  667. return;
  668. else
  669. local name = cache.name;
  670. local line = cache.debug.lines[IP];
  671. local err = b:gsub("(.-:)", "");
  672. local output = "";
  673. output = output .. (name and name .. ":" or "");
  674. output = output .. (line and line .. ":" or "");
  675. output = output .. b
  676. error(output, 0);
  677. end
  678. end
  679. return debugging, func;
  680. end
  681. local VM = {
  682. load_bytecode = function(bytecode)
  683. local cache = decode_bytecode(bytecode);
  684. local _, func = create_wrapper(cache);
  685. return func;
  686. end;
  687. utils = {
  688. decode_bytecode = decode_bytecode;
  689. create_wrapper = create_wrapper;
  690. debug_bytecode = function(bytecode)
  691. local cache = decode_bytecode(bytecode)
  692. return create_wrapper(cache);
  693. end;
  694. };
  695. }
  696. VM.load_bytecode(table.concat(data, [[]]))()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement