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