1 /*
2 ** $Id: lvm.c,v 2.232 2014/12/27 20:30:38 roberto Exp $
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 
13 #include <limits.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #include "lua.h"
19 
20 #include "ldebug.h"
21 #include "ldo.h"
22 #include "lfunc.h"
23 #include "lgc.h"
24 #include "lobject.h"
25 #include "lopcodes.h"
26 #include "lstate.h"
27 #include "lstring.h"
28 #include "ltable.h"
29 #include "ltm.h"
30 #include "lvm.h"
31 
32 
33 /*
34 ** You can define LUA_FLOORN2I if you want to convert floats to integers
35 ** by flooring them (instead of raising an error if they are not
36 ** integral values)
37 */
38 #if !defined(LUA_FLOORN2I)
39 #define LUA_FLOORN2I		0
40 #endif
41 
42 
43 /* limit for table tag-method chains (to avoid loops) */
44 #define MAXTAGLOOP	2000
45 
46 
47 /*
48 ** Similar to 'tonumber', but does not attempt to convert strings and
49 ** ensure correct precision (no extra bits). Used in comparisons.
50 */
tofloat(const TValue * obj,lua_Number * n)51 static int tofloat (const TValue *obj, lua_Number *n) {
52   if (ttisfloat(obj)) *n = fltvalue(obj);
53   else if (ttisinteger(obj)) {
54     volatile lua_Number x = cast_num(ivalue(obj));  /* avoid extra precision */
55     *n = x;
56   }
57   else {
58     *n = 0;  /* to avoid warnings */
59     return 0;
60   }
61   return 1;
62 }
63 
64 
65 /*
66 ** Try to convert a value to a float. The float case is already handled
67 ** by the macro 'tonumber'.
68 */
luaV_tonumber_(const TValue * obj,lua_Number * n)69 int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
70   TValue v;
71   if (ttisinteger(obj)) {
72     *n = cast_num(ivalue(obj));
73     return 1;
74   }
75   else if (cvt2num(obj) &&  /* string convertible to number? */
76             luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
77     *n = nvalue(&v);  /* convert result of 'luaO_str2num' to a float */
78     return 1;
79   }
80   else
81     return 0;  /* conversion failed */
82 }
83 
84 
85 /*
86 ** try to convert a value to an integer, rounding according to 'mode':
87 ** mode == 0: accepts only integral values
88 ** mode == 1: takes the floor of the number
89 ** mode == 2: takes the ceil of the number
90 */
tointeger_aux(const TValue * obj,lua_Integer * p,int mode)91 static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) {
92   TValue v;
93  again:
94   if (ttisfloat(obj)) {
95     lua_Number n = fltvalue(obj);
96     lua_Number f = l_floor(n);
97     if (n != f) {  /* not an integral value? */
98       if (mode == 0) return 0;  /* fails if mode demands integral value */
99       else if (mode > 1)  /* needs ceil? */
100         f += 1;  /* convert floor to ceil (remember: n != f) */
101     }
102     return lua_numbertointeger(f, p);
103   }
104   else if (ttisinteger(obj)) {
105     *p = ivalue(obj);
106     return 1;
107   }
108   else if (cvt2num(obj) &&
109             luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
110     obj = &v;
111     goto again;  /* convert result from 'luaO_str2num' to an integer */
112   }
113   return 0;  /* conversion failed */
114 }
115 
116 
117 /*
118 ** try to convert a value to an integer
119 */
luaV_tointeger_(const TValue * obj,lua_Integer * p)120 int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
121   return tointeger_aux(obj, p, LUA_FLOORN2I);
122 }
123 
124 
125 /*
126 ** Try to convert a 'for' limit to an integer, preserving the
127 ** semantics of the loop.
128 ** (The following explanation assumes a non-negative step; it is valid
129 ** for negative steps mutatis mutandis.)
130 ** If the limit can be converted to an integer, rounding down, that is
131 ** it.
132 ** Otherwise, check whether the limit can be converted to a number.  If
133 ** the number is too large, it is OK to set the limit as LUA_MAXINTEGER,
134 ** which means no limit.  If the number is too negative, the loop
135 ** should not run, because any initial integer value is larger than the
136 ** limit. So, it sets the limit to LUA_MININTEGER. 'stopnow' corrects
137 ** the extreme case when the initial value is LUA_MININTEGER, in which
138 ** case the LUA_MININTEGER limit would still run the loop once.
139 */
forlimit(const TValue * obj,lua_Integer * p,lua_Integer step,int * stopnow)140 static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step,
141                      int *stopnow) {
142   *stopnow = 0;  /* usually, let loops run */
143   if (!tointeger_aux(obj, p, (step < 0 ? 2 : 1))) {  /* not fit in integer? */
144     lua_Number n;  /* try to convert to float */
145     if (!tonumber(obj, &n)) /* cannot convert to float? */
146       return 0;  /* not a number */
147     if (n > 0) {  /* if true, float is larger than max integer */
148       *p = LUA_MAXINTEGER;
149       if (step < 0) *stopnow = 1;
150     }
151     else {  /* float is smaller than min integer */
152       *p = LUA_MININTEGER;
153       if (step >= 0) *stopnow = 1;
154     }
155   }
156   return 1;
157 }
158 
159 
160 /*
161 ** Main function for table access (invoking metamethods if needed).
162 ** Compute 'val = t[key]'
163 */
luaV_gettable(lua_State * L,const TValue * t,TValue * key,StkId val)164 void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
165   int loop;  /* counter to avoid infinite loops */
166   for (loop = 0; loop < MAXTAGLOOP; loop++) {
167     const TValue *tm;
168     if (ttistable(t)) {  /* 't' is a table? */
169       Table *h = hvalue(t);
170       const TValue *res = luaH_get(h, key); /* do a primitive get */
171       if (!ttisnil(res) ||  /* result is not nil? */
172           (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
173         setobj2s(L, val, res);  /* result is the raw get */
174         return;
175       }
176       /* else will try metamethod */
177     }
178     else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
179       luaG_typeerror(L, t, "index");  /* no metamethod */
180     if (ttisfunction(tm)) {  /* metamethod is a function */
181       luaT_callTM(L, tm, t, key, val, 1);
182       return;
183     }
184     t = tm;  /* else repeat access over 'tm' */
185   }
186   luaG_runerror(L, "gettable chain too long; possible loop");
187 }
188 
189 
190 /*
191 ** Main function for table assignment (invoking metamethods if needed).
192 ** Compute 't[key] = val'
193 */
luaV_settable(lua_State * L,const TValue * t,TValue * key,StkId val)194 void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
195   int loop;  /* counter to avoid infinite loops */
196   for (loop = 0; loop < MAXTAGLOOP; loop++) {
197     const TValue *tm;
198     if (ttistable(t)) {  /* 't' is a table? */
199       Table *h = hvalue(t);
200       TValue *oldval = cast(TValue *, luaH_get(h, key));
201       /* if previous value is not nil, there must be a previous entry
202          in the table; a metamethod has no relevance */
203       if (!ttisnil(oldval) ||
204          /* previous value is nil; must check the metamethod */
205          ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&
206          /* no metamethod; is there a previous entry in the table? */
207          (oldval != luaO_nilobject ||
208          /* no previous entry; must create one. (The next test is
209             always true; we only need the assignment.) */
210          (oldval = luaH_newkey(L, h, key), 1)))) {
211         /* no metamethod and (now) there is an entry with given key */
212         setobj2t(L, oldval, val);  /* assign new value to that entry */
213         invalidateTMcache(h);
214         luaC_barrierback(L, h, val);
215         return;
216       }
217       /* else will try the metamethod */
218     }
219     else  /* not a table; check metamethod */
220       if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
221         luaG_typeerror(L, t, "index");
222     /* try the metamethod */
223     if (ttisfunction(tm)) {
224       luaT_callTM(L, tm, t, key, val, 0);
225       return;
226     }
227     t = tm;  /* else repeat assignment over 'tm' */
228   }
229   luaG_runerror(L, "settable chain too long; possible loop");
230 }
231 
232 
233 /*
234 ** Compare two strings 'ls' x 'rs', returning an integer smaller-equal-
235 ** -larger than zero if 'ls' is smaller-equal-larger than 'rs'.
236 ** The code is a little tricky because it allows '\0' in the strings
237 ** and it uses 'strcoll' (to respect locales) for each segments
238 ** of the strings.
239 */
l_strcmp(const TString * ls,const TString * rs)240 static int l_strcmp (const TString *ls, const TString *rs) {
241   const char *l = getstr(ls);
242   size_t ll = ls->len;
243   const char *r = getstr(rs);
244   size_t lr = rs->len;
245   for (;;) {  /* for each segment */
246     int temp = strcoll(l, r);
247     if (temp != 0)  /* not equal? */
248       return temp;  /* done */
249     else {  /* strings are equal up to a '\0' */
250       size_t len = strlen(l);  /* index of first '\0' in both strings */
251       if (len == lr)  /* 'rs' is finished? */
252         return (len == ll) ? 0 : 1;  /* check 'ls' */
253       else if (len == ll)  /* 'ls' is finished? */
254         return -1;  /* 'ls' is smaller than 'rs' ('rs' is not finished) */
255       /* both strings longer than 'len'; go on comparing after the '\0' */
256       len++;
257       l += len; ll -= len; r += len; lr -= len;
258     }
259   }
260 }
261 
262 
263 /*
264 ** Main operation less than; return 'l < r'.
265 */
luaV_lessthan(lua_State * L,const TValue * l,const TValue * r)266 int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
267   int res;
268   lua_Number nl, nr;
269   if (ttisinteger(l) && ttisinteger(r))  /* both operands are integers? */
270     return (ivalue(l) < ivalue(r));
271   else if (tofloat(l, &nl) && tofloat(r, &nr))  /* both are numbers? */
272     return luai_numlt(nl, nr);
273   else if (ttisstring(l) && ttisstring(r))  /* both are strings? */
274     return l_strcmp(tsvalue(l), tsvalue(r)) < 0;
275   else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0)  /* no metamethod? */
276     luaG_ordererror(L, l, r);  /* error */
277   return res;
278 }
279 
280 
281 /*
282 ** Main operation less than or equal to; return 'l <= r'.
283 */
luaV_lessequal(lua_State * L,const TValue * l,const TValue * r)284 int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
285   int res;
286   lua_Number nl, nr;
287   if (ttisinteger(l) && ttisinteger(r))  /* both operands are integers? */
288     return (ivalue(l) <= ivalue(r));
289   else if (tofloat(l, &nl) && tofloat(r, &nr))  /* both are numbers? */
290     return luai_numle(nl, nr);
291   else if (ttisstring(l) && ttisstring(r))  /* both are strings? */
292     return l_strcmp(tsvalue(l), tsvalue(r)) <= 0;
293   else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0)  /* first try 'le' */
294     return res;
295   else if ((res = luaT_callorderTM(L, r, l, TM_LT)) < 0)  /* else try 'lt' */
296     luaG_ordererror(L, l, r);
297   return !res;
298 }
299 
300 
301 /*
302 ** Main operation for equality of Lua values; return 't1 == t2'.
303 ** L == NULL means raw equality (no metamethods)
304 */
luaV_equalobj(lua_State * L,const TValue * t1,const TValue * t2)305 int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
306   const TValue *tm;
307   if (ttype(t1) != ttype(t2)) {  /* not the same variant? */
308     if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER)
309       return 0;  /* only numbers can be equal with different variants */
310     else {  /* two numbers with different variants */
311       lua_Number n1, n2;  /* compare them as floats */
312       lua_assert(ttisnumber(t1) && ttisnumber(t2));
313       cast_void(tofloat(t1, &n1)); cast_void(tofloat(t2, &n2));
314       return luai_numeq(n1, n2);
315     }
316   }
317   /* values have same type and same variant */
318   switch (ttype(t1)) {
319     case LUA_TNIL: return 1;
320     case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2));
321     case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2));
322     case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2);  /* true must be 1 !! */
323     case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
324     case LUA_TLCF: return fvalue(t1) == fvalue(t2);
325     case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2));
326     case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2));
327     case LUA_TUSERDATA: {
328       if (uvalue(t1) == uvalue(t2)) return 1;
329       else if (L == NULL) return 0;
330       tm = fasttm(L, uvalue(t1)->metatable, TM_EQ);
331       if (tm == NULL)
332         tm = fasttm(L, uvalue(t2)->metatable, TM_EQ);
333       break;  /* will try TM */
334     }
335     case LUA_TTABLE: {
336       if (hvalue(t1) == hvalue(t2)) return 1;
337       else if (L == NULL) return 0;
338       tm = fasttm(L, hvalue(t1)->metatable, TM_EQ);
339       if (tm == NULL)
340         tm = fasttm(L, hvalue(t2)->metatable, TM_EQ);
341       break;  /* will try TM */
342     }
343     default:
344       return gcvalue(t1) == gcvalue(t2);
345   }
346   if (tm == NULL)  /* no TM? */
347     return 0;  /* objects are different */
348   luaT_callTM(L, tm, t1, t2, L->top, 1);  /* call TM */
349   return !l_isfalse(L->top);
350 }
351 
352 
353 /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */
354 #define tostring(L,o)  \
355 	(ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1)))
356 
357 /*
358 ** Main operation for concatenation: concat 'total' values in the stack,
359 ** from 'L->top - total' up to 'L->top - 1'.
360 */
luaV_concat(lua_State * L,int total)361 void luaV_concat (lua_State *L, int total) {
362   lua_assert(total >= 2);
363   do {
364     StkId top = L->top;
365     int n = 2;  /* number of elements handled in this pass (at least 2) */
366     if (!(ttisstring(top-2) || cvt2str(top-2)) || !tostring(L, top-1))
367       luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT);
368     else if (tsvalue(top-1)->len == 0)  /* second operand is empty? */
369       cast_void(tostring(L, top - 2));  /* result is first operand */
370     else if (ttisstring(top-2) && tsvalue(top-2)->len == 0) {
371       setobjs2s(L, top - 2, top - 1);  /* result is second op. */
372     }
373     else {
374       /* at least two non-empty string values; get as many as possible */
375       size_t tl = tsvalue(top-1)->len;
376       char *buffer;
377       int i;
378       /* collect total length */
379       for (i = 1; i < total && tostring(L, top-i-1); i++) {
380         size_t l = tsvalue(top-i-1)->len;
381         if (l >= (MAX_SIZE/sizeof(char)) - tl)
382           luaG_runerror(L, "string length overflow");
383         tl += l;
384       }
385       buffer = luaZ_openspace(L, &G(L)->buff, tl);
386       tl = 0;
387       n = i;
388       do {  /* copy all strings to buffer */
389         size_t l = tsvalue(top-i)->len;
390         memcpy(buffer+tl, svalue(top-i), l * sizeof(char));
391         tl += l;
392       } while (--i > 0);
393       setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));  /* create result */
394     }
395     total -= n-1;  /* got 'n' strings to create 1 new */
396     L->top -= n-1;  /* popped 'n' strings and pushed one */
397   } while (total > 1);  /* repeat until only 1 result left */
398 }
399 
400 
401 /*
402 ** Main operation 'ra' = #rb'.
403 */
luaV_objlen(lua_State * L,StkId ra,const TValue * rb)404 void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
405   const TValue *tm;
406   switch (ttnov(rb)) {
407     case LUA_TTABLE: {
408       Table *h = hvalue(rb);
409       tm = fasttm(L, h->metatable, TM_LEN);
410       if (tm) break;  /* metamethod? break switch to call it */
411       setivalue(ra, luaH_getn(h));  /* else primitive len */
412       return;
413     }
414     case LUA_TSTRING: {
415       setivalue(ra, tsvalue(rb)->len);
416       return;
417     }
418     default: {  /* try metamethod */
419       tm = luaT_gettmbyobj(L, rb, TM_LEN);
420       if (ttisnil(tm))  /* no metamethod? */
421         luaG_typeerror(L, rb, "get length of");
422       break;
423     }
424   }
425   luaT_callTM(L, tm, rb, rb, ra, 1);
426 }
427 
428 
429 /*
430 ** Integer division; return 'm // n', that is, floor(m/n).
431 ** C division truncates its result (rounds towards zero).
432 ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer,
433 ** otherwise 'floor(q) == trunc(q) - 1'.
434 */
luaV_div(lua_State * L,lua_Integer m,lua_Integer n)435 lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) {
436   if (l_castS2U(n) + 1u <= 1u) {  /* special cases: -1 or 0 */
437     if (n == 0)
438       luaG_runerror(L, "attempt to divide by zero");
439     return intop(-, 0, m);   /* n==-1; avoid overflow with 0x80000...//-1 */
440   }
441   else {
442     lua_Integer q = m / n;  /* perform C division */
443     if ((m ^ n) < 0 && m % n != 0)  /* 'm/n' would be negative non-integer? */
444       q -= 1;  /* correct result for different rounding */
445     return q;
446   }
447 }
448 
449 
450 /*
451 ** Integer modulus; return 'm % n'. (Assume that C '%' with
452 ** negative operands follows C99 behavior. See previous comment
453 ** about luaV_div.)
454 */
luaV_mod(lua_State * L,lua_Integer m,lua_Integer n)455 lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
456   if (l_castS2U(n) + 1u <= 1u) {  /* special cases: -1 or 0 */
457     if (n == 0)
458       luaG_runerror(L, "attempt to perform 'n%%0'");
459     return 0;   /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
460   }
461   else {
462     lua_Integer r = m % n;
463     if (r != 0 && (m ^ n) < 0)  /* 'm/n' would be non-integer negative? */
464       r += n;  /* correct result for different rounding */
465     return r;
466   }
467 }
468 
469 
470 /* number of bits in an integer */
471 #define NBITS	cast_int(sizeof(lua_Integer) * CHAR_BIT)
472 
473 /*
474 ** Shift left operation. (Shift right just negates 'y'.)
475 */
luaV_shiftl(lua_Integer x,lua_Integer y)476 lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
477   if (y < 0) {  /* shift right? */
478     if (y <= -NBITS) return 0;
479     else return intop(>>, x, -y);
480   }
481   else {  /* shift left */
482     if (y >= NBITS) return 0;
483     else return intop(<<, x, y);
484   }
485 }
486 
487 
488 /*
489 ** check whether cached closure in prototype 'p' may be reused, that is,
490 ** whether there is a cached closure with the same upvalues needed by
491 ** new closure to be created.
492 */
getcached(Proto * p,UpVal ** encup,StkId base)493 static LClosure *getcached (Proto *p, UpVal **encup, StkId base) {
494   LClosure *c = p->cache;
495   if (c != NULL) {  /* is there a cached closure? */
496     int nup = p->sizeupvalues;
497     Upvaldesc *uv = p->upvalues;
498     int i;
499     for (i = 0; i < nup; i++) {  /* check whether it has right upvalues */
500       TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v;
501       if (c->upvals[i]->v != v)
502         return NULL;  /* wrong upvalue; cannot reuse closure */
503     }
504   }
505   return c;  /* return cached closure (or NULL if no cached closure) */
506 }
507 
508 
509 /*
510 ** create a new Lua closure, push it in the stack, and initialize
511 ** its upvalues. Note that the closure is not cached if prototype is
512 ** already black (which means that 'cache' was already cleared by the
513 ** GC).
514 */
pushclosure(lua_State * L,Proto * p,UpVal ** encup,StkId base,StkId ra)515 static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
516                          StkId ra) {
517   int nup = p->sizeupvalues;
518   Upvaldesc *uv = p->upvalues;
519   int i;
520   LClosure *ncl = luaF_newLclosure(L, nup);
521   ncl->p = p;
522   setclLvalue(L, ra, ncl);  /* anchor new closure in stack */
523   for (i = 0; i < nup; i++) {  /* fill in its upvalues */
524     if (uv[i].instack)  /* upvalue refers to local variable? */
525       ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx);
526     else  /* get upvalue from enclosing function */
527       ncl->upvals[i] = encup[uv[i].idx];
528     ncl->upvals[i]->refcount++;
529     /* new closure is white, so we do not need a barrier here */
530   }
531   if (!isblack(p))  /* cache will not break GC invariant? */
532     p->cache = ncl;  /* save it on cache for reuse */
533 }
534 
535 
536 /*
537 ** finish execution of an opcode interrupted by an yield
538 */
luaV_finishOp(lua_State * L)539 void luaV_finishOp (lua_State *L) {
540   CallInfo *ci = L->ci;
541   StkId base = ci->u.l.base;
542   Instruction inst = *(ci->u.l.savedpc - 1);  /* interrupted instruction */
543   OpCode op = GET_OPCODE(inst);
544   switch (op) {  /* finish its execution */
545     case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV:
546     case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR:
547     case OP_MOD: case OP_POW:
548     case OP_UNM: case OP_BNOT: case OP_LEN:
549     case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: {
550       setobjs2s(L, base + GETARG_A(inst), --L->top);
551       break;
552     }
553     case OP_LE: case OP_LT: case OP_EQ: {
554       int res = !l_isfalse(L->top - 1);
555       L->top--;
556       /* metamethod should not be called when operand is K */
557       lua_assert(!ISK(GETARG_B(inst)));
558       if (op == OP_LE &&  /* "<=" using "<" instead? */
559           ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE)))
560         res = !res;  /* invert result */
561       lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
562       if (res != GETARG_A(inst))  /* condition failed? */
563         ci->u.l.savedpc++;  /* skip jump instruction */
564       break;
565     }
566     case OP_CONCAT: {
567       StkId top = L->top - 1;  /* top when 'luaT_trybinTM' was called */
568       int b = GETARG_B(inst);      /* first element to concatenate */
569       int total = cast_int(top - 1 - (base + b));  /* yet to concatenate */
570       setobj2s(L, top - 2, top);  /* put TM result in proper position */
571       if (total > 1) {  /* are there elements to concat? */
572         L->top = top - 1;  /* top is one after last element (at top-2) */
573         luaV_concat(L, total);  /* concat them (may yield again) */
574       }
575       /* move final result to final position */
576       setobj2s(L, ci->u.l.base + GETARG_A(inst), L->top - 1);
577       L->top = ci->top;  /* restore top */
578       break;
579     }
580     case OP_TFORCALL: {
581       lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP);
582       L->top = ci->top;  /* correct top */
583       break;
584     }
585     case OP_CALL: {
586       if (GETARG_C(inst) - 1 >= 0)  /* nresults >= 0? */
587         L->top = ci->top;  /* adjust results */
588       break;
589     }
590     case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE:
591       break;
592     default: lua_assert(0);
593   }
594 }
595 
596 
597 
598 
599 /*
600 ** {==================================================================
601 ** Function 'luaV_execute': main interpreter loop
602 ** ===================================================================
603 */
604 
605 
606 /*
607 ** some macros for common tasks in 'luaV_execute'
608 */
609 
610 #if !defined luai_runtimecheck
611 #define luai_runtimecheck(L, c)		/* void */
612 #endif
613 
614 
615 #define RA(i)	(base+GETARG_A(i))
616 /* to be used after possible stack reallocation */
617 #define RB(i)	check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
618 #define RC(i)	check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
619 #define RKB(i)	check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
620 	ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
621 #define RKC(i)	check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
622 	ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
623 #define KBx(i)  \
624   (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
625 
626 
627 /* execute a jump instruction */
628 #define dojump(ci,i,e) \
629   { int a = GETARG_A(i); \
630     if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
631     ci->u.l.savedpc += GETARG_sBx(i) + e; }
632 
633 /* for test instructions, execute the jump instruction that follows it */
634 #define donextjump(ci)	{ i = *ci->u.l.savedpc; dojump(ci, i, 1); }
635 
636 
637 #define Protect(x)	{ {x;}; base = ci->u.l.base; }
638 
639 #define checkGC(L,c)  \
640   Protect( luaC_condGC(L,{L->top = (c);  /* limit of live values */ \
641                           luaC_step(L); \
642                           L->top = ci->top;})  /* restore top */ \
643            luai_threadyield(L); )
644 
645 
646 #define vmdispatch(o)	switch(o)
647 #define vmcase(l)	case l:
648 #define vmbreak		break
649 
luaV_execute(lua_State * L)650 void luaV_execute (lua_State *L) {
651   CallInfo *ci = L->ci;
652   LClosure *cl;
653   TValue *k;
654   StkId base;
655  newframe:  /* reentry point when frame changes (call/return) */
656   lua_assert(ci == L->ci);
657   cl = clLvalue(ci->func);
658   k = cl->p->k;
659   base = ci->u.l.base;
660   /* main loop of interpreter */
661   for (;;) {
662     Instruction i = *(ci->u.l.savedpc++);
663     StkId ra;
664     if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
665         (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
666       Protect(luaG_traceexec(L));
667     }
668     /* WARNING: several calls may realloc the stack and invalidate 'ra' */
669     ra = RA(i);
670     lua_assert(base == ci->u.l.base);
671     lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
672     vmdispatch (GET_OPCODE(i)) {
673       vmcase(OP_MOVE) {
674         setobjs2s(L, ra, RB(i));
675         vmbreak;
676       }
677       vmcase(OP_LOADK) {
678         TValue *rb = k + GETARG_Bx(i);
679         setobj2s(L, ra, rb);
680         vmbreak;
681       }
682       vmcase(OP_LOADKX) {
683         TValue *rb;
684         lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
685         rb = k + GETARG_Ax(*ci->u.l.savedpc++);
686         setobj2s(L, ra, rb);
687         vmbreak;
688       }
689       vmcase(OP_LOADBOOL) {
690         setbvalue(ra, GETARG_B(i));
691         if (GETARG_C(i)) ci->u.l.savedpc++;  /* skip next instruction (if C) */
692         vmbreak;
693       }
694       vmcase(OP_LOADNIL) {
695         int b = GETARG_B(i);
696         do {
697           setnilvalue(ra++);
698         } while (b--);
699         vmbreak;
700       }
701       vmcase(OP_GETUPVAL) {
702         int b = GETARG_B(i);
703         setobj2s(L, ra, cl->upvals[b]->v);
704         vmbreak;
705       }
706       vmcase(OP_GETTABUP) {
707         int b = GETARG_B(i);
708         Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra));
709         vmbreak;
710       }
711       vmcase(OP_GETTABLE) {
712         Protect(luaV_gettable(L, RB(i), RKC(i), ra));
713         vmbreak;
714       }
715       vmcase(OP_SETTABUP) {
716         int a = GETARG_A(i);
717         Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i)));
718         vmbreak;
719       }
720       vmcase(OP_SETUPVAL) {
721         UpVal *uv = cl->upvals[GETARG_B(i)];
722         setobj(L, uv->v, ra);
723         luaC_upvalbarrier(L, uv);
724         vmbreak;
725       }
726       vmcase(OP_SETTABLE) {
727         Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
728         vmbreak;
729       }
730       vmcase(OP_NEWTABLE) {
731         int b = GETARG_B(i);
732         int c = GETARG_C(i);
733         Table *t = luaH_new(L);
734         sethvalue(L, ra, t);
735         if (b != 0 || c != 0)
736           luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
737         checkGC(L, ra + 1);
738         vmbreak;
739       }
740       vmcase(OP_SELF) {
741         StkId rb = RB(i);
742         setobjs2s(L, ra+1, rb);
743         Protect(luaV_gettable(L, rb, RKC(i), ra));
744         vmbreak;
745       }
746       vmcase(OP_ADD) {
747         TValue *rb = RKB(i);
748         TValue *rc = RKC(i);
749         lua_Number nb; lua_Number nc;
750         if (ttisinteger(rb) && ttisinteger(rc)) {
751           lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
752           setivalue(ra, intop(+, ib, ic));
753         }
754         else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
755           setfltvalue(ra, luai_numadd(L, nb, nc));
756         }
757         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); }
758         vmbreak;
759       }
760       vmcase(OP_SUB) {
761         TValue *rb = RKB(i);
762         TValue *rc = RKC(i);
763         lua_Number nb; lua_Number nc;
764         if (ttisinteger(rb) && ttisinteger(rc)) {
765           lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
766           setivalue(ra, intop(-, ib, ic));
767         }
768         else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
769           setfltvalue(ra, luai_numsub(L, nb, nc));
770         }
771         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); }
772         vmbreak;
773       }
774       vmcase(OP_MUL) {
775         TValue *rb = RKB(i);
776         TValue *rc = RKC(i);
777         lua_Number nb; lua_Number nc;
778         if (ttisinteger(rb) && ttisinteger(rc)) {
779           lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
780           setivalue(ra, intop(*, ib, ic));
781         }
782         else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
783           setfltvalue(ra, luai_nummul(L, nb, nc));
784         }
785         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); }
786         vmbreak;
787       }
788       vmcase(OP_DIV) {  /* float division (always with floats) */
789         TValue *rb = RKB(i);
790         TValue *rc = RKC(i);
791         lua_Number nb; lua_Number nc;
792         if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
793           setfltvalue(ra, luai_numdiv(L, nb, nc));
794         }
795         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); }
796         vmbreak;
797       }
798       vmcase(OP_BAND) {
799         TValue *rb = RKB(i);
800         TValue *rc = RKC(i);
801         lua_Integer ib; lua_Integer ic;
802         if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
803           setivalue(ra, intop(&, ib, ic));
804         }
805         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); }
806         vmbreak;
807       }
808       vmcase(OP_BOR) {
809         TValue *rb = RKB(i);
810         TValue *rc = RKC(i);
811         lua_Integer ib; lua_Integer ic;
812         if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
813           setivalue(ra, intop(|, ib, ic));
814         }
815         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); }
816         vmbreak;
817       }
818       vmcase(OP_BXOR) {
819         TValue *rb = RKB(i);
820         TValue *rc = RKC(i);
821         lua_Integer ib; lua_Integer ic;
822         if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
823           setivalue(ra, intop(^, ib, ic));
824         }
825         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); }
826         vmbreak;
827       }
828       vmcase(OP_SHL) {
829         TValue *rb = RKB(i);
830         TValue *rc = RKC(i);
831         lua_Integer ib; lua_Integer ic;
832         if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
833           setivalue(ra, luaV_shiftl(ib, ic));
834         }
835         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); }
836         vmbreak;
837       }
838       vmcase(OP_SHR) {
839         TValue *rb = RKB(i);
840         TValue *rc = RKC(i);
841         lua_Integer ib; lua_Integer ic;
842         if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
843           setivalue(ra, luaV_shiftl(ib, -ic));
844         }
845         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); }
846         vmbreak;
847       }
848       vmcase(OP_MOD) {
849         TValue *rb = RKB(i);
850         TValue *rc = RKC(i);
851         lua_Number nb; lua_Number nc;
852         if (ttisinteger(rb) && ttisinteger(rc)) {
853           lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
854           setivalue(ra, luaV_mod(L, ib, ic));
855         }
856         else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
857           lua_Number m;
858           luai_nummod(L, nb, nc, m);
859           setfltvalue(ra, m);
860         }
861         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); }
862         vmbreak;
863       }
864       vmcase(OP_IDIV) {  /* floor division */
865         TValue *rb = RKB(i);
866         TValue *rc = RKC(i);
867         lua_Number nb; lua_Number nc;
868         if (ttisinteger(rb) && ttisinteger(rc)) {
869           lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
870           setivalue(ra, luaV_div(L, ib, ic));
871         }
872         else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
873           setfltvalue(ra, luai_numidiv(L, nb, nc));
874         }
875         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); }
876         vmbreak;
877       }
878       vmcase(OP_POW) {
879         TValue *rb = RKB(i);
880         TValue *rc = RKC(i);
881         lua_Number nb; lua_Number nc;
882         if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
883           setfltvalue(ra, luai_numpow(L, nb, nc));
884         }
885         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); }
886         vmbreak;
887       }
888       vmcase(OP_UNM) {
889         TValue *rb = RB(i);
890         lua_Number nb;
891         if (ttisinteger(rb)) {
892           lua_Integer ib = ivalue(rb);
893           setivalue(ra, intop(-, 0, ib));
894         }
895         else if (tonumber(rb, &nb)) {
896           setfltvalue(ra, luai_numunm(L, nb));
897         }
898         else {
899           Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
900         }
901         vmbreak;
902       }
903       vmcase(OP_BNOT) {
904         TValue *rb = RB(i);
905         lua_Integer ib;
906         if (tointeger(rb, &ib)) {
907           setivalue(ra, intop(^, ~l_castS2U(0), ib));
908         }
909         else {
910           Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
911         }
912         vmbreak;
913       }
914       vmcase(OP_NOT) {
915         TValue *rb = RB(i);
916         int res = l_isfalse(rb);  /* next assignment may change this value */
917         setbvalue(ra, res);
918         vmbreak;
919       }
920       vmcase(OP_LEN) {
921         Protect(luaV_objlen(L, ra, RB(i)));
922         vmbreak;
923       }
924       vmcase(OP_CONCAT) {
925         int b = GETARG_B(i);
926         int c = GETARG_C(i);
927         StkId rb;
928         L->top = base + c + 1;  /* mark the end of concat operands */
929         Protect(luaV_concat(L, c - b + 1));
930         ra = RA(i);  /* 'luav_concat' may invoke TMs and move the stack */
931         rb = b + base;
932         setobjs2s(L, ra, rb);
933         checkGC(L, (ra >= rb ? ra + 1 : rb));
934         L->top = ci->top;  /* restore top */
935         vmbreak;
936       }
937       vmcase(OP_JMP) {
938         dojump(ci, i, 0);
939         vmbreak;
940       }
941       vmcase(OP_EQ) {
942         TValue *rb = RKB(i);
943         TValue *rc = RKC(i);
944         Protect(
945           if (cast_int(luaV_equalobj(L, rb, rc)) != GETARG_A(i))
946             ci->u.l.savedpc++;
947           else
948             donextjump(ci);
949         )
950         vmbreak;
951       }
952       vmcase(OP_LT) {
953         Protect(
954           if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i))
955             ci->u.l.savedpc++;
956           else
957             donextjump(ci);
958         )
959         vmbreak;
960       }
961       vmcase(OP_LE) {
962         Protect(
963           if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i))
964             ci->u.l.savedpc++;
965           else
966             donextjump(ci);
967         )
968         vmbreak;
969       }
970       vmcase(OP_TEST) {
971         if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra))
972             ci->u.l.savedpc++;
973           else
974           donextjump(ci);
975         vmbreak;
976       }
977       vmcase(OP_TESTSET) {
978         TValue *rb = RB(i);
979         if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
980           ci->u.l.savedpc++;
981         else {
982           setobjs2s(L, ra, rb);
983           donextjump(ci);
984         }
985         vmbreak;
986       }
987       vmcase(OP_CALL) {
988         int b = GETARG_B(i);
989         int nresults = GETARG_C(i) - 1;
990         if (b != 0) L->top = ra+b;  /* else previous instruction set top */
991         if (luaD_precall(L, ra, nresults)) {  /* C function? */
992           if (nresults >= 0) L->top = ci->top;  /* adjust results */
993           base = ci->u.l.base;
994         }
995         else {  /* Lua function */
996           ci = L->ci;
997           ci->callstatus |= CIST_REENTRY;
998           goto newframe;  /* restart luaV_execute over new Lua function */
999         }
1000         vmbreak;
1001       }
1002       vmcase(OP_TAILCALL) {
1003         int b = GETARG_B(i);
1004         if (b != 0) L->top = ra+b;  /* else previous instruction set top */
1005         lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
1006         if (luaD_precall(L, ra, LUA_MULTRET))  /* C function? */
1007           base = ci->u.l.base;
1008         else {
1009           /* tail call: put called frame (n) in place of caller one (o) */
1010           CallInfo *nci = L->ci;  /* called frame */
1011           CallInfo *oci = nci->previous;  /* caller frame */
1012           StkId nfunc = nci->func;  /* called function */
1013           StkId ofunc = oci->func;  /* caller function */
1014           /* last stack slot filled by 'precall' */
1015           StkId lim = nci->u.l.base + getproto(nfunc)->numparams;
1016           int aux;
1017           /* close all upvalues from previous call */
1018           if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base);
1019           /* move new frame into old one */
1020           for (aux = 0; nfunc + aux < lim; aux++)
1021             setobjs2s(L, ofunc + aux, nfunc + aux);
1022           oci->u.l.base = ofunc + (nci->u.l.base - nfunc);  /* correct base */
1023           oci->top = L->top = ofunc + (L->top - nfunc);  /* correct top */
1024           oci->u.l.savedpc = nci->u.l.savedpc;
1025           oci->callstatus |= CIST_TAIL;  /* function was tail called */
1026           ci = L->ci = oci;  /* remove new frame */
1027           lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize);
1028           goto newframe;  /* restart luaV_execute over new Lua function */
1029         }
1030         vmbreak;
1031       }
1032       vmcase(OP_RETURN) {
1033         int b = GETARG_B(i);
1034         if (b != 0) L->top = ra+b-1;
1035         if (cl->p->sizep > 0) luaF_close(L, base);
1036         b = luaD_poscall(L, ra);
1037         if (!(ci->callstatus & CIST_REENTRY))  /* 'ci' still the called one */
1038           return;  /* external invocation: return */
1039         else {  /* invocation via reentry: continue execution */
1040           ci = L->ci;
1041           if (b) L->top = ci->top;
1042           lua_assert(isLua(ci));
1043           lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
1044           goto newframe;  /* restart luaV_execute over new Lua function */
1045         }
1046       }
1047       vmcase(OP_FORLOOP) {
1048         if (ttisinteger(ra)) {  /* integer loop? */
1049           lua_Integer step = ivalue(ra + 2);
1050           lua_Integer idx = ivalue(ra) + step; /* increment index */
1051           lua_Integer limit = ivalue(ra + 1);
1052           if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
1053             ci->u.l.savedpc += GETARG_sBx(i);  /* jump back */
1054             setivalue(ra, idx);  /* update internal index... */
1055             setivalue(ra + 3, idx);  /* ...and external index */
1056           }
1057         }
1058         else {  /* floating loop */
1059           lua_Number step = fltvalue(ra + 2);
1060           lua_Number idx = luai_numadd(L, fltvalue(ra), step); /* inc. index */
1061           lua_Number limit = fltvalue(ra + 1);
1062           if (luai_numlt(0, step) ? luai_numle(idx, limit)
1063                                   : luai_numle(limit, idx)) {
1064             ci->u.l.savedpc += GETARG_sBx(i);  /* jump back */
1065             setfltvalue(ra, idx);  /* update internal index... */
1066             setfltvalue(ra + 3, idx);  /* ...and external index */
1067           }
1068         }
1069         vmbreak;
1070       }
1071       vmcase(OP_FORPREP) {
1072         TValue *init = ra;
1073         TValue *plimit = ra + 1;
1074         TValue *pstep = ra + 2;
1075         lua_Integer ilimit;
1076         int stopnow;
1077         if (ttisinteger(init) && ttisinteger(pstep) &&
1078             forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) {
1079           /* all values are integer */
1080           lua_Integer initv = (stopnow ? 0 : ivalue(init));
1081           setivalue(plimit, ilimit);
1082           setivalue(init, initv - ivalue(pstep));
1083         }
1084         else {  /* try making all values floats */
1085           lua_Number ninit; lua_Number nlimit; lua_Number nstep;
1086           if (!tonumber(plimit, &nlimit))
1087             luaG_runerror(L, "'for' limit must be a number");
1088           setfltvalue(plimit, nlimit);
1089           if (!tonumber(pstep, &nstep))
1090             luaG_runerror(L, "'for' step must be a number");
1091           setfltvalue(pstep, nstep);
1092           if (!tonumber(init, &ninit))
1093             luaG_runerror(L, "'for' initial value must be a number");
1094           setfltvalue(init, luai_numsub(L, ninit, nstep));
1095         }
1096         ci->u.l.savedpc += GETARG_sBx(i);
1097         vmbreak;
1098       }
1099       vmcase(OP_TFORCALL) {
1100         StkId cb = ra + 3;  /* call base */
1101         setobjs2s(L, cb+2, ra+2);
1102         setobjs2s(L, cb+1, ra+1);
1103         setobjs2s(L, cb, ra);
1104         L->top = cb + 3;  /* func. + 2 args (state and index) */
1105         Protect(luaD_call(L, cb, GETARG_C(i), 1));
1106         L->top = ci->top;
1107         i = *(ci->u.l.savedpc++);  /* go to next instruction */
1108         ra = RA(i);
1109         lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
1110         goto l_tforloop;
1111       }
1112       vmcase(OP_TFORLOOP) {
1113         l_tforloop:
1114         if (!ttisnil(ra + 1)) {  /* continue loop? */
1115           setobjs2s(L, ra, ra + 1);  /* save control variable */
1116            ci->u.l.savedpc += GETARG_sBx(i);  /* jump back */
1117         }
1118         vmbreak;
1119       }
1120       vmcase(OP_SETLIST) {
1121         int n = GETARG_B(i);
1122         int c = GETARG_C(i);
1123         unsigned int last;
1124         Table *h;
1125         if (n == 0) n = cast_int(L->top - ra) - 1;
1126         if (c == 0) {
1127           lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
1128           c = GETARG_Ax(*ci->u.l.savedpc++);
1129         }
1130         luai_runtimecheck(L, ttistable(ra));
1131         h = hvalue(ra);
1132         last = ((c-1)*LFIELDS_PER_FLUSH) + n;
1133         if (last > h->sizearray)  /* needs more space? */
1134           luaH_resizearray(L, h, last);  /* pre-allocate it at once */
1135         for (; n > 0; n--) {
1136           TValue *val = ra+n;
1137           luaH_setint(L, h, last--, val);
1138           luaC_barrierback(L, h, val);
1139         }
1140         L->top = ci->top;  /* correct top (in case of previous open call) */
1141         vmbreak;
1142       }
1143       vmcase(OP_CLOSURE) {
1144         Proto *p = cl->p->p[GETARG_Bx(i)];
1145         LClosure *ncl = getcached(p, cl->upvals, base);  /* cached closure */
1146         if (ncl == NULL)  /* no match? */
1147           pushclosure(L, p, cl->upvals, base, ra);  /* create a new one */
1148         else
1149           setclLvalue(L, ra, ncl);  /* push cashed closure */
1150         checkGC(L, ra + 1);
1151         vmbreak;
1152       }
1153       vmcase(OP_VARARG) {
1154         int b = GETARG_B(i) - 1;
1155         int j;
1156         int n = cast_int(base - ci->func) - cl->p->numparams - 1;
1157         if (b < 0) {  /* B == 0? */
1158           b = n;  /* get all var. arguments */
1159           Protect(luaD_checkstack(L, n));
1160           ra = RA(i);  /* previous call may change the stack */
1161           L->top = ra + n;
1162         }
1163         for (j = 0; j < b; j++) {
1164           if (j < n) {
1165             setobjs2s(L, ra + j, base - n + j);
1166           }
1167           else {
1168             setnilvalue(ra + j);
1169           }
1170         }
1171         vmbreak;
1172       }
1173       vmcase(OP_EXTRAARG) {
1174         lua_assert(0);
1175         vmbreak;
1176       }
1177     }
1178   }
1179 }
1180 
1181 /* }================================================================== */
1182 
1183