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