forked from viegg69/LuaJGG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm.lua
More file actions
398 lines (398 loc) · 10.4 KB
/
vm.lua
File metadata and controls
398 lines (398 loc) · 10.4 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
local opcodes = require("opcodes");
local tostring, unpack = tostring, unpack or table.unpack;
local pack = table.pack or function(...)
return { n = select("#", ...), ... };
end;
local OpCode = opcodes.OpCode;
local vm = {};
local function debug(...)
if vm.debug then
print(...);
end;
end;
local function attemptCall(v)
if vm.typechecking then
local t = type(v);
if not (t == "function" or t == "table" and (getmetatable(v) and type((getmetatable(v)).__call) == "function")) then
error("attempt to call a " .. (t .. " value"));
end;
end;
return v;
end;
local function attemptMetatable(v, n, typ, meta)
if vm.typechecking then
local t = type(v);
if not (t == typ or t == "table" and (getmetatable(v) and type((getmetatable(v))[meta]) == "function")) then
error("attempt to " .. (n .. (" a " .. (t .. " value"))));
end;
end;
return v;
end;
local function attempt(v, to, ...)
if vm.typechecking then
local t = type(v);
for i = 1, select("#", ...), 1 do
if t == select(i, ...) then
return v;
end;
end;
error("attempt to " .. (to .. (" a " .. (t .. " value"))));
end;
return v;
end;
function vm.run(chunk, args, upvals, globals, hook)
local R = {};
local top = 0;
local pc = 0;
local code = chunk.code;
local constants = chunk.k;
args = args or {};
globals = globals or _G;
upvals = upvals or { [0] = globals };
local openUpvalues = {};
for i = 1, chunk.numparams, 1 do
R[i - 1] = args[i];
top = i - 1;
end;
local function RK(n)
if n >= 256 then
return constants[n - 256].value;
else
return R[n];
end;
end;
local ret = pack(pcall(function()
while true do
local cd = code[pc];
cd.Bx = (cd.Bx and cd.Bx > 65535) and opcodes:GETARG_sBx(cd) or cd.Bx;
local o, a, b, c = cd.OP, cd.A, cd.Bx or cd.B, cd.C;
if vm.debug then
debug(pc, opcodes.opnames[o], a, b, c);
end;
pc = pc + 1;
if hook then
hook(o, a, b, c, pc - 1, opcodes.opnames[o]);
end;
if o == OpCode.OP_MOVE then
R[a] = R[b];
elseif o == OpCode.OP_LOADNIL then
for i = a, a + b, 1 do
R[i] = nil;
end;
elseif o == OpCode.OP_LOADK then
R[a] = constants[b] and constants[b].value;
elseif o == OpCode.OP_LOADBOOL then
R[a] = b ~= 0;
if c ~= 0 then
pc = pc + 1;
end;
elseif o == OpCode.OP_GETTABUP then
R[a] = (attempt(upvals[b], "index", "table", "string"))[RK(c)];
elseif o == OpCode.OP_SETTABUP then
(attempt(upvals[a], "index", "table", "string"))[RK(b)] = RK(c);
elseif o == OpCode.OP_GETUPVAL then
R[a] = upvals[b];
elseif o == OpCode.OP_SETUPVAL then
upvals[b] = R[a];
elseif o == OpCode.OP_GETTABLE then
R[a] = R[b][RK(c)];
elseif o == OpCode.OP_SETTABLE then
R[a][RK(b)] = RK(c);
elseif o == OpCode.OP_ADD then
R[a] = RK(b) + RK(c);
elseif o == OpCode.OP_SUB then
R[a] = RK(b) - RK(c);
elseif o == OpCode.OP_MUL then
R[a] = RK(b) * RK(c);
elseif o == OpCode.OP_DIV then
R[a] = RK(b) / RK(c);
elseif o == OpCode.OP_MOD then
R[a] = RK(b) % RK(c);
elseif o == OpCode.OP_POW then
R[a] = RK(b) ^ RK(c);
elseif o == OpCode.OP_IDIV then
R[a] = RK(b) // RK(c);
elseif o == OpCode.OP_BAND then
R[a] = RK(b) & RK(c);
elseif o == OpCode.OP_BOR then
R[a] = RK(b) | RK(c);
elseif o == OpCode.OP_BXOR then
R[a] = RK(b) ~ RK(c);
elseif o == OpCode.OP_SHL then
R[a] = RK(b) << RK(c);
elseif o == OpCode.OP_SHR then
R[a] = RK(b) >> RK(c);
elseif o == OpCode.OP_BNOT then
R[a] = ~R[b];
elseif o == OpCode.OP_UNM then
R[a] = -R[b];
elseif o == OpCode.OP_NOT then
R[a] = not R[b];
elseif o == OpCode.OP_LEN then
R[a] = #R[b];
elseif o == OpCode.OP_CONCAT then
local sct = {};
for i = b, c, 1 do
sct[#sct + 1] = tostring(R[i]);
end;
R[a] = table.concat(sct);
elseif o == OpCode.OP_JMP then
pc = pc + b;
if a > 0 then
for i = a - 1, chunk.maxstacksize, 1 do
if openUpvalues[i] then
local ouv = openUpvalues[i];
ouv.type = 2;
ouv.storage = R[ouv.reg];
openUpvalues[i] = nil;
end;
end;
end;
elseif o == OpCode.OP_CALL then
attemptCall(R[a]);
local ret;
if b == 1 then
if c == 1 then
R[a]();
elseif c == 2 then
R[a] = R[a]();
else
ret = pack(R[a]());
if c == 0 then
for i = 1, ret.n, 1 do
R[(a + i) - 1] = ret[i];
end;
top = (a + ret.n) - 1;
else
local g = 1;
for i = a, (a + c) - 1, 1 do
R[i] = ret[g];
g = g + 1;
end;
end;
end;
else
local s, e;
if b == 0 then
s, e = a + 1, top;
else
s, e = a + 1, (a + b) - 1;
end;
if c == 1 then
R[a](unpack(R, s, e));
elseif c == 2 then
R[a] = R[a](unpack(R, s, e));
else
ret = pack(R[a](unpack(R, s, e)));
if c == 0 then
for i = 1, ret.n, 1 do
R[(a + i) - 1] = ret[i];
end;
top = (a + ret.n) - 1;
else
local g = 1;
for i = a, (a + c) - 2, 1 do
R[i] = ret[g];
g = g + 1;
end;
end;
end;
end;
elseif o == OpCode.OP_RETURN then
local ret = {};
local rti = 1;
if b == 0 then
for i = a, top, 1 do
ret[rti] = R[i];
rti = rti + 1;
end;
else
for i = a, (a + b) - 2, 1 do
ret[rti] = R[i];
rti = rti + 1;
end;
end;
return unpack(ret, 1, rti - 1);
elseif o == OpCode.OP_TAILCALL then
local cargs = {};
local ai = 1;
if b == 0 then
for i = a + 1, top, 1 do
cargs[ai] = R[i];
ai = ai + 1;
end;
else
for i = a + 1, (a + b) - 1, 1 do
cargs[ai] = R[i];
ai = ai + 1;
end;
end;
return (attemptCall(R[a]))(unpack(cargs, 1, ai - 1));
elseif o == OpCode.OP_VARARG then
if b > 0 then
local i = 1;
for n = a, (a + b) - 1, 1 do
R[n] = args[i];
i = i + 1;
end;
else
local base = chunk.numparams + 1;
local nargs = #args;
for i = base, nargs, 1 do
R[(a + i) - base] = args[i];
end;
top = (a + nargs) - base;
end;
elseif o == OpCode.OP_SELF then
R[a + 1] = R[b];
R[a] = R[b][RK(c)];
elseif o == OpCode.OP_EQ then
if (RK(b) == RK(c)) == (a ~= 0) then
pc = (pc + opcodes:GETARG_sBx(code[pc])) + 1;
else
pc = pc + 1;
end;
elseif o == OpCode.OP_LT then
if (RK(b) < RK(c)) == (a ~= 0) then
pc = (pc + opcodes:GETARG_sBx(code[pc])) + 1;
else
pc = pc + 1;
end;
elseif o == OpCode.OP_LE then
if (RK(b) <= RK(c)) == (a ~= 0) then
pc = (pc + opcodes:GETARG_sBx(code[pc])) + 1;
else
pc = pc + 1;
end;
elseif o == OpCode.OP_TEST then
if not R[a] ~= (c ~= 0) then
pc = (pc + opcodes:GETARG_sBx(code[pc])) + 1;
else
pc = pc + 1;
end;
elseif o == OpCode.OP_TESTSET then
if not R[b] ~= (c ~= 0) then
R[a] = R[b];
pc = (pc + opcodes:GETARG_sBx(code[pc])) + 1;
else
pc = pc + 1;
end;
elseif o == OpCode.OP_FORPREP then
R[a] = R[a] - R[a + 2];
pc = pc + b;
elseif o == OpCode.OP_FORLOOP then
local step = R[a + 2];
R[a] = R[a] + step;
local idx = R[a];
local limit = R[a + 1];
local can;
if step < 0 then
can = limit <= idx;
else
can = limit >= idx;
end;
if can then
pc = pc + b;
R[a + 3] = R[a];
end;
elseif o == OpCode.OP_TFORCALL then
local ret = { R[a](R[a + 1], R[a + 2]) };
local i = 1;
for n = a + 3, (a + 2) + c, 1 do
R[n] = ret[i];
i = i + 1;
end;
local cd = code[pc];
cd.Bx = (cd.Bx and cd.Bx > 65535) and opcodes:GETARG_sBx(cd) or cd.Bx;
o, a, b = cd.OP, cd.A, cd.Bx or cd.B;
pc = pc + 1;
if R[a + 1] ~= nil then
R[a] = R[a + 1];
pc = pc + b;
end;
elseif o == OpCode.OP_TFORLOOP then
if R[a + 1] ~= nil then
R[a] = R[a + 1];
pc = pc + b;
end;
elseif o == OpCode.OP_NEWTABLE then
R[a] = {};
elseif o == OpCode.OP_SETLIST then
if b > 0 then
for i = 1, b, 1 do
R[a][(c - 1) * 50 + i] = R[a + i];
end;
else
for i = 1, top - a, 1 do
R[a][(c - 1) * 50 + i] = R[a + i];
end;
end;
elseif o == OpCode.OP_CLOSURE then
local proto = chunk.p[b];
local upvaldef = {};
local upvalues = setmetatable({}, { __index = function(_, i)
if not upvaldef[i] then
error("unknown upvalue");
end;
local uvd = upvaldef[i];
if uvd.type == 0 then
return R[uvd.reg];
elseif uvd.type == 1 then
return upvals[uvd.reg];
else
return uvd.storage;
end;
end, __newindex = function(_, i, v)
if not upvaldef[i] then
error("unknown upvalue");
end;
local uvd = upvaldef[i];
if uvd.type == 0 then
R[uvd.reg] = v;
elseif uvd.type == 1 then
upvals[uvd.reg] = v;
else
uvd.storage = v;
end;
end });
R[a] = function(...)
return vm.run(proto, { ... }, upvalues, globals, hook);
end;
for i = 1, proto.nups, 1 do
local cd = code[(pc + i) - 1];
cd.Bx = (cd.Bx and cd.Bx > 65535) and opcodes:GETARG_sBx(cd) or cd.Bx;
local o, a, b, c = cd.OP, cd.A, cd.Bx or cd.B, cd.C;
debug(pc + i, "PSD", opcodes.opnames[o], a, b, c);
if o == OpCode.OP_MOVE then
upvaldef[i - 1] = openUpvalues[b] or { type = 0, reg = b };
openUpvalues[b] = upvaldef[i - 1];
elseif o == OpCode.OP_GETUPVAL then
upvaldef[i - 1] = { type = 1, reg = b };
else
error("unknown upvalue psuedop");
end;
end;
pc = pc + proto.nups;
else
error("Unknown opcode!");
end;
end;
end));
for i = 0, chunk.maxstacksize, 1 do
if openUpvalues[i] then
local ouv = openUpvalues[i];
ouv.type = 2;
ouv.storage = R[ouv.reg];
openUpvalues[i] = nil;
end;
end;
if not ret[1] then
error(tostring(ret[2]) .. ("\n" .. (tostring(chunk.source) .. (" at pc " .. (pc - 1 .. (" line " .. tostring(chunk.lineinfo[pc - 1])))))), 0);
else
return unpack(ret, 2, ret.n);
end;
end;
function vm.call(self, ...)
return vm.run(self.chunk, ...);
end;
return setmetatable({}, { __call = vm.call });