private static void GCTM(lua_State L, int propagateerrors) { global_State g = G(L); Udata udata = udata2finalize(g); /*const*/ TValue tm = gfasttm(g, udata.uv.metatable, TMS.TM_GC); if (tm != null && ttisfunction(tm)) { int status; lu_byte oldah = L.allowhook; lu_mem oldt = (lu_mem)g.GCthreshold; L.allowhook = 0; /* stop debug hooks during GC tag method */ g.GCthreshold = 2 * g.totalbytes; /* avoid GC steps */ setobj2s(L, L.top, tm); setuvalue(L, L.top + 1, udata); L.top += 2; status = luaD_pcall(L, dothecall, null, savestack(L, L.top - 2), 0); if (status != LUA_OK && propagateerrors != 0) /* error while running __gc? */ { if (status == LUA_ERRRUN) /* is there an error msg.? */ { luaO_pushfstring(L, "error in __gc tag method (%s)", lua_tostring(L, -1)); status = LUA_ERRGCMM; /* error in __gc metamethod */ } luaD_throw(L, status); /* re-send error */ } L.allowhook = oldah; /* restore hooks */ g.GCthreshold = (uint)oldt; /* restore threshold */ } }
private static void traceexec(lua_State L, InstructionPtr pc) { lu_byte mask = L.hookmask; InstructionPtr oldpc = InstructionPtr.Assign(L.savedpc); L.savedpc = InstructionPtr.Assign(pc); if (((mask & LUA_MASKCOUNT) != 0) && (L.hookcount == 0)) { resethookcount(L); luaD_callhook(L, LUA_HOOKCOUNT, -1); } if ((mask & LUA_MASKLINE) != 0) { Proto p = ci_func(L.ci).l.p; int npc = pcRel(pc, p); int newline = getline(p, npc); /* call linehook when enter a new function, when jump back (loop), * or when enter a new line */ if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p))) { luaD_callhook(L, LUA_HOOKLINE, newline); } } }
public static lua_State lua_newstate(lua_Alloc f, object ud) { int i; lua_State L; global_State g; LG l = (LG)f(typeof(LG)); //FIXME:(LG)(f(ud, null, 0, (uint)(GetUnmanagedSize(typeof(LG))))); if (l == null) { return(null); } L = l.l.l; g = l.g; L.next = null; L.tt = LUA_TTHREAD; g.currentwhite = (lu_byte)bit2mask(WHITE0BIT, FIXEDBIT); L.marked = luaC_white(g); g.gckind = KGC_NORMAL; g.nCcalls = 0; lu_byte marked = L.marked; // can't pass properties in as ref ???//FIXME:??? set2bits(ref marked, FIXEDBIT, SFIXEDBIT); L.marked = marked; //remove this //FIXME:??? preinit_state(L, g); g.frealloc = f; g.ud = ud; g.mainthread = L; g.uvhead.u.l.prev = g.uvhead; g.uvhead.u.l.next = g.uvhead; g.GCthreshold = MAX_LUMEM; /* mark it as unfinished state */ g.strt.size = 0; g.strt.nuse = 0; g.strt.hash = null; setnilvalue(g.l_registry); g.l_gt = null; luaZ_initbuffer(L, g.buff); g.panic = null; g.version = lua_version(null); g.gcstate = GCSpause; g.rootgc = obj2gco(L); g.tobefnz = null; g.totalbytes = (uint)GetUnmanagedSize(typeof(LG)); g.gcpause = LUAI_GCPAUSE; g.gcstepmul = LUAI_GCMUL; for (i = 0; i < NUM_TAGS; i++) { g.mt[i] = null; } if (luaD_rawrunprotected(L, f_luaopen, null) != LUA_OK) { /* memory allocation error: free partial state */ close_state(L); L = null; } else { luai_userstateopen(L); } return(L); }
public static void luaS_fix(TString s) { lu_byte marked = s.tsv.marked; // can't pass properties in as ref l_setbit(ref marked, FIXEDBIT); s.tsv.marked = marked; }
private static void GCTM(lua_State L, int propagateerrors) { global_State g = G(L); /*const*/ TValue tm; TValue v = new TValue(); setgcovalue(L, v, udata2finalize(g)); tm = luaT_gettmbyobj(L, v, TMS.TM_GC); if (tm != null && ttisfunction(tm)) /* is there a finalizer? */ { int status; lu_byte oldah = L.allowhook; int running = g.gcrunning; L.allowhook = 0; /* stop debug hooks during GC tag method */ g.gcrunning = 0; /* avoid GC steps */ setobj2s(L, L.top, tm); /* push finalizer... */ setobj2s(L, L.top + 1, v); /* ... and its argument */ L.top += 2; /* and (next line) call the finalizer */ status = luaD_pcall(L, dothecall, null, savestack(L, L.top - 2), 0); L.allowhook = oldah; /* restore hooks */ g.gcrunning = (byte)running; /* restore state */ //FIXME:changed, (byte) if (status != LUA_OK && propagateerrors != 0) /* error while running __gc? */ { if (status == LUA_ERRRUN) /* is there an error msg.? */ { luaO_pushfstring(L, "error in __gc tag method (%s)", lua_tostring(L, -1)); status = LUA_ERRGCMM; /* error in __gc metamethod */ } luaD_throw(L, status); /* re-send error */ } } }
private static void traceexec(LuaState L, InstructionPtr pc) { lu_byte mask = L.hookmask; InstructionPtr oldpc = InstructionPtr.Assign(L.savedpc); L.savedpc = InstructionPtr.Assign(pc); if (((mask & LUA_MASKCOUNT) != 0) && (L.hookcount == 0)) { ResetHookCount(L); LuaDCallHook(L, LUA_HOOKCOUNT, -1); } if ((mask & LUA_MASKLINE) != 0) { Proto p = CIFunc(L.ci).l.p; int npc = PCRel(pc, p); int newline = GetLine(p, npc); /* call linehook when enter a new function, when jump back (loop), * or when enter a new line */ if (npc == 0 || pc <= oldpc || newline != GetLine(p, PCRel(oldpc, p))) { LuaDCallHook(L, LUA_HOOKLINE, newline); } } }
public static void markfinalized(Udata_uv u) { lu_byte marked = u.marked; // can't pass properties in as ref l_setbit(ref marked, FINALIZEDBIT); u.marked = marked; }
private static void GCTM(lua_State L) { global_State g = G(L); GCObject o = g.tmudata.gch.next; /* get first element */ Udata udata = rawgco2u(o); TValue tm; /* remove udata from `tmudata' */ if (o == g.tmudata) /* last element? */ { g.tmudata = null; } else { g.tmudata.gch.next = udata.uv.next; } udata.uv.next = g.mainthread.next; /* return it to `root' list */ g.mainthread.next = o; makewhite(g, o); tm = fasttm(L, udata.uv.metatable, TMS.TM_GC); if (tm != null) { lu_byte oldah = L.allowhook; lu_mem oldt = (lu_mem)g.GCthreshold; L.allowhook = 0; /* stop debug hooks during GC tag method */ g.GCthreshold = 2 * g.totalbytes; /* avoid GC steps */ setobj2s(L, L.top, tm); setuvalue(L, L.top + 1, udata); L.top += 2; luaD_call(L, L.top - 2, 0); L.allowhook = oldah; /* restore hooks */ g.GCthreshold = (uint)oldt; /* restore threshold */ } }
public static int luaD_pcall(lua_State L, Pfunc func, object u, ptrdiff_t old_top, ptrdiff_t ef) { int status; ushort oldnCcalls = L.nCcalls; ptrdiff_t old_ci = saveci(L, L.ci); lu_byte old_allowhooks = L.allowhook; ptrdiff_t old_errfunc = L.errfunc; L.errfunc = ef; status = luaD_rawrunprotected(L, func, u); if (status != 0) /* an error occurred? */ { StkId oldtop = restorestack(L, old_top); luaF_close(L, oldtop); /* close eventual pending closures */ luaD_seterrorobj(L, status, oldtop); L.nCcalls = oldnCcalls; L.ci = restoreci(L, old_ci); L.base_ = L.ci.base_; L.savedpc = InstructionPtr.Assign(L.ci.savedpc); L.allowhook = old_allowhooks; restore_stack_limit(L); } L.errfunc = old_errfunc; return(status); }
public static void luaC_link(lua_State L, GCObject o, lu_byte tt) { global_State g = G(L); o.gch.next = g.rootgc; g.rootgc = o; o.gch.marked = luaC_white(g); o.gch.tt = tt; }
public static lua_State lua_newstate(lua_Alloc f, object ud) { int i; lua_State L; global_State g; LG l = (LG)f(typeof(LG)); //FIXME:(LG)(f(ud, null, LUA_TTHREAD, (uint)(GetUnmanagedSize(typeof(LG))))); //FIXME:???not sync, LUA_TTHREAD if (l == null) { return(null); } L = l.l.l; g = l.g; L.next = null; L.tt = LUA_TTHREAD; g.currentwhite = (lu_byte)bitmask(WHITE0BIT); L.marked = luaC_white(g); lu_byte marked = L.marked; // can't pass properties in as ref ???//FIXME:??? //FIXME:added L.marked = marked; //remove this //FIXME:??? //FIXME:added preinit_thread(L, g); g.frealloc = f; g.ud = ud; g.mainthread = L; g.seed = makeseed(L); g.gcrunning = 0; /* no GC while building state */ g.GCestimate = 0; g.strt.size = g.strt.nuse = 0; g.strt.hash = null; setnilvalue(g.l_registry); luaZ_initbuffer(L, g.buff); g.panic = null; g.version = null; g.gcstate = GCSpause; g.gckind = KGC_NORMAL; g.allgc = g.finobj = g.tobefnz = g.fixedgc = null; g.sweepgc = null; g.gray = g.grayagain = null; g.weak = g.ephemeron = g.allweak = null; g.twups = null; g.totalbytes = (uint)GetUnmanagedSize(typeof(LG)); g.GCdebt = 0; g.gcfinnum = 0; g.gcpause = LUAI_GCPAUSE; g.gcstepmul = LUAI_GCMUL; for (i = 0; i < LUA_NUMTAGS; i++) { g.mt[i] = null; } if (luaD_rawrunprotected(L, f_luaopen, null) != LUA_OK) { /* memory allocation error: free partial state */ close_state(L); L = null; } return(L); }
public static void luaG_traceexec(lua_State L) { CallInfo ci = L.ci; lu_byte mask = L.hookmask; int counthook = (--L.hookcount == 0 && (mask & LUA_MASKCOUNT) != 0)?1:0; if (counthook != 0) { resethookcount(L); /* reset count */ } else if (0 == (mask & LUA_MASKLINE)) { return; /* no line hook and count != 0; nothing to be done */ } if ((ci.callstatus & CIST_HOOKYIELD) != 0) /* called hook last time? */ { ci.callstatus &= (byte)((~CIST_HOOKYIELD) & 0xff); /* erase mark */ return; /* do not call hook again (VM yielded, so it did not move) */ } if (counthook != 0) { luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ } if ((mask & LUA_MASKLINE) != 0) { Proto p = ci_func(ci).p; int npc = pcRel(ci.u.l.savedpc, p); int newline = getfuncline(p, npc); if (npc == 0 || /* call linehook when enter a new function, */ ci.u.l.savedpc <= L.oldpc || /* when jump back (loop), or when */ newline != getfuncline(p, pcRel(L.oldpc, p))) /* enter a new line */ { luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */ } } L.oldpc = ci.u.l.savedpc; if (L.status == LUA_YIELD) /* did hook yield? */ { if (counthook != 0) { L.hookcount = 1; /* undo decrement to zero */ } InstructionPtr.dec(ref ci.u.l.savedpc); /* undo increment (resume will increment it again) */ ci.callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ ci.func = L.top - 1; /* protect stack below results */ luaD_throw(L, LUA_YIELD); } }
private static void GCTM(lua_State L) { global_State g = G(L); Udata udata = udata2finalize(g); /*const*/ TValue tm = fasttm(L, udata.uv.metatable, TMS.TM_GC); if (tm != null) { lu_byte oldah = L.allowhook; lu_mem oldt = (lu_mem)g.GCthreshold; L.allowhook = 0; /* stop debug hooks during GC tag method */ g.GCthreshold = 2 * g.totalbytes; /* avoid GC steps */ setobj2s(L, L.top, tm); setuvalue(L, L.top + 1, udata); L.top += 2; luaD_call(L, L.top - 2, 0); L.allowhook = oldah; /* restore hooks */ g.GCthreshold = (uint)oldt; /* restore threshold */ } }
public static int luaD_pcall (lua_State L, Pfunc func, object u, ptrdiff_t old_top, ptrdiff_t ef) { int status; CallInfo old_ci = L.ci; lu_byte old_allowhooks = L.allowhook; ushort old_nny = L.nny; ptrdiff_t old_errfunc = L.errfunc; L.errfunc = ef; status = luaD_rawrunprotected(L, func, u); if (status != LUA_OK) { /* an error occurred? */ StkId oldtop = restorestack(L, old_top); luaF_close(L, oldtop); /* close possible pending closures */ seterrorobj(L, status, oldtop); L.ci = old_ci; L.allowhook = old_allowhooks; L.nny = old_nny; luaD_shrinkstack(L); } L.errfunc = old_errfunc; return status; }
private static void traceexec(lua_State L) { lu_byte mask = L.hookmask; if (((mask & LUA_MASKCOUNT) != 0) && (L.hookcount == 0)) { resethookcount(L); luaD_callhook(L, LUA_HOOKCOUNT, -1); } if ((mask & LUA_MASKLINE) != 0) { Proto p = ci_func(L.ci).l.p; int npc = pcRel(L.savedpc, p); int newline = getline(p, npc); if (npc == 0 || /* call linehook when enter a new function, */ L.savedpc <= L.oldpc || /* when jump back (loop), or when */ newline != getline(p, pcRel(L.oldpc, p))) /* enter a new line */ { luaD_callhook(L, LUA_HOOKLINE, newline); } } L.oldpc = L.savedpc; }
public static int resetbit(ref lu_byte x, int b) { return(resetbits(ref x, bitmask(b))); }
public static bool testbit(lu_byte x, int b) { return(testbits(x, bitmask(b))); }
public static int setbits(ref lu_byte x, int m) { x |= (lu_byte)m; return(x); }
public static bool testbits(lu_byte x, int m) { return((x & (lu_byte)m) != 0); }
public static void luaC_link (LuaState L, GCObject o, lu_byte tt) { GlobalState g = G(L); o.gch.next = g.rootgc; g.rootgc = o; o.gch.marked = luaC_white(g); o.gch.tt = tt; }
/* ** some userful bit tricks */ public static int resetbits(ref lu_byte x, int m) { x &= (lu_byte) ~m; return(x); }
private static void enterblock (FuncState fs, BlockCnt bl, lu_byte isbreakable) { bl.breaklist = NO_JUMP; bl.isbreakable = isbreakable; bl.nactvar = fs.nactvar; bl.upval = 0; bl.previous = fs.bl; fs.bl = bl; LuaAssert(fs.freereg == fs.nactvar); }
public priority_(lu_byte left, lu_byte right) { this.left = left; this.right = right; }
public static int resetbit(ref lu_byte x, int b) { return resetbits(ref x, bitmask(b)); }
public static bool test2bits(lu_byte x, int b1, int b2) { return testbits(x, (bit2mask(b1, b2))); }
public static int reset2bits(ref lu_byte x, int b1, int b2) { return resetbits(ref x, (bit2mask(b1, b2))); }
public static bool test2bits(lu_byte x, int b1, int b2) { return(testbits(x, (bit2mask(b1, b2)))); }
public static LuaState LuaNewState(lua_Alloc f, object ud) { int i; LuaState L; GlobalState g; //object l = f(ud, null, 0, (uint)state_size(typeof(LG))); object l = f(typeof(LG)); if (l == null) { return(null); } L = tostate(l); g = (L as LG).g; L.next = null; L.tt = LUA_TTHREAD; g.currentwhite = (lu_byte)Bit2Mask(WHITE0BIT, FIXEDBIT); L.marked = LuaCWhite(g); lu_byte marked = L.marked; // can't pass properties in as ref Set2Bits(ref marked, FIXEDBIT, SFIXEDBIT); L.marked = marked; preinit_state(L, g); g.frealloc = f; g.ud = ud; g.mainthread = L; g.uvhead.u.l.prev = g.uvhead; g.uvhead.u.l.next = g.uvhead; g.GCthreshold = 0; /* mark it as unfinished state */ g.strt.size = 0; g.strt.nuse = 0; g.strt.hash = null; SetNilValue(Registry(L)); luaZ_initbuffer(L, g.buff); g.panic = null; g.gcstate = GCSpause; g.rootgc = obj2gco(L); g.sweepstrgc = 0; g.sweepgc = new RootGCRef(g); g.gray = null; g.grayagain = null; g.weak = null; g.tmudata = null; g.totalbytes = (uint)GetUnmanagedSize(typeof(LG)); g.gcpause = LUAI_GCPAUSE; g.gcstepmul = LUAI_GCMUL; g.gcdept = 0; for (i = 0; i < NUMTAGS; i++) { g.mt[i] = null; } if (LuaDRawRunProtected(L, f_luaopen, null) != 0) { /* memory allocation error: free partial state */ close_state(L); L = null; } else { luai_userstateopen(L); } return(L); }
private static void DumpByte(int y, DumpState D) { lu_byte x = (lu_byte)y; DumpVar(x, D); }
private static lu_byte opmode(lu_byte t, lu_byte a, OpArgMask b, OpArgMask c, OpMode m) { return (lu_byte)(((t) << 7) | ((a) << 6) | (((lu_byte)b) << 4) | (((lu_byte)c) << 2) | ((lu_byte)m)); }
public static int reset2bits(ref lu_byte x, int b1, int b2) { return(resetbits(ref x, (bit2mask(b1, b2)))); }
/* ** Garbage-collection function */ public static int lua_gc(lua_State L, int what, int data) { int res = 0; global_State g; lua_lock(L); g = G(L); switch (what) { case LUA_GCSTOP: { g.gcrunning = 0; break; } case LUA_GCRESTART: { luaE_setdebt(g, 0); g.gcrunning = 1; break; } case LUA_GCCOLLECT: { luaC_fullgc(L, 0); break; } case LUA_GCCOUNT: { /* GC values are expressed in Kbytes: #bytes/2^10 */ res = cast_int(gettotalbytes(g) >> 10); break; } case LUA_GCCOUNTB: { res = cast_int(gettotalbytes(g) & 0x3ff); break; } case LUA_GCSTEP: { l_mem debt = 1; /* =1 to signal that it did an actual step */ lu_byte oldrunning = g.gcrunning; g.gcrunning = 1; /* allow GC to run */ if (data == 0) { luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */ luaC_step(L); } else /* add 'data' to total debt */ { debt = ((l_mem)data) * 1024 + g.GCdebt; luaE_setdebt(g, debt); luaC_checkGC(L); } g.gcrunning = oldrunning; /* restore previous state */ if (debt > 0 && g.gcstate == GCSpause) /* end of cycle? */ { res = 1; /* signal it */ } break; } case LUA_GCSETPAUSE: { res = g.gcpause; g.gcpause = data; break; } case LUA_GCSETSTEPMUL: { res = g.gcstepmul; if (data < 40) { data = 40; /* avoid ridiculous low values (and 0) */ } g.gcstepmul = data; break; } case LUA_GCISRUNNING: { res = g.gcrunning; break; } default: res = -1; /* invalid option */ break; //FIXME: added } lua_unlock(L); return(res); }
private static lu_byte opmode(lu_byte t, lu_byte a, OpArgMask b, OpArgMask c, OpMode m) { return((lu_byte)(((t) << 7) | ((a) << 6) | (((lu_byte)b) << 4) | (((lu_byte)c) << 2) | ((lu_byte)m))); }
public static bool testbit(lu_byte x, int b) { return testbits(x, bitmask(b)); }
public static lua_State lua_newstate(lua_Alloc f, object ud) { int i; lua_State L; global_State g; //object l = f(ud, null, 0, (uint)state_size(typeof(LG))); object l = f(typeof(LG)); if (l == null) { return(null); } L = tostate(l); g = (L as LG).g; L.next = null; L.tt = LUA_TTHREAD; g.currentwhite = (lu_byte)bit2mask(WHITE0BIT, FIXEDBIT); L.marked = luaC_white(g); g.emergencygc = 0; g.nCcalls = 0; lu_byte marked = L.marked; // can't pass properties in as ref ???//FIXME:??? set2bits(ref marked, FIXEDBIT, SFIXEDBIT); L.marked = marked; //remove this //FIXME:??? preinit_state(L, g); g.frealloc = f; g.ud = ud; g.mainthread = L; g.uvhead.u.l.prev = g.uvhead; g.uvhead.u.l.next = g.uvhead; g.GCthreshold = MAX_LUMEM; /* mark it as unfinished state */ g.strt.size = 0; g.strt.nuse = 0; g.strt.hash = null; setnilvalue(registry(L)); luaZ_initbuffer(L, g.buff); g.panic = null; g.gcstate = GCSpause; g.rootgc = obj2gco(L); g.sweepstrgc = 0; g.sweepgc = new RootGCRef(g); g.gray = null; g.grayagain = null; g.weak = g.ephemeron = g.allweak = null; g.tmudata = null; g.totalbytes = (uint)GetUnmanagedSize(typeof(LG)); g.gcpause = LUAI_GCPAUSE; g.gcstepmul = LUAI_GCMUL; g.gcdept = 0; for (i = 0; i < NUM_TAGS; i++) { g.mt[i] = null; } if (luaD_rawrunprotected(L, f_luaopen, null) != LUA_OK) { /* memory allocation error: free partial state */ close_state(L); L = null; } else { luai_userstateopen(L); } return(L); }
public static int setbits(ref lu_byte x, int m) { x |= (lu_byte)m; return x; }
/* ** some userful bit tricks */ public static int resetbits(ref lu_byte x, int m) { x &= (lu_byte)~m; return x; }
public static bool testbits(lu_byte x, int m) { return (x & (lu_byte)m) != 0; }