root/lib/lua/lvm.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. luai_ipow
  2. luaV_tonumber
  3. luaV_tostring
  4. traceexec
  5. callTMres
  6. callTM
  7. luaV_gettable
  8. luaV_settable
  9. call_binTM
  10. get_compTM
  11. call_orderTM
  12. l_strcmp
  13. luaV_lessthan
  14. lessequal
  15. luaV_equalval
  16. luaV_concat
  17. Arith
  18. luaV_execute

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

/* [<][>][^][v][top][bottom][index][help] */