static void Arith (lua_State *L, StkId ra, const TValue *rb, const TValue *rc, TMS op) { TValue tempb, tempc; const TValue *b, *c; #if LUA_REFCOUNT luarc_newvalue(&tempb); luarc_newvalue(&tempc); if ((b = luaV_tonumber(L, rb, &tempb)) != NULL && (c = luaV_tonumber(L, rc, &tempc)) != NULL) { #else if ((b = luaV_tonumber(rb, &tempb)) != NULL && (c = luaV_tonumber(rc, &tempc)) != NULL) { #endif /* LUA_REFCOUNT */ lua_Number nb = nvalue(b), nc = nvalue(c); #if LUA_REFCOUNT luarc_cleanvalue(&tempb); luarc_cleanvalue(&tempc); #endif /* LUA_REFCOUNT */ switch (op) { case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break; case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break; case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break; case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break; case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break; case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break; case TM_UNM: setnvalue(ra, luai_numunm(nb)); break; default: lua_assert(0); break; } } #if LUA_REFCOUNT else if (!call_binTM(L, rb, rc, ra, op)) { luarc_cleanvalue(&tempb); luarc_cleanvalue(&tempc); luaG_aritherror(L, rb, rc); } #else else if (!call_binTM(L, rb, rc, ra, op)) luaG_aritherror(L, rb, rc); #endif /* LUA_REFCOUNT */ } /* ** some macros for common tasks in `luaV_execute' */ #define runtime_check(L, c) { if (!(c)) break; } #define RA(i) (base+GETARG_A(i)) /* to be used after possible stack reallocation */ #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i)) #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i)) #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) #define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i)) #define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);} #define Protect(x) { L->savedpc = pc; {x;}; base = L->base; } #define arith_op(op,tm) { \ TValue *rb = RKB(i); \ TValue *rc = RKC(i); \ if (ttisnumber(rb) && ttisnumber(rc)) { \ lua_Number nb = nvalue(rb), nc = nvalue(rc); \ setnvalue(ra, op(nb, nc)); \ } \ else \ Protect(Arith(L, ra, rb, rc, tm)); \ } #if LUA_BITFIELD_OPS #define bit_op(op) { \ TValue *rb = RKB(i); \ TValue *rc = RKC(i); \ if (ttisnumber(rb) && ttisnumber(rc)) { \ unsigned int nb = (unsigned int)nvalue(rb), nc = (unsigned int)nvalue(rc); \ setnvalue(ra, nb op nc); \ } \ else \ luaG_aritherror(L, rb, rc); \ } #endif /* LUA_BITFIELD_OPS */ void luaV_execute (lua_State *L, int nexeccalls) { LClosure *cl; StkId base; TValue *k; const Instruction *pc; reentry: /* entry point */ lua_assert(isLua(L->ci)); pc = L->savedpc; cl = &clvalue(L->ci->func)->l; base = L->base; k = cl->p->k; /* main loop of interpreter */ for (;;) { const Instruction i = *pc++; StkId ra; if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) && (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) { traceexec(L, pc); if (L->status == LUA_YIELD) { /* did hook yield? */ L->savedpc = pc - 1; return; } base = L->base; } /* warning!! several calls may realloc the stack and invalidate `ra' */ ra = RA(i); lua_assert(base == L->base && L->base == L->ci->base); lua_assert(base <= L->top && L->top <= L->stack + L->stacksize); lua_assert(L->top == L->ci->top || luaG_checkopenop(i)); switch (GET_OPCODE(i)) { case OP_MOVE: { setobjs2s(L, ra, RB(i)); continue; } case OP_LOADK: { setobj2s(L, ra, KBx(i)); continue; } case OP_LOADBOOL: { setbvalue(ra, GETARG_B(i)); if (GETARG_C(i)) pc++; /* skip next instruction (if C) */ continue; } case OP_LOADNIL: { TValue *rb = RB(i); do { setnilvalue(rb--); } while (rb >= ra); continue; } case OP_GETUPVAL: { int b = GETARG_B(i); setobj2s(L, ra, cl->upvals[b]->v); continue; } case OP_GETGLOBAL: { TValue g; TValue *rb = KBx(i); #if LUA_REFCOUNT sethvalue2n(L, &g, cl->env); #else sethvalue(L, &g, cl->env); #endif /* LUA_REFCOUNT */ lua_assert(ttisstring(rb)); Protect(luaV_gettable(L, &g, rb, ra)); #if LUA_REFCOUNT setnilvalue(&g); #endif /* LUA_REFCOUNT */ continue; } case OP_GETTABLE: { Protect(luaV_gettable(L, RB(i), RKC(i), ra)); continue; } case OP_SETGLOBAL: { TValue g; #if LUA_REFCOUNT sethvalue2n(L, &g, cl->env); #else sethvalue(L, &g, cl->env); #endif /* LUA_REFCOUNT */ lua_assert(ttisstring(KBx(i))); Protect(luaV_settable(L, &g, KBx(i), ra)); #if LUA_REFCOUNT setnilvalue(&g); #endif /* LUA_REFCOUNT */ continue; } case OP_SETUPVAL: { UpVal *uv = cl->upvals[GETARG_B(i)]; setobj(L, uv->v, ra); luaC_barrier(L, uv, ra); continue; } case OP_SETTABLE: { Protect(luaV_settable(L, ra, RKB(i), RKC(i))); continue; } case OP_NEWTABLE: { int b = GETARG_B(i); int c = GETARG_C(i); sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c))); Protect(luaC_checkGC(L)); continue; } case OP_SELF: { StkId rb = RB(i); setobjs2s(L, ra+1, rb); Protect(luaV_gettable(L, rb, RKC(i), ra)); continue; } case OP_ADD: { arith_op(luai_numadd, TM_ADD); continue; } case OP_SUB: { arith_op(luai_numsub, TM_SUB); continue; } case OP_MUL: { arith_op(luai_nummul, TM_MUL); continue; } case OP_DIV: { arith_op(luai_numdiv, TM_DIV); continue; } case OP_MOD: { arith_op(luai_nummod, TM_MOD); continue; } case OP_POW: { arith_op(luai_numpow, TM_POW); continue; } case OP_UNM: { TValue *rb = RB(i); if (ttisnumber(rb)) { lua_Number nb = nvalue(rb); setnvalue(ra, luai_numunm(nb)); } else { Protect(Arith(L, ra, rb, rb, TM_UNM)); } continue; } case OP_NOT: { int res = l_isfalse(RB(i)); /* next assignment may change this value */ setbvalue(ra, res); continue; } #if LUA_BITFIELD_OPS case OP_BAND: { bit_op(&); continue; } case OP_BOR: { bit_op(|); continue; } case OP_BXOR: { bit_op(^); continue; } case OP_BSHL: { bit_op(<<); continue; } case OP_BSHR: { bit_op(>>); continue; } #endif /* LUA_BITFIELD_OPS */ case OP_LEN: { const TValue *rb = RB(i); switch (ttype(rb)) { case LUA_TTABLE: { setnvalue(ra, cast_num(luaH_getn(hvalue(rb)))); break; } case LUA_TSTRING: { setnvalue(ra, cast_num(tsvalue(rb)->len)); break; } #if LUA_WIDESTRING case LUA_TWSTRING: { setnvalue(ra, cast_num(tsvalue(rb)->len)); break; } #endif /* LUA_WIDESTRING */ default: { /* try metamethod */ Protect( if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN)) luaG_typeerror(L, rb, "get length of"); ) } } continue; } case OP_CONCAT: { int b = GETARG_B(i); int c = GETARG_C(i); Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L)); setobjs2s(L, RA(i), base+b); continue; } case OP_JMP: { dojump(L, pc, GETARG_sBx(i)); continue; } case OP_EQ: { TValue *rb = RKB(i); TValue *rc = RKC(i); Protect( if (equalobj(L, rb, rc) == GETARG_A(i)) dojump(L, pc, GETARG_sBx(*pc)); ) pc++; continue; } case OP_LT: { Protect( if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i)) dojump(L, pc, GETARG_sBx(*pc)); ) pc++; continue; } case OP_LE: { Protect( if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i)) dojump(L, pc, GETARG_sBx(*pc)); ) pc++; continue; }
/* ** open parts that may cause memory-allocation errors */ static void f_luaopen (lua_State *L, void *ud) { int i; global_State globalState; lua_State luaState; global_State *g; #ifdef _DEBUG luaState.allocName = "Lua_global_State"; #endif _DEBUG luaState.l_G = &globalState; globalState.reallocFunc = luaHelper_Realloc; globalState.freeFunc = luaHelper_Free; globalState.memData = luaHelper_memData; globalState.nblocks = sizeof(lua_State) + sizeof(global_State); // Bogus. /* create a new global state */ g = luaM_new(&luaState, global_State); UNUSED(ud); if (g == NULL) luaD_throw(L, LUA_ERRMEM); L->l_G = g; g->mainthread = L; g->GCthreshold = 0; /* mark it as unfinished state */ g->strt.size = 0; g->strt.nuse = 0; g->strt.hash = NULL; setnilvalue2n(defaultmeta(L)); setnilvalue2n(registry(L)); luaZ_initbuffer(L, &g->buff); g->panic = default_panic; #if !LUA_REFCOUNT g->rootgc = NULL; g->rootudata = NULL; g->tmudata = NULL; #else LUA_REFCOUNT g->rootgc_head.next = (GCObject*)&g->rootgc_tail; g->rootgc_head.prev = NULL; g->rootgc_tail.next = NULL; g->rootgc_tail.prev = (GCObject*)&g->rootgc_head; g->rootgc_head.tt = LUA_TNIL; g->rootgc_head.marked = 0; g->rootgc_head.ref = 0; g->rootgc_tail.tt = LUA_TNIL; g->rootgc_tail.marked = 0; g->rootgc_tail.ref = 0; g->rootudata_head.next = (GCObject*)&g->rootudata_tail; g->rootudata_head.prev = NULL; g->rootudata_tail.next = NULL; g->rootudata_tail.prev = (GCObject*)&g->rootudata_head; g->rootudata_head.tt = LUA_TNIL; g->rootudata_head.marked = 0; g->rootudata_head.ref = 0; g->rootudata_tail.tt = LUA_TNIL; g->rootudata_tail.marked = 0; g->rootudata_tail.ref = 0; g->tmudata_head.next = (GCObject*)&g->tmudata_tail; g->tmudata_head.prev = NULL; g->tmudata_tail.next = NULL; g->tmudata_tail.prev = (GCObject*)&g->tmudata_head; g->tmudata_head.tt = LUA_TNIL; g->tmudata_head.marked = 0; g->tmudata_head.ref = 0; g->tmudata_tail.tt = LUA_TNIL; g->tmudata_tail.marked = 0; g->tmudata_tail.ref = 0; #endif LUA_REFCOUNT setnilvalue2n(gkey(g->dummynode)); setnilvalue2n(gval(g->dummynode)); g->dummynode->next = NULL; g->nblocks = sizeof(lua_State) + sizeof(global_State); g->reallocFunc = luaHelper_Realloc; g->freeFunc = luaHelper_Free; g->memData = luaHelper_memData; g->fatalErrorFunc = defaultFatalErrorFunc; #ifdef LUA_MTSUPPORT g->lockData = NULL; g->lockFunc = NULL; g->unlockFunc = NULL; #endif LUA_MTSUPPORT g->userGCFunction = NULL; g->globalUserData = NULL; stack_init(L, L); /* init stack */ for (i = 0; i < LUA_NTYPES; i++) { defaultmetatypes(L, i)->value.gc = NULL; } /* create default meta table with a dummy table, and then close the loop */ defaultmeta(L)->tt = LUA_TNUMBER; defaultmeta(L)->value.gc = NULL; sethvalue2n(defaultmeta(L), luaH_new(L, 0, 0)); __AddRefDirect(hvalue(defaultmeta(L))); hvalue(defaultmeta(L))->metatable = hvalue(defaultmeta(L)); __AddRefDirect(hvalue(defaultmeta(L))->metatable); /* build meta tables */ for (i = 0; i < LUA_NTYPES; i++) { luaM_setname(L, "Lua_defaultMetaTypes"); sethvalue2n(defaultmetatypes(L, i), luaH_new(L, 0, 0)); hvalue(defaultmetatypes(L, i))->metatable = hvalue(defaultmeta(L)); } luaM_setname(L, "Lua_Globals"); sethvalue(gt(L), luaH_new(L, 0, 4)); /* table of globals */ __AddRefDirect(hvalue(gt(L))); luaM_setname(L, "Lua_Registry"); sethvalue(registry(L), luaH_new(L, 4, 4)); /* registry */ __AddRef(registry(L)); g->minimumstrings = lua_minimumnumstrings; luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ luaT_init(L); luaX_init(L); luaS_fix(luaS_newliteral(L, MEMERRMSG)); g->GCthreshold = 4*G(L)->nblocks; luaZ_openspace(L, &g->buff, lua_minimumauxspace); }