1/*
   2** $Id: lvm.c $
   3** Lua virtual machine
   4** See Copyright Notice in lua.h
   5*/
   6
   7#define lvm_c
   8#define LUA_CORE
   9
  10#include "lprefix.h"
  11
  12#include <float.h>
  13#include <limits.h>
  14#include <math.h>
  15#include <stdio.h>
  16#include <stdlib.h>
  17#include <string.h>
  18
  19#include "lua.h"
  20
  21#include "ldebug.h"
  22#include "ldo.h"
  23#include "lfunc.h"
  24#include "lgc.h"
  25#include "lobject.h"
  26#include "lopcodes.h"
  27#include "lstate.h"
  28#include "lstring.h"
  29#include "ltable.h"
  30#include "ltm.h"
  31#include "lvm.h"
  32
  33
  34/*
  35** By default, use jump tables in the main interpreter loop on gcc
  36** and compatible compilers.
  37*/
  38#if !defined(LUA_USE_JUMPTABLE)
  39#if defined(__GNUC__)
  40#define LUA_USE_JUMPTABLE	1
  41#else
  42#define LUA_USE_JUMPTABLE	0
  43#endif
  44#endif
  45
  46
  47
  48/* limit for table tag-method chains (to avoid infinite loops) */
  49#define MAXTAGLOOP	2000
  50
  51
  52/*
  53** 'l_intfitsf' checks whether a given integer is in the range that
  54** can be converted to a float without rounding. Used in comparisons.
  55*/
  56
  57/* number of bits in the mantissa of a float */
  58#define NBM		(l_floatatt(MANT_DIG))
  59
  60/*
  61** Check whether some integers may not fit in a float, testing whether
  62** (maxinteger >> NBM) > 0. (That implies (1 << NBM) <= maxinteger.)
  63** (The shifts are done in parts, to avoid shifting by more than the size
  64** of an integer. In a worst case, NBM == 113 for long double and
  65** sizeof(long) == 32.)
  66*/
  67#if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \
  68	>> (NBM - (3 * (NBM / 4))))  >  0
  69
  70/* limit for integers that fit in a float */
  71#define MAXINTFITSF	((lua_Unsigned)1 << NBM)
  72
  73/* check whether 'i' is in the interval [-MAXINTFITSF, MAXINTFITSF] */
  74#define l_intfitsf(i)	((MAXINTFITSF + l_castS2U(i)) <= (2 * MAXINTFITSF))
  75
  76#else  /* all integers fit in a float precisely */
  77
  78#define l_intfitsf(i)	1
  79
  80#endif
  81
  82
  83/*
  84** Try to convert a value from string to a number value.
  85** If the value is not a string or is a string not representing
  86** a valid numeral (or if coercions from strings to numbers
  87** are disabled via macro 'cvt2num'), do not modify 'result'
  88** and return 0.
  89*/
  90static int l_strton (const TValue *obj, TValue *result) {
  91  lua_assert(obj != result);
  92  if (!cvt2num(obj))  /* is object not a string? */
  93    return 0;
  94  else {
  95    TString *st = tsvalue(obj);
  96    return (luaO_str2num(getstr(st), result) == tsslen(st) + 1);
  97  }
  98}
  99
 100
 101/*
 102** Try to convert a value to a float. The float case is already handled
 103** by the macro 'tonumber'.
 104*/
 105int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
 106  TValue v;
 107  if (ttisinteger(obj)) {
 108    *n = cast_num(ivalue(obj));
 109    return 1;
 110  }
 111  else if (l_strton(obj, &v)) {  /* string coercible to number? */
 112    *n = nvalue(&v);  /* convert result of 'luaO_str2num' to a float */
 113    return 1;
 114  }
 115  else
 116    return 0;  /* conversion failed */
 117}
 118
 119
 120/*
 121** try to convert a float to an integer, rounding according to 'mode'.
 122*/
 123int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) {
 124  lua_Number f = l_floor(n);
 125  if (n != f) {  /* not an integral value? */
 126    if (mode == F2Ieq) return 0;  /* fails if mode demands integral value */
 127    else if (mode == F2Iceil)  /* needs ceil? */
 128      f += 1;  /* convert floor to ceil (remember: n != f) */
 129  }
 130  return lua_numbertointeger(f, p);
 131}
 132
 133
 134/*
 135** try to convert a value to an integer, rounding according to 'mode',
 136** without string coercion.
 137** ("Fast track" handled by macro 'tointegerns'.)
 138*/
 139int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) {
 140  if (ttisfloat(obj))
 141    return luaV_flttointeger(fltvalue(obj), p, mode);
 142  else if (ttisinteger(obj)) {
 143    *p = ivalue(obj);
 144    return 1;
 145  }
 146  else
 147    return 0;
 148}
 149
 150
 151/*
 152** try to convert a value to an integer.
 153*/
 154int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) {
 155  TValue v;
 156  if (l_strton(obj, &v))  /* does 'obj' point to a numerical string? */
 157    obj = &v;  /* change it to point to its corresponding number */
 158  return luaV_tointegerns(obj, p, mode);
 159}
 160
 161
 162/*
 163** Try to convert a 'for' limit to an integer, preserving the semantics
 164** of the loop. Return true if the loop must not run; otherwise, '*p'
 165** gets the integer limit.
 166** (The following explanation assumes a positive step; it is valid for
 167** negative steps mutatis mutandis.)
 168** If the limit is an integer or can be converted to an integer,
 169** rounding down, that is the limit.
 170** Otherwise, check whether the limit can be converted to a float. If
 171** the float is too large, clip it to LUA_MAXINTEGER.  If the float
 172** is too negative, the loop should not run, because any initial
 173** integer value is greater than such limit; so, the function returns
 174** true to signal that. (For this latter case, no integer limit would be
 175** correct; even a limit of LUA_MININTEGER would run the loop once for
 176** an initial value equal to LUA_MININTEGER.)
 177*/
 178static int forlimit (lua_State *L, lua_Integer init, const TValue *lim,
 179                                   lua_Integer *p, lua_Integer step) {
 180  if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) {
 181    /* not coercible to in integer */
 182    lua_Number flim;  /* try to convert to float */
 183    if (!tonumber(lim, &flim)) /* cannot convert to float? */
 184      luaG_forerror(L, lim, "limit");
 185    /* else 'flim' is a float out of integer bounds */
 186    if (luai_numlt(0, flim)) {  /* if it is positive, it is too large */
 187      if (step < 0) return 1;  /* initial value must be less than it */
 188      *p = LUA_MAXINTEGER;  /* truncate */
 189    }
 190    else {  /* it is less than min integer */
 191      if (step > 0) return 1;  /* initial value must be greater than it */
 192      *p = LUA_MININTEGER;  /* truncate */
 193    }
 194  }
 195  return (step > 0 ? init > *p : init < *p);  /* not to run? */
 196}
 197
 198
 199/*
 200** Prepare a numerical for loop (opcode OP_FORPREP).
 201** Return true to skip the loop. Otherwise,
 202** after preparation, stack will be as follows:
 203**   ra : internal index (safe copy of the control variable)
 204**   ra + 1 : loop counter (integer loops) or limit (float loops)
 205**   ra + 2 : step
 206**   ra + 3 : control variable
 207*/
 208static int forprep (lua_State *L, StkId ra) {
 209  TValue *pinit = s2v(ra);
 210  TValue *plimit = s2v(ra + 1);
 211  TValue *pstep = s2v(ra + 2);
 212  if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */
 213    lua_Integer init = ivalue(pinit);
 214    lua_Integer step = ivalue(pstep);
 215    lua_Integer limit;
 216    if (step == 0)
 217      luaG_runerror(L, "'for' step is zero");
 218    setivalue(s2v(ra + 3), init);  /* control variable */
 219    if (forlimit(L, init, plimit, &limit, step))
 220      return 1;  /* skip the loop */
 221    else {  /* prepare loop counter */
 222      lua_Unsigned count;
 223      if (step > 0) {  /* ascending loop? */
 224        count = l_castS2U(limit) - l_castS2U(init);
 225        if (step != 1)  /* avoid division in the too common case */
 226          count /= l_castS2U(step);
 227      }
 228      else {  /* step < 0; descending loop */
 229        count = l_castS2U(init) - l_castS2U(limit);
 230        /* 'step+1' avoids negating 'mininteger' */
 231        count /= l_castS2U(-(step + 1)) + 1u;
 232      }
 233      /* store the counter in place of the limit (which won't be
 234         needed anymore) */
 235      setivalue(plimit, l_castU2S(count));
 236    }
 237  }
 238  else {  /* try making all values floats */
 239    lua_Number init; lua_Number limit; lua_Number step;
 240    if (l_unlikely(!tonumber(plimit, &limit)))
 241      luaG_forerror(L, plimit, "limit");
 242    if (l_unlikely(!tonumber(pstep, &step)))
 243      luaG_forerror(L, pstep, "step");
 244    if (l_unlikely(!tonumber(pinit, &init)))
 245      luaG_forerror(L, pinit, "initial value");
 246    if (step == 0)
 247      luaG_runerror(L, "'for' step is zero");
 248    if (luai_numlt(0, step) ? luai_numlt(limit, init)
 249                            : luai_numlt(init, limit))
 250      return 1;  /* skip the loop */
 251    else {
 252      /* make sure internal values are all floats */
 253      setfltvalue(plimit, limit);
 254      setfltvalue(pstep, step);
 255      setfltvalue(s2v(ra), init);  /* internal index */
 256      setfltvalue(s2v(ra + 3), init);  /* control variable */
 257    }
 258  }
 259  return 0;
 260}
 261
 262
 263/*
 264** Execute a step of a float numerical for loop, returning
 265** true iff the loop must continue. (The integer case is
 266** written online with opcode OP_FORLOOP, for performance.)
 267*/
 268static int floatforloop (StkId ra) {
 269  lua_Number step = fltvalue(s2v(ra + 2));
 270  lua_Number limit = fltvalue(s2v(ra + 1));
 271  lua_Number idx = fltvalue(s2v(ra));  /* internal index */
 272  idx = luai_numadd(L, idx, step);  /* increment index */
 273  if (luai_numlt(0, step) ? luai_numle(idx, limit)
 274                          : luai_numle(limit, idx)) {
 275    chgfltvalue(s2v(ra), idx);  /* update internal index */
 276    setfltvalue(s2v(ra + 3), idx);  /* and control variable */
 277    return 1;  /* jump back */
 278  }
 279  else
 280    return 0;  /* finish the loop */
 281}
 282
 283
 284/*
 285** Finish the table access 'val = t[key]'.
 286** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to
 287** t[k] entry (which must be empty).
 288*/
 289void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val,
 290                      const TValue *slot) {
 291  int loop;  /* counter to avoid infinite loops */
 292  const TValue *tm;  /* metamethod */
 293  for (loop = 0; loop < MAXTAGLOOP; loop++) {
 294    if (slot == NULL) {  /* 't' is not a table? */
 295      lua_assert(!ttistable(t));
 296      tm = luaT_gettmbyobj(L, t, TM_INDEX);
 297      if (l_unlikely(notm(tm)))
 298        luaG_typeerror(L, t, "index");  /* no metamethod */
 299      /* else will try the metamethod */
 300    }
 301    else {  /* 't' is a table */
 302      lua_assert(isempty(slot));
 303      tm = fasttm(L, hvalue(t)->metatable, TM_INDEX);  /* table's metamethod */
 304      if (tm == NULL) {  /* no metamethod? */
 305        setnilvalue(s2v(val));  /* result is nil */
 306        return;
 307      }
 308      /* else will try the metamethod */
 309    }
 310    if (ttisfunction(tm)) {  /* is metamethod a function? */
 311      luaT_callTMres(L, tm, t, key, val);  /* call it */
 312      return;
 313    }
 314    t = tm;  /* else try to access 'tm[key]' */
 315    if (luaV_fastget(L, t, key, slot, luaH_get)) {  /* fast track? */
 316      setobj2s(L, val, slot);  /* done */
 317      return;
 318    }
 319    /* else repeat (tail call 'luaV_finishget') */
 320  }
 321  luaG_runerror(L, "'__index' chain too long; possible loop");
 322}
 323
 324
 325/*
 326** Finish a table assignment 't[key] = val'.
 327** If 'slot' is NULL, 't' is not a table.  Otherwise, 'slot' points
 328** to the entry 't[key]', or to a value with an absent key if there
 329** is no such entry.  (The value at 'slot' must be empty, otherwise
 330** 'luaV_fastget' would have done the job.)
 331*/
 332void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
 333                     TValue *val, const TValue *slot) {
 334  int loop;  /* counter to avoid infinite loops */
 335  for (loop = 0; loop < MAXTAGLOOP; loop++) {
 336    const TValue *tm;  /* '__newindex' metamethod */
 337    if (slot != NULL) {  /* is 't' a table? */
 338      Table *h = hvalue(t);  /* save 't' table */
 339      lua_assert(isempty(slot));  /* slot must be empty */
 340      tm = fasttm(L, h->metatable, TM_NEWINDEX);  /* get metamethod */
 341      if (tm == NULL) {  /* no metamethod? */
 342        sethvalue2s(L, L->top.p, h);  /* anchor 't' */
 343        L->top.p++;  /* assume EXTRA_STACK */
 344        luaH_finishset(L, h, key, slot, val);  /* set new value */
 345        L->top.p--;
 346        invalidateTMcache(h);
 347        luaC_barrierback(L, obj2gco(h), val);
 348        return;
 349      }
 350      /* else will try the metamethod */
 351    }
 352    else {  /* not a table; check metamethod */
 353      tm = luaT_gettmbyobj(L, t, TM_NEWINDEX);
 354      if (l_unlikely(notm(tm)))
 355        luaG_typeerror(L, t, "index");
 356    }
 357    /* try the metamethod */
 358    if (ttisfunction(tm)) {
 359      luaT_callTM(L, tm, t, key, val);
 360      return;
 361    }
 362    t = tm;  /* else repeat assignment over 'tm' */
 363    if (luaV_fastget(L, t, key, slot, luaH_get)) {
 364      luaV_finishfastset(L, t, slot, val);
 365      return;  /* done */
 366    }
 367    /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */
 368  }
 369  luaG_runerror(L, "'__newindex' chain too long; possible loop");
 370}
 371
 372
 373/*
 374** Compare two strings 'ts1' x 'ts2', returning an integer less-equal-
 375** -greater than zero if 'ts1' is less-equal-greater than 'ts2'.
 376** The code is a little tricky because it allows '\0' in the strings
 377** and it uses 'strcoll' (to respect locales) for each segment
 378** of the strings. Note that segments can compare equal but still
 379** have different lengths.
 380*/
 381static int l_strcmp (const TString *ts1, const TString *ts2) {
 382  const char *s1 = getstr(ts1);
 383  size_t rl1 = tsslen(ts1);  /* real length */
 384  const char *s2 = getstr(ts2);
 385  size_t rl2 = tsslen(ts2);
 386  for (;;) {  /* for each segment */
 387    int temp = strcoll(s1, s2);
 388    if (temp != 0)  /* not equal? */
 389      return temp;  /* done */
 390    else {  /* strings are equal up to a '\0' */
 391      size_t zl1 = strlen(s1);  /* index of first '\0' in 's1' */
 392      size_t zl2 = strlen(s2);  /* index of first '\0' in 's2' */
 393      if (zl2 == rl2)  /* 's2' is finished? */
 394        return (zl1 == rl1) ? 0 : 1;  /* check 's1' */
 395      else if (zl1 == rl1)  /* 's1' is finished? */
 396        return -1;  /* 's1' is less than 's2' ('s2' is not finished) */
 397      /* both strings longer than 'zl'; go on comparing after the '\0' */
 398      zl1++; zl2++;
 399      s1 += zl1; rl1 -= zl1; s2 += zl2; rl2 -= zl2;
 400    }
 401  }
 402}
 403
 404
 405/*
 406** Check whether integer 'i' is less than float 'f'. If 'i' has an
 407** exact representation as a float ('l_intfitsf'), compare numbers as
 408** floats. Otherwise, use the equivalence 'i < f <=> i < ceil(f)'.
 409** If 'ceil(f)' is out of integer range, either 'f' is greater than
 410** all integers or less than all integers.
 411** (The test with 'l_intfitsf' is only for performance; the else
 412** case is correct for all values, but it is slow due to the conversion
 413** from float to int.)
 414** When 'f' is NaN, comparisons must result in false.
 415*/
 416l_sinline int LTintfloat (lua_Integer i, lua_Number f) {
 417  if (l_intfitsf(i))
 418    return luai_numlt(cast_num(i), f);  /* compare them as floats */
 419  else {  /* i < f <=> i < ceil(f) */
 420    lua_Integer fi;
 421    if (luaV_flttointeger(f, &fi, F2Iceil))  /* fi = ceil(f) */
 422      return i < fi;   /* compare them as integers */
 423    else  /* 'f' is either greater or less than all integers */
 424      return f > 0;  /* greater? */
 425  }
 426}
 427
 428
 429/*
 430** Check whether integer 'i' is less than or equal to float 'f'.
 431** See comments on previous function.
 432*/
 433l_sinline int LEintfloat (lua_Integer i, lua_Number f) {
 434  if (l_intfitsf(i))
 435    return luai_numle(cast_num(i), f);  /* compare them as floats */
 436  else {  /* i <= f <=> i <= floor(f) */
 437    lua_Integer fi;
 438    if (luaV_flttointeger(f, &fi, F2Ifloor))  /* fi = floor(f) */
 439      return i <= fi;   /* compare them as integers */
 440    else  /* 'f' is either greater or less than all integers */
 441      return f > 0;  /* greater? */
 442  }
 443}
 444
 445
 446/*
 447** Check whether float 'f' is less than integer 'i'.
 448** See comments on previous function.
 449*/
 450l_sinline int LTfloatint (lua_Number f, lua_Integer i) {
 451  if (l_intfitsf(i))
 452    return luai_numlt(f, cast_num(i));  /* compare them as floats */
 453  else {  /* f < i <=> floor(f) < i */
 454    lua_Integer fi;
 455    if (luaV_flttointeger(f, &fi, F2Ifloor))  /* fi = floor(f) */
 456      return fi < i;   /* compare them as integers */
 457    else  /* 'f' is either greater or less than all integers */
 458      return f < 0;  /* less? */
 459  }
 460}
 461
 462
 463/*
 464** Check whether float 'f' is less than or equal to integer 'i'.
 465** See comments on previous function.
 466*/
 467l_sinline int LEfloatint (lua_Number f, lua_Integer i) {
 468  if (l_intfitsf(i))
 469    return luai_numle(f, cast_num(i));  /* compare them as floats */
 470  else {  /* f <= i <=> ceil(f) <= i */
 471    lua_Integer fi;
 472    if (luaV_flttointeger(f, &fi, F2Iceil))  /* fi = ceil(f) */
 473      return fi <= i;   /* compare them as integers */
 474    else  /* 'f' is either greater or less than all integers */
 475      return f < 0;  /* less? */
 476  }
 477}
 478
 479
 480/*
 481** Return 'l < r', for numbers.
 482*/
 483l_sinline int LTnum (const TValue *l, const TValue *r) {
 484  lua_assert(ttisnumber(l) && ttisnumber(r));
 485  if (ttisinteger(l)) {
 486    lua_Integer li = ivalue(l);
 487    if (ttisinteger(r))
 488      return li < ivalue(r);  /* both are integers */
 489    else  /* 'l' is int and 'r' is float */
 490      return LTintfloat(li, fltvalue(r));  /* l < r ? */
 491  }
 492  else {
 493    lua_Number lf = fltvalue(l);  /* 'l' must be float */
 494    if (ttisfloat(r))
 495      return luai_numlt(lf, fltvalue(r));  /* both are float */
 496    else  /* 'l' is float and 'r' is int */
 497      return LTfloatint(lf, ivalue(r));
 498  }
 499}
 500
 501
 502/*
 503** Return 'l <= r', for numbers.
 504*/
 505l_sinline int LEnum (const TValue *l, const TValue *r) {
 506  lua_assert(ttisnumber(l) && ttisnumber(r));
 507  if (ttisinteger(l)) {
 508    lua_Integer li = ivalue(l);
 509    if (ttisinteger(r))
 510      return li <= ivalue(r);  /* both are integers */
 511    else  /* 'l' is int and 'r' is float */
 512      return LEintfloat(li, fltvalue(r));  /* l <= r ? */
 513  }
 514  else {
 515    lua_Number lf = fltvalue(l);  /* 'l' must be float */
 516    if (ttisfloat(r))
 517      return luai_numle(lf, fltvalue(r));  /* both are float */
 518    else  /* 'l' is float and 'r' is int */
 519      return LEfloatint(lf, ivalue(r));
 520  }
 521}
 522
 523
 524/*
 525** return 'l < r' for non-numbers.
 526*/
 527static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) {
 528  lua_assert(!ttisnumber(l) || !ttisnumber(r));
 529  if (ttisstring(l) && ttisstring(r))  /* both are strings? */
 530    return l_strcmp(tsvalue(l), tsvalue(r)) < 0;
 531  else
 532    return luaT_callorderTM(L, l, r, TM_LT);
 533}
 534
 535
 536/*
 537** Main operation less than; return 'l < r'.
 538*/
 539int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
 540  if (ttisnumber(l) && ttisnumber(r))  /* both operands are numbers? */
 541    return LTnum(l, r);
 542  else return lessthanothers(L, l, r);
 543}
 544
 545
 546/*
 547** return 'l <= r' for non-numbers.
 548*/
 549static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) {
 550  lua_assert(!ttisnumber(l) || !ttisnumber(r));
 551  if (ttisstring(l) && ttisstring(r))  /* both are strings? */
 552    return l_strcmp(tsvalue(l), tsvalue(r)) <= 0;
 553  else
 554    return luaT_callorderTM(L, l, r, TM_LE);
 555}
 556
 557
 558/*
 559** Main operation less than or equal to; return 'l <= r'.
 560*/
 561int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
 562  if (ttisnumber(l) && ttisnumber(r))  /* both operands are numbers? */
 563    return LEnum(l, r);
 564  else return lessequalothers(L, l, r);
 565}
 566
 567
 568/*
 569** Main operation for equality of Lua values; return 't1 == t2'.
 570** L == NULL means raw equality (no metamethods)
 571*/
 572int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
 573  const TValue *tm;
 574  if (ttypetag(t1) != ttypetag(t2)) {  /* not the same variant? */
 575    if (ttype(t1) != ttype(t2) || ttype(t1) != LUA_TNUMBER)
 576      return 0;  /* only numbers can be equal with different variants */
 577    else {  /* two numbers with different variants */
 578      /* One of them is an integer. If the other does not have an
 579         integer value, they cannot be equal; otherwise, compare their
 580         integer values. */
 581      lua_Integer i1, i2;
 582      return (luaV_tointegerns(t1, &i1, F2Ieq) &&
 583              luaV_tointegerns(t2, &i2, F2Ieq) &&
 584              i1 == i2);
 585    }
 586  }
 587  /* values have same type and same variant */
 588  switch (ttypetag(t1)) {
 589    case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: return 1;
 590    case LUA_VNUMINT: return (ivalue(t1) == ivalue(t2));
 591    case LUA_VNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2));
 592    case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
 593    case LUA_VLCF: return fvalue(t1) == fvalue(t2);
 594    case LUA_VSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2));
 595    case LUA_VLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2));
 596    case LUA_VUSERDATA: {
 597      if (uvalue(t1) == uvalue(t2)) return 1;
 598      else if (L == NULL) return 0;
 599      tm = fasttm(L, uvalue(t1)->metatable, TM_EQ);
 600      if (tm == NULL)
 601        tm = fasttm(L, uvalue(t2)->metatable, TM_EQ);
 602      break;  /* will try TM */
 603    }
 604    case LUA_VTABLE: {
 605      if (hvalue(t1) == hvalue(t2)) return 1;
 606      else if (L == NULL) return 0;
 607      tm = fasttm(L, hvalue(t1)->metatable, TM_EQ);
 608      if (tm == NULL)
 609        tm = fasttm(L, hvalue(t2)->metatable, TM_EQ);
 610      break;  /* will try TM */
 611    }
 612    default:
 613      return gcvalue(t1) == gcvalue(t2);
 614  }
 615  if (tm == NULL)  /* no TM? */
 616    return 0;  /* objects are different */
 617  else {
 618    luaT_callTMres(L, tm, t1, t2, L->top.p);  /* call TM */
 619    return !l_isfalse(s2v(L->top.p));
 620  }
 621}
 622
 623
 624/* macro used by 'luaV_concat' to ensure that element at 'o' is a string */
 625#define tostring(L,o)  \
 626	(ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1)))
 627
 628#define isemptystr(o)	(ttisshrstring(o) && tsvalue(o)->shrlen == 0)
 629
 630/* copy strings in stack from top - n up to top - 1 to buffer */
 631static void copy2buff (StkId top, int n, char *buff) {
 632  size_t tl = 0;  /* size already copied */
 633  do {
 634    TString *st = tsvalue(s2v(top - n));
 635    size_t l = tsslen(st);  /* length of string being copied */
 636    memcpy(buff + tl, getstr(st), l * sizeof(char));
 637    tl += l;
 638  } while (--n > 0);
 639}
 640
 641
 642/*
 643** Main operation for concatenation: concat 'total' values in the stack,
 644** from 'L->top.p - total' up to 'L->top.p - 1'.
 645*/
 646void luaV_concat (lua_State *L, int total) {
 647  if (total == 1)
 648    return;  /* "all" values already concatenated */
 649  do {
 650    StkId top = L->top.p;
 651    int n = 2;  /* number of elements handled in this pass (at least 2) */
 652    if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) ||
 653        !tostring(L, s2v(top - 1)))
 654      luaT_tryconcatTM(L);  /* may invalidate 'top' */
 655    else if (isemptystr(s2v(top - 1)))  /* second operand is empty? */
 656      cast_void(tostring(L, s2v(top - 2)));  /* result is first operand */
 657    else if (isemptystr(s2v(top - 2))) {  /* first operand is empty string? */
 658      setobjs2s(L, top - 2, top - 1);  /* result is second op. */
 659    }
 660    else {
 661      /* at least two non-empty string values; get as many as possible */
 662      size_t tl = tsslen(tsvalue(s2v(top - 1)));
 663      TString *ts;
 664      /* collect total length and number of strings */
 665      for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) {
 666        size_t l = tsslen(tsvalue(s2v(top - n - 1)));
 667        if (l_unlikely(l >= MAX_SIZE - sizeof(TString) - tl)) {
 668          L->top.p = top - total;  /* pop strings to avoid wasting stack */
 669          luaG_runerror(L, "string length overflow");
 670        }
 671        tl += l;
 672      }
 673      if (tl <= LUAI_MAXSHORTLEN) {  /* is result a short string? */
 674        char buff[LUAI_MAXSHORTLEN];
 675        copy2buff(top, n, buff);  /* copy strings to buffer */
 676        ts = luaS_newlstr(L, buff, tl);
 677      }
 678      else {  /* long string; copy strings directly to final result */
 679        ts = luaS_createlngstrobj(L, tl);
 680        copy2buff(top, n, getlngstr(ts));
 681      }
 682      setsvalue2s(L, top - n, ts);  /* create result */
 683    }
 684    total -= n - 1;  /* got 'n' strings to create one new */
 685    L->top.p -= n - 1;  /* popped 'n' strings and pushed one */
 686  } while (total > 1);  /* repeat until only 1 result left */
 687}
 688
 689
 690/*
 691** Main operation 'ra = #rb'.
 692*/
 693void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
 694  const TValue *tm;
 695  switch (ttypetag(rb)) {
 696    case LUA_VTABLE: {
 697      Table *h = hvalue(rb);
 698      tm = fasttm(L, h->metatable, TM_LEN);
 699      if (tm) break;  /* metamethod? break switch to call it */
 700      setivalue(s2v(ra), luaH_getn(h));  /* else primitive len */
 701      return;
 702    }
 703    case LUA_VSHRSTR: {
 704      setivalue(s2v(ra), tsvalue(rb)->shrlen);
 705      return;
 706    }
 707    case LUA_VLNGSTR: {
 708      setivalue(s2v(ra), tsvalue(rb)->u.lnglen);
 709      return;
 710    }
 711    default: {  /* try metamethod */
 712      tm = luaT_gettmbyobj(L, rb, TM_LEN);
 713      if (l_unlikely(notm(tm)))  /* no metamethod? */
 714        luaG_typeerror(L, rb, "get length of");
 715      break;
 716    }
 717  }
 718  luaT_callTMres(L, tm, rb, rb, ra);
 719}
 720
 721
 722/*
 723** Integer division; return 'm // n', that is, floor(m/n).
 724** C division truncates its result (rounds towards zero).
 725** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer,
 726** otherwise 'floor(q) == trunc(q) - 1'.
 727*/
 728lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) {
 729  if (l_unlikely(l_castS2U(n) + 1u <= 1u)) {  /* special cases: -1 or 0 */
 730    if (n == 0)
 731      luaG_runerror(L, "attempt to divide by zero");
 732    return intop(-, 0, m);   /* n==-1; avoid overflow with 0x80000...//-1 */
 733  }
 734  else {
 735    lua_Integer q = m / n;  /* perform C division */
 736    if ((m ^ n) < 0 && m % n != 0)  /* 'm/n' would be negative non-integer? */
 737      q -= 1;  /* correct result for different rounding */
 738    return q;
 739  }
 740}
 741
 742
 743/*
 744** Integer modulus; return 'm % n'. (Assume that C '%' with
 745** negative operands follows C99 behavior. See previous comment
 746** about luaV_idiv.)
 747*/
 748lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
 749  if (l_unlikely(l_castS2U(n) + 1u <= 1u)) {  /* special cases: -1 or 0 */
 750    if (n == 0)
 751      luaG_runerror(L, "attempt to perform 'n%%0'");
 752    return 0;   /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
 753  }
 754  else {
 755    lua_Integer r = m % n;
 756    if (r != 0 && (r ^ n) < 0)  /* 'm/n' would be non-integer negative? */
 757      r += n;  /* correct result for different rounding */
 758    return r;
 759  }
 760}
 761
 762
 763/*
 764** Float modulus
 765*/
 766lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) {
 767  lua_Number r;
 768  luai_nummod(L, m, n, r);
 769  return r;
 770}
 771
 772
 773/* number of bits in an integer */
 774#define NBITS	cast_int(sizeof(lua_Integer) * CHAR_BIT)
 775
 776
 777/*
 778** Shift left operation. (Shift right just negates 'y'.)
 779*/
 780lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
 781  if (y < 0) {  /* shift right? */
 782    if (y <= -NBITS) return 0;
 783    else return intop(>>, x, -y);
 784  }
 785  else {  /* shift left */
 786    if (y >= NBITS) return 0;
 787    else return intop(<<, x, y);
 788  }
 789}
 790
 791
 792/*
 793** create a new Lua closure, push it in the stack, and initialize
 794** its upvalues.
 795*/
 796static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
 797                         StkId ra) {
 798  int nup = p->sizeupvalues;
 799  Upvaldesc *uv = p->upvalues;
 800  int i;
 801  LClosure *ncl = luaF_newLclosure(L, nup);
 802  ncl->p = p;
 803  setclLvalue2s(L, ra, ncl);  /* anchor new closure in stack */
 804  for (i = 0; i < nup; i++) {  /* fill in its upvalues */
 805    if (uv[i].instack)  /* upvalue refers to local variable? */
 806      ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx);
 807    else  /* get upvalue from enclosing function */
 808      ncl->upvals[i] = encup[uv[i].idx];
 809    luaC_objbarrier(L, ncl, ncl->upvals[i]);
 810  }
 811}
 812
 813
 814/*
 815** finish execution of an opcode interrupted by a yield
 816*/
 817void luaV_finishOp (lua_State *L) {
 818  CallInfo *ci = L->ci;
 819  StkId base = ci->func.p + 1;
 820  Instruction inst = *(ci->u.l.savedpc - 1);  /* interrupted instruction */
 821  OpCode op = GET_OPCODE(inst);
 822  switch (op) {  /* finish its execution */
 823    case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
 824      setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top.p);
 825      break;
 826    }
 827    case OP_UNM: case OP_BNOT: case OP_LEN:
 828    case OP_GETTABUP: case OP_GETTABLE: case OP_GETI:
 829    case OP_GETFIELD: case OP_SELF: {
 830      setobjs2s(L, base + GETARG_A(inst), --L->top.p);
 831      break;
 832    }
 833    case OP_LT: case OP_LE:
 834    case OP_LTI: case OP_LEI:
 835    case OP_GTI: case OP_GEI:
 836    case OP_EQ: {  /* note that 'OP_EQI'/'OP_EQK' cannot yield */
 837      int res = !l_isfalse(s2v(L->top.p - 1));
 838      L->top.p--;
 839#if defined(LUA_COMPAT_LT_LE)
 840      if (ci->callstatus & CIST_LEQ) {  /* "<=" using "<" instead? */
 841        ci->callstatus ^= CIST_LEQ;  /* clear mark */
 842        res = !res;  /* negate result */
 843      }
 844#endif
 845      lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
 846      if (res != GETARG_k(inst))  /* condition failed? */
 847        ci->u.l.savedpc++;  /* skip jump instruction */
 848      break;
 849    }
 850    case OP_CONCAT: {
 851      StkId top = L->top.p - 1;  /* top when 'luaT_tryconcatTM' was called */
 852      int a = GETARG_A(inst);      /* first element to concatenate */
 853      int total = cast_int(top - 1 - (base + a));  /* yet to concatenate */
 854      setobjs2s(L, top - 2, top);  /* put TM result in proper position */
 855      L->top.p = top - 1;  /* top is one after last element (at top-2) */
 856      luaV_concat(L, total);  /* concat them (may yield again) */
 857      break;
 858    }
 859    case OP_CLOSE: {  /* yielded closing variables */
 860      ci->u.l.savedpc--;  /* repeat instruction to close other vars. */
 861      break;
 862    }
 863    case OP_RETURN: {  /* yielded closing variables */
 864      StkId ra = base + GETARG_A(inst);
 865      /* adjust top to signal correct number of returns, in case the
 866         return is "up to top" ('isIT') */
 867      L->top.p = ra + ci->u2.nres;
 868      /* repeat instruction to close other vars. and complete the return */
 869      ci->u.l.savedpc--;
 870      break;
 871    }
 872    default: {
 873      /* only these other opcodes can yield */
 874      lua_assert(op == OP_TFORCALL || op == OP_CALL ||
 875           op == OP_TAILCALL || op == OP_SETTABUP || op == OP_SETTABLE ||
 876           op == OP_SETI || op == OP_SETFIELD);
 877      break;
 878    }
 879  }
 880}
 881
 882
 883
 884
 885/*
 886** {==================================================================
 887** Macros for arithmetic/bitwise/comparison opcodes in 'luaV_execute'
 888** ===================================================================
 889*/
 890
 891#define l_addi(L,a,b)	intop(+, a, b)
 892#define l_subi(L,a,b)	intop(-, a, b)
 893#define l_muli(L,a,b)	intop(*, a, b)
 894#define l_band(a,b)	intop(&, a, b)
 895#define l_bor(a,b)	intop(|, a, b)
 896#define l_bxor(a,b)	intop(^, a, b)
 897
 898#define l_lti(a,b)	(a < b)
 899#define l_lei(a,b)	(a <= b)
 900#define l_gti(a,b)	(a > b)
 901#define l_gei(a,b)	(a >= b)
 902
 903
 904/*
 905** Arithmetic operations with immediate operands. 'iop' is the integer
 906** operation, 'fop' is the float operation.
 907*/
 908#define op_arithI(L,iop,fop) {  \
 909  StkId ra = RA(i); \
 910  TValue *v1 = vRB(i);  \
 911  int imm = GETARG_sC(i);  \
 912  if (ttisinteger(v1)) {  \
 913    lua_Integer iv1 = ivalue(v1);  \
 914    pc++; setivalue(s2v(ra), iop(L, iv1, imm));  \
 915  }  \
 916  else if (ttisfloat(v1)) {  \
 917    lua_Number nb = fltvalue(v1);  \
 918    lua_Number fimm = cast_num(imm);  \
 919    pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \
 920  }}
 921
 922
 923/*
 924** Auxiliary function for arithmetic operations over floats and others
 925** with two register operands.
 926*/
 927#define op_arithf_aux(L,v1,v2,fop) {  \
 928  lua_Number n1; lua_Number n2;  \
 929  if (tonumberns(v1, n1) && tonumberns(v2, n2)) {  \
 930    pc++; setfltvalue(s2v(ra), fop(L, n1, n2));  \
 931  }}
 932
 933
 934/*
 935** Arithmetic operations over floats and others with register operands.
 936*/
 937#define op_arithf(L,fop) {  \
 938  StkId ra = RA(i); \
 939  TValue *v1 = vRB(i);  \
 940  TValue *v2 = vRC(i);  \
 941  op_arithf_aux(L, v1, v2, fop); }
 942
 943
 944/*
 945** Arithmetic operations with K operands for floats.
 946*/
 947#define op_arithfK(L,fop) {  \
 948  StkId ra = RA(i); \
 949  TValue *v1 = vRB(i);  \
 950  TValue *v2 = KC(i); lua_assert(ttisnumber(v2));  \
 951  op_arithf_aux(L, v1, v2, fop); }
 952
 953
 954/*
 955** Arithmetic operations over integers and floats.
 956*/
 957#define op_arith_aux(L,v1,v2,iop,fop) {  \
 958  StkId ra = RA(i); \
 959  if (ttisinteger(v1) && ttisinteger(v2)) {  \
 960    lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2);  \
 961    pc++; setivalue(s2v(ra), iop(L, i1, i2));  \
 962  }  \
 963  else op_arithf_aux(L, v1, v2, fop); }
 964
 965
 966/*
 967** Arithmetic operations with register operands.
 968*/
 969#define op_arith(L,iop,fop) {  \
 970  TValue *v1 = vRB(i);  \
 971  TValue *v2 = vRC(i);  \
 972  op_arith_aux(L, v1, v2, iop, fop); }
 973
 974
 975/*
 976** Arithmetic operations with K operands.
 977*/
 978#define op_arithK(L,iop,fop) {  \
 979  TValue *v1 = vRB(i);  \
 980  TValue *v2 = KC(i); lua_assert(ttisnumber(v2));  \
 981  op_arith_aux(L, v1, v2, iop, fop); }
 982
 983
 984/*
 985** Bitwise operations with constant operand.
 986*/
 987#define op_bitwiseK(L,op) {  \
 988  StkId ra = RA(i); \
 989  TValue *v1 = vRB(i);  \
 990  TValue *v2 = KC(i);  \
 991  lua_Integer i1;  \
 992  lua_Integer i2 = ivalue(v2);  \
 993  if (tointegerns(v1, &i1)) {  \
 994    pc++; setivalue(s2v(ra), op(i1, i2));  \
 995  }}
 996
 997
 998/*
 999** Bitwise operations with register operands.
1000*/
1001#define op_bitwise(L,op) {  \
1002  StkId ra = RA(i); \
1003  TValue *v1 = vRB(i);  \
1004  TValue *v2 = vRC(i);  \
1005  lua_Integer i1; lua_Integer i2;  \
1006  if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) {  \
1007    pc++; setivalue(s2v(ra), op(i1, i2));  \
1008  }}
1009
1010
1011/*
1012** Order operations with register operands. 'opn' actually works
1013** for all numbers, but the fast track improves performance for
1014** integers.
1015*/
1016#define op_order(L,opi,opn,other) {  \
1017  StkId ra = RA(i); \
1018  int cond;  \
1019  TValue *rb = vRB(i);  \
1020  if (ttisinteger(s2v(ra)) && ttisinteger(rb)) {  \
1021    lua_Integer ia = ivalue(s2v(ra));  \
1022    lua_Integer ib = ivalue(rb);  \
1023    cond = opi(ia, ib);  \
1024  }  \
1025  else if (ttisnumber(s2v(ra)) && ttisnumber(rb))  \
1026    cond = opn(s2v(ra), rb);  \
1027  else  \
1028    Protect(cond = other(L, s2v(ra), rb));  \
1029  docondjump(); }
1030
1031
1032/*
1033** Order operations with immediate operand. (Immediate operand is
1034** always small enough to have an exact representation as a float.)
1035*/
1036#define op_orderI(L,opi,opf,inv,tm) {  \
1037  StkId ra = RA(i); \
1038  int cond;  \
1039  int im = GETARG_sB(i);  \
1040  if (ttisinteger(s2v(ra)))  \
1041    cond = opi(ivalue(s2v(ra)), im);  \
1042  else if (ttisfloat(s2v(ra))) {  \
1043    lua_Number fa = fltvalue(s2v(ra));  \
1044    lua_Number fim = cast_num(im);  \
1045    cond = opf(fa, fim);  \
1046  }  \
1047  else {  \
1048    int isf = GETARG_C(i);  \
1049    Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm));  \
1050  }  \
1051  docondjump(); }
1052
1053/* }================================================================== */
1054
1055
1056/*
1057** {==================================================================
1058** Function 'luaV_execute': main interpreter loop
1059** ===================================================================
1060*/
1061
1062/*
1063** some macros for common tasks in 'luaV_execute'
1064*/
1065
1066
1067#define RA(i)	(base+GETARG_A(i))
1068#define RB(i)	(base+GETARG_B(i))
1069#define vRB(i)	s2v(RB(i))
1070#define KB(i)	(k+GETARG_B(i))
1071#define RC(i)	(base+GETARG_C(i))
1072#define vRC(i)	s2v(RC(i))
1073#define KC(i)	(k+GETARG_C(i))
1074#define RKC(i)	((TESTARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i)))
1075
1076
1077
1078#define updatetrap(ci)  (trap = ci->u.l.trap)
1079
1080#define updatebase(ci)	(base = ci->func.p + 1)
1081
1082
1083#define updatestack(ci)  \
1084	{ if (l_unlikely(trap)) { updatebase(ci); ra = RA(i); } }
1085
1086
1087/*
1088** Execute a jump instruction. The 'updatetrap' allows signals to stop
1089** tight loops. (Without it, the local copy of 'trap' could never change.)
1090*/
1091#define dojump(ci,i,e)	{ pc += GETARG_sJ(i) + e; updatetrap(ci); }
1092
1093
1094/* for test instructions, execute the jump instruction that follows it */
1095#define donextjump(ci)	{ Instruction ni = *pc; dojump(ci, ni, 1); }
1096
1097/*
1098** do a conditional jump: skip next instruction if 'cond' is not what
1099** was expected (parameter 'k'), else do next instruction, which must
1100** be a jump.
1101*/
1102#define docondjump()	if (cond != GETARG_k(i)) pc++; else donextjump(ci);
1103
1104
1105/*
1106** Correct global 'pc'.
1107*/
1108#define savepc(L)	(ci->u.l.savedpc = pc)
1109
1110
1111/*
1112** Whenever code can raise errors, the global 'pc' and the global
1113** 'top' must be correct to report occasional errors.
1114*/
1115#define savestate(L,ci)		(savepc(L), L->top.p = ci->top.p)
1116
1117
1118/*
1119** Protect code that, in general, can raise errors, reallocate the
1120** stack, and change the hooks.
1121*/
1122#define Protect(exp)  (savestate(L,ci), (exp), updatetrap(ci))
1123
1124/* special version that does not change the top */
1125#define ProtectNT(exp)  (savepc(L), (exp), updatetrap(ci))
1126
1127/*
1128** Protect code that can only raise errors. (That is, it cannot change
1129** the stack or hooks.)
1130*/
1131#define halfProtect(exp)  (savestate(L,ci), (exp))
1132
1133/* 'c' is the limit of live values in the stack */
1134#define checkGC(L,c)  \
1135	{ luaC_condGC(L, (savepc(L), L->top.p = (c)), \
1136                         updatetrap(ci)); \
1137           luai_threadyield(L); }
1138
1139
1140/* fetch an instruction and prepare its execution */
1141#define vmfetch()	{ \
1142  if (l_unlikely(trap)) {  /* stack reallocation or hooks? */ \
1143    trap = luaG_traceexec(L, pc);  /* handle hooks */ \
1144    updatebase(ci);  /* correct stack */ \
1145  } \
1146  i = *(pc++); \
1147}
1148
1149#define vmdispatch(o)	switch(o)
1150#define vmcase(l)	case l:
1151#define vmbreak		break
1152
1153
1154void luaV_execute (lua_State *L, CallInfo *ci) {
1155  LClosure *cl;
1156  TValue *k;
1157  StkId base;
1158  const Instruction *pc;
1159  int trap;
1160#if LUA_USE_JUMPTABLE
1161#include "ljumptab.h"
1162#endif
1163 startfunc:
1164  trap = L->hookmask;
1165 returning:  /* trap already set */
1166  cl = ci_func(ci);
1167  k = cl->p->k;
1168  pc = ci->u.l.savedpc;
1169  if (l_unlikely(trap))
1170    trap = luaG_tracecall(L);
1171  base = ci->func.p + 1;
1172  /* main loop of interpreter */
1173  for (;;) {
1174    Instruction i;  /* instruction being executed */
1175    vmfetch();
1176    #if 0
1177      /* low-level line tracing for debugging Lua */
1178      printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p)));
1179    #endif
1180    lua_assert(base == ci->func.p + 1);
1181    lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p);
1182    /* invalidate top for instructions not expecting it */
1183    lua_assert(isIT(i) || (cast_void(L->top.p = base), 1));
1184    vmdispatch (GET_OPCODE(i)) {
1185      vmcase(OP_MOVE) {
1186        StkId ra = RA(i);
1187        setobjs2s(L, ra, RB(i));
1188        vmbreak;
1189      }
1190      vmcase(OP_LOADI) {
1191        StkId ra = RA(i);
1192        lua_Integer b = GETARG_sBx(i);
1193        setivalue(s2v(ra), b);
1194        vmbreak;
1195      }
1196      vmcase(OP_LOADF) {
1197        StkId ra = RA(i);
1198        int b = GETARG_sBx(i);
1199        setfltvalue(s2v(ra), cast_num(b));
1200        vmbreak;
1201      }
1202      vmcase(OP_LOADK) {
1203        StkId ra = RA(i);
1204        TValue *rb = k + GETARG_Bx(i);
1205        setobj2s(L, ra, rb);
1206        vmbreak;
1207      }
1208      vmcase(OP_LOADKX) {
1209        StkId ra = RA(i);
1210        TValue *rb;
1211        rb = k + GETARG_Ax(*pc); pc++;
1212        setobj2s(L, ra, rb);
1213        vmbreak;
1214      }
1215      vmcase(OP_LOADFALSE) {
1216        StkId ra = RA(i);
1217        setbfvalue(s2v(ra));
1218        vmbreak;
1219      }
1220      vmcase(OP_LFALSESKIP) {
1221        StkId ra = RA(i);
1222        setbfvalue(s2v(ra));
1223        pc++;  /* skip next instruction */
1224        vmbreak;
1225      }
1226      vmcase(OP_LOADTRUE) {
1227        StkId ra = RA(i);
1228        setbtvalue(s2v(ra));
1229        vmbreak;
1230      }
1231      vmcase(OP_LOADNIL) {
1232        StkId ra = RA(i);
1233        int b = GETARG_B(i);
1234        do {
1235          setnilvalue(s2v(ra++));
1236        } while (b--);
1237        vmbreak;
1238      }
1239      vmcase(OP_GETUPVAL) {
1240        StkId ra = RA(i);
1241        int b = GETARG_B(i);
1242        setobj2s(L, ra, cl->upvals[b]->v.p);
1243        vmbreak;
1244      }
1245      vmcase(OP_SETUPVAL) {
1246        StkId ra = RA(i);
1247        UpVal *uv = cl->upvals[GETARG_B(i)];
1248        setobj(L, uv->v.p, s2v(ra));
1249        luaC_barrier(L, uv, s2v(ra));
1250        vmbreak;
1251      }
1252      vmcase(OP_GETTABUP) {
1253        StkId ra = RA(i);
1254        const TValue *slot;
1255        TValue *upval = cl->upvals[GETARG_B(i)]->v.p;
1256        TValue *rc = KC(i);
1257        TString *key = tsvalue(rc);  /* key must be a short string */
1258        if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
1259          setobj2s(L, ra, slot);
1260        }
1261        else
1262          Protect(luaV_finishget(L, upval, rc, ra, slot));
1263        vmbreak;
1264      }
1265      vmcase(OP_GETTABLE) {
1266        StkId ra = RA(i);
1267        const TValue *slot;
1268        TValue *rb = vRB(i);
1269        TValue *rc = vRC(i);
1270        lua_Unsigned n;
1271        if (ttisinteger(rc)  /* fast track for integers? */
1272            ? (cast_void(n = ivalue(rc)), luaV_fastgeti(L, rb, n, slot))
1273            : luaV_fastget(L, rb, rc, slot, luaH_get)) {
1274          setobj2s(L, ra, slot);
1275        }
1276        else
1277          Protect(luaV_finishget(L, rb, rc, ra, slot));
1278        vmbreak;
1279      }
1280      vmcase(OP_GETI) {
1281        StkId ra = RA(i);
1282        const TValue *slot;
1283        TValue *rb = vRB(i);
1284        int c = GETARG_C(i);
1285        if (luaV_fastgeti(L, rb, c, slot)) {
1286          setobj2s(L, ra, slot);
1287        }
1288        else {
1289          TValue key;
1290          setivalue(&key, c);
1291          Protect(luaV_finishget(L, rb, &key, ra, slot));
1292        }
1293        vmbreak;
1294      }
1295      vmcase(OP_GETFIELD) {
1296        StkId ra = RA(i);
1297        const TValue *slot;
1298        TValue *rb = vRB(i);
1299        TValue *rc = KC(i);
1300        TString *key = tsvalue(rc);  /* key must be a short string */
1301        if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) {
1302          setobj2s(L, ra, slot);
1303        }
1304        else
1305          Protect(luaV_finishget(L, rb, rc, ra, slot));
1306        vmbreak;
1307      }
1308      vmcase(OP_SETTABUP) {
1309        const TValue *slot;
1310        TValue *upval = cl->upvals[GETARG_A(i)]->v.p;
1311        TValue *rb = KB(i);
1312        TValue *rc = RKC(i);
1313        TString *key = tsvalue(rb);  /* key must be a short string */
1314        if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
1315          luaV_finishfastset(L, upval, slot, rc);
1316        }
1317        else
1318          Protect(luaV_finishset(L, upval, rb, rc, slot));
1319        vmbreak;
1320      }
1321      vmcase(OP_SETTABLE) {
1322        StkId ra = RA(i);
1323        const TValue *slot;
1324        TValue *rb = vRB(i);  /* key (table is in 'ra') */
1325        TValue *rc = RKC(i);  /* value */
1326        lua_Unsigned n;
1327        if (ttisinteger(rb)  /* fast track for integers? */
1328            ? (cast_void(n = ivalue(rb)), luaV_fastgeti(L, s2v(ra), n, slot))
1329            : luaV_fastget(L, s2v(ra), rb, slot, luaH_get)) {
1330          luaV_finishfastset(L, s2v(ra), slot, rc);
1331        }
1332        else
1333          Protect(luaV_finishset(L, s2v(ra), rb, rc, slot));
1334        vmbreak;
1335      }
1336      vmcase(OP_SETI) {
1337        StkId ra = RA(i);
1338        const TValue *slot;
1339        int c = GETARG_B(i);
1340        TValue *rc = RKC(i);
1341        if (luaV_fastgeti(L, s2v(ra), c, slot)) {
1342          luaV_finishfastset(L, s2v(ra), slot, rc);
1343        }
1344        else {
1345          TValue key;
1346          setivalue(&key, c);
1347          Protect(luaV_finishset(L, s2v(ra), &key, rc, slot));
1348        }
1349        vmbreak;
1350      }
1351      vmcase(OP_SETFIELD) {
1352        StkId ra = RA(i);
1353        const TValue *slot;
1354        TValue *rb = KB(i);
1355        TValue *rc = RKC(i);
1356        TString *key = tsvalue(rb);  /* key must be a short string */
1357        if (luaV_fastget(L, s2v(ra), key, slot, luaH_getshortstr)) {
1358          luaV_finishfastset(L, s2v(ra), slot, rc);
1359        }
1360        else
1361          Protect(luaV_finishset(L, s2v(ra), rb, rc, slot));
1362        vmbreak;
1363      }
1364      vmcase(OP_NEWTABLE) {
1365        StkId ra = RA(i);
1366        int b = GETARG_B(i);  /* log2(hash size) + 1 */
1367        int c = GETARG_C(i);  /* array size */
1368        Table *t;
1369        if (b > 0)
1370          b = 1 << (b - 1);  /* size is 2^(b - 1) */
1371        lua_assert((!TESTARG_k(i)) == (GETARG_Ax(*pc) == 0));
1372        if (TESTARG_k(i))  /* non-zero extra argument? */
1373          c += GETARG_Ax(*pc) * (MAXARG_C + 1);  /* add it to size */
1374        pc++;  /* skip extra argument */
1375        L->top.p = ra + 1;  /* correct top in case of emergency GC */
1376        t = luaH_new(L);  /* memory allocation */
1377        sethvalue2s(L, ra, t);
1378        if (b != 0 || c != 0)
1379          luaH_resize(L, t, c, b);  /* idem */
1380        checkGC(L, ra + 1);
1381        vmbreak;
1382      }
1383      vmcase(OP_SELF) {
1384        StkId ra = RA(i);
1385        const TValue *slot;
1386        TValue *rb = vRB(i);
1387        TValue *rc = RKC(i);
1388        TString *key = tsvalue(rc);  /* key must be a string */
1389        setobj2s(L, ra + 1, rb);
1390        if (luaV_fastget(L, rb, key, slot, luaH_getstr)) {
1391          setobj2s(L, ra, slot);
1392        }
1393        else
1394          Protect(luaV_finishget(L, rb, rc, ra, slot));
1395        vmbreak;
1396      }
1397      vmcase(OP_ADDI) {
1398        op_arithI(L, l_addi, luai_numadd);
1399        vmbreak;
1400      }
1401      vmcase(OP_ADDK) {
1402        op_arithK(L, l_addi, luai_numadd);
1403        vmbreak;
1404      }
1405      vmcase(OP_SUBK) {
1406        op_arithK(L, l_subi, luai_numsub);
1407        vmbreak;
1408      }
1409      vmcase(OP_MULK) {
1410        op_arithK(L, l_muli, luai_nummul);
1411        vmbreak;
1412      }
1413      vmcase(OP_MODK) {
1414        savestate(L, ci);  /* in case of division by 0 */
1415        op_arithK(L, luaV_mod, luaV_modf);
1416        vmbreak;
1417      }
1418      vmcase(OP_POWK) {
1419        op_arithfK(L, luai_numpow);
1420        vmbreak;
1421      }
1422      vmcase(OP_DIVK) {
1423        op_arithfK(L, luai_numdiv);
1424        vmbreak;
1425      }
1426      vmcase(OP_IDIVK) {
1427        savestate(L, ci);  /* in case of division by 0 */
1428        op_arithK(L, luaV_idiv, luai_numidiv);
1429        vmbreak;
1430      }
1431      vmcase(OP_BANDK) {
1432        op_bitwiseK(L, l_band);
1433        vmbreak;
1434      }
1435      vmcase(OP_BORK) {
1436        op_bitwiseK(L, l_bor);
1437        vmbreak;
1438      }
1439      vmcase(OP_BXORK) {
1440        op_bitwiseK(L, l_bxor);
1441        vmbreak;
1442      }
1443      vmcase(OP_SHRI) {
1444        StkId ra = RA(i);
1445        TValue *rb = vRB(i);
1446        int ic = GETARG_sC(i);
1447        lua_Integer ib;
1448        if (tointegerns(rb, &ib)) {
1449          pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic));
1450        }
1451        vmbreak;
1452      }
1453      vmcase(OP_SHLI) {
1454        StkId ra = RA(i);
1455        TValue *rb = vRB(i);
1456        int ic = GETARG_sC(i);
1457        lua_Integer ib;
1458        if (tointegerns(rb, &ib)) {
1459          pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib));
1460        }
1461        vmbreak;
1462      }
1463      vmcase(OP_ADD) {
1464        op_arith(L, l_addi, luai_numadd);
1465        vmbreak;
1466      }
1467      vmcase(OP_SUB) {
1468        op_arith(L, l_subi, luai_numsub);
1469        vmbreak;
1470      }
1471      vmcase(OP_MUL) {
1472        op_arith(L, l_muli, luai_nummul);
1473        vmbreak;
1474      }
1475      vmcase(OP_MOD) {
1476        savestate(L, ci);  /* in case of division by 0 */
1477        op_arith(L, luaV_mod, luaV_modf);
1478        vmbreak;
1479      }
1480      vmcase(OP_POW) {
1481        op_arithf(L, luai_numpow);
1482        vmbreak;
1483      }
1484      vmcase(OP_DIV) {  /* float division (always with floats) */
1485        op_arithf(L, luai_numdiv);
1486        vmbreak;
1487      }
1488      vmcase(OP_IDIV) {  /* floor division */
1489        savestate(L, ci);  /* in case of division by 0 */
1490        op_arith(L, luaV_idiv, luai_numidiv);
1491        vmbreak;
1492      }
1493      vmcase(OP_BAND) {
1494        op_bitwise(L, l_band);
1495        vmbreak;
1496      }
1497      vmcase(OP_BOR) {
1498        op_bitwise(L, l_bor);
1499        vmbreak;
1500      }
1501      vmcase(OP_BXOR) {
1502        op_bitwise(L, l_bxor);
1503        vmbreak;
1504      }
1505      vmcase(OP_SHR) {
1506        op_bitwise(L, luaV_shiftr);
1507        vmbreak;
1508      }
1509      vmcase(OP_SHL) {
1510        op_bitwise(L, luaV_shiftl);
1511        vmbreak;
1512      }
1513      vmcase(OP_MMBIN) {
1514        StkId ra = RA(i);
1515        Instruction pi = *(pc - 2);  /* original arith. expression */
1516        TValue *rb = vRB(i);
1517        TMS tm = (TMS)GETARG_C(i);
1518        StkId result = RA(pi);
1519        lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR);
1520        Protect(luaT_trybinTM(L, s2v(ra), rb, result, tm));
1521        vmbreak;
1522      }
1523      vmcase(OP_MMBINI) {
1524        StkId ra = RA(i);
1525        Instruction pi = *(pc - 2);  /* original arith. expression */
1526        int imm = GETARG_sB(i);
1527        TMS tm = (TMS)GETARG_C(i);
1528        int flip = GETARG_k(i);
1529        StkId result = RA(pi);
1530        Protect(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm));
1531        vmbreak;
1532      }
1533      vmcase(OP_MMBINK) {
1534        StkId ra = RA(i);
1535        Instruction pi = *(pc - 2);  /* original arith. expression */
1536        TValue *imm = KB(i);
1537        TMS tm = (TMS)GETARG_C(i);
1538        int flip = GETARG_k(i);
1539        StkId result = RA(pi);
1540        Protect(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm));
1541        vmbreak;
1542      }
1543      vmcase(OP_UNM) {
1544        StkId ra = RA(i);
1545        TValue *rb = vRB(i);
1546        lua_Number nb;
1547        if (ttisinteger(rb)) {
1548          lua_Integer ib = ivalue(rb);
1549          setivalue(s2v(ra), intop(-, 0, ib));
1550        }
1551        else if (tonumberns(rb, nb)) {
1552          setfltvalue(s2v(ra), luai_numunm(L, nb));
1553        }
1554        else
1555          Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
1556        vmbreak;
1557      }
1558      vmcase(OP_BNOT) {
1559        StkId ra = RA(i);
1560        TValue *rb = vRB(i);
1561        lua_Integer ib;
1562        if (tointegerns(rb, &ib)) {
1563          setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib));
1564        }
1565        else
1566          Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
1567        vmbreak;
1568      }
1569      vmcase(OP_NOT) {
1570        StkId ra = RA(i);
1571        TValue *rb = vRB(i);
1572        if (l_isfalse(rb))
1573          setbtvalue(s2v(ra));
1574        else
1575          setbfvalue(s2v(ra));
1576        vmbreak;
1577      }
1578      vmcase(OP_LEN) {
1579        StkId ra = RA(i);
1580        Protect(luaV_objlen(L, ra, vRB(i)));
1581        vmbreak;
1582      }
1583      vmcase(OP_CONCAT) {
1584        StkId ra = RA(i);
1585        int n = GETARG_B(i);  /* number of elements to concatenate */
1586        L->top.p = ra + n;  /* mark the end of concat operands */
1587        ProtectNT(luaV_concat(L, n));
1588        checkGC(L, L->top.p); /* 'luaV_concat' ensures correct top */
1589        vmbreak;
1590      }
1591      vmcase(OP_CLOSE) {
1592        StkId ra = RA(i);
1593        Protect(luaF_close(L, ra, LUA_OK, 1));
1594        vmbreak;
1595      }
1596      vmcase(OP_TBC) {
1597        StkId ra = RA(i);
1598        /* create new to-be-closed upvalue */
1599        halfProtect(luaF_newtbcupval(L, ra));
1600        vmbreak;
1601      }
1602      vmcase(OP_JMP) {
1603        dojump(ci, i, 0);
1604        vmbreak;
1605      }
1606      vmcase(OP_EQ) {
1607        StkId ra = RA(i);
1608        int cond;
1609        TValue *rb = vRB(i);
1610        Protect(cond = luaV_equalobj(L, s2v(ra), rb));
1611        docondjump();
1612        vmbreak;
1613      }
1614      vmcase(OP_LT) {
1615        op_order(L, l_lti, LTnum, lessthanothers);
1616        vmbreak;
1617      }
1618      vmcase(OP_LE) {
1619        op_order(L, l_lei, LEnum, lessequalothers);
1620        vmbreak;
1621      }
1622      vmcase(OP_EQK) {
1623        StkId ra = RA(i);
1624        TValue *rb = KB(i);
1625        /* basic types do not use '__eq'; we can use raw equality */
1626        int cond = luaV_rawequalobj(s2v(ra), rb);
1627        docondjump();
1628        vmbreak;
1629      }
1630      vmcase(OP_EQI) {
1631        StkId ra = RA(i);
1632        int cond;
1633        int im = GETARG_sB(i);
1634        if (ttisinteger(s2v(ra)))
1635          cond = (ivalue(s2v(ra)) == im);
1636        else if (ttisfloat(s2v(ra)))
1637          cond = luai_numeq(fltvalue(s2v(ra)), cast_num(im));
1638        else
1639          cond = 0;  /* other types cannot be equal to a number */
1640        docondjump();
1641        vmbreak;
1642      }
1643      vmcase(OP_LTI) {
1644        op_orderI(L, l_lti, luai_numlt, 0, TM_LT);
1645        vmbreak;
1646      }
1647      vmcase(OP_LEI) {
1648        op_orderI(L, l_lei, luai_numle, 0, TM_LE);
1649        vmbreak;
1650      }
1651      vmcase(OP_GTI) {
1652        op_orderI(L, l_gti, luai_numgt, 1, TM_LT);
1653        vmbreak;
1654      }
1655      vmcase(OP_GEI) {
1656        op_orderI(L, l_gei, luai_numge, 1, TM_LE);
1657        vmbreak;
1658      }
1659      vmcase(OP_TEST) {
1660        StkId ra = RA(i);
1661        int cond = !l_isfalse(s2v(ra));
1662        docondjump();
1663        vmbreak;
1664      }
1665      vmcase(OP_TESTSET) {
1666        StkId ra = RA(i);
1667        TValue *rb = vRB(i);
1668        if (l_isfalse(rb) == GETARG_k(i))
1669          pc++;
1670        else {
1671          setobj2s(L, ra, rb);
1672          donextjump(ci);
1673        }
1674        vmbreak;
1675      }
1676      vmcase(OP_CALL) {
1677        StkId ra = RA(i);
1678        CallInfo *newci;
1679        int b = GETARG_B(i);
1680        int nresults = GETARG_C(i) - 1;
1681        if (b != 0)  /* fixed number of arguments? */
1682          L->top.p = ra + b;  /* top signals number of arguments */
1683        /* else previous instruction set top */
1684        savepc(L);  /* in case of errors */
1685        if ((newci = luaD_precall(L, ra, nresults)) == NULL)
1686          updatetrap(ci);  /* C call; nothing else to be done */
1687        else {  /* Lua call: run function in this same C frame */
1688          ci = newci;
1689          goto startfunc;
1690        }
1691        vmbreak;
1692      }
1693      vmcase(OP_TAILCALL) {
1694        StkId ra = RA(i);
1695        int b = GETARG_B(i);  /* number of arguments + 1 (function) */
1696        int n;  /* number of results when calling a C function */
1697        int nparams1 = GETARG_C(i);
1698        /* delta is virtual 'func' - real 'func' (vararg functions) */
1699        int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0;
1700        if (b != 0)
1701          L->top.p = ra + b;
1702        else  /* previous instruction set top */
1703          b = cast_int(L->top.p - ra);
1704        savepc(ci);  /* several calls here can raise errors */
1705        if (TESTARG_k(i)) {
1706          luaF_closeupval(L, base);  /* close upvalues from current call */
1707          lua_assert(L->tbclist.p < base);  /* no pending tbc variables */
1708          lua_assert(base == ci->func.p + 1);
1709        }
1710        if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0)  /* Lua function? */
1711          goto startfunc;  /* execute the callee */
1712        else {  /* C function? */
1713          ci->func.p -= delta;  /* restore 'func' (if vararg) */
1714          luaD_poscall(L, ci, n);  /* finish caller */
1715          updatetrap(ci);  /* 'luaD_poscall' can change hooks */
1716          goto ret;  /* caller returns after the tail call */
1717        }
1718      }
1719      vmcase(OP_RETURN) {
1720        StkId ra = RA(i);
1721        int n = GETARG_B(i) - 1;  /* number of results */
1722        int nparams1 = GETARG_C(i);
1723        if (n < 0)  /* not fixed? */
1724          n = cast_int(L->top.p - ra);  /* get what is available */
1725        savepc(ci);
1726        if (TESTARG_k(i)) {  /* may there be open upvalues? */
1727          ci->u2.nres = n;  /* save number of returns */
1728          if (L->top.p < ci->top.p)
1729            L->top.p = ci->top.p;
1730          luaF_close(L, base, CLOSEKTOP, 1);
1731          updatetrap(ci);
1732          updatestack(ci);
1733        }
1734        if (nparams1)  /* vararg function? */
1735          ci->func.p -= ci->u.l.nextraargs + nparams1;
1736        L->top.p = ra + n;  /* set call for 'luaD_poscall' */
1737        luaD_poscall(L, ci, n);
1738        updatetrap(ci);  /* 'luaD_poscall' can change hooks */
1739        goto ret;
1740      }
1741      vmcase(OP_RETURN0) {
1742        if (l_unlikely(L->hookmask)) {
1743          StkId ra = RA(i);
1744          L->top.p = ra;
1745          savepc(ci);
1746          luaD_poscall(L, ci, 0);  /* no hurry... */
1747          trap = 1;
1748        }
1749        else {  /* do the 'poscall' here */
1750          int nres;
1751          L->ci = ci->previous;  /* back to caller */
1752          L->top.p = base - 1;
1753          for (nres = ci->nresults; l_unlikely(nres > 0); nres--)
1754            setnilvalue(s2v(L->top.p++));  /* all results are nil */
1755        }
1756        goto ret;
1757      }
1758      vmcase(OP_RETURN1) {
1759        if (l_unlikely(L->hookmask)) {
1760          StkId ra = RA(i);
1761          L->top.p = ra + 1;
1762          savepc(ci);
1763          luaD_poscall(L, ci, 1);  /* no hurry... */
1764          trap = 1;
1765        }
1766        else {  /* do the 'poscall' here */
1767          int nres = ci->nresults;
1768          L->ci = ci->previous;  /* back to caller */
1769          if (nres == 0)
1770            L->top.p = base - 1;  /* asked for no results */
1771          else {
1772            StkId ra = RA(i);
1773            setobjs2s(L, base - 1, ra);  /* at least this result */
1774            L->top.p = base;
1775            for (; l_unlikely(nres > 1); nres--)
1776              setnilvalue(s2v(L->top.p++));  /* complete missing results */
1777          }
1778        }
1779       ret:  /* return from a Lua function */
1780        if (ci->callstatus & CIST_FRESH)
1781          return;  /* end this frame */
1782        else {
1783          ci = ci->previous;
1784          goto returning;  /* continue running caller in this frame */
1785        }
1786      }
1787      vmcase(OP_FORLOOP) {
1788        StkId ra = RA(i);
1789        if (ttisinteger(s2v(ra + 2))) {  /* integer loop? */
1790          lua_Unsigned count = l_castS2U(ivalue(s2v(ra + 1)));
1791          if (count > 0) {  /* still more iterations? */
1792            lua_Integer step = ivalue(s2v(ra + 2));
1793            lua_Integer idx = ivalue(s2v(ra));  /* internal index */
1794            chgivalue(s2v(ra + 1), count - 1);  /* update counter */
1795            idx = intop(+, idx, step);  /* add step to index */
1796            chgivalue(s2v(ra), idx);  /* update internal index */
1797            setivalue(s2v(ra + 3), idx);  /* and control variable */
1798            pc -= GETARG_Bx(i);  /* jump back */
1799          }
1800        }
1801        else if (floatforloop(ra))  /* float loop */
1802          pc -= GETARG_Bx(i);  /* jump back */
1803        updatetrap(ci);  /* allows a signal to break the loop */
1804        vmbreak;
1805      }
1806      vmcase(OP_FORPREP) {
1807        StkId ra = RA(i);
1808        savestate(L, ci);  /* in case of errors */
1809        if (forprep(L, ra))
1810          pc += GETARG_Bx(i) + 1;  /* skip the loop */
1811        vmbreak;
1812      }
1813      vmcase(OP_TFORPREP) {
1814       StkId ra = RA(i);
1815        /* create to-be-closed upvalue (if needed) */
1816        halfProtect(luaF_newtbcupval(L, ra + 3));
1817        pc += GETARG_Bx(i);
1818        i = *(pc++);  /* go to next instruction */
1819        lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i));
1820        goto l_tforcall;
1821      }
1822      vmcase(OP_TFORCALL) {
1823       l_tforcall: {
1824        StkId ra = RA(i);
1825        /* 'ra' has the iterator function, 'ra + 1' has the state,
1826           'ra + 2' has the control variable, and 'ra + 3' has the
1827           to-be-closed variable. The call will use the stack after
1828           these values (starting at 'ra + 4')
1829        */
1830        /* push function, state, and control variable */
1831        memcpy(ra + 4, ra, 3 * sizeof(*ra));
1832        L->top.p = ra + 4 + 3;
1833        ProtectNT(luaD_call(L, ra + 4, GETARG_C(i)));  /* do the call */
1834        updatestack(ci);  /* stack may have changed */
1835        i = *(pc++);  /* go to next instruction */
1836        lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i));
1837        goto l_tforloop;
1838      }}
1839      vmcase(OP_TFORLOOP) {
1840       l_tforloop: {
1841        StkId ra = RA(i);
1842        if (!ttisnil(s2v(ra + 4))) {  /* continue loop? */
1843          setobjs2s(L, ra + 2, ra + 4);  /* save control variable */
1844          pc -= GETARG_Bx(i);  /* jump back */
1845        }
1846        vmbreak;
1847      }}
1848      vmcase(OP_SETLIST) {
1849        StkId ra = RA(i);
1850        int n = GETARG_B(i);
1851        unsigned int last = GETARG_C(i);
1852        Table *h = hvalue(s2v(ra));
1853        if (n == 0)
1854          n = cast_int(L->top.p - ra) - 1;  /* get up to the top */
1855        else
1856          L->top.p = ci->top.p;  /* correct top in case of emergency GC */
1857        last += n;
1858        if (TESTARG_k(i)) {
1859          last += GETARG_Ax(*pc) * (MAXARG_C + 1);
1860          pc++;
1861        }
1862        if (last > luaH_realasize(h))  /* needs more space? */
1863          luaH_resizearray(L, h, last);  /* preallocate it at once */
1864        for (; n > 0; n--) {
1865          TValue *val = s2v(ra + n);
1866          setobj2t(L, &h->array[last - 1], val);
1867          last--;
1868          luaC_barrierback(L, obj2gco(h), val);
1869        }
1870        vmbreak;
1871      }
1872      vmcase(OP_CLOSURE) {
1873        StkId ra = RA(i);
1874        Proto *p = cl->p->p[GETARG_Bx(i)];
1875        halfProtect(pushclosure(L, p, cl->upvals, base, ra));
1876        checkGC(L, ra + 1);
1877        vmbreak;
1878      }
1879      vmcase(OP_VARARG) {
1880        StkId ra = RA(i);
1881        int n = GETARG_C(i) - 1;  /* required results */
1882        Protect(luaT_getvarargs(L, ci, ra, n));
1883        vmbreak;
1884      }
1885      vmcase(OP_VARARGPREP) {
1886        ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p));
1887        if (l_unlikely(trap)) {  /* previous "Protect" updated trap */
1888          luaD_hookcall(L, ci);
1889          L->oldpc = 1;  /* next opcode will be seen as a "new" line */
1890        }
1891        updatebase(ci);  /* function has new base after adjustment */
1892        vmbreak;
1893      }
1894      vmcase(OP_EXTRAARG) {
1895        lua_assert(0);
1896        vmbreak;
1897      }
1898    }
1899  }
1900}
1901
1902/* }================================================================== */