interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / lvm.c

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