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