00001
00002
00003
00004
00005
00006
00007
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011
00012 #define lvm_c
00013 #define LUA_CORE
00014
00015 #include "lua.h"
00016
00017 #include "ldebug.h"
00018 #include "ldo.h"
00019 #include "lfunc.h"
00020 #include "lgc.h"
00021 #include "lobject.h"
00022 #include "lopcodes.h"
00023 #include "lstate.h"
00024 #include "lstring.h"
00025 #include "ltable.h"
00026 #include "ltm.h"
00027 #include "lvm.h"
00028
00029
00030
00031
00032 #define MAXTAGLOOP 100
00033
00034
00035 const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
00036 lua_Number num;
00037 if (ttisnumber(obj)) return obj;
00038 if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
00039 setnvalue(n, num);
00040 return n;
00041 }
00042 else
00043 return NULL;
00044 }
00045
00046
00047 int luaV_tostring (lua_State *L, StkId obj) {
00048 if (!ttisnumber(obj))
00049 return 0;
00050 else {
00051 char s[LUAI_MAXNUMBER2STR];
00052 lua_Number n = nvalue(obj);
00053 lua_number2str(s, n);
00054 setsvalue2s(L, obj, luaS_new(L, s));
00055 return 1;
00056 }
00057 }
00058
00059
00060 static void traceexec (lua_State *L, const Instruction *pc) {
00061 lu_byte mask = L->hookmask;
00062 const Instruction *oldpc = L->savedpc;
00063 L->savedpc = pc;
00064 if (mask > LUA_MASKLINE) {
00065 if (L->hookcount == 0) {
00066 resethookcount(L);
00067 luaD_callhook(L, LUA_HOOKCOUNT, -1);
00068 }
00069 }
00070 if (mask & LUA_MASKLINE) {
00071 Proto *p = ci_func(L->ci)->l.p;
00072 int npc = pcRel(pc, p);
00073 int newline = getline(p, npc);
00074
00075
00076 if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p)))
00077 luaD_callhook(L, LUA_HOOKLINE, newline);
00078 }
00079 }
00080
00081
00082 static void callTMres (lua_State *L, StkId res, const TValue *f,
00083 const TValue *p1, const TValue *p2) {
00084 ptrdiff_t result = savestack(L, res);
00085 setobj2s(L, L->top, f);
00086 setobj2s(L, L->top+1, p1);
00087 setobj2s(L, L->top+2, p2);
00088 luaD_checkstack(L, 3);
00089 L->top += 3;
00090 luaD_call(L, L->top - 3, 1);
00091 res = restorestack(L, result);
00092 L->top--;
00093 setobjs2s(L, res, L->top);
00094 }
00095
00096
00097
00098 static void callTM (lua_State *L, const TValue *f, const TValue *p1,
00099 const TValue *p2, const TValue *p3) {
00100 setobj2s(L, L->top, f);
00101 setobj2s(L, L->top+1, p1);
00102 setobj2s(L, L->top+2, p2);
00103 setobj2s(L, L->top+3, p3);
00104 luaD_checkstack(L, 4);
00105 L->top += 4;
00106 luaD_call(L, L->top - 4, 0);
00107 }
00108
00109
00110 void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
00111 int loop;
00112 for (loop = 0; loop < MAXTAGLOOP; loop++) {
00113 const TValue *tm;
00114 if (ttistable(t)) {
00115 Table *h = hvalue(t);
00116 const TValue *res = luaH_get(h, key);
00117 if (!ttisnil(res) ||
00118 (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) {
00119 setobj2s(L, val, res);
00120 return;
00121 }
00122
00123 }
00124 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
00125 luaG_typeerror(L, t, "index");
00126 if (ttisfunction(tm)) {
00127 callTMres(L, val, tm, t, key);
00128 return;
00129 }
00130 t = tm;
00131 }
00132 luaG_runerror(L, "loop in gettable");
00133 }
00134
00135
00136 void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
00137 int loop;
00138 for (loop = 0; loop < MAXTAGLOOP; loop++) {
00139 const TValue *tm;
00140 if (ttistable(t)) {
00141 Table *h = hvalue(t);
00142 TValue *oldval = luaH_set(L, h, key);
00143 if (!ttisnil(oldval) ||
00144 (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) {
00145 setobj2t(L, oldval, val);
00146 luaC_barriert(L, h, val);
00147 return;
00148 }
00149
00150 }
00151 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
00152 luaG_typeerror(L, t, "index");
00153 if (ttisfunction(tm)) {
00154 callTM(L, tm, t, key, val);
00155 return;
00156 }
00157 t = tm;
00158 }
00159 luaG_runerror(L, "loop in settable");
00160 }
00161
00162
00163 static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
00164 StkId res, TMS event) {
00165 const TValue *tm = luaT_gettmbyobj(L, p1, event);
00166 if (ttisnil(tm))
00167 tm = luaT_gettmbyobj(L, p2, event);
00168 if (ttisnil(tm)) return 0;
00169 callTMres(L, res, tm, p1, p2);
00170 return 1;
00171 }
00172
00173
00174 static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2,
00175 TMS event) {
00176 const TValue *tm1 = fasttm(L, mt1, event);
00177 const TValue *tm2;
00178 if (tm1 == NULL) return NULL;
00179 if (mt1 == mt2) return tm1;
00180 tm2 = fasttm(L, mt2, event);
00181 if (tm2 == NULL) return NULL;
00182 if (luaO_rawequalObj(tm1, tm2))
00183 return tm1;
00184 return NULL;
00185 }
00186
00187
00188 static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
00189 TMS event) {
00190 const TValue *tm1 = luaT_gettmbyobj(L, p1, event);
00191 const TValue *tm2;
00192 if (ttisnil(tm1)) return -1;
00193 tm2 = luaT_gettmbyobj(L, p2, event);
00194 if (!luaO_rawequalObj(tm1, tm2))
00195 return -1;
00196 callTMres(L, L->top, tm1, p1, p2);
00197 return !l_isfalse(L->top);
00198 }
00199
00200
00201 static int l_strcmp (const TString *ls, const TString *rs) {
00202 const char *l = getstr(ls);
00203 size_t ll = ls->tsv.len;
00204 const char *r = getstr(rs);
00205 size_t lr = rs->tsv.len;
00206 for (;;) {
00207 int temp = strcoll(l, r);
00208 if (temp != 0) return temp;
00209 else {
00210 size_t len = strlen(l);
00211 if (len == lr)
00212 return (len == ll) ? 0 : 1;
00213 else if (len == ll)
00214 return -1;
00215
00216 len++;
00217 l += len; ll -= len; r += len; lr -= len;
00218 }
00219 }
00220 }
00221
00222
00223 int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
00224 int res;
00225 if (ttype(l) != ttype(r))
00226 return luaG_ordererror(L, l, r);
00227 else if (ttisnumber(l))
00228 return luai_numlt(nvalue(l), nvalue(r));
00229 else if (ttisstring(l))
00230 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
00231 else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
00232 return res;
00233 return luaG_ordererror(L, l, r);
00234 }
00235
00236
00237 static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
00238 int res;
00239 if (ttype(l) != ttype(r))
00240 return luaG_ordererror(L, l, r);
00241 else if (ttisnumber(l))
00242 return luai_numle(nvalue(l), nvalue(r));
00243 else if (ttisstring(l))
00244 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
00245 else if ((res = call_orderTM(L, l, r, TM_LE)) != -1)
00246 return res;
00247 else if ((res = call_orderTM(L, r, l, TM_LT)) != -1)
00248 return !res;
00249 return luaG_ordererror(L, l, r);
00250 }
00251
00252
00253 int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
00254 const TValue *tm;
00255 lua_assert(ttype(t1) == ttype(t2));
00256 switch (ttype(t1)) {
00257 case LUA_TNIL: return 1;
00258 case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
00259 case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2);
00260 case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
00261 case LUA_TUSERDATA: {
00262 if (uvalue(t1) == uvalue(t2)) return 1;
00263 tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
00264 TM_EQ);
00265 break;
00266 }
00267 case LUA_TTABLE: {
00268 if (hvalue(t1) == hvalue(t2)) return 1;
00269 tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
00270 break;
00271 }
00272 default: return gcvalue(t1) == gcvalue(t2);
00273 }
00274 if (tm == NULL) return 0;
00275 callTMres(L, L->top, tm, t1, t2);
00276 return !l_isfalse(L->top);
00277 }
00278
00279
00280 void luaV_concat (lua_State *L, int total, int last) {
00281 do {
00282 StkId top = L->base + last + 1;
00283 int n = 2;
00284 if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
00285 if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
00286 luaG_concaterror(L, top-2, top-1);
00287 } else if (tsvalue(top-1)->len == 0)
00288 (void)tostring(L, top - 2);
00289 else {
00290
00291 size_t tl = tsvalue(top-1)->len;
00292 char *buffer;
00293 int i;
00294
00295 for (n = 1; n < total && tostring(L, top-n-1); n++) {
00296 size_t l = tsvalue(top-n-1)->len;
00297 if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
00298 tl += l;
00299 }
00300 buffer = luaZ_openspace(L, &G(L)->buff, tl);
00301 tl = 0;
00302 for (i=n; i>0; i--) {
00303 size_t l = tsvalue(top-i)->len;
00304 memcpy(buffer+tl, svalue(top-i), l);
00305 tl += l;
00306 }
00307 setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
00308 }
00309 total -= n-1;
00310 last -= n-1;
00311 } while (total > 1);
00312 }
00313
00314
00315 static void Arith (lua_State *L, StkId ra, const TValue *rb,
00316 const TValue *rc, TMS op) {
00317 TValue tempb, tempc;
00318 const TValue *b, *c;
00319 if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
00320 (c = luaV_tonumber(rc, &tempc)) != NULL) {
00321 lua_Number nb = nvalue(b), nc = nvalue(c);
00322 switch (op) {
00323 case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
00324 case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
00325 case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
00326 case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
00327 case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
00328 case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
00329 case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
00330 default: lua_assert(0); break;
00331 }
00332 }
00333 else if (!call_binTM(L, rb, rc, ra, op))
00334 luaG_aritherror(L, rb, rc);
00335 }
00336
00337
00338
00339
00340
00341
00342
00343 #define runtime_check(L, c) { if (!(c)) break; }
00344
00345 #define RA(i) (base+GETARG_A(i))
00346
00347 #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
00348 #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
00349 #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
00350 ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
00351 #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
00352 ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
00353 #define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
00354
00355
00356 #define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);}
00357
00358
00359 #define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
00360
00361
00362 #define arith_op(op,tm) { \
00363 TValue *rb = RKB(i); \
00364 TValue *rc = RKC(i); \
00365 if (ttisnumber(rb) && ttisnumber(rc)) { \
00366 lua_Number nb = nvalue(rb), nc = nvalue(rc); \
00367 setnvalue(ra, op(nb, nc)); \
00368 } \
00369 else \
00370 Protect(Arith(L, ra, rb, rc, tm)); \
00371 }
00372
00373
00374
00375 void luaV_execute (lua_State *L, int nexeccalls) {
00376 LClosure *cl;
00377 StkId base;
00378 TValue *k;
00379 const Instruction *pc;
00380 reentry:
00381 lua_assert(isLua(L->ci));
00382 pc = L->savedpc;
00383 cl = &clvalue(L->ci->func)->l;
00384 base = L->base;
00385 k = cl->p->k;
00386
00387 for (;;) {
00388 const Instruction i = *pc++;
00389 StkId ra;
00390 if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
00391 (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
00392 traceexec(L, pc);
00393 if (L->status == LUA_YIELD) {
00394 L->savedpc = pc - 1;
00395 return;
00396 }
00397 base = L->base;
00398 }
00399
00400 ra = RA(i);
00401 lua_assert(base == L->base && L->base == L->ci->base);
00402 lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
00403 lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
00404 switch (GET_OPCODE(i)) {
00405 case OP_MOVE: {
00406 setobjs2s(L, ra, RB(i));
00407 continue;
00408 }
00409 case OP_LOADK: {
00410 setobj2s(L, ra, KBx(i));
00411 continue;
00412 }
00413 case OP_LOADBOOL: {
00414 setbvalue(ra, GETARG_B(i));
00415 if (GETARG_C(i)) pc++;
00416 continue;
00417 }
00418 case OP_LOADNIL: {
00419 TValue *rb = RB(i);
00420 do {
00421 setnilvalue(rb--);
00422 } while (rb >= ra);
00423 continue;
00424 }
00425 case OP_GETUPVAL: {
00426 int b = GETARG_B(i);
00427 setobj2s(L, ra, cl->upvals[b]->v);
00428 continue;
00429 }
00430 case OP_GETGLOBAL: {
00431 TValue g;
00432 TValue *rb = KBx(i);
00433 sethvalue(L, &g, cl->env);
00434 lua_assert(ttisstring(rb));
00435 Protect(luaV_gettable(L, &g, rb, ra));
00436 continue;
00437 }
00438 case OP_GETTABLE: {
00439 Protect(luaV_gettable(L, RB(i), RKC(i), ra));
00440 continue;
00441 }
00442 case OP_SETGLOBAL: {
00443 TValue g;
00444 sethvalue(L, &g, cl->env);
00445 lua_assert(ttisstring(KBx(i)));
00446 Protect(luaV_settable(L, &g, KBx(i), ra));
00447 continue;
00448 }
00449 case OP_SETUPVAL: {
00450 UpVal *uv = cl->upvals[GETARG_B(i)];
00451 setobj(L, uv->v, ra);
00452 luaC_barrier(L, uv, ra);
00453 continue;
00454 }
00455 case OP_SETTABLE: {
00456 Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
00457 continue;
00458 }
00459 case OP_NEWTABLE: {
00460 int b = GETARG_B(i);
00461 int c = GETARG_C(i);
00462 sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
00463 Protect(luaC_checkGC(L));
00464 continue;
00465 }
00466 case OP_SELF: {
00467 StkId rb = RB(i);
00468 setobjs2s(L, ra+1, rb);
00469 Protect(luaV_gettable(L, rb, RKC(i), ra));
00470 continue;
00471 }
00472 case OP_ADD: {
00473 arith_op(luai_numadd, TM_ADD);
00474 continue;
00475 }
00476 case OP_SUB: {
00477 arith_op(luai_numsub, TM_SUB);
00478 continue;
00479 }
00480 case OP_MUL: {
00481 arith_op(luai_nummul, TM_MUL);
00482 continue;
00483 }
00484 case OP_DIV: {
00485 arith_op(luai_numdiv, TM_DIV);
00486 continue;
00487 }
00488 case OP_MOD: {
00489 arith_op(luai_nummod, TM_MOD);
00490 continue;
00491 }
00492 case OP_POW: {
00493 arith_op(luai_numpow, TM_POW);
00494 continue;
00495 }
00496 case OP_UNM: {
00497 TValue *rb = RB(i);
00498 if (ttisnumber(rb)) {
00499 lua_Number nb = nvalue(rb);
00500 setnvalue(ra, luai_numunm(nb));
00501 }
00502 else {
00503 Protect(Arith(L, ra, rb, rb, TM_UNM));
00504 }
00505 continue;
00506 }
00507 case OP_NOT: {
00508 int res = l_isfalse(RB(i));
00509 setbvalue(ra, res);
00510 continue;
00511 }
00512 case OP_LEN: {
00513 const TValue *rb = RB(i);
00514 switch (ttype(rb)) {
00515 case LUA_TTABLE: {
00516 setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
00517 break;
00518 }
00519 case LUA_TSTRING: {
00520 setnvalue(ra, cast_num(tsvalue(rb)->len));
00521 break;
00522 }
00523 default: {
00524 Protect(
00525 if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN))
00526 luaG_typeerror(L, rb, "get length of");
00527 )
00528 }
00529 }
00530 continue;
00531 }
00532 case OP_CONCAT: {
00533 int b = GETARG_B(i);
00534 int c = GETARG_C(i);
00535 Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
00536 setobjs2s(L, RA(i), base+b);
00537 continue;
00538 }
00539 case OP_JMP: {
00540 dojump(L, pc, GETARG_sBx(i));
00541 continue;
00542 }
00543 case OP_EQ: {
00544 TValue *rb = RKB(i);
00545 TValue *rc = RKC(i);
00546 Protect(
00547 if (equalobj(L, rb, rc) == GETARG_A(i))
00548 dojump(L, pc, GETARG_sBx(*pc));
00549 )
00550 pc++;
00551 continue;
00552 }
00553 case OP_LT: {
00554 Protect(
00555 if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
00556 dojump(L, pc, GETARG_sBx(*pc));
00557 )
00558 pc++;
00559 continue;
00560 }
00561 case OP_LE: {
00562 Protect(
00563 if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
00564 dojump(L, pc, GETARG_sBx(*pc));
00565 )
00566 pc++;
00567 continue;
00568 }
00569 case OP_TEST: {
00570 if (l_isfalse(ra) != GETARG_C(i))
00571 dojump(L, pc, GETARG_sBx(*pc));
00572 pc++;
00573 continue;
00574 }
00575 case OP_TESTSET: {
00576 TValue *rb = RB(i);
00577 if (l_isfalse(rb) != GETARG_C(i)) {
00578 setobjs2s(L, ra, rb);
00579 dojump(L, pc, GETARG_sBx(*pc));
00580 }
00581 pc++;
00582 continue;
00583 }
00584 case OP_CALL: {
00585 int b = GETARG_B(i);
00586 int nresults = GETARG_C(i) - 1;
00587 if (b != 0) L->top = ra+b;
00588 L->savedpc = pc;
00589 switch (luaD_precall(L, ra, nresults)) {
00590 case PCRLUA: {
00591 nexeccalls++;
00592 goto reentry;
00593 }
00594 case PCRC: {
00595
00596 if (nresults >= 0) L->top = L->ci->top;
00597 base = L->base;
00598 continue;
00599 }
00600 default: {
00601 return;
00602 }
00603 }
00604 }
00605 case OP_TAILCALL: {
00606 int b = GETARG_B(i);
00607 if (b != 0) L->top = ra+b;
00608 L->savedpc = pc;
00609 lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
00610 switch (luaD_precall(L, ra, LUA_MULTRET)) {
00611 case PCRLUA: {
00612
00613 CallInfo *ci = L->ci - 1;
00614 int aux;
00615 StkId func = ci->func;
00616 StkId pfunc = (ci+1)->func;
00617 if (L->openupval) luaF_close(L, ci->base);
00618 L->base = ci->base = ci->func + ((ci+1)->base - pfunc);
00619 for (aux = 0; pfunc+aux < L->top; aux++)
00620 setobjs2s(L, func+aux, pfunc+aux);
00621 ci->top = L->top = func+aux;
00622 lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
00623 ci->savedpc = L->savedpc;
00624 ci->tailcalls++;
00625 L->ci--;
00626 goto reentry;
00627 }
00628 case PCRC: {
00629 base = L->base;
00630 continue;
00631 }
00632 default: {
00633 return;
00634 }
00635 }
00636 }
00637 case OP_RETURN: {
00638 int b = GETARG_B(i);
00639 if (b != 0) L->top = ra+b-1;
00640 if (L->openupval) luaF_close(L, base);
00641 L->savedpc = pc;
00642 b = luaD_poscall(L, ra);
00643 if (--nexeccalls == 0)
00644 return;
00645 else {
00646 if (b) L->top = L->ci->top;
00647 lua_assert(isLua(L->ci));
00648 lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL);
00649 goto reentry;
00650 }
00651 }
00652 case OP_FORLOOP: {
00653 lua_Number step = nvalue(ra+2);
00654 lua_Number idx = luai_numadd(nvalue(ra), step);
00655 lua_Number limit = nvalue(ra+1);
00656 if (luai_numlt(0, step) ? luai_numle(idx, limit)
00657 : luai_numle(limit, idx)) {
00658 dojump(L, pc, GETARG_sBx(i));
00659 setnvalue(ra, idx);
00660 setnvalue(ra+3, idx);
00661 }
00662 continue;
00663 }
00664 case OP_FORPREP: {
00665 const TValue *init = ra;
00666 const TValue *plimit = ra+1;
00667 const TValue *pstep = ra+2;
00668 L->savedpc = pc;
00669 if (!tonumber(init, ra))
00670 luaG_runerror(L, LUA_QL("for") " initial value must be a number");
00671 else if (!tonumber(plimit, ra+1))
00672 luaG_runerror(L, LUA_QL("for") " limit must be a number");
00673 else if (!tonumber(pstep, ra+2))
00674 luaG_runerror(L, LUA_QL("for") " step must be a number");
00675 setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
00676 dojump(L, pc, GETARG_sBx(i));
00677 continue;
00678 }
00679 case OP_TFORLOOP: {
00680 StkId cb = ra + 3;
00681 setobjs2s(L, cb+2, ra+2);
00682 setobjs2s(L, cb+1, ra+1);
00683 setobjs2s(L, cb, ra);
00684 L->top = cb+3;
00685 Protect(luaD_call(L, cb, GETARG_C(i)));
00686 L->top = L->ci->top;
00687 cb = RA(i) + 3;
00688 if (!ttisnil(cb)) {
00689 setobjs2s(L, cb-1, cb);
00690 dojump(L, pc, GETARG_sBx(*pc));
00691 }
00692 pc++;
00693 continue;
00694 }
00695 case OP_SETLIST: {
00696 int n = GETARG_B(i);
00697 int c = GETARG_C(i);
00698 int last;
00699 Table *h;
00700 if (n == 0) {
00701 n = cast_int(L->top - ra) - 1;
00702 L->top = L->ci->top;
00703 }
00704 if (c == 0) c = cast_int(*pc++);
00705 runtime_check(L, ttistable(ra));
00706 h = hvalue(ra);
00707 last = ((c-1)*LFIELDS_PER_FLUSH) + n;
00708 if (last > h->sizearray)
00709 luaH_resizearray(L, h, last);
00710 for (; n > 0; n--) {
00711 TValue *val = ra+n;
00712 setobj2t(L, luaH_setnum(L, h, last--), val);
00713 luaC_barriert(L, h, val);
00714 }
00715 continue;
00716 }
00717 case OP_CLOSE: {
00718 luaF_close(L, ra);
00719 continue;
00720 }
00721 case OP_CLOSURE: {
00722 Proto *p;
00723 Closure *ncl;
00724 int nup, j;
00725 p = cl->p->p[GETARG_Bx(i)];
00726 nup = p->nups;
00727 ncl = luaF_newLclosure(L, nup, cl->env);
00728 ncl->l.p = p;
00729 for (j=0; j<nup; j++, pc++) {
00730 if (GET_OPCODE(*pc) == OP_GETUPVAL)
00731 ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
00732 else {
00733 lua_assert(GET_OPCODE(*pc) == OP_MOVE);
00734 ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
00735 }
00736 }
00737 setclvalue(L, ra, ncl);
00738 Protect(luaC_checkGC(L));
00739 continue;
00740 }
00741 case OP_VARARG: {
00742 int b = GETARG_B(i) - 1;
00743 int j;
00744 CallInfo *ci = L->ci;
00745 int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1;
00746 if (b == LUA_MULTRET) {
00747 Protect(luaD_checkstack(L, n));
00748 ra = RA(i);
00749 b = n;
00750 L->top = ra + n;
00751 }
00752 for (j = 0; j < b; j++) {
00753 if (j < n) {
00754 setobjs2s(L, ra + j, ci->base - n + j);
00755 }
00756 else {
00757 setnilvalue(ra + j);
00758 }
00759 }
00760 continue;
00761 }
00762 }
00763 }
00764 }
00765