public static int luaO_rawequalObj(TValue t1, TValue t2) { if (ttype(t1) != ttype(t2)) { return(0); } else { switch (ttype(t1)) { case LUA_TNIL: return(1); case LUA_TNUMBER: return(luai_numeq(nvalue(t1), nvalue(t2)) ? 1 : 0); case LUA_TBOOLEAN: return(bvalue(t1) == bvalue(t2) ? 1 : 0); /* boolean true must be 1....but not in C# !! */ case LUA_TLIGHTUSERDATA: return(pvalue(t1) == pvalue(t2) ? 1 : 0); default: lua_assert(iscollectable(t1)); return(gcvalue(t1) == gcvalue(t2) ? 1 : 0); } } }
private static void PrintConstant(Proto f, int i) { /*const*/ TValue o = f.k[i]; switch (ttype(o)) { case LUA_TNIL: printf("nil"); break; case LUA_TBOOLEAN: printf(bvalue(o) != 0 ? "true" : "false"); break; case LUA_TNUMBER: printf(LUA_NUMBER_FMT, nvalue(o)); break; case LUA_TSTRING: PrintString(rawtsvalue(o)); break; default: /* cannot happen */ printf("? type=%d", ttype(o)); break; } }
private static void traversestack(global_State g, lua_State l) { StkId o, lim; CallInfo ci; markvalue(g, gt(l)); lim = l.top; for (ci = l.base_ci[0]; ci <= l.ci; CallInfo.inc(ref ci)) { lua_assert(ci.top <= l.stack_last); if (lim < ci.top) { lim = ci.top; } } for (o = l.stack[0]; o < l.top; StkId.inc(ref o)) { markvalue(g, o); } for (; o <= lim; StkId.inc(ref o)) { setnilvalue(o); } checkstacksizes(l, lim); }
public static void luaC_barriert(lua_State L, Table t, TValue v) { if (valiswhite(v) && isblack(obj2gco(t))) { luaC_barrierback(L, t); } }
public static int luaD_poscall(lua_State L, StkId firstResult) { StkId res; int wanted, i; CallInfo ci; if ((L.hookmask & LUA_MASKRET) != 0) { firstResult = callrethooks(L, firstResult); } ci = CallInfo.dec(ref L.ci); res = ci.func; /* res == final position of 1st result */ wanted = ci.nresults; L.base_ = (ci - 1).base_; /* restore base */ InstructionPtr.Assign((ci - 1).savedpc, ref L.savedpc); /* restore savedpc */ /* move results to correct place */ for (i = wanted; i != 0 && firstResult < L.top; i--) { setobjs2s(L, res, firstResult); res = res + 1; firstResult = firstResult + 1; } while (i-- > 0) { setnilvalue(StkId.inc(ref res)); } L.top = res; return(wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */ }
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_; InstructionPtr.Assign(L.ci.savedpc, ref L.savedpc); L.allowhook = old_allowhooks; restore_stack_limit(L); } L.errfunc = old_errfunc; return(status); }
public static int luaV_fastget_luaH_get(lua_State L, TValue t, TValue k, ref TValue aux) { if (!ttistable(t)) { aux = null; return(0); /* not a table; 'aux' is NULL and result is 0 */ } else { aux = luaH_get(hvalue(t), k); /* else, do raw access */ if (!ttisnil(aux)) { return(1); /* result not nil? 'aux' has it */ } else { aux = fasttm(L, hvalue(t).metatable, TMS.TM_INDEX); /* get metamethod */ if (aux != null) { return(0); /* has metamethod? must call it */ } else { aux = luaO_nilobject; return(1); /* else, final result is nil */ } } } }
/* ** clear collected entries from weaktables */ private static void cleartable(GCObject l) { while (l != null) { Table h = gco2h(l); int i = h.sizearray; lua_assert(testbit(h.marked, VALUEWEAKBIT) || testbit(h.marked, KEYWEAKBIT)); if (testbit(h.marked, VALUEWEAKBIT)) { while (i-- != 0) { TValue o = h.array[i]; if (iscleared(o, false)) /* value was collected? */ { setnilvalue(o); /* remove value */ } } } i = sizenode(h); while (i-- != 0) { Node n = gnode(h, i); if (!ttisnil(gval(n)) && /* non-empty entry? */ (iscleared(key2tval(n), true) || iscleared(gval(n), false))) { setnilvalue(gval(n)); /* remove value ... */ removeentry(n); /* remove entry from Table */ } } l = h.gclist; } }
public static UpVal luaF_findupval(lua_State L, StkId level) { global_State g = G(L); GCObjectRef pp = new OpenValRef(L); UpVal p; UpVal uv; while (pp.get() != null && (p = ngcotouv(pp.get())).v >= level) { lua_assert(p.v != p.u.value); if (p.v == level) { /* found a corresponding upvalue? */ if (isdead(g, obj2gco(p))) /* is it dead? */ changewhite(obj2gco(p)); /* ressurect it */ return p; } pp = new NextRef(p); } uv = luaM_new<UpVal>(L); /* not found: create a new one */ uv.tt = LUA_TUPVAL; uv.marked = luaC_white(g); uv.v = level; /* current value lives in the stack */ uv.next = pp.get(); /* chain it in the proper position */ pp.set( obj2gco(uv) ); uv.u.l.prev = g.uvhead; /* double link it in `uvhead' list */ uv.u.l.next = g.uvhead.u.l.next; uv.u.l.next.u.l.prev = uv; g.uvhead.u.l.next = uv; lua_assert(uv.u.l.next.u.l.prev == uv && uv.u.l.prev.u.l.next == uv); return uv; }
public static UpVal luaF_findupval(lua_State L, StkId level) { global_State g = G(L); GCObjectRef pp = new OpenValRef(L); UpVal p; UpVal uv; while (pp.get() != null && (p = ngcotouv(pp.get())).v >= level) { lua_assert(p.v != p.u.value); if (p.v == level) /* found a corresponding upvalue? */ { if (isdead(g, obj2gco(p))) /* is it dead? */ { changewhite(obj2gco(p)); /* ressurect it */ } return(p); } pp = new NextRef(p); } uv = luaM_new <UpVal>(L); /* not found: create a new one */ uv.tt = LUA_TUPVAL; uv.marked = luaC_white(g); uv.v = level; /* current value lives in the stack */ uv.next = pp.get(); /* chain it in the proper position */ pp.set(obj2gco(uv)); uv.u.l.prev = g.uvhead; /* double link it in `uvhead' list */ uv.u.l.next = g.uvhead.u.l.next; uv.u.l.next.u.l.prev = uv; g.uvhead.u.l.next = uv; lua_assert(uv.u.l.next.u.l.prev == uv && uv.u.l.prev.u.l.next == uv); return(uv); }
public static void luaC_barrierback(lua_State L, GCObject p, TValue v) { if (valiswhite(v) && isblack(obj2gco(p))) { luaC_barrierback_(L, p); } }
public static void luaC_barrier(lua_State L, object p, TValue v) { if (valiswhite(v) && isblack(obj2gco(p))) { luaC_barrierf(L, obj2gco(p), gcvalue(v)); } }
public static void luaC_barrierback(lua_State L, GCUnion p, TValue v) { if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) { luaC_barrierback_(L, (Table)p); } } //FIXME:???(Table)p
} //FIXME: macro in {} public static void luaC_barrier(lua_State L, GCObject p, TValue v) { if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) { luaC_barrier_(L, obj2gco(p), gcvalue(v)); } }
public static void luaG_aritherror(lua_State L, TValue p1, TValue p2) { TValue temp = new TValue(); if (luaV_tonumber(p1, temp) == null) p2 = p1; /* first operand is wrong */ luaG_typeerror(L, p2, "perform arithmetic on"); }
private static void resume(lua_State L, object ud) { StkId firstArg = (StkId)ud; CallInfo ci = L.ci; if (L.status == 0) /* start coroutine? */ { lua_assert(ci == L.base_ci[0] && firstArg > L.base_); if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA) { return; } } else /* resuming from previous yield */ { lua_assert(L.status == LUA_YIELD); L.status = 0; if (!f_isLua(ci)) /* `common' yield? */ /* finish interrupted execution of `OP_CALL' */ { lua_assert(GET_OPCODE((ci - 1).savedpc[-1]) == OpCode.OP_CALL || GET_OPCODE((ci - 1).savedpc[-1]) == OpCode.OP_TAILCALL); if (luaD_poscall(L, firstArg) != 0) /* complete it... */ { L.top = L.ci.top; /* and correct top if not multiple results */ } } else /* yielded inside a hook: just continue its execution */ { L.base_ = L.ci.base_; } } luaV_execute(L, L.ci - L.base_ci); }
public static void markvalue(global_State g, TValue o) { checkconsistency(o); if (iscollectable(o) && iswhite(gcvalue(o))) { reallymarkobject(g, gcvalue(o)); } }
public static void luaG_concaterror(lua_State L, StkId p1, StkId p2) { if (ttisstring(p1) || ttisnumber(p1)) { p1 = p2; } lua_assert(!ttisstring(p1) && !ttisnumber(p1)); luaG_typeerror(L, p1, "concatenate"); }
public static void luaV_settable(lua_State L, StkId t, StkId k, StkId v) { TValue slot = null; if (0 == luaV_fastset_luaH_get(L, t, k, ref slot, v)) { luaV_finishset(L, t, k, v, slot); } }
public static void luaG_aritherror(lua_State L, TValue p1, TValue p2) { TValue temp = new TValue(); if (luaV_tonumber(p1, temp) == null) { p2 = p1; /* first operand is wrong */ } luaG_typeerror(L, p2, "perform arithmetic on"); }
//#else //#define cvt2num(o) 0 /* no convertion from strings to numbers */ //#endif public static int tonumber(ref StkId o, ref lua_Number n) { if (ttisfloat(o)) { n = fltvalue(o); return(1); } else { return(luaV_tonumber_(o, ref n)); } }
public static int tointeger(ref StkId o, ref lua_Integer i) { if (ttisinteger(o)) { i = ivalue(o); return(1); } else { return(luaV_tointeger_(o, ref i)); } }
public static TString luaX_newstring(LexState ls, CharPtr str, uint l) { lua_State L = ls.L; TString ts = luaS_newlstr(L, str, l); TValue o = luaH_setstr(L, ls.fs.h, ts); /* entry for `str' */ if (ttisnil(o)) { setbvalue(o, 1); /* make sure `str' will not be collected */ } return(ts); }
/* ** standard implementation for 'gettable' */ public static void luaV_gettable(lua_State L, TValue t, TValue k, StkId v) { TValue slot = null; if (0 != luaV_fastget_luaH_get(L, t, k, ref slot)) { setobj2s(L, v, slot); } else { luaV_finishget(L, t, k, v, slot); } }
public static int luaV_fastget_luaH_get(lua_State L, TValue t, TValue k, ref TValue slot) { if (!ttistable(t)) { slot = null; return(0); /* not a table; 'slot' is NULL and result is 0 */ } else { slot = luaH_get(hvalue(t), k); /* else, do raw access */ return((!ttisnil(slot))?1:0); /* result not nil? */ } }
/* only ANSI way to check whether a pointer points to an array */ private static int isinstack(CallInfo ci, TValue o) { StkId p; for (p = ci.base_; p < ci.top; StkId.inc(ref p)) { if (o == p) { return(1); } } return(0); }
public static CharPtr lua_setlocal(lua_State L, lua_Debug ar, int n) { CallInfo ci = L.base_ci[ar.i_ci]; CharPtr name = findlocal(L, ci, n); lua_lock(L); if (name != null) { setobjs2s(L, ci.base_[n - 1], L.top - 1); } StkId.dec(ref L.top); /* pop value */ lua_unlock(L); return(name); }
/* ** The next function tells whether a key or value can be cleared from ** a weak table. Non-collectable objects are never removed from weak ** tables. Strings behave as `values', so are never removed too. for ** other objects: if really collected, cannot keep them; for userdata ** being finalized, keep them in keys, but not in values */ private static bool iscleared(TValue o, bool iskey) { if (!iscollectable(o)) { return(false); } if (ttisstring(o)) { stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */ return(false); } return(iswhite(gcvalue(o)) || (ttisuserdata(o) && (!iskey && isfinalized(uvalue(o))))); }
private static StkId callrethooks(lua_State L, StkId firstResult) { ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */ luaD_callhook(L, LUA_HOOKRET, -1); if (f_isLua(L.ci)) /* Lua function? */ { while (((L.hookmask & LUA_MASKRET) != 0) && (L.ci.tailcalls-- != 0)) /* tail calls */ { luaD_callhook(L, LUA_HOOKTAILRET, -1); } } return(restorestack(L, fr)); }
/* ** function to be used with macro "fasttm": optimized for absence of ** tag methods */ public static TValue luaT_gettm(Table events, TMS event_, TString ename) { /*const*/ TValue tm = luaH_getstr(events, ename); lua_assert(event_ <= TMS.TM_EQ); if (ttisnil(tm)) /* no tag method? */ { events.flags |= (byte)(1 << (int)event_); /* cache this fact */ return(null); } else { return(tm); } }
public static int luaG_ordererror(lua_State L, TValue p1, TValue p2) { CharPtr t1 = luaT_typenames[ttype(p1)]; CharPtr t2 = luaT_typenames[ttype(p2)]; if (t1[2] == t2[2]) { luaG_runerror(L, "attempt to compare two %s values", t1); } else { luaG_runerror(L, "attempt to compare %s with %s", t1, t2); } return(0); }
public static void luaG_errormsg(lua_State L) { if (L.errfunc != 0) /* is there an error handling function? */ { StkId errfunc = restorestack(L, L.errfunc); if (!ttisfunction(errfunc)) { luaD_throw(L, LUA_ERRERR); } setobjs2s(L, L.top, L.top - 1); /* move argument */ setobjs2s(L, L.top - 1, errfunc); /* push function */ incr_top(L); luaD_call(L, L.top - 2, 1); /* call it */ } luaD_throw(L, LUA_ERRRUN); }
public static void arith_op(lua_State L, op_delegate op, TMS tm, StkId base_, Instruction i, TValue[] k, StkId ra, InstructionPtr pc) { TValue rb = RKB(L, base_, i, k); TValue rc = RKC(L, base_, i, k); if (ttisnumber(rb) && ttisnumber(rc)) { lua_Number nb = nvalue(rb), nc = nvalue(rc); setnvalue(ra, op(nb, nc)); } else { //Protect( L.savedpc = InstructionPtr.Assign(pc); Arith(L, ra, rb, rc, tm); base_ = L.base_; //); } }
public static void luaF_close(lua_State L, StkId level) { UpVal uv; global_State g = G(L); while (L.openupval != null && (uv = ngcotouv(L.openupval)).v >= level) { GCObject o = obj2gco(uv); lua_assert(!isblack(o) && uv.v != uv.u.value); L.openupval = uv.next; /* remove from `open' list */ if (isdead(g, o)) luaF_freeupval(L, uv); /* free upvalue */ else { unlinkupval(uv); setobj(L, uv.u.value, uv.v); uv.v = uv.u.value; /* now current value lives here */ luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */ } } }
public static void Arith(lua_State L, StkId ra, TValue rb, TValue rc, TMS op) { TValue tempb = new TValue(), tempc = new TValue(); TValue b, c; if ((b = luaV_tonumber(rb, tempb)) != null && (c = luaV_tonumber(rc, tempc)) != null) { lua_Number nb = nvalue(b), nc = nvalue(c); switch (op) { case TMS.TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break; case TMS.TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break; case TMS.TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break; case TMS.TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break; case TMS.TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break; case TMS.TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break; case TMS.TM_UNM: setnvalue(ra, luai_numunm(nb)); break; default: lua_assert(false); break; } } else if (call_binTM(L, rb, rc, ra, op) == 0) luaG_aritherror(L, rb, rc); }
internal static bool ttisuserdata(TValue o) { return (ttype(o) == LUA_TUSERDATA); }
static CharPtr aux_upvalue(StkId fi, int n, ref TValue val) { Closure f; if (!ttisfunction(fi)) return null; f = clvalue(fi); if (f.c.isC != 0) { if (!(1 <= n && n <= f.c.nupvalues)) return null; val = f.c.upvalue[n-1]; return ""; } else { Proto p = f.l.p; if (!(1 <= n && n <= p.sizeupvalues)) return null; val = f.l.upvals[n-1].v; return getstr(p.upvalues[n-1]); } }
public static bool isLfunction(TValue o) { return ((ttype(o) == LUA_TFUNCTION) && (clvalue(o).c.isC==0)); }
public Node(TValue i_val, TKey i_key) { this.values = new Node[] { this }; this.index = 0; this.i_val = i_val; this.i_key = i_key; }
public static lua_Integer lua_tointeger(lua_State L, int idx) { TValue n = new TValue(); TValue o = index2adr(L, idx); if (tonumber(ref o, n) != 0) { lua_Integer res; lua_Number num = nvalue(o); lua_number2integer(out res, num); return res; } else return 0; }
internal static bool ttisthread(TValue o) { return (ttype(o) == LUA_TTHREAD); }
internal static bool ttistable(TValue o) { return (ttype(o) == LUA_TTABLE); }
internal static bool ttisstring(TValue o) { return (ttype(o) == LUA_TSTRING); }
internal static bool ttisnumber(TValue o) { return (ttype(o) == LUA_TNUMBER); }
/* Macros to test type */ internal static bool ttisnil(TValue o) { return (ttype(o) == LUA_TNIL); }
internal static bool ttisfunction(TValue o) { return (ttype(o) == LUA_TFUNCTION); }
public Node() { this.i_val = new TValue(); this.i_key = new TKey(); }
public Node(Node copy) { this.values = copy.values; this.index = copy.index; this.i_val = new TValue(copy.i_val); this.i_key = new TKey(copy.i_key); }
public static CharPtr lua_getupvalue(lua_State L, int funcindex, int n) { CharPtr name; TValue val = new TValue(); lua_lock(L); name = aux_upvalue(index2adr(L, funcindex), n, ref val); if (name != null) { setobj2s(L, L.top, val); api_incr_top(L); } lua_unlock(L); return name; }
public static void lua_setfield(lua_State L, int idx, CharPtr k) { StkId t; TValue key = new TValue(); lua_lock(L); api_checknelems(L, 1); t = index2adr(L, idx); api_checkvalidindex(L, t); setsvalue(L, key, luaS_new(L, k)); luaV_settable(L, t, key, L.top - 1); StkId.dec(ref L.top); /* pop value */ lua_unlock(L); }
public static void lua_getfield(lua_State L, int idx, CharPtr k) { StkId t; TValue key = new TValue(); lua_lock(L); t = index2adr(L, idx); api_checkvalidindex(L, t); setsvalue(L, key, luaS_new(L, k)); luaV_gettable(L, t, key, L.top); api_incr_top(L); lua_unlock(L); }
public static void api_checkvalidindex(lua_State L, StkId i) { api_check(L, i != luaO_nilobject); }
public static int lua_isnumber(lua_State L, int idx) { TValue n = new TValue(); TValue o = index2adr(L, idx); return tonumber(ref o, n); }
internal static void setthvalue(lua_State L, TValue obj, GCObject x) { obj.value.gc = x; obj.tt = LUA_TTHREAD; checkliveness(G(L), obj); }
public static void luaA_pushobject(lua_State L, TValue o) { setobj2s(L, L.top, o); api_incr_top(L); }
internal static void setttype(TValue obj, int tt) { obj.tt = tt; }
public static CharPtr lua_setupvalue(lua_State L, int funcindex, int n) { CharPtr name; TValue val = new TValue(); StkId fi; lua_lock(L); fi = index2adr(L, funcindex); api_checknelems(L, 1); name = aux_upvalue(fi, n, ref val); if (name != null) { StkId.dec(ref L.top); setobj(L, val, L.top); luaC_barrier(L, clvalue(fi), L.top); } lua_unlock(L); return name; }
internal static void setuvalue(lua_State L, TValue obj, GCObject x) { obj.value.gc = x; obj.tt = LUA_TUSERDATA; checkliveness(G(L), obj); }
public static lua_Number lua_tonumber(lua_State L, int idx) { TValue n = new TValue(); TValue o = index2adr(L, idx); if (tonumber(ref o, n) != 0) return nvalue(o); else return 0; }
internal static bool ttisboolean(TValue o) { return (ttype(o) == LUA_TBOOLEAN); }