Lua 5.1.4: lvm.c
L0001
L0006
L0007
L0008 #include <stdio.h>
L0009 #include <stdlib.h>
L0010 #include <string.h>
L0011
L0012 #define lvm_c
L0013 #define LUA_CORE
L0014
L0015 #include "lua.h"
L0016
L0017 #include "ldebug.h"
L0018 #include "ldo.h"
L0019 #include "lfunc.h"
L0020 #include "lgc.h"
L0021 #include "lobject.h"
L0022 #include "lopcodes.h"
L0023 #include "lstate.h"
L0024 #include "lstring.h"
L0025 #include "ltable.h"
L0026 #include "ltm.h"
L0027 #include "lvm.h"
L0028
L0029
L0030
L0031
L0032 #define MAXTAGLOOP 100
L0033
L0034
L0035 const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
L0036 lua_Number num;
L0037 if (ttisnumber(obj)) return obj;
L0038 if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
L0039 setnvalue(n, num);
L0040 return n;
L0041 }
L0042 else
L0043 return NULL;
L0044 }
L0045
L0046
L0047 int luaV_tostring (lua_State *L, StkId obj) {
L0048 if (!ttisnumber(obj))
L0049 return 0;
L0050 else {
L0051 char s[LUAI_MAXNUMBER2STR];
L0052 lua_Number n = nvalue(obj);
L0053 lua_number2str(s, n);
L0054 setsvalue2s(L, obj, luaS_new(L, s));
L0055 return 1;
L0056 }
L0057 }
L0058
L0059
L0060 static void traceexec (lua_State *L, const Instruction *pc) {
L0061 lu_byte mask = L->hookmask;
L0062 const Instruction *oldpc = L->savedpc;
L0063 L->savedpc = pc;
L0064 if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) {
L0065 resethookcount(L);
L0066 luaD_callhook(L, LUA_HOOKCOUNT, -1);
L0067 }
L0068 if (mask & LUA_MASKLINE) {
L0069 Proto *p = ci_func(L->ci)->l.p;
L0070 int npc = pcRel(pc, p);
L0071 int newline = getline(p, npc);
L0072
L0074 if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p)))
L0075 luaD_callhook(L, LUA_HOOKLINE, newline);
L0076 }
L0077 }
L0078
L0079
L0080 static void callTMres (lua_State *L, StkId res, const TValue *f,
L0081 const TValue *p1, const TValue *p2) {
L0082 ptrdiff_t result = savestack(L, res);
L0083 setobj2s(L, L->top, f);
L0084 setobj2s(L, L->top+1, p1);
L0085 setobj2s(L, L->top+2, p2);
L0086 luaD_checkstack(L, 3);
L0087 L->top += 3;
L0088 luaD_call(L, L->top - 3, 1);
L0089 res = restorestack(L, result);
L0090 L->top--;
L0091 setobjs2s(L, res, L->top);
L0092 }
L0093
L0094
L0095
L0096 static void callTM (lua_State *L, const TValue *f, const TValue *p1,
L0097 const TValue *p2, const TValue *p3) {
L0098 setobj2s(L, L->top, f);
L0099 setobj2s(L, L->top+1, p1);
L0100 setobj2s(L, L->top+2, p2);
L0101 setobj2s(L, L->top+3, p3);
L0102 luaD_checkstack(L, 4);
L0103 L->top += 4;
L0104 luaD_call(L, L->top - 4, 0);
L0105 }
L0106
L0107
L0108 void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
L0109 int loop;
L0110 for (loop = 0; loop < MAXTAGLOOP; loop++) {
L0111 const TValue *tm;
L0112 if (ttistable(t)) {
L0113 Table *h = hvalue(t);
L0114 const TValue *res = luaH_get(h, key);
L0115 if (!ttisnil(res) ||
L0116 (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) {
L0117 setobj2s(L, val, res);
L0118 return;
L0119 }
L0120
L0121 }
L0122 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
L0123 luaG_typeerror(L, t, "index");
L0124 if (ttisfunction(tm)) {
L0125 callTMres(L, val, tm, t, key);
L0126 return;
L0127 }
L0128 t = tm;
L0129 }
L0130 luaG_runerror(L, "loop in gettable");
L0131 }
L0132
L0133
L0134 void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
L0135 int loop;
L0136 for (loop = 0; loop < MAXTAGLOOP; loop++) {
L0137 const TValue *tm;
L0138 if (ttistable(t)) {
L0139 Table *h = hvalue(t);
L0140 TValue *oldval = luaH_set(L, h, key);
L0141 if (!ttisnil(oldval) ||
L0142 (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) {
L0143 setobj2t(L, oldval, val);
L0144 luaC_barriert(L, h, val);
L0145 return;
L0146 }
L0147
L0148 }
L0149 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
L0150 luaG_typeerror(L, t, "index");
L0151 if (ttisfunction(tm)) {
L0152 callTM(L, tm, t, key, val);
L0153 return;
L0154 }
L0155 t = tm;
L0156 }
L0157 luaG_runerror(L, "loop in settable");
L0158 }
L0159
L0160
L0161 static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
L0162 StkId res, TMS event) {
L0163 const TValue *tm = luaT_gettmbyobj(L, p1, event);
L0164 if (ttisnil(tm))
L0165 tm = luaT_gettmbyobj(L, p2, event);
L0166 if (ttisnil(tm)) return 0;
L0167 callTMres(L, res, tm, p1, p2);
L0168 return 1;
L0169 }
L0170
L0171
L0172 static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2,
L0173 TMS event) {
L0174 const TValue *tm1 = fasttm(L, mt1, event);
L0175 const TValue *tm2;
L0176 if (tm1 == NULL) return NULL;
L0177 if (mt1 == mt2) return tm1;
L0178 tm2 = fasttm(L, mt2, event);
L0179 if (tm2 == NULL) return NULL;
L0180 if (luaO_rawequalObj(tm1, tm2))
L0181 return tm1;
L0182 return NULL;
L0183 }
L0184
L0185
L0186 static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
L0187 TMS event) {
L0188 const TValue *tm1 = luaT_gettmbyobj(L, p1, event);
L0189 const TValue *tm2;
L0190 if (ttisnil(tm1)) return -1;
L0191 tm2 = luaT_gettmbyobj(L, p2, event);
L0192 if (!luaO_rawequalObj(tm1, tm2))
L0193 return -1;
L0194 callTMres(L, L->top, tm1, p1, p2);
L0195 return !l_isfalse(L->top);
L0196 }
L0197
L0198
L0199 static int l_strcmp (const TString *ls, const TString *rs) {
L0200 const char *l = getstr(ls);
L0201 size_t ll = ls->tsv.len;
L0202 const char *r = getstr(rs);
L0203 size_t lr = rs->tsv.len;
L0204 for (;;) {
L0205 int temp = strcoll(l, r);
L0206 if (temp != 0) return temp;
L0207 else {
L0208 size_t len = strlen(l);
L0209 if (len == lr)
L0210 return (len == ll) ? 0 : 1;
L0211 else if (len == ll)
L0212 return -1;
L0213
L0214 len++;
L0215 l += len; ll -= len; r += len; lr -= len;
L0216 }
L0217 }
L0218 }
L0219
L0220
L0221 int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
L0222 int res;
L0223 if (ttype(l) != ttype(r))
L0224 return luaG_ordererror(L, l, r);
L0225 else if (ttisnumber(l))
L0226 return luai_numlt(nvalue(l), nvalue(r));
L0227 else if (ttisstring(l))
L0228 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
L0229 else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
L0230 return res;
L0231 return luaG_ordererror(L, l, r);
L0232 }
L0233
L0234
L0235 static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
L0236 int res;
L0237 if (ttype(l) != ttype(r))
L0238 return luaG_ordererror(L, l, r);
L0239 else if (ttisnumber(l))
L0240 return luai_numle(nvalue(l), nvalue(r));
L0241 else if (ttisstring(l))
L0242 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
L0243 else if ((res = call_orderTM(L, l, r, TM_LE)) != -1)
L0244 return res;
L0245 else if ((res = call_orderTM(L, r, l, TM_LT)) != -1)
L0246 return !res;
L0247 return luaG_ordererror(L, l, r);
L0248 }
L0249
L0250
L0251 int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
L0252 const TValue *tm;
L0253 lua_assert(ttype(t1) == ttype(t2));
L0254 switch (ttype(t1)) {
L0255 case LUA_TNIL: return 1;
L0256 case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
L0257 case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2);
L0258 case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
L0259 case LUA_TUSERDATA: {
L0260 if (uvalue(t1) == uvalue(t2)) return 1;
L0261 tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
L0262 TM_EQ);
L0263 break;
L0264 }
L0265 case LUA_TTABLE: {
L0266 if (hvalue(t1) == hvalue(t2)) return 1;
L0267 tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
L0268 break;
L0269 }
L0270 default: return gcvalue(t1) == gcvalue(t2);
L0271 }
L0272 if (tm == NULL) return 0;
L0273 callTMres(L, L->top, tm, t1, t2);
L0274 return !l_isfalse(L->top);
L0275 }
L0276
L0277
L0278 void luaV_concat (lua_State *L, int total, int last) {
L0279 do {
L0280 StkId top = L->base + last + 1;
L0281 int n = 2;
L0282 if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
L0283 if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
L0284 luaG_concaterror(L, top-2, top-1);
L0285 } else if (tsvalue(top-1)->len == 0)
L0286 (void)tostring(L, top - 2);
L0287 else {
L0288
L0289 size_t tl = tsvalue(top-1)->len;
L0290 char *buffer;
L0291 int i;
L0292
L0293 for (n = 1; n < total && tostring(L, top-n-1); n++) {
L0294 size_t l = tsvalue(top-n-1)->len;
L0295 if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
L0296 tl += l;
L0297 }
L0298 buffer = luaZ_openspace(L, &G(L)->buff, tl);
L0299 tl = 0;
L0300 for (i=n; i>0; i--) {
L0301 size_t l = tsvalue(top-i)->len;
L0302 memcpy(buffer+tl, svalue(top-i), l);
L0303 tl += l;
L0304 }
L0305 setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
L0306 }
L0307 total -= n-1;
L0308 last -= n-1;
L0309 } while (total > 1);
L0310 }
L0311
L0312
L0313 static void Arith (lua_State *L, StkId ra, const TValue *rb,
L0314 const TValue *rc, TMS op) {
L0315 TValue tempb, tempc;
L0316 const TValue *b, *c;
L0317 if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
L0318 (c = luaV_tonumber(rc, &tempc)) != NULL) {
L0319 lua_Number nb = nvalue(b), nc = nvalue(c);
L0320 switch (op) {
L0321 case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
L0322 case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
L0323 case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
L0324 case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
L0325 case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
L0326 case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
L0327 case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
L0328 default: lua_assert(0); break;
L0329 }
L0330 }
L0331 else if (!call_binTM(L, rb, rc, ra, op))
L0332 luaG_aritherror(L, rb, rc);
L0333 }
L0334
L0335
L0336
L0337
L0340
L0341 #define runtime_check(L, c) { if (!(c)) break; }
L0342
L0343 #define RA(i) (base+GETARG_A(i))
L0344
L0345 #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
L0346 #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
L0347 #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
L0348 ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
L0349 #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
L0350 ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
L0351 #define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
L0352
L0353
L0354 #define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);}
L0355
L0356
L0357 #define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
L0358
L0359
L0360 #define arith_op(op,tm) { \
L0361 TValue *rb = RKB(i); \
L0362 TValue *rc = RKC(i); \
L0363 if (ttisnumber(rb) && ttisnumber(rc)) { \
L0364 lua_Number nb = nvalue(rb), nc = nvalue(rc); \
L0365 setnvalue(ra, op(nb, nc)); \
L0366 } \
L0367 else \
L0368 Protect(Arith(L, ra, rb, rc, tm)); \
L0369 }
L0370
L0371
L0372
L0373 void luaV_execute (lua_State *L, int nexeccalls) {
L0374 LClosure *cl;
L0375 StkId base;
L0376 TValue *k;
L0377 const Instruction *pc;
L0378 reentry:
L0379 lua_assert(isLua(L->ci));
L0380 pc = L->savedpc;
L0381 cl = &clvalue(L->ci->func)->l;
L0382 base = L->base;
L0383 k = cl->p->k;
L0384
L0385 for (;;) {
L0386 const Instruction i = *pc++;
L0387 StkId ra;
L0388 if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
L0389 (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
L0390 traceexec(L, pc);
L0391 if (L->status == LUA_YIELD) {
L0392 L->savedpc = pc - 1;
L0393 return;
L0394 }
L0395 base = L->base;
L0396 }
L0397
L0398 ra = RA(i);
L0399 lua_assert(base == L->base && L->base == L->ci->base);
L0400 lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
L0401 lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
L0402 switch (GET_OPCODE(i)) {
L0403 case OP_MOVE: {
L0404 setobjs2s(L, ra, RB(i));
L0405 continue;
L0406 }
L0407 case OP_LOADK: {
L0408 setobj2s(L, ra, KBx(i));
L0409 continue;
L0410 }
L0411 case OP_LOADBOOL: {
L0412 setbvalue(ra, GETARG_B(i));
L0413 if (GETARG_C(i)) pc++;
L0414 continue;
L0415 }
L0416 case OP_LOADNIL: {
L0417 TValue *rb = RB(i);
L0418 do {
L0419 setnilvalue(rb--);
L0420 } while (rb >= ra);
L0421 continue;
L0422 }
L0423 case OP_GETUPVAL: {
L0424 int b = GETARG_B(i);
L0425 setobj2s(L, ra, cl->upvals[b]->v);
L0426 continue;
L0427 }
L0428 case OP_GETGLOBAL: {
L0429 TValue g;
L0430 TValue *rb = KBx(i);
L0431 sethvalue(L, &g, cl->env);
L0432 lua_assert(ttisstring(rb));
L0433 Protect(luaV_gettable(L, &g, rb, ra));
L0434 continue;
L0435 }
L0436 case OP_GETTABLE: {
L0437 Protect(luaV_gettable(L, RB(i), RKC(i), ra));
L0438 continue;
L0439 }
L0440 case OP_SETGLOBAL: {
L0441 TValue g;
L0442 sethvalue(L, &g, cl->env);
L0443 lua_assert(ttisstring(KBx(i)));
L0444 Protect(luaV_settable(L, &g, KBx(i), ra));
L0445 continue;
L0446 }
L0447 case OP_SETUPVAL: {
L0448 UpVal *uv = cl->upvals[GETARG_B(i)];
L0449 setobj(L, uv->v, ra);
L0450 luaC_barrier(L, uv, ra);
L0451 continue;
L0452 }
L0453 case OP_SETTABLE: {
L0454 Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
L0455 continue;
L0456 }
L0457 case OP_NEWTABLE: {
L0458 int b = GETARG_B(i);
L0459 int c = GETARG_C(i);
L0460 sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
L0461 Protect(luaC_checkGC(L));
L0462 continue;
L0463 }
L0464 case OP_SELF: {
L0465 StkId rb = RB(i);
L0466 setobjs2s(L, ra+1, rb);
L0467 Protect(luaV_gettable(L, rb, RKC(i), ra));
L0468 continue;
L0469 }
L0470 case OP_ADD: {
L0471 arith_op(luai_numadd, TM_ADD);
L0472 continue;
L0473 }
L0474 case OP_SUB: {
L0475 arith_op(luai_numsub, TM_SUB);
L0476 continue;
L0477 }
L0478 case OP_MUL: {
L0479 arith_op(luai_nummul, TM_MUL);
L0480 continue;
L0481 }
L0482 case OP_DIV: {
L0483 arith_op(luai_numdiv, TM_DIV);
L0484 continue;
L0485 }
L0486 case OP_MOD: {
L0487 arith_op(luai_nummod, TM_MOD);
L0488 continue;
L0489 }
L0490 case OP_POW: {
L0491 arith_op(luai_numpow, TM_POW);
L0492 continue;
L0493 }
L0494 case OP_UNM: {
L0495 TValue *rb = RB(i);
L0496 if (ttisnumber(rb)) {
L0497 lua_Number nb = nvalue(rb);
L0498 setnvalue(ra, luai_numunm(nb));
L0499 }
L0500 else {
L0501 Protect(Arith(L, ra, rb, rb, TM_UNM));
L0502 }
L0503 continue;
L0504 }
L0505 case OP_NOT: {
L0506 int res = l_isfalse(RB(i));
L0507 setbvalue(ra, res);
L0508 continue;
L0509 }
L0510 case OP_LEN: {
L0511 const TValue *rb = RB(i);
L0512 switch (ttype(rb)) {
L0513 case LUA_TTABLE: {
L0514 setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
L0515 break;
L0516 }
L0517 case LUA_TSTRING: {
L0518 setnvalue(ra, cast_num(tsvalue(rb)->len));
L0519 break;
L0520 }
L0521 default: {
L0522 Protect(
L0523 if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN))
L0524 luaG_typeerror(L, rb, "get length of");
L0525 )
L0526 }
L0527 }
L0528 continue;
L0529 }
L0530 case OP_CONCAT: {
L0531 int b = GETARG_B(i);
L0532 int c = GETARG_C(i);
L0533 Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
L0534 setobjs2s(L, RA(i), base+b);
L0535 continue;
L0536 }
L0537 case OP_JMP: {
L0538 dojump(L, pc, GETARG_sBx(i));
L0539 continue;
L0540 }
L0541 case OP_EQ: {
L0542 TValue *rb = RKB(i);
L0543 TValue *rc = RKC(i);
L0544 Protect(
L0545 if (equalobj(L, rb, rc) == GETARG_A(i))
L0546 dojump(L, pc, GETARG_sBx(*pc));
L0547 )
L0548 pc++;
L0549 continue;
L0550 }
L0551 case OP_LT: {
L0552 Protect(
L0553 if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
L0554 dojump(L, pc, GETARG_sBx(*pc));
L0555 )
L0556 pc++;
L0557 continue;
L0558 }
L0559 case OP_LE: {
L0560 Protect(
L0561 if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
L0562 dojump(L, pc, GETARG_sBx(*pc));
L0563 )
L0564 pc++;
L0565 continue;
L0566 }
L0567 case OP_TEST: {
L0568 if (l_isfalse(ra) != GETARG_C(i))
L0569 dojump(L, pc, GETARG_sBx(*pc));
L0570 pc++;
L0571 continue;
L0572 }
L0573 case OP_TESTSET: {
L0574 TValue *rb = RB(i);
L0575 if (l_isfalse(rb) != GETARG_C(i)) {
L0576 setobjs2s(L, ra, rb);
L0577 dojump(L, pc, GETARG_sBx(*pc));
L0578 }
L0579 pc++;
L0580 continue;
L0581 }
L0582 case OP_CALL: {
L0583 int b = GETARG_B(i);
L0584 int nresults = GETARG_C(i) - 1;
L0585 if (b != 0) L->top = ra+b;
L0586 L->savedpc = pc;
L0587 switch (luaD_precall(L, ra, nresults)) {
L0588 case PCRLUA: {
L0589 nexeccalls++;
L0590 goto reentry;
L0591 }
L0592 case PCRC: {
L0593
L0594 if (nresults >= 0) L->top = L->ci->top;
L0595 base = L->base;
L0596 continue;
L0597 }
L0598 default: {
L0599 return;
L0600 }
L0601 }
L0602 }
L0603 case OP_TAILCALL: {
L0604 int b = GETARG_B(i);
L0605 if (b != 0) L->top = ra+b;
L0606 L->savedpc = pc;
L0607 lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
L0608 switch (luaD_precall(L, ra, LUA_MULTRET)) {
L0609 case PCRLUA: {
L0610
L0611 CallInfo *ci = L->ci - 1;
L0612 int aux;
L0613 StkId func = ci->func;
L0614 StkId pfunc = (ci+1)->func;
L0615 if (L->openupval) luaF_close(L, ci->base);
L0616 L->base = ci->base = ci->func + ((ci+1)->base - pfunc);
L0617 for (aux = 0; pfunc+aux < L->top; aux++)
L0618 setobjs2s(L, func+aux, pfunc+aux);
L0619 ci->top = L->top = func+aux;
L0620 lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
L0621 ci->savedpc = L->savedpc;
L0622 ci->tailcalls++;
L0623 L->ci--;
L0624 goto reentry;
L0625 }
L0626 case PCRC: {
L0627 base = L->base;
L0628 continue;
L0629 }
L0630 default: {
L0631 return;
L0632 }
L0633 }
L0634 }
L0635 case OP_RETURN: {
L0636 int b = GETARG_B(i);
L0637 if (b != 0) L->top = ra+b-1;
L0638 if (L->openupval) luaF_close(L, base);
L0639 L->savedpc = pc;
L0640 b = luaD_poscall(L, ra);
L0641 if (--nexeccalls == 0)
L0642 return;
L0643 else {
L0644 if (b) L->top = L->ci->top;
L0645 lua_assert(isLua(L->ci));
L0646 lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL);
L0647 goto reentry;
L0648 }
L0649 }
L0650 case OP_FORLOOP: {
L0651 lua_Number step = nvalue(ra+2);
L0652 lua_Number idx = luai_numadd(nvalue(ra), step);
L0653 lua_Number limit = nvalue(ra+1);
L0654 if (luai_numlt(0, step) ? luai_numle(idx, limit)
L0655 : luai_numle(limit, idx)) {
L0656 dojump(L, pc, GETARG_sBx(i));
L0657 setnvalue(ra, idx);
L0658 setnvalue(ra+3, idx);
L0659 }
L0660 continue;
L0661 }
L0662 case OP_FORPREP: {
L0663 const TValue *init = ra;
L0664 const TValue *plimit = ra+1;
L0665 const TValue *pstep = ra+2;
L0666 L->savedpc = pc;
L0667 if (!tonumber(init, ra))
L0668 luaG_runerror(L, LUA_QL("for") " initial value must be a number");
L0669 else if (!tonumber(plimit, ra+1))
L0670 luaG_runerror(L, LUA_QL("for") " limit must be a number");
L0671 else if (!tonumber(pstep, ra+2))
L0672 luaG_runerror(L, LUA_QL("for") " step must be a number");
L0673 setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
L0674 dojump(L, pc, GETARG_sBx(i));
L0675 continue;
L0676 }
L0677 case OP_TFORLOOP: {
L0678 StkId cb = ra + 3;
L0679 setobjs2s(L, cb+2, ra+2);
L0680 setobjs2s(L, cb+1, ra+1);
L0681 setobjs2s(L, cb, ra);
L0682 L->top = cb+3;
L0683 Protect(luaD_call(L, cb, GETARG_C(i)));
L0684 L->top = L->ci->top;
L0685 cb = RA(i) + 3;
L0686 if (!ttisnil(cb)) {
L0687 setobjs2s(L, cb-1, cb);
L0688 dojump(L, pc, GETARG_sBx(*pc));
L0689 }
L0690 pc++;
L0691 continue;
L0692 }
L0693 case OP_SETLIST: {
L0694 int n = GETARG_B(i);
L0695 int c = GETARG_C(i);
L0696 int last;
L0697 Table *h;
L0698 if (n == 0) {
L0699 n = cast_int(L->top - ra) - 1;
L0700 L->top = L->ci->top;
L0701 }
L0702 if (c == 0) c = cast_int(*pc++);
L0703 runtime_check(L, ttistable(ra));
L0704 h = hvalue(ra);
L0705 last = ((c-1)*LFIELDS_PER_FLUSH) + n;
L0706 if (last > h->sizearray)
L0707 luaH_resizearray(L, h, last);
L0708 for (; n > 0; n--) {
L0709 TValue *val = ra+n;
L0710 setobj2t(L, luaH_setnum(L, h, last--), val);
L0711 luaC_barriert(L, h, val);
L0712 }
L0713 continue;
L0714 }
L0715 case OP_CLOSE: {
L0716 luaF_close(L, ra);
L0717 continue;
L0718 }
L0719 case OP_CLOSURE: {
L0720 Proto *p;
L0721 Closure *ncl;
L0722 int nup, j;
L0723 p = cl->p->p[GETARG_Bx(i)];
L0724 nup = p->nups;
L0725 ncl = luaF_newLclosure(L, nup, cl->env);
L0726 ncl->l.p = p;
L0727 for (j=0; j<nup; j++, pc++) {
L0728 if (GET_OPCODE(*pc) == OP_GETUPVAL)
L0729 ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
L0730 else {
L0731 lua_assert(GET_OPCODE(*pc) == OP_MOVE);
L0732 ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
L0733 }
L0734 }
L0735 setclvalue(L, ra, ncl);
L0736 Protect(luaC_checkGC(L));
L0737 continue;
L0738 }
L0739 case OP_VARARG: {
L0740 int b = GETARG_B(i) - 1;
L0741 int j;
L0742 CallInfo *ci = L->ci;
L0743 int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1;
L0744 if (b == LUA_MULTRET) {
L0745 Protect(luaD_checkstack(L, n));
L0746 ra = RA(i);
L0747 b = n;
L0748 L->top = ra + n;
L0749 }
L0750 for (j = 0; j < b; j++) {
L0751 if (j < n) {
L0752 setobjs2s(L, ra + j, ci->base - n + j);
L0753 }
L0754 else {
L0755 setnilvalue(ra + j);
L0756 }
L0757 }
L0758 continue;
L0759 }
L0760 }
L0761 }
L0762 }
L0763
Generated by pretty.lua