use lua_mem(L, +-size) to track your additional userdata sizes. keep it balanced...
[blua.git] / src / lvm.c
blob305235768978b24ef37c53f90573fba78f36e88a
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 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
12 #define lvm_c
13 #define LUA_CORE
15 #include "lua.h"
17 #include "ldebug.h"
18 #include "ldo.h"
19 #include "lfunc.h"
20 #include "lgc.h"
21 #include "lobject.h"
22 #include "lopcodes.h"
23 #include "lstate.h"
24 #include "lstring.h"
25 #include "ltable.h"
26 #include "ltm.h"
27 #include "lvm.h"
31 /* limit for table tag-method chains (to avoid loops) */
32 #define MAXTAGLOOP 100
35 const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
36 lua_Number num;
37 if (ttisnumber(obj)) return obj;
38 if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
39 setnvalue(n, num);
40 return n;
42 else
43 return NULL;
47 int luaV_tostring (lua_State *L, StkId obj) {
48 if (!ttisnumber(obj))
49 return 0;
50 else {
51 char s[LUAI_MAXNUMBER2STR];
52 lua_Number n = nvalue(obj);
53 lua_number2str(s, n);
54 setsvalue2s(L, obj, luaS_new(L, s));
55 return 1;
60 static void traceexec (lua_State *L, const Instruction *pc) {
61 lu_byte mask = L->hookmask;
62 const Instruction *oldpc = L->savedpc;
63 L->savedpc = pc;
64 if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) {
65 resethookcount(L);
66 luaD_callhook(L, LUA_HOOKCOUNT, -1);
68 if (mask & LUA_MASKLINE) {
69 Proto *p = ci_func(L->ci)->l.p;
70 int npc = pcRel(pc, p);
71 int newline = getline(p, npc);
72 /* call linehook when enter a new function, when jump back (loop),
73 or when enter a new line */
74 if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p)))
75 luaD_callhook(L, LUA_HOOKLINE, newline);
80 static void callTMres (lua_State *L, StkId res, const TValue *f,
81 const TValue *p1, const TValue *p2) {
82 ptrdiff_t result = savestack(L, res);
83 setobj2s(L, L->top, f); /* push function */
84 setobj2s(L, L->top+1, p1); /* 1st argument */
85 setobj2s(L, L->top+2, p2); /* 2nd argument */
86 luaD_checkstack(L, 3);
87 L->top += 3;
88 luaD_call(L, L->top - 3, 1);
89 res = restorestack(L, result);
90 L->top--;
91 setobjs2s(L, res, L->top);
96 static void callTM (lua_State *L, const TValue *f, const TValue *p1,
97 const TValue *p2, const TValue *p3) {
98 setobj2s(L, L->top, f); /* push function */
99 setobj2s(L, L->top+1, p1); /* 1st argument */
100 setobj2s(L, L->top+2, p2); /* 2nd argument */
101 setobj2s(L, L->top+3, p3); /* 3th argument */
102 luaD_checkstack(L, 4);
103 L->top += 4;
104 luaD_call(L, L->top - 4, 0);
108 void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
109 int loop;
110 for (loop = 0; loop < MAXTAGLOOP; loop++) {
111 const TValue *tm;
112 if (ttistable(t)) { /* `t' is a table? */
113 Table *h = hvalue(t);
114 const TValue *res = luaH_get(h, key); /* do a primitive get */
115 if (!ttisnil(res) || /* result is no nil? */
116 (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
117 setobj2s(L, val, res);
118 return;
120 /* else will try the tag method */
122 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
123 luaG_typeerror(L, t, "index");
124 if (ttisfunction(tm)) {
125 callTMres(L, val, tm, t, key);
126 return;
128 t = tm; /* else repeat with `tm' */
130 luaG_runerror(L, "loop in gettable");
134 void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
135 int loop;
136 for (loop = 0; loop < MAXTAGLOOP; loop++) {
137 const TValue *tm;
138 if (ttistable(t)) { /* `t' is a table? */
139 Table *h = hvalue(t);
140 TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
141 /* oldval is nil=> look for newindex, oldval is not nil => look for usedindex */
142 if (!((ttisnil(oldval) && (tm = fasttm(L, h->metatable, TM_NEWINDEX))) ||
143 ((!ttisnil(oldval)) && (tm = fasttm(L, h->metatable, TM_USEDINDEX))))) {
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 case TM_AND: setnvalue(ra, luai_numand(nb, nc)); break;
330 case TM_OR: setnvalue(ra, luai_numor(nb, nc)); break;
331 case TM_XOR: setnvalue(ra, luai_numxor(nb, nc)); break;
332 case TM_SHL: setnvalue(ra, luai_numshl(nb, nc)); break;
333 case TM_SHR: setnvalue(ra, luai_numshr(nb, nc)); break;
334 case TM_NOT: setnvalue(ra, luai_numnot(nb)); break;
335 default: lua_assert(0); break;
338 else if (!call_binTM(L, rb, rc, ra, op))
339 luaG_aritherror(L, rb, rc);
345 ** some macros for common tasks in `luaV_execute'
348 #define runtime_check(L, c) { if (!(c)) break; }
350 #define RA(i) (base+GETARG_A(i))
351 /* to be used after possible stack reallocation */
352 #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
353 #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
354 #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
355 ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
356 #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
357 ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
358 #define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
361 #define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);}
364 #define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
367 #define arith_op(op,tm) { \
368 TValue *rb = RKB(i); \
369 TValue *rc = RKC(i); \
370 if (ttisnumber(rb) && ttisnumber(rc)) { \
371 lua_Number nb = nvalue(rb), nc = nvalue(rc); \
372 setnvalue(ra, op(nb, nc)); \
374 else \
375 Protect(Arith(L, ra, rb, rc, tm)); \
380 void luaV_execute (lua_State *L, int nexeccalls) {
381 LClosure *cl;
382 StkId base;
383 TValue *k;
384 const Instruction *pc;
385 reentry: /* entry point */
386 lua_assert(isLua(L->ci));
387 pc = L->savedpc;
388 cl = &clvalue(L->ci->func)->l;
389 base = L->base;
390 k = cl->p->k;
391 /* main loop of interpreter */
392 for (;;) {
393 const Instruction i = *pc++;
394 StkId ra;
395 if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
396 (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
397 traceexec(L, pc);
398 if (L->status == LUA_YIELD) { /* did hook yield? */
399 L->savedpc = pc - 1;
400 return;
402 base = L->base;
404 /* warning!! several calls may realloc the stack and invalidate `ra' */
405 ra = RA(i);
406 lua_assert(base == L->base && L->base == L->ci->base);
407 lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
408 lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
409 switch (GET_OPCODE(i)) {
410 case OP_MOVE: {
411 setobjs2s(L, ra, RB(i));
412 continue;
414 case OP_LOADK: {
415 setobj2s(L, ra, KBx(i));
416 continue;
418 case OP_LOADBOOL: {
419 setbvalue(ra, GETARG_B(i));
420 if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
421 continue;
423 case OP_LOADNIL: {
424 TValue *rb = RB(i);
425 do {
426 setnilvalue(rb--);
427 } while (rb >= ra);
428 continue;
430 case OP_GETUPVAL: {
431 int b = GETARG_B(i);
432 setobj2s(L, ra, cl->upvals[b]->v);
433 continue;
435 case OP_GETGLOBAL: {
436 TValue g;
437 TValue *rb = KBx(i);
438 sethvalue(L, &g, cl->env);
439 lua_assert(ttisstring(rb));
440 Protect(luaV_gettable(L, &g, rb, ra));
441 continue;
443 case OP_GETTABLE: {
444 Protect(luaV_gettable(L, RB(i), RKC(i), ra));
445 continue;
447 case OP_SETGLOBAL: {
448 TValue g;
449 sethvalue(L, &g, cl->env);
450 lua_assert(ttisstring(KBx(i)));
451 Protect(luaV_settable(L, &g, KBx(i), ra));
452 continue;
454 case OP_SETUPVAL: {
455 UpVal *uv = cl->upvals[GETARG_B(i)];
456 setobj(L, uv->v, ra);
457 luaC_barrier(L, uv, ra);
458 continue;
460 case OP_SETTABLE: {
461 Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
462 continue;
464 case OP_NEWTABLE: {
465 int b = GETARG_B(i);
466 int c = GETARG_C(i);
467 sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
468 Protect(luaC_checkGC(L));
469 continue;
471 case OP_SELF: {
472 StkId rb = RB(i);
473 setobjs2s(L, ra+1, rb);
474 Protect(luaV_gettable(L, rb, RKC(i), ra));
475 continue;
477 case OP_ADD: {
478 arith_op(luai_numadd, TM_ADD);
479 continue;
481 case OP_SUB: {
482 arith_op(luai_numsub, TM_SUB);
483 continue;
485 case OP_MUL: {
486 arith_op(luai_nummul, TM_MUL);
487 continue;
489 case OP_DIV: {
490 arith_op(luai_numdiv, TM_DIV);
491 continue;
493 case OP_MOD: {
494 arith_op(luai_nummod, TM_MOD);
495 continue;
497 case OP_POW: {
498 arith_op(luai_numpow, TM_POW);
499 continue;
501 case OP_BAND: {
502 arith_op(luai_numand, TM_AND);
503 continue;
505 case OP_BOR: {
506 arith_op(luai_numor, TM_OR);
507 continue;
509 case OP_BXOR: {
510 arith_op(luai_numxor, TM_XOR);
511 continue;
513 case OP_BSHL: {
514 arith_op(luai_numshl, TM_SHL);
515 continue;
517 case OP_BSHR: {
518 arith_op(luai_numshr, TM_SHR);
519 continue;
521 case OP_BNOT: {
522 TValue *rb = RB(i);
523 if (ttisnumber(rb)) {
524 lua_Number nb = nvalue(rb);
525 setnvalue(ra, luai_numnot(nb));
527 else {
528 Protect(Arith(L, ra, rb, rb, TM_NOT));
530 continue;
532 case OP_UNM: {
533 TValue *rb = RB(i);
534 if (ttisnumber(rb)) {
535 lua_Number nb = nvalue(rb);
536 setnvalue(ra, luai_numunm(nb));
538 else {
539 Protect(Arith(L, ra, rb, rb, TM_UNM));
541 continue;
543 case OP_NOT: {
544 int res = l_isfalse(RB(i)); /* next assignment may change this value */
545 setbvalue(ra, res);
546 continue;
548 case OP_LEN: {
549 const TValue *rb = RB(i);
550 switch (ttype(rb)) {
551 case LUA_TTABLE: {
552 setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
553 break;
555 case LUA_TSTRING: {
556 setnvalue(ra, cast_num(tsvalue(rb)->len));
557 break;
559 default: { /* try metamethod */
560 Protect(
561 if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN))
562 luaG_typeerror(L, rb, "get length of");
566 continue;
568 case OP_CONCAT: {
569 int b = GETARG_B(i);
570 int c = GETARG_C(i);
571 Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
572 setobjs2s(L, RA(i), base+b);
573 continue;
575 case OP_JMP: {
576 dojump(L, pc, GETARG_sBx(i));
577 continue;
579 case OP_EQ: {
580 TValue *rb = RKB(i);
581 TValue *rc = RKC(i);
582 Protect(
583 if (equalobj(L, rb, rc) == GETARG_A(i))
584 dojump(L, pc, GETARG_sBx(*pc));
586 pc++;
587 continue;
589 case OP_LT: {
590 Protect(
591 if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
592 dojump(L, pc, GETARG_sBx(*pc));
594 pc++;
595 continue;
597 case OP_LE: {
598 Protect(
599 if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
600 dojump(L, pc, GETARG_sBx(*pc));
602 pc++;
603 continue;
605 case OP_TEST: {
606 if (l_isfalse(ra) != GETARG_C(i))
607 dojump(L, pc, GETARG_sBx(*pc));
608 pc++;
609 continue;
611 case OP_TESTSET: {
612 TValue *rb = RB(i);
613 if (l_isfalse(rb) != GETARG_C(i)) {
614 setobjs2s(L, ra, rb);
615 dojump(L, pc, GETARG_sBx(*pc));
617 pc++;
618 continue;
620 case OP_CALL: {
621 int b = GETARG_B(i);
622 int nresults = GETARG_C(i) - 1;
623 if (b != 0) L->top = ra+b; /* else previous instruction set top */
624 L->savedpc = pc;
625 switch (luaD_precall(L, ra, nresults)) {
626 case PCRLUA: {
627 nexeccalls++;
628 goto reentry; /* restart luaV_execute over new Lua function */
630 case PCRC: {
631 /* it was a C function (`precall' called it); adjust results */
632 if (nresults >= 0) L->top = L->ci->top;
633 base = L->base;
634 continue;
636 default: {
637 return; /* yield */
641 case OP_TAILCALL: {
642 int b = GETARG_B(i);
643 if (b != 0) L->top = ra+b; /* else previous instruction set top */
644 L->savedpc = pc;
645 lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
646 switch (luaD_precall(L, ra, LUA_MULTRET)) {
647 case PCRLUA: {
648 /* tail call: put new frame in place of previous one */
649 CallInfo *ci = L->ci - 1; /* previous frame */
650 int aux;
651 StkId func = ci->func;
652 StkId pfunc = (ci+1)->func; /* previous function index */
653 if (L->openupval) luaF_close(L, ci->base);
654 L->base = ci->base = ci->func + ((ci+1)->base - pfunc);
655 for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
656 setobjs2s(L, func+aux, pfunc+aux);
657 ci->top = L->top = func+aux; /* correct top */
658 lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
659 ci->savedpc = L->savedpc;
660 ci->tailcalls++; /* one more call lost */
661 L->ci--; /* remove new frame */
662 goto reentry;
664 case PCRC: { /* it was a C function (`precall' called it) */
665 base = L->base;
666 continue;
668 default: {
669 return; /* yield */
673 case OP_RETURN: {
674 int b = GETARG_B(i);
675 if (b != 0) L->top = ra+b-1;
676 if (L->openupval) luaF_close(L, base);
677 L->savedpc = pc;
678 b = luaD_poscall(L, ra);
679 if (--nexeccalls == 0) /* was previous function running `here'? */
680 return; /* no: return */
681 else { /* yes: continue its execution */
682 if (b) L->top = L->ci->top;
683 lua_assert(isLua(L->ci));
684 lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL);
685 goto reentry;
688 case OP_FORLOOP: {
689 lua_Number step = nvalue(ra+2);
690 lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
691 lua_Number limit = nvalue(ra+1);
692 if (luai_numlt(0, step) ? luai_numle(idx, limit)
693 : luai_numle(limit, idx)) {
694 dojump(L, pc, GETARG_sBx(i)); /* jump back */
695 setnvalue(ra, idx); /* update internal index... */
696 setnvalue(ra+3, idx); /* ...and external index */
698 continue;
700 case OP_FORPREP: {
701 const TValue *init = ra;
702 const TValue *plimit = ra+1;
703 const TValue *pstep = ra+2;
704 L->savedpc = pc; /* next steps may throw errors */
705 if (!tonumber(init, ra))
706 luaG_runerror(L, LUA_QL("for") " initial value must be a number");
707 else if (!tonumber(plimit, ra+1))
708 luaG_runerror(L, LUA_QL("for") " limit must be a number");
709 else if (!tonumber(pstep, ra+2))
710 luaG_runerror(L, LUA_QL("for") " step must be a number");
711 setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
712 dojump(L, pc, GETARG_sBx(i));
713 continue;
715 case OP_TFORLOOP: {
716 StkId cb = ra + 3; /* call base */
717 setobjs2s(L, cb+2, ra+2);
718 setobjs2s(L, cb+1, ra+1);
719 setobjs2s(L, cb, ra);
720 L->top = cb+3; /* func. + 2 args (state and index) */
721 Protect(luaD_call(L, cb, GETARG_C(i)));
722 L->top = L->ci->top;
723 cb = RA(i) + 3; /* previous call may change the stack */
724 if (!ttisnil(cb)) { /* continue loop? */
725 setobjs2s(L, cb-1, cb); /* save control variable */
726 dojump(L, pc, GETARG_sBx(*pc)); /* jump back */
728 pc++;
729 continue;
731 case OP_SETLIST: {
732 int n = GETARG_B(i);
733 int c = GETARG_C(i);
734 int last;
735 Table *h;
736 if (n == 0) {
737 n = cast_int(L->top - ra) - 1;
738 L->top = L->ci->top;
740 if (c == 0) c = cast_int(*pc++);
741 runtime_check(L, ttistable(ra));
742 h = hvalue(ra);
743 last = ((c-1)*LFIELDS_PER_FLUSH) + n;
744 if (last > h->sizearray) /* needs more space? */
745 luaH_resizearray(L, h, last); /* pre-alloc it at once */
746 for (; n > 0; n--) {
747 TValue *val = ra+n;
748 setobj2t(L, luaH_setnum(L, h, last--), val);
749 luaC_barriert(L, h, val);
751 continue;
753 case OP_CLOSE: {
754 luaF_close(L, ra);
755 continue;
757 case OP_CLOSURE: {
758 Proto *p;
759 Closure *ncl;
760 int nup, j;
761 p = cl->p->p[GETARG_Bx(i)];
762 nup = p->nups;
763 ncl = luaF_newLclosure(L, nup, cl->env);
764 ncl->l.p = p;
765 for (j=0; j<nup; j++, pc++) {
766 if (GET_OPCODE(*pc) == OP_GETUPVAL)
767 ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
768 else {
769 lua_assert(GET_OPCODE(*pc) == OP_MOVE);
770 ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
773 setclvalue(L, ra, ncl);
774 Protect(luaC_checkGC(L));
775 continue;
777 case OP_VARARG: {
778 int b = GETARG_B(i) - 1;
779 int j;
780 CallInfo *ci = L->ci;
781 int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1;
782 if (b == LUA_MULTRET) {
783 Protect(luaD_checkstack(L, n));
784 ra = RA(i); /* previous call may change the stack */
785 b = n;
786 L->top = ra + n;
788 for (j = 0; j < b; j++) {
789 if (j < n) {
790 setobjs2s(L, ra + j, ci->base - n + j);
792 else {
793 setnilvalue(ra + j);
796 continue;