?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

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