1 /* $NetBSD: lvm.c,v 1.2 2014/03/26 22:03:26 christos Exp $ */ 2 3 /* 4 ** $Id: lvm.c,v 1.2 2014/03/26 22:03:26 christos Exp $ 5 ** Lua virtual machine 6 ** See Copyright Notice in lua.h 7 */ 8 9 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <string.h> 13 14 #define lvm_c 15 #define LUA_CORE 16 17 #include "lua.h" 18 19 #include "ldebug.h" 20 #include "ldo.h" 21 #include "lfunc.h" 22 #include "lgc.h" 23 #include "lobject.h" 24 #include "lopcodes.h" 25 #include "lstate.h" 26 #include "lstring.h" 27 #include "ltable.h" 28 #include "ltm.h" 29 #include "lvm.h" 30 31 32 33 /* limit for table tag-method chains (to avoid loops) */ 34 #define MAXTAGLOOP 100 35 36 37 const TValue *luaV_tonumber (const TValue *obj, TValue *n) { 38 lua_Number num; 39 if (ttisnumber(obj)) return obj; 40 if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) { 41 setnvalue(n, num); 42 return n; 43 } 44 else 45 return NULL; 46 } 47 48 49 int luaV_tostring (lua_State *L, StkId obj) { 50 if (!ttisnumber(obj)) 51 return 0; 52 else { 53 char s[LUAI_MAXNUMBER2STR]; 54 lua_Number n = nvalue(obj); 55 lua_number2str(s, sizeof(s), n); 56 setsvalue2s(L, obj, luaS_new(L, s)); 57 return 1; 58 } 59 } 60 61 62 static void traceexec (lua_State *L, const Instruction *pc) { 63 lu_byte mask = L->hookmask; 64 const Instruction *oldpc = L->savedpc; 65 L->savedpc = pc; 66 if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) { 67 resethookcount(L); 68 luaD_callhook(L, LUA_HOOKCOUNT, -1); 69 } 70 if (mask & LUA_MASKLINE) { 71 Proto *p = ci_func(L->ci)->l.p; 72 int npc = pcRel(pc, p); 73 int newline = getline(p, npc); 74 /* call linehook when enter a new function, when jump back (loop), 75 or when enter a new line */ 76 if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p))) 77 luaD_callhook(L, LUA_HOOKLINE, newline); 78 } 79 } 80 81 82 static void callTMres (lua_State *L, StkId res, const TValue *f, 83 const TValue *p1, const TValue *p2) { 84 ptrdiff_t result = savestack(L, res); 85 setobj2s(L, L->top, f); /* push function */ 86 setobj2s(L, L->top+1, p1); /* 1st argument */ 87 setobj2s(L, L->top+2, p2); /* 2nd argument */ 88 luaD_checkstack(L, 3); 89 L->top += 3; 90 luaD_call(L, L->top - 3, 1); 91 res = restorestack(L, result); 92 L->top--; 93 setobjs2s(L, res, L->top); 94 } 95 96 97 98 static void callTM (lua_State *L, const TValue *f, const TValue *p1, 99 const TValue *p2, const TValue *p3) { 100 setobj2s(L, L->top, f); /* push function */ 101 setobj2s(L, L->top+1, p1); /* 1st argument */ 102 setobj2s(L, L->top+2, p2); /* 2nd argument */ 103 setobj2s(L, L->top+3, p3); /* 3th argument */ 104 luaD_checkstack(L, 4); 105 L->top += 4; 106 luaD_call(L, L->top - 4, 0); 107 } 108 109 110 void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { 111 int loop; 112 for (loop = 0; loop < MAXTAGLOOP; loop++) { 113 const TValue *tm; 114 if (ttistable(t)) { /* `t' is a table? */ 115 Table *h = hvalue(t); 116 const TValue *res = luaH_get(h, key); /* do a primitive get */ 117 if (!ttisnil(res) || /* result is no nil? */ 118 (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ 119 setobj2s(L, val, res); 120 return; 121 } 122 /* else will try the tag method */ 123 } 124 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) 125 luaG_typeerror(L, t, "index"); 126 if (ttisfunction(tm)) { 127 callTMres(L, val, tm, t, key); 128 return; 129 } 130 t = tm; /* else repeat with `tm' */ 131 } 132 luaG_runerror(L, "loop in gettable"); 133 } 134 135 136 void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { 137 int loop; 138 TValue temp; 139 for (loop = 0; loop < MAXTAGLOOP; loop++) { 140 const TValue *tm; 141 if (ttistable(t)) { /* `t' is a table? */ 142 Table *h = hvalue(t); 143 TValue *oldval = luaH_set(L, h, key); /* do a primitive set */ 144 if (!ttisnil(oldval) || /* result is no nil? */ 145 (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */ 146 setobj2t(L, oldval, val); 147 h->flags = 0; 148 luaC_barriert(L, h, val); 149 return; 150 } 151 /* else will try the tag method */ 152 } 153 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) 154 luaG_typeerror(L, t, "index"); 155 if (ttisfunction(tm)) { 156 callTM(L, tm, t, key, val); 157 return; 158 } 159 /* else repeat with `tm' */ 160 setobj(L, &temp, tm); /* avoid pointing inside table (may rehash) */ 161 t = &temp; 162 } 163 luaG_runerror(L, "loop in settable"); 164 } 165 166 167 static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2, 168 StkId res, TMS event) { 169 const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ 170 if (ttisnil(tm)) 171 tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ 172 if (ttisnil(tm)) return 0; 173 callTMres(L, res, tm, p1, p2); 174 return 1; 175 } 176 177 178 static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2, 179 TMS event) { 180 const TValue *tm1 = fasttm(L, mt1, event); 181 const TValue *tm2; 182 if (tm1 == NULL) return NULL; /* no metamethod */ 183 if (mt1 == mt2) return tm1; /* same metatables => same metamethods */ 184 tm2 = fasttm(L, mt2, event); 185 if (tm2 == NULL) return NULL; /* no metamethod */ 186 if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */ 187 return tm1; 188 return NULL; 189 } 190 191 192 static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2, 193 TMS event) { 194 const TValue *tm1 = luaT_gettmbyobj(L, p1, event); 195 const TValue *tm2; 196 if (ttisnil(tm1)) return -1; /* no metamethod? */ 197 tm2 = luaT_gettmbyobj(L, p2, event); 198 if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */ 199 return -1; 200 callTMres(L, L->top, tm1, p1, p2); 201 return !l_isfalse(L->top); 202 } 203 204 205 static int l_strcmp (const TString *ls, const TString *rs) { 206 const char *l = getstr(ls); 207 size_t ll = ls->tsv.len; 208 const char *r = getstr(rs); 209 size_t lr = rs->tsv.len; 210 for (;;) { 211 int temp = strcoll(l, r); 212 if (temp != 0) return temp; 213 else { /* strings are equal up to a `\0' */ 214 size_t len = strlen(l); /* index of first `\0' in both strings */ 215 if (len == lr) /* r is finished? */ 216 return (len == ll) ? 0 : 1; 217 else if (len == ll) /* l is finished? */ 218 return -1; /* l is smaller than r (because r is not finished) */ 219 /* both strings longer than `len'; go on comparing (after the `\0') */ 220 len++; 221 l += len; ll -= len; r += len; lr -= len; 222 } 223 } 224 } 225 226 227 int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { 228 int res; 229 if (ttype(l) != ttype(r)) 230 return luaG_ordererror(L, l, r); 231 else if (ttisnumber(l)) 232 return luai_numlt(nvalue(l), nvalue(r)); 233 else if (ttisstring(l)) 234 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; 235 else if ((res = call_orderTM(L, l, r, TM_LT)) != -1) 236 return res; 237 return luaG_ordererror(L, l, r); 238 } 239 240 241 static int lessequal (lua_State *L, const TValue *l, const TValue *r) { 242 int res; 243 if (ttype(l) != ttype(r)) 244 return luaG_ordererror(L, l, r); 245 else if (ttisnumber(l)) 246 return luai_numle(nvalue(l), nvalue(r)); 247 else if (ttisstring(l)) 248 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; 249 else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */ 250 return res; 251 else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */ 252 return !res; 253 return luaG_ordererror(L, l, r); 254 } 255 256 257 int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) { 258 const TValue *tm; 259 lua_assert(ttype(t1) == ttype(t2)); 260 switch (ttype(t1)) { 261 case LUA_TNIL: return 1; 262 case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2)); 263 case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ 264 case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); 265 case LUA_TUSERDATA: { 266 if (uvalue(t1) == uvalue(t2)) return 1; 267 tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, 268 TM_EQ); 269 break; /* will try TM */ 270 } 271 case LUA_TTABLE: { 272 if (hvalue(t1) == hvalue(t2)) return 1; 273 tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ); 274 break; /* will try TM */ 275 } 276 default: return gcvalue(t1) == gcvalue(t2); 277 } 278 if (tm == NULL) return 0; /* no TM? */ 279 callTMres(L, L->top, tm, t1, t2); /* call TM */ 280 return !l_isfalse(L->top); 281 } 282 283 284 void luaV_concat (lua_State *L, int total, int last) { 285 do { 286 StkId top = L->base + last + 1; 287 int n = 2; /* number of elements handled in this pass (at least 2) */ 288 if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) { 289 if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT)) 290 luaG_concaterror(L, top-2, top-1); 291 } else if (tsvalue(top-1)->len == 0) /* second op is empty? */ 292 (void)tostring(L, top - 2); /* result is first op (as string) */ 293 else { 294 /* at least two string values; get as many as possible */ 295 size_t tl = tsvalue(top-1)->len; 296 char *buffer; 297 int i; 298 /* collect total length */ 299 for (n = 1; n < total && tostring(L, top-n-1); n++) { 300 size_t l = tsvalue(top-n-1)->len; 301 if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow"); 302 tl += l; 303 } 304 buffer = luaZ_openspace(L, &G(L)->buff, tl); 305 tl = 0; 306 for (i=n; i>0; i--) { /* concat all strings */ 307 size_t l = tsvalue(top-i)->len; 308 memcpy(buffer+tl, svalue(top-i), l); 309 tl += l; 310 } 311 setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); 312 } 313 total -= n-1; /* got `n' strings to create 1 new */ 314 last -= n-1; 315 } while (total > 1); /* repeat until only 1 result left */ 316 } 317 318 319 static void Arith (lua_State *L, StkId ra, const TValue *rb, 320 const TValue *rc, TMS op) { 321 TValue tempb, tempc; 322 const TValue *b, *c; 323 if ((b = luaV_tonumber(rb, &tempb)) != NULL && 324 (c = luaV_tonumber(rc, &tempc)) != NULL) { 325 lua_Number nb = nvalue(b), nc = nvalue(c); 326 switch (op) { 327 case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break; 328 case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break; 329 case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break; 330 case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break; 331 case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break; 332 case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break; 333 case TM_UNM: setnvalue(ra, luai_numunm(nb)); break; 334 default: lua_assert(0); break; 335 } 336 } 337 else if (!call_binTM(L, rb, rc, ra, op)) 338 luaG_aritherror(L, rb, rc); 339 } 340 341 342 343 /* 344 ** some macros for common tasks in `luaV_execute' 345 */ 346 347 #define runtime_check(L, c) { if (!(c)) break; } 348 349 #define RA(i) (base+GETARG_A(i)) 350 /* to be used after possible stack reallocation */ 351 #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i)) 352 #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) 353 #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ 354 ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i)) 355 #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ 356 ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) 357 #define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i)) 358 359 360 #define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);} 361 362 363 #define Protect(x) { L->savedpc = pc; {x;}; base = L->base; } 364 365 366 #define arith_op(op,tm) { \ 367 TValue *rb = RKB(i); \ 368 TValue *rc = RKC(i); \ 369 if (ttisnumber(rb) && ttisnumber(rc)) { \ 370 lua_Number nb = nvalue(rb), nc = nvalue(rc); \ 371 setnvalue(ra, op(nb, nc)); \ 372 } \ 373 else \ 374 Protect(Arith(L, ra, rb, rc, tm)); \ 375 } 376 377 378 379 void luaV_execute (lua_State *L, int nexeccalls) { 380 LClosure *cl; 381 StkId base; 382 TValue *k; 383 const Instruction *pc; 384 reentry: /* entry point */ 385 lua_assert(isLua(L->ci)); 386 pc = L->savedpc; 387 cl = &clvalue(L->ci->func)->l; 388 base = L->base; 389 k = cl->p->k; 390 /* main loop of interpreter */ 391 for (;;) { 392 const Instruction i = *pc++; 393 StkId ra; 394 if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) && 395 (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) { 396 traceexec(L, pc); 397 if (L->status == LUA_YIELD) { /* did hook yield? */ 398 L->savedpc = pc - 1; 399 return; 400 } 401 base = L->base; 402 } 403 /* warning!! several calls may realloc the stack and invalidate `ra' */ 404 ra = RA(i); 405 lua_assert(base == L->base && L->base == L->ci->base); 406 lua_assert(base <= L->top && L->top <= L->stack + L->stacksize); 407 lua_assert(L->top == L->ci->top || luaG_checkopenop(i)); 408 switch (GET_OPCODE(i)) { 409 case OP_MOVE: { 410 setobjs2s(L, ra, RB(i)); 411 continue; 412 } 413 case OP_LOADK: { 414 setobj2s(L, ra, KBx(i)); 415 continue; 416 } 417 case OP_LOADBOOL: { 418 setbvalue(ra, GETARG_B(i)); 419 if (GETARG_C(i)) pc++; /* skip next instruction (if C) */ 420 continue; 421 } 422 case OP_LOADNIL: { 423 TValue *rb = RB(i); 424 do { 425 setnilvalue(rb--); 426 } while (rb >= ra); 427 continue; 428 } 429 case OP_GETUPVAL: { 430 int b = GETARG_B(i); 431 setobj2s(L, ra, cl->upvals[b]->v); 432 continue; 433 } 434 case OP_GETGLOBAL: { 435 TValue g; 436 TValue *rb = KBx(i); 437 sethvalue(L, &g, cl->env); 438 lua_assert(ttisstring(rb)); 439 Protect(luaV_gettable(L, &g, rb, ra)); 440 continue; 441 } 442 case OP_GETTABLE: { 443 Protect(luaV_gettable(L, RB(i), RKC(i), ra)); 444 continue; 445 } 446 case OP_SETGLOBAL: { 447 TValue g; 448 sethvalue(L, &g, cl->env); 449 lua_assert(ttisstring(KBx(i))); 450 Protect(luaV_settable(L, &g, KBx(i), ra)); 451 continue; 452 } 453 case OP_SETUPVAL: { 454 UpVal *uv = cl->upvals[GETARG_B(i)]; 455 setobj(L, uv->v, ra); 456 luaC_barrier(L, uv, ra); 457 continue; 458 } 459 case OP_SETTABLE: { 460 Protect(luaV_settable(L, ra, RKB(i), RKC(i))); 461 continue; 462 } 463 case OP_NEWTABLE: { 464 int b = GETARG_B(i); 465 int c = GETARG_C(i); 466 sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c))); 467 Protect(luaC_checkGC(L)); 468 continue; 469 } 470 case OP_SELF: { 471 StkId rb = RB(i); 472 setobjs2s(L, ra+1, rb); 473 Protect(luaV_gettable(L, rb, RKC(i), ra)); 474 continue; 475 } 476 case OP_ADD: { 477 arith_op(luai_numadd, TM_ADD); 478 continue; 479 } 480 case OP_SUB: { 481 arith_op(luai_numsub, TM_SUB); 482 continue; 483 } 484 case OP_MUL: { 485 arith_op(luai_nummul, TM_MUL); 486 continue; 487 } 488 case OP_DIV: { 489 arith_op(luai_numdiv, TM_DIV); 490 continue; 491 } 492 case OP_MOD: { 493 arith_op(luai_nummod, TM_MOD); 494 continue; 495 } 496 case OP_POW: { 497 arith_op(luai_numpow, TM_POW); 498 continue; 499 } 500 case OP_UNM: { 501 TValue *rb = RB(i); 502 if (ttisnumber(rb)) { 503 lua_Number nb = nvalue(rb); 504 setnvalue(ra, luai_numunm(nb)); 505 } 506 else { 507 Protect(Arith(L, ra, rb, rb, TM_UNM)); 508 } 509 continue; 510 } 511 case OP_NOT: { 512 int res = l_isfalse(RB(i)); /* next assignment may change this value */ 513 setbvalue(ra, res); 514 continue; 515 } 516 case OP_LEN: { 517 const TValue *rb = RB(i); 518 switch (ttype(rb)) { 519 case LUA_TTABLE: { 520 setnvalue(ra, cast_num(luaH_getn(hvalue(rb)))); 521 break; 522 } 523 case LUA_TSTRING: { 524 setnvalue(ra, cast_num(tsvalue(rb)->len)); 525 break; 526 } 527 default: { /* try metamethod */ 528 Protect( 529 if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN)) 530 luaG_typeerror(L, rb, "get length of"); 531 ) 532 } 533 } 534 continue; 535 } 536 case OP_CONCAT: { 537 int b = GETARG_B(i); 538 int c = GETARG_C(i); 539 Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L)); 540 setobjs2s(L, RA(i), base+b); 541 continue; 542 } 543 case OP_JMP: { 544 dojump(L, pc, GETARG_sBx(i)); 545 continue; 546 } 547 case OP_EQ: { 548 TValue *rb = RKB(i); 549 TValue *rc = RKC(i); 550 Protect( 551 if (equalobj(L, rb, rc) == GETARG_A(i)) 552 dojump(L, pc, GETARG_sBx(*pc)); 553 ) 554 pc++; 555 continue; 556 } 557 case OP_LT: { 558 Protect( 559 if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i)) 560 dojump(L, pc, GETARG_sBx(*pc)); 561 ) 562 pc++; 563 continue; 564 } 565 case OP_LE: { 566 Protect( 567 if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i)) 568 dojump(L, pc, GETARG_sBx(*pc)); 569 ) 570 pc++; 571 continue; 572 } 573 case OP_TEST: { 574 if (l_isfalse(ra) != GETARG_C(i)) 575 dojump(L, pc, GETARG_sBx(*pc)); 576 pc++; 577 continue; 578 } 579 case OP_TESTSET: { 580 TValue *rb = RB(i); 581 if (l_isfalse(rb) != GETARG_C(i)) { 582 setobjs2s(L, ra, rb); 583 dojump(L, pc, GETARG_sBx(*pc)); 584 } 585 pc++; 586 continue; 587 } 588 case OP_CALL: { 589 int b = GETARG_B(i); 590 int nresults = GETARG_C(i) - 1; 591 if (b != 0) L->top = ra+b; /* else previous instruction set top */ 592 L->savedpc = pc; 593 switch (luaD_precall(L, ra, nresults)) { 594 case PCRLUA: { 595 nexeccalls++; 596 goto reentry; /* restart luaV_execute over new Lua function */ 597 } 598 case PCRC: { 599 /* it was a C function (`precall' called it); adjust results */ 600 if (nresults >= 0) L->top = L->ci->top; 601 base = L->base; 602 continue; 603 } 604 default: { 605 return; /* yield */ 606 } 607 } 608 } 609 case OP_TAILCALL: { 610 int b = GETARG_B(i); 611 if (b != 0) L->top = ra+b; /* else previous instruction set top */ 612 L->savedpc = pc; 613 lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); 614 switch (luaD_precall(L, ra, LUA_MULTRET)) { 615 case PCRLUA: { 616 /* tail call: put new frame in place of previous one */ 617 CallInfo *ci = L->ci - 1; /* previous frame */ 618 int aux; 619 StkId func = ci->func; 620 StkId pfunc = (ci+1)->func; /* previous function index */ 621 if (L->openupval) luaF_close(L, ci->base); 622 L->base = ci->base = ci->func + ((ci+1)->base - pfunc); 623 for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */ 624 setobjs2s(L, func+aux, pfunc+aux); 625 ci->top = L->top = func+aux; /* correct top */ 626 lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize); 627 ci->savedpc = L->savedpc; 628 ci->tailcalls++; /* one more call lost */ 629 L->ci--; /* remove new frame */ 630 goto reentry; 631 } 632 case PCRC: { /* it was a C function (`precall' called it) */ 633 base = L->base; 634 continue; 635 } 636 default: { 637 return; /* yield */ 638 } 639 } 640 } 641 case OP_RETURN: { 642 int b = GETARG_B(i); 643 if (b != 0) L->top = ra+b-1; 644 if (L->openupval) luaF_close(L, base); 645 L->savedpc = pc; 646 b = luaD_poscall(L, ra); 647 if (--nexeccalls == 0) /* was previous function running `here'? */ 648 return; /* no: return */ 649 else { /* yes: continue its execution */ 650 if (b) L->top = L->ci->top; 651 lua_assert(isLua(L->ci)); 652 lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL); 653 goto reentry; 654 } 655 } 656 case OP_FORLOOP: { 657 lua_Number step = nvalue(ra+2); 658 lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */ 659 lua_Number limit = nvalue(ra+1); 660 if (luai_numlt(0, step) ? luai_numle(idx, limit) 661 : luai_numle(limit, idx)) { 662 dojump(L, pc, GETARG_sBx(i)); /* jump back */ 663 setnvalue(ra, idx); /* update internal index... */ 664 setnvalue(ra+3, idx); /* ...and external index */ 665 } 666 continue; 667 } 668 case OP_FORPREP: { 669 const TValue *init = ra; 670 const TValue *plimit = ra+1; 671 const TValue *pstep = ra+2; 672 L->savedpc = pc; /* next steps may throw errors */ 673 if (!tonumber(init, ra)) 674 luaG_runerror(L, LUA_QL("for") " initial value must be a number"); 675 else if (!tonumber(plimit, ra+1)) 676 luaG_runerror(L, LUA_QL("for") " limit must be a number"); 677 else if (!tonumber(pstep, ra+2)) 678 luaG_runerror(L, LUA_QL("for") " step must be a number"); 679 setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep))); 680 dojump(L, pc, GETARG_sBx(i)); 681 continue; 682 } 683 case OP_TFORLOOP: { 684 StkId cb = ra + 3; /* call base */ 685 setobjs2s(L, cb+2, ra+2); 686 setobjs2s(L, cb+1, ra+1); 687 setobjs2s(L, cb, ra); 688 L->top = cb+3; /* func. + 2 args (state and index) */ 689 Protect(luaD_call(L, cb, GETARG_C(i))); 690 L->top = L->ci->top; 691 cb = RA(i) + 3; /* previous call may change the stack */ 692 if (!ttisnil(cb)) { /* continue loop? */ 693 setobjs2s(L, cb-1, cb); /* save control variable */ 694 dojump(L, pc, GETARG_sBx(*pc)); /* jump back */ 695 } 696 pc++; 697 continue; 698 } 699 case OP_SETLIST: { 700 int n = GETARG_B(i); 701 int c = GETARG_C(i); 702 int last; 703 Table *h; 704 if (n == 0) { 705 n = cast_int(L->top - ra) - 1; 706 L->top = L->ci->top; 707 } 708 if (c == 0) c = cast_int(*pc++); 709 runtime_check(L, ttistable(ra)); 710 h = hvalue(ra); 711 last = ((c-1)*LFIELDS_PER_FLUSH) + n; 712 if (last > h->sizearray) /* needs more space? */ 713 luaH_resizearray(L, h, last); /* pre-alloc it at once */ 714 for (; n > 0; n--) { 715 TValue *val = ra+n; 716 setobj2t(L, luaH_setnum(L, h, last--), val); 717 luaC_barriert(L, h, val); 718 } 719 continue; 720 } 721 case OP_CLOSE: { 722 luaF_close(L, ra); 723 continue; 724 } 725 case OP_CLOSURE: { 726 Proto *p; 727 Closure *ncl; 728 int nup, j; 729 p = cl->p->p[GETARG_Bx(i)]; 730 nup = p->nups; 731 ncl = luaF_newLclosure(L, nup, cl->env); 732 ncl->l.p = p; 733 for (j=0; j<nup; j++, pc++) { 734 if (GET_OPCODE(*pc) == OP_GETUPVAL) 735 ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)]; 736 else { 737 lua_assert(GET_OPCODE(*pc) == OP_MOVE); 738 ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc)); 739 } 740 } 741 setclvalue(L, ra, ncl); 742 Protect(luaC_checkGC(L)); 743 continue; 744 } 745 case OP_VARARG: { 746 int b = GETARG_B(i) - 1; 747 int j; 748 CallInfo *ci = L->ci; 749 int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1; 750 if (b == LUA_MULTRET) { 751 Protect(luaD_checkstack(L, n)); 752 ra = RA(i); /* previous call may change the stack */ 753 b = n; 754 L->top = ra + n; 755 } 756 for (j = 0; j < b; j++) { 757 if (j < n) { 758 setobjs2s(L, ra + j, ci->base - n + j); 759 } 760 else { 761 setnilvalue(ra + j); 762 } 763 } 764 continue; 765 } 766 } 767 } 768 } 769 770