Fix hang/crash when requesting ZPOOL with incorrect backing device
[grub2/phcoder.git] / script / lua / lvm.c
blob0ff1fa04828863ac21fba4c56a2497d62070857b
1 /*
2 ** $Id: lvm.c,v 2.63.1.3 2007/12/28 15:32:23 roberto Exp $
3 ** Lua virtual machine
4 ** See Copyright Notice in lua.h
5 */
7 #if 0
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #endif
13 #define lvm_c
14 #define LUA_CORE
16 #include "lua.h"
18 #include "ldebug.h"
19 #include "ldo.h"
20 #include "lfunc.h"
21 #include "lgc.h"
22 #include "lobject.h"
23 #include "lopcodes.h"
24 #include "lstate.h"
25 #include "lstring.h"
26 #include "ltable.h"
27 #include "ltm.h"
28 #include "lvm.h"
32 /* limit for table tag-method chains (to avoid loops) */
33 #define MAXTAGLOOP 100
36 const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
37 lua_Number num;
38 if (ttisnumber(obj)) return obj;
39 if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
40 setnvalue(n, num);
41 return n;
43 else
44 return NULL;
48 int luaV_tostring (lua_State *L, StkId obj) {
49 if (!ttisnumber(obj))
50 return 0;
51 else {
52 char s[LUAI_MAXNUMBER2STR];
53 lua_Number n = nvalue(obj);
54 lua_number2str(s, n);
55 setsvalue2s(L, obj, luaS_new(L, s));
56 return 1;
61 static void traceexec (lua_State *L, const Instruction *pc) {
62 lu_byte mask = L->hookmask;
63 const Instruction *oldpc = L->savedpc;
64 L->savedpc = pc;
65 if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) {
66 resethookcount(L);
67 luaD_callhook(L, LUA_HOOKCOUNT, -1);
69 if (mask & LUA_MASKLINE) {
70 Proto *p = ci_func(L->ci)->l.p;
71 int npc = pcRel(pc, p);
72 int newline = getline(p, npc);
73 /* call linehook when enter a new function, when jump back (loop),
74 or when enter a new line */
75 if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p)))
76 luaD_callhook(L, LUA_HOOKLINE, newline);
81 static void callTMres (lua_State *L, StkId res, const TValue *f,
82 const TValue *p1, const TValue *p2) {
83 ptrdiff_t result = savestack(L, res);
84 setobj2s(L, L->top, f); /* push function */
85 setobj2s(L, L->top+1, p1); /* 1st argument */
86 setobj2s(L, L->top+2, p2); /* 2nd argument */
87 luaD_checkstack(L, 3);
88 L->top += 3;
89 luaD_call(L, L->top - 3, 1);
90 res = restorestack(L, result);
91 L->top--;
92 setobjs2s(L, res, L->top);
97 static void callTM (lua_State *L, const TValue *f, const TValue *p1,
98 const TValue *p2, const TValue *p3) {
99 setobj2s(L, L->top, f); /* push function */
100 setobj2s(L, L->top+1, p1); /* 1st argument */
101 setobj2s(L, L->top+2, p2); /* 2nd argument */
102 setobj2s(L, L->top+3, p3); /* 3th argument */
103 luaD_checkstack(L, 4);
104 L->top += 4;
105 luaD_call(L, L->top - 4, 0);
109 void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
110 int loop;
111 for (loop = 0; loop < MAXTAGLOOP; loop++) {
112 const TValue *tm;
113 if (ttistable(t)) { /* `t' is a table? */
114 Table *h = hvalue(t);
115 const TValue *res = luaH_get(h, key); /* do a primitive get */
116 if (!ttisnil(res) || /* result is no nil? */
117 (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
118 setobj2s(L, val, res);
119 return;
121 /* else will try the tag method */
123 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
124 luaG_typeerror(L, t, "index");
125 if (ttisfunction(tm)) {
126 callTMres(L, val, tm, t, key);
127 return;
129 t = tm; /* else repeat with `tm' */
131 luaG_runerror(L, "loop in gettable");
135 void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
136 int loop;
137 for (loop = 0; loop < MAXTAGLOOP; loop++) {
138 const TValue *tm;
139 if (ttistable(t)) { /* `t' is a table? */
140 Table *h = hvalue(t);
141 TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
142 if (!ttisnil(oldval) || /* result is no nil? */
143 (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
144 setobj2t(L, oldval, val);
145 luaC_barriert(L, h, val);
146 return;
148 /* else will try the tag method */
150 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
151 luaG_typeerror(L, t, "index");
152 if (ttisfunction(tm)) {
153 callTM(L, tm, t, key, val);
154 return;
156 t = tm; /* else repeat with `tm' */
158 luaG_runerror(L, "loop in settable");
162 static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
163 StkId res, TMS event) {
164 const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
165 if (ttisnil(tm))
166 tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
167 if (ttisnil(tm)) return 0;
168 callTMres(L, res, tm, p1, p2);
169 return 1;
173 static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2,
174 TMS event) {
175 const TValue *tm1 = fasttm(L, mt1, event);
176 const TValue *tm2;
177 if (tm1 == NULL) return NULL; /* no metamethod */
178 if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
179 tm2 = fasttm(L, mt2, event);
180 if (tm2 == NULL) return NULL; /* no metamethod */
181 if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */
182 return tm1;
183 return NULL;
187 static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
188 TMS event) {
189 const TValue *tm1 = luaT_gettmbyobj(L, p1, event);
190 const TValue *tm2;
191 if (ttisnil(tm1)) return -1; /* no metamethod? */
192 tm2 = luaT_gettmbyobj(L, p2, event);
193 if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */
194 return -1;
195 callTMres(L, L->top, tm1, p1, p2);
196 return !l_isfalse(L->top);
200 static int l_strcmp (const TString *ls, const TString *rs) {
201 const char *l = getstr(ls);
202 size_t ll = ls->tsv.len;
203 const char *r = getstr(rs);
204 size_t lr = rs->tsv.len;
205 for (;;) {
206 int temp = strcoll(l, r);
207 if (temp != 0) return temp;
208 else { /* strings are equal up to a `\0' */
209 size_t len = strlen(l); /* index of first `\0' in both strings */
210 if (len == lr) /* r is finished? */
211 return (len == ll) ? 0 : 1;
212 else if (len == ll) /* l is finished? */
213 return -1; /* l is smaller than r (because r is not finished) */
214 /* both strings longer than `len'; go on comparing (after the `\0') */
215 len++;
216 l += len; ll -= len; r += len; lr -= len;
222 int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
223 int res;
224 if (ttype(l) != ttype(r))
225 return luaG_ordererror(L, l, r);
226 else if (ttisnumber(l))
227 return luai_numlt(nvalue(l), nvalue(r));
228 else if (ttisstring(l))
229 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
230 else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
231 return res;
232 return luaG_ordererror(L, l, r);
236 static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
237 int res;
238 if (ttype(l) != ttype(r))
239 return luaG_ordererror(L, l, r);
240 else if (ttisnumber(l))
241 return luai_numle(nvalue(l), nvalue(r));
242 else if (ttisstring(l))
243 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
244 else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
245 return res;
246 else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */
247 return !res;
248 return luaG_ordererror(L, l, r);
252 int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
253 const TValue *tm;
254 lua_assert(ttype(t1) == ttype(t2));
255 switch (ttype(t1)) {
256 case LUA_TNIL: return 1;
257 case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
258 case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
259 case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
260 case LUA_TUSERDATA: {
261 if (uvalue(t1) == uvalue(t2)) return 1;
262 tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
263 TM_EQ);
264 break; /* will try TM */
266 case LUA_TTABLE: {
267 if (hvalue(t1) == hvalue(t2)) return 1;
268 tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
269 break; /* will try TM */
271 default: return gcvalue(t1) == gcvalue(t2);
273 if (tm == NULL) return 0; /* no TM? */
274 callTMres(L, L->top, tm, t1, t2); /* call TM */
275 return !l_isfalse(L->top);
279 void luaV_concat (lua_State *L, int total, int last) {
280 do {
281 StkId top = L->base + last + 1;
282 int n = 2; /* number of elements handled in this pass (at least 2) */
283 if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
284 if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
285 luaG_concaterror(L, top-2, top-1);
286 } else if (tsvalue(top-1)->len == 0) /* second op is empty? */
287 (void)tostring(L, top - 2); /* result is first op (as string) */
288 else {
289 /* at least two string values; get as many as possible */
290 size_t tl = tsvalue(top-1)->len;
291 char *buffer;
292 int i;
293 /* collect total length */
294 for (n = 1; n < total && tostring(L, top-n-1); n++) {
295 size_t l = tsvalue(top-n-1)->len;
296 if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
297 tl += l;
299 buffer = luaZ_openspace(L, &G(L)->buff, tl);
300 tl = 0;
301 for (i=n; i>0; i--) { /* concat all strings */
302 size_t l = tsvalue(top-i)->len;
303 memcpy(buffer+tl, svalue(top-i), l);
304 tl += l;
306 setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
308 total -= n-1; /* got `n' strings to create 1 new */
309 last -= n-1;
310 } while (total > 1); /* repeat until only 1 result left */
314 static void Arith (lua_State *L, StkId ra, const TValue *rb,
315 const TValue *rc, TMS op) {
316 TValue tempb, tempc;
317 const TValue *b, *c;
318 if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
319 (c = luaV_tonumber(rc, &tempc)) != NULL) {
320 lua_Number nb = nvalue(b), nc = nvalue(c);
321 switch (op) {
322 case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
323 case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
324 case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
325 case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
326 case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
327 case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
328 case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
329 default: lua_assert(0); break;
332 else if (!call_binTM(L, rb, rc, ra, op))
333 luaG_aritherror(L, rb, rc);
339 ** some macros for common tasks in `luaV_execute'
342 #define runtime_check(L, c) { if (!(c)) break; }
344 #define RA(i) (base+GETARG_A(i))
345 /* to be used after possible stack reallocation */
346 #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
347 #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
348 #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
349 ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
350 #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
351 ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
352 #define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
355 #define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);}
358 #define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
361 #define arith_op(op,tm) { \
362 TValue *rb = RKB(i); \
363 TValue *rc = RKC(i); \
364 if (ttisnumber(rb) && ttisnumber(rc)) { \
365 lua_Number nb = nvalue(rb), nc = nvalue(rc); \
366 setnvalue(ra, op(nb, nc)); \
368 else \
369 Protect(Arith(L, ra, rb, rc, tm)); \
374 void luaV_execute (lua_State *L, int nexeccalls) {
375 LClosure *cl;
376 StkId base;
377 TValue *k;
378 const Instruction *pc;
379 reentry: /* entry point */
380 lua_assert(isLua(L->ci));
381 pc = L->savedpc;
382 cl = &clvalue(L->ci->func)->l;
383 base = L->base;
384 k = cl->p->k;
385 /* main loop of interpreter */
386 for (;;) {
387 const Instruction i = *pc++;
388 StkId ra;
389 if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
390 (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
391 traceexec(L, pc);
392 if (L->status == LUA_YIELD) { /* did hook yield? */
393 L->savedpc = pc - 1;
394 return;
396 base = L->base;
398 /* warning!! several calls may realloc the stack and invalidate `ra' */
399 ra = RA(i);
400 lua_assert(base == L->base && L->base == L->ci->base);
401 lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
402 lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
403 switch (GET_OPCODE(i)) {
404 case OP_MOVE: {
405 setobjs2s(L, ra, RB(i));
406 continue;
408 case OP_LOADK: {
409 setobj2s(L, ra, KBx(i));
410 continue;
412 case OP_LOADBOOL: {
413 setbvalue(ra, GETARG_B(i));
414 if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
415 continue;
417 case OP_LOADNIL: {
418 TValue *rb = RB(i);
419 do {
420 setnilvalue(rb--);
421 } while (rb >= ra);
422 continue;
424 case OP_GETUPVAL: {
425 int b = GETARG_B(i);
426 setobj2s(L, ra, cl->upvals[b]->v);
427 continue;
429 case OP_GETGLOBAL: {
430 TValue g;
431 TValue *rb = KBx(i);
432 sethvalue(L, &g, cl->env);
433 lua_assert(ttisstring(rb));
434 Protect(luaV_gettable(L, &g, rb, ra));
435 continue;
437 case OP_GETTABLE: {
438 Protect(luaV_gettable(L, RB(i), RKC(i), ra));
439 continue;
441 case OP_SETGLOBAL: {
442 TValue g;
443 sethvalue(L, &g, cl->env);
444 lua_assert(ttisstring(KBx(i)));
445 Protect(luaV_settable(L, &g, KBx(i), ra));
446 continue;
448 case OP_SETUPVAL: {
449 UpVal *uv = cl->upvals[GETARG_B(i)];
450 setobj(L, uv->v, ra);
451 luaC_barrier(L, uv, ra);
452 continue;
454 case OP_SETTABLE: {
455 Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
456 continue;
458 case OP_NEWTABLE: {
459 int b = GETARG_B(i);
460 int c = GETARG_C(i);
461 sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
462 Protect(luaC_checkGC(L));
463 continue;
465 case OP_SELF: {
466 StkId rb = RB(i);
467 setobjs2s(L, ra+1, rb);
468 Protect(luaV_gettable(L, rb, RKC(i), ra));
469 continue;
471 case OP_ADD: {
472 arith_op(luai_numadd, TM_ADD);
473 continue;
475 case OP_SUB: {
476 arith_op(luai_numsub, TM_SUB);
477 continue;
479 case OP_MUL: {
480 arith_op(luai_nummul, TM_MUL);
481 continue;
483 case OP_DIV: {
484 arith_op(luai_numdiv, TM_DIV);
485 continue;
487 case OP_MOD: {
488 arith_op(luai_nummod, TM_MOD);
489 continue;
491 case OP_POW: {
492 arith_op(luai_numpow, TM_POW);
493 continue;
495 case OP_UNM: {
496 TValue *rb = RB(i);
497 if (ttisnumber(rb)) {
498 lua_Number nb = nvalue(rb);
499 setnvalue(ra, luai_numunm(nb));
501 else {
502 Protect(Arith(L, ra, rb, rb, TM_UNM));
504 continue;
506 case OP_NOT: {
507 int res = l_isfalse(RB(i)); /* next assignment may change this value */
508 setbvalue(ra, res);
509 continue;
511 case OP_LEN: {
512 const TValue *rb = RB(i);
513 switch (ttype(rb)) {
514 case LUA_TTABLE: {
515 setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
516 break;
518 case LUA_TSTRING: {
519 setnvalue(ra, cast_num(tsvalue(rb)->len));
520 break;
522 default: { /* try metamethod */
523 Protect(
524 if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN))
525 luaG_typeerror(L, rb, "get length of");
529 continue;
531 case OP_CONCAT: {
532 int b = GETARG_B(i);
533 int c = GETARG_C(i);
534 Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
535 setobjs2s(L, RA(i), base+b);
536 continue;
538 case OP_JMP: {
539 dojump(L, pc, GETARG_sBx(i));
540 continue;
542 case OP_EQ: {
543 TValue *rb = RKB(i);
544 TValue *rc = RKC(i);
545 Protect(
546 if (equalobj(L, rb, rc) == GETARG_A(i))
547 dojump(L, pc, GETARG_sBx(*pc));
549 pc++;
550 continue;
552 case OP_LT: {
553 Protect(
554 if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
555 dojump(L, pc, GETARG_sBx(*pc));
557 pc++;
558 continue;
560 case OP_LE: {
561 Protect(
562 if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
563 dojump(L, pc, GETARG_sBx(*pc));
565 pc++;
566 continue;
568 case OP_TEST: {
569 if (l_isfalse(ra) != GETARG_C(i))
570 dojump(L, pc, GETARG_sBx(*pc));
571 pc++;
572 continue;
574 case OP_TESTSET: {
575 TValue *rb = RB(i);
576 if (l_isfalse(rb) != GETARG_C(i)) {
577 setobjs2s(L, ra, rb);
578 dojump(L, pc, GETARG_sBx(*pc));
580 pc++;
581 continue;
583 case OP_CALL: {
584 int b = GETARG_B(i);
585 int nresults = GETARG_C(i) - 1;
586 if (b != 0) L->top = ra+b; /* else previous instruction set top */
587 L->savedpc = pc;
588 switch (luaD_precall(L, ra, nresults)) {
589 case PCRLUA: {
590 nexeccalls++;
591 goto reentry; /* restart luaV_execute over new Lua function */
593 case PCRC: {
594 /* it was a C function (`precall' called it); adjust results */
595 if (nresults >= 0) L->top = L->ci->top;
596 base = L->base;
597 continue;
599 default: {
600 return; /* yield */
604 case OP_TAILCALL: {
605 int b = GETARG_B(i);
606 if (b != 0) L->top = ra+b; /* else previous instruction set top */
607 L->savedpc = pc;
608 lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
609 switch (luaD_precall(L, ra, LUA_MULTRET)) {
610 case PCRLUA: {
611 /* tail call: put new frame in place of previous one */
612 CallInfo *ci = L->ci - 1; /* previous frame */
613 int aux;
614 StkId func = ci->func;
615 StkId pfunc = (ci+1)->func; /* previous function index */
616 if (L->openupval) luaF_close(L, ci->base);
617 L->base = ci->base = ci->func + ((ci+1)->base - pfunc);
618 for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
619 setobjs2s(L, func+aux, pfunc+aux);
620 ci->top = L->top = func+aux; /* correct top */
621 lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
622 ci->savedpc = L->savedpc;
623 ci->tailcalls++; /* one more call lost */
624 L->ci--; /* remove new frame */
625 goto reentry;
627 case PCRC: { /* it was a C function (`precall' called it) */
628 base = L->base;
629 continue;
631 default: {
632 return; /* yield */
636 case OP_RETURN: {
637 int b = GETARG_B(i);
638 if (b != 0) L->top = ra+b-1;
639 if (L->openupval) luaF_close(L, base);
640 L->savedpc = pc;
641 b = luaD_poscall(L, ra);
642 if (--nexeccalls == 0) /* was previous function running `here'? */
643 return; /* no: return */
644 else { /* yes: continue its execution */
645 if (b) L->top = L->ci->top;
646 lua_assert(isLua(L->ci));
647 lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL);
648 goto reentry;
651 case OP_FORLOOP: {
652 lua_Number step = nvalue(ra+2);
653 lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
654 lua_Number limit = nvalue(ra+1);
655 if (luai_numlt(0, step) ? luai_numle(idx, limit)
656 : luai_numle(limit, idx)) {
657 dojump(L, pc, GETARG_sBx(i)); /* jump back */
658 setnvalue(ra, idx); /* update internal index... */
659 setnvalue(ra+3, idx); /* ...and external index */
661 continue;
663 case OP_FORPREP: {
664 const TValue *init = ra;
665 const TValue *plimit = ra+1;
666 const TValue *pstep = ra+2;
667 L->savedpc = pc; /* next steps may throw errors */
668 if (!tonumber(init, ra))
669 luaG_runerror(L, LUA_QL("for") " initial value must be a number");
670 else if (!tonumber(plimit, ra+1))
671 luaG_runerror(L, LUA_QL("for") " limit must be a number");
672 else if (!tonumber(pstep, ra+2))
673 luaG_runerror(L, LUA_QL("for") " step must be a number");
674 setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
675 dojump(L, pc, GETARG_sBx(i));
676 continue;
678 case OP_TFORLOOP: {
679 StkId cb = ra + 3; /* call base */
680 setobjs2s(L, cb+2, ra+2);
681 setobjs2s(L, cb+1, ra+1);
682 setobjs2s(L, cb, ra);
683 L->top = cb+3; /* func. + 2 args (state and index) */
684 Protect(luaD_call(L, cb, GETARG_C(i)));
685 L->top = L->ci->top;
686 cb = RA(i) + 3; /* previous call may change the stack */
687 if (!ttisnil(cb)) { /* continue loop? */
688 setobjs2s(L, cb-1, cb); /* save control variable */
689 dojump(L, pc, GETARG_sBx(*pc)); /* jump back */
691 pc++;
692 continue;
694 case OP_SETLIST: {
695 int n = GETARG_B(i);
696 int c = GETARG_C(i);
697 int last;
698 Table *h;
699 if (n == 0) {
700 n = cast_int(L->top - ra) - 1;
701 L->top = L->ci->top;
703 if (c == 0) c = cast_int(*pc++);
704 runtime_check(L, ttistable(ra));
705 h = hvalue(ra);
706 last = ((c-1)*LFIELDS_PER_FLUSH) + n;
707 if (last > h->sizearray) /* needs more space? */
708 luaH_resizearray(L, h, last); /* pre-alloc it at once */
709 for (; n > 0; n--) {
710 TValue *val = ra+n;
711 setobj2t(L, luaH_setnum(L, h, last--), val);
712 luaC_barriert(L, h, val);
714 continue;
716 case OP_CLOSE: {
717 luaF_close(L, ra);
718 continue;
720 case OP_CLOSURE: {
721 Proto *p;
722 Closure *ncl;
723 int nup, j;
724 p = cl->p->p[GETARG_Bx(i)];
725 nup = p->nups;
726 ncl = luaF_newLclosure(L, nup, cl->env);
727 ncl->l.p = p;
728 for (j=0; j<nup; j++, pc++) {
729 if (GET_OPCODE(*pc) == OP_GETUPVAL)
730 ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
731 else {
732 lua_assert(GET_OPCODE(*pc) == OP_MOVE);
733 ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
736 setclvalue(L, ra, ncl);
737 Protect(luaC_checkGC(L));
738 continue;
740 case OP_VARARG: {
741 int b = GETARG_B(i) - 1;
742 int j;
743 CallInfo *ci = L->ci;
744 int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1;
745 if (b == LUA_MULTRET) {
746 Protect(luaD_checkstack(L, n));
747 ra = RA(i); /* previous call may change the stack */
748 b = n;
749 L->top = ra + n;
751 for (j = 0; j < b; j++) {
752 if (j < n) {
753 setobjs2s(L, ra + j, ci->base - n + j);
755 else {
756 setnilvalue(ra + j);
759 continue;