private StkId T_GetTMByObj( ref TValue o, TMS tm ) { LuaTable mt = null; switch( o.Tt ) { case (int)LuaType.LUA_TTABLE: { var tbl = o.HValue(); mt = tbl.MetaTable; break; } case (int)LuaType.LUA_TUSERDATA: { var ud = o.RawUValue(); mt = ud.MetaTable; break; } default: { mt = G.MetaTables[o.Tt]; break; } } return (mt != null) ? mt.GetStr( GetTagMethodName( tm ) ) : TheNilValue; }
public static TValue luaV_tonumber (TValue obj, TValue n) { lua_Number num; if (ttisnumber(obj)) return obj; if (ttisstring(obj) && (luaO_str2d(svalue(obj), out num)!=0)) { setnvalue(n, num); return n; } else return null; }
public static TValue luaV_tonumber (TValue obj, TValue n) { lua_Number num; if (TTIsNumber(obj)) return obj; if (TTIsString(obj) && (LuaOStr2d(SValue(obj), out num)!=0)) { SetNValue(n, num); return n; } else return null; }
private static void callTMres (lua_State L, StkId res, TValue f, TValue p1, TValue p2) { ptrdiff_t result = savestack(L, res); setobj2s(L, L.top, f); /* push function */ setobj2s(L, L.top+1, p1); /* 1st argument */ setobj2s(L, L.top+2, p2); /* 2nd argument */ luaD_checkstack(L, 3); L.top += 3; luaD_call(L, L.top-3, 1); res = restorestack(L, result); StkId.dec(ref L.top); setobjs2s(L, res, L.top); }
public static unsafe int Main () { TValue[] values = new TValue[10]; values[0] = new TValue (0L); values[1] = new TValue (1000L); values[2] = new TValue (1L); Console.WriteLine ("values: {0} {1} {2}", values[0], values[1], values[2]); fixed (TValue* vals = values) { Console.WriteLine ("fixed: {0} {1} {2}", vals[0], vals[1], vals[2]); if (vals[0].ToString () != "0") return 1; if (vals[1].ToString() != "1000") return 2; if (vals[2].ToString() != "1") return 3; } Console.WriteLine ("ok"); return 0; }
public static lua_Number fltvalue(TValue o) { return((lua_Number)check_exp(ttisfloat(o), val_(o).n)); }
public static Udata uvalue(TValue o) { return((Udata)check_exp(ttisfulluserdata(o), gco2u(val_(o).gc))); }
public static lua_Integer lua_tointeger(LuaState 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; }
/* Macros to access values */ public static lua_Integer ivalue(TValue o) { return((lua_Integer)check_exp(ttisinteger(o), val_(o).i)); }
private static int boolK (FuncState fs, int b) { TValue o = new TValue(); setbvalue(o, b); return addk(fs, o, o); }
public static CharPtr lua_getupvalue(LuaState 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 lua_State thvalue(TValue o) { return((lua_State)check_exp(ttisthread(o), gco2th(val_(o).gc))); }
public Wrapper(TValue value) { Value = value; }
public static Table hvalue(TValue o) { return((Table)check_exp(ttistable(o), gco2t(val_(o).gc))); }
public static int bvalue(TValue o) { return((int)check_exp(ttisboolean(o), val_(o).b)); }
public static lua_CFunction fvalue(TValue o) { return((lua_CFunction)check_exp(ttislcf(o), val_(o).f)); }
public static CClosure clCvalue(TValue o) { return((CClosure)check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))); }
public static LClosure clLvalue(TValue o) { return((LClosure)check_exp(ttisLclosure(o), gco2lcl(val_(o).gc))); }
/* ** inserts a new key into a hash table; first, check whether key's main ** position is free. If not, check whether colliding node is in its main ** position or not: if it is not, move colliding node to an empty place and ** put new key in its main position; otherwise (colliding node is in its main ** position), new key goes to an empty position. */ private static TValue newkey (LuaState L, Table t, TValue key) { Node mp = mainposition(t, key); if (!TTIsNil(gval(mp)) || mp == dummynode) { Node othern; Node n = getfreepos(t); /* get a free place */ if (n == null) { /* cannot find a free place? */ rehash(L, t, key); /* grow table */ return luaH_set(L, t, key); /* re-insert key into grown table */ } LuaAssert(n != dummynode); othern = mainposition(t, key2tval(mp)); if (othern != mp) { /* is colliding node out of its main position? */ /* yes; move colliding node into free position */ while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ gnext_set(othern, n); /* redo the chain with `n' in place of `mp' */ n.i_val = new LuaTypeValue(mp.i_val); /* copy colliding node into free pos. (mp.next also goes) */ n.i_key = new TKey(mp.i_key); gnext_set(mp, null); /* now `mp' is free */ SetNilValue(gval(mp)); } else { /* colliding node is in its own main position */ /* new node will go into free position */ gnext_set(n, gnext(mp)); /* chain new position */ gnext_set(mp, n); mp = n; } } gkey(mp).value.Copy(key.value); gkey(mp).tt = key.tt; LuaCBarrierT(L, t, key); LuaAssert(TTIsNil(gval(mp))); return gval(mp); }
/* a dead value may get the 'gc' field, but cannot access its contents */ public static object deadvalue(TValue o) { return((object)check_exp(ttisdeadkey(o), (object)(val_(o).gc))); }
public static TValue luaH_set (LuaState L, Table t, TValue key) { TValue p = luaH_get(t, key); t.flags = 0; if (p != LuaONilObject) return (TValue)p; else { if (TTIsNil(key)) LuaGRunError(L, "table index is nil"); else if (TTIsNumber(key) && luai_numisnan(NValue(key))) LuaGRunError(L, "table index is NaN"); return newkey(L, t, key); } }
public static int l_isfalse(TValue o) { return(((ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))) ? 1 : 0); }
public static int luaK_stringK (FuncState fs, TString s) { TValue o = new TValue(); setsvalue(fs.L, o, s); return addk(fs, o, o); }
public static bool iscollectable(TValue o) { return((rttype(o) & BIT_ISCOLLECTABLE) != 0); }
private static int AddK (FuncState fs, TValue k, TValue v) { LuaState L = fs.L; TValue idx = luaH_set(L, fs.h, k); Proto f = fs.f; int oldsize = f.sizek; if (TTIsNumber(idx)) { LuaAssert(LuaORawEqualObj(fs.f.k[CastInt(NValue(idx))], v)); return CastInt(NValue(idx)); } else { /* constant not found; create a new entry */ SetNValue(idx, CastNum(fs.nk)); LuaMGrowVector(L, ref f.k, fs.nk, ref f.sizek, MAXARG_Bx, "constant table overflow"); while (oldsize < f.sizek) SetNilValue(f.k[oldsize++]); SetObj(L, f.k[fs.nk], v); LuaCBarrier(L, f, v); return fs.nk++; } }
/* Macros for internal tests */ public static bool righttt(TValue obj) { return(ttype(obj) == gcvalue(obj).tt); }
public static int lua_isnumber(LuaState L, int idx) { TValue n = new TValue(); TValue o = index2adr(L, idx); return tonumber(ref o, n); }
public static object pvalue(TValue o) { return((object)check_exp(ttislightuserdata(o), val_(o).p)); }
public static void lua_setfield(LuaState 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 int LuaGOrderError (LuaState L, TValue p1, TValue p2) { CharPtr t1 = luaT_typenames[TType(p1)]; CharPtr t2 = luaT_typenames[TType(p2)]; if (t1[2] == t2[2]) LuaGRunError(L, "attempt to compare two %s values", t1); else LuaGRunError(L, "attempt to compare %s with %s", t1, t2); return 0; }
public static GCObject gcvalue(TValue o) { return((GCObject)check_exp(iscollectable(o), val_(o).gc)); }
private static int countint (TValue key, int[] nums) { int k = arrayindex(key); if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ nums[CeilLog2(k)]++; /* count as such */ return 1; } else return 0; }
public static void checkliveness(global_State g, TValue obj) { lua_longassert(!iscollectable(obj) || (righttt(obj) && !isdead(g, gcvalue(obj)))); }
public static TString tsvalue(TValue o) { return((TString)check_exp(ttisstring(o), gco2ts(val_(o).gc))); }
public static void LuaGArithError (LuaState L, TValue p1, TValue p2) { TValue temp = new LuaTypeValue(); if (luaV_tonumber(p1, temp) == null) p2 = p1; /* first operand is wrong */ LuaGTypeError(L, p2, "perform arithmetic on"); }
/* Macros to set values */ public static void settt_(TValue o, int t) { o.tt_ = t; }
/* ** returns the index for `key' if `key' is an appropriate key to live in ** the array part of the table, -1 otherwise. */ private static int arrayindex (TValue key) { if (TTIsNumber(key)) { lua_Number n = NValue(key); int k; lua_number2int(out k, n); if (luai_numeq(CastNum(k), n)) return k; } return -1; /* `key' did not match some condition */ }
public static void setfltvalue(TValue obj, lua_Number x) { TValue io = obj; io.value_.n = x; settt_(io, LUA_TNUMFLT); }
private static void rehash (LuaState L, Table t, TValue ek) { int nasize, na; int[] nums = new int[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */ int i; int totaluse; for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */ nasize = numusearray(t, nums); /* count keys in array part */ totaluse = nasize; /* all those keys are integer keys */ totaluse += numusehash(t, nums, ref nasize); /* count keys in hash part */ /* count extra key */ nasize += countint(ek, nums); totaluse++; /* compute new size for array part */ na = computesizes(nums, ref nasize); /* resize the table to new computed sizes */ resize(L, t, nasize, totaluse - na); }
public static void setivalue(TValue obj, lua_Integer x) { TValue io = obj; io.value_.i = x; settt_(io, LUA_TNUMINT); }
/* ** main search function */ public static TValue luaH_get (Table t, TValue key) { switch (TType(key)) { case LUA_TNIL: return LuaONilObject; case LUA_TSTRING: return luaH_getstr(t, RawTSValue(key)); case LUA_TNUMBER: { int k; lua_Number n = NValue(key); lua_number2int(out k, n); if (luai_numeq(CastNum(k), NValue(key))) /* index is int? */ return luaH_getnum(t, k); /* use specialized version */ /* else go through ... actually on second thoughts don't, because this is C#*/ Node node = mainposition(t, key); do { /* check whether `key' is somewhere in the chain */ if (LuaORawEqualObj(key2tval(node), key) != 0) return gval(node); /* that's it */ else node = gnext(node); } while (node != null); return LuaONilObject; } default: { Node node = mainposition(t, key); do { /* check whether `key' is somewhere in the chain */ if (LuaORawEqualObj(key2tval(node), key) != 0) return gval(node); /* that's it */ else node = gnext(node); } while (node != null); return LuaONilObject; } } }
public static void setnilvalue(TValue obj) { settt_(obj, LUA_TNIL); }
/* ** returns the `main' position of an element in a table (that is, the index ** of its hash value) */ private static Node mainposition (Table t, TValue key) { switch (TType(key)) { case LUA_TNUMBER: return hashnum(t, NValue(key)); case LUA_TSTRING: return hashstr(t, RawTSValue(key)); case LUA_TBOOLEAN: return hashboolean(t, BValue(key)); case LUA_TLIGHTUSERDATA: return hashpointer(t, PValue(key)); default: return hashpointer(t, GCValue(key)); } }
public static void setfvalue(TValue obj, lua_CFunction x) { TValue io = obj; io.value_.f = x; settt_(io, LUA_TLCF); } //FIXME:chagned, val_(io)
private static int addk (FuncState fs, TValue k, TValue v) { lua_State L = fs.L; TValue idx = luaH_set(L, fs.h, k); Proto f = fs.f; int oldsize = f.sizek; if (ttisnumber(idx)) { lua_assert(luaO_rawequalObj(fs.f.k[cast_int(nvalue(idx))], v)); return cast_int(nvalue(idx)); } else { /* constant not found; create a new entry */ setnvalue(idx, cast_num(fs.nk)); luaM_growvector(L, ref f.k, fs.nk, ref f.sizek, MAXARG_Bx, "constant table overflow"); while (oldsize < f.sizek) setnilvalue(f.k[oldsize++]); setobj(L, f.k[fs.nk], v); luaC_barrier(L, f, v); return fs.nk++; } }
} //FIXME:chagned, val_(io) public static void setpvalue(TValue obj, object x) { TValue io = obj; io.value_.p = x; settt_(io, LUA_TLIGHTUSERDATA); } //FIXME:chagned, val_(io)
public static int luaK_numberK (FuncState fs, lua_Number r) { TValue o = new TValue(); setnvalue(o, r); return addk(fs, o, o); }
} //FIXME:chagned, val_(io) public static void setbvalue(TValue obj, int x) { TValue io = obj; io.value_.b = x; settt_(io, LUA_TBOOLEAN); } //FIXME:chagned, val_(io)
private static int nilK (FuncState fs) { TValue k = new TValue(), v = new TValue(); setnilvalue(v); /* cannot use nil as key; instead use table itself to represent nil */ sethvalue(fs.L, k, fs.h); return addk(fs, k, v); }
} //FIXME:chagned, val_(io) public static void setgcovalue(lua_State L, TValue obj, GCObject x) { TValue io = obj; GCObject i_g = x; io.value_.gc = i_g; settt_(io, ctb(i_g.tt)); } //FIXME:chagned, val_(io)
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 void setdeadvalue(TValue obj) { settt_(obj, LUA_TDEADKEY); }
public static CharPtr lua_setupvalue(LuaState 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; }
public static void setobj(lua_State L, TValue obj1, TValue obj2) { TValue io1 = (obj1); lua_TValue.copy(io1, obj2); /*(void)L;*/ checkliveness(G(L), io1); }
public static lua_Number lua_tonumber(LuaState L, int idx) { TValue n = new TValue(); TValue o = index2adr(L, idx); if (tonumber(ref o, n) != 0) return nvalue(o); else return 0; }
//#define setsvalue2s setsvalue public static void setsvalue2s(lua_State L, TValue obj, TString x) { setsvalue(L, obj, x); }
public static void lua_getfield(LuaState 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); }
//#define sethvalue2s sethvalue public static void sethvalue2s(lua_State L, TValue obj, Table x) { sethvalue(L, obj, x); }
public static void luaA_pushobject(LuaState L, TValue o) { setobj2s(L, L.top, o); api_incr_top(L); }
//#define setptvalue2s setptvalue //public static void setptvalue2s(lua_State L, TValue obj, Proto x) { setptvalue(L, obj, x); } ///* from table to same table */ //#define setobjt2t setobj public static void setobjt2t(lua_State L, TValue obj, TValue x) { setobj(L, obj, x); }