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