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