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