public static int lua_isnumber(lua_State L, int idx) { TValue o = imp.index2addr(L, idx); double n = 0; return(imp.tonumber(o, ref n) ? 1 : 0); }
public static string lua_setupvalue(lua_State L, int funcindex, int n) { lua_lock(L); TValue val = null; CClosure owner = null; UpVal uv = null; TValue fi = imp.index2addr(L, funcindex); imp.api_checknelems(L, 1); string name = imp.aux_upvalue(fi, n, ref val, ref owner, ref uv); if (name != null) { L.top--; imp.setobj(L, val, L.top); if (owner != null) { imp.luaC_barrier(L, owner, L.top); } else if (uv != null) { imp.luaC_upvalbarrier(L, uv); } } lua_unlock(L); return(name); }
public static int lua_rawequal(lua_State L, int index1, int index2) { TValue o1 = imp.index2addr(L, index1); TValue o2 = imp.index2addr(L, index2); return((imp.isvalid(o1) && imp.isvalid(o2)) ? (imp.luaV_rawequalobj(o1, o2) ? 1 : 0) : 0); }
public static int lua_load(lua_State L, lua_Reader reader, object data, string chunkname, string mode) { lua_lock(L); if (chunkname == null) { chunkname = "?"; } Zio z = new Zio(); imp.luaZ_init(L, z, reader, data); int status = imp.luaD_protectedparser(L, z, chunkname, mode); if (status == LUA_OK) /* no errors? */ { LClosure f = imp.clLvalue(L, L.top - 1); /* get newly created function */ if (f.nupvalues >= 1) /* does it have an upvalue? */ /* get global table from registry */ { Table reg = imp.hvalue(imp.G(L).l_registry); TValue gt = imp.luaH_getint(reg, LUA_RIDX_GLOBALS); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ imp.setobj(L, f.upvals[0].v, gt); imp.luaC_upvalbarrier(L, f.upvals[0]); } } lua_unlock(L); return(status); }
public static int lua_getmetatable(lua_State L, int objindex) { lua_lock(L); int res = 0; TValue obj = imp.index2addr(L, objindex); Table mt = null; switch (imp.ttnov(obj)) { case LUA_TTABLE: mt = imp.hvalue(obj).metatable; break; case LUA_TUSERDATA: mt = imp.uvalue(obj).metatable; break; default: mt = imp.G(L).mt[imp.ttnov(obj)]; break; } if (mt != null) { imp.sethvalue(L, L.top, mt); imp.api_incr_top(L); res = 1; } lua_unlock(L); return(res); }
public Table[] mt; /* metatables for basic types */ public global_State() { strt = luaM_newobject <stringtable> (null); l_registry = luaM_newobject <TValue> (null); buff = luaM_newobject <MBuffer> (null); tmname = luaM_emptyvector <TString> (null, (int)TMS.TM_N); mt = luaM_emptyvector <Table> (null, cc.LUA_NUMTAGS); }
public static void lua_len(lua_State L, int idx) { lua_lock(L); TValue t = imp.index2addr(L, idx); imp.luaV_objlen(L, L.top, t); imp.api_incr_top(L); lua_unlock(L); }
public static int lua_gettable(lua_State L, int idx) { lua_lock(L); TValue t = imp.index2addr(L, idx); imp.luaV_gettable(L, t, L.top - 1, L.top - 1); lua_unlock(L); return(imp.ttnov(L, L.top - 1)); }
public static void lua_settable(lua_State L, int idx) { lua_lock(L); imp.api_checknelems(L, 2); TValue t = imp.index2addr(L, idx); imp.luaV_settable(L, t, L.top - 2, L.top - 1); L.top -= 2; /* pop index and value */ lua_unlock(L); }
public static int lua_rawget(lua_State L, int idx) { lua_lock(L); TValue t = imp.index2addr(L, idx); imp.api_check(imp.ttistable(t), "table expected"); imp.setobj2s(L, L.top - 1, imp.luaH_get(imp.hvalue(t), L.stack[L.top - 1])); lua_unlock(L); return(imp.ttnov(L, L.top - 1)); }
public static int lua_getuservalue(lua_State L, int idx) { lua_lock(L); TValue o = imp.index2addr(L, idx); imp.getuservalue(L, imp.uvalue(o), L.top); imp.api_incr_top(L); lua_unlock(L); return(imp.ttnov(L, L.top - 1)); }
public static int lua_getfield(lua_State L, int idx, string k) { lua_lock(L); TValue t = imp.index2addr(L, idx); imp.setsvalue2s(L, L.top, imp.luaS_new(L, k)); imp.api_incr_top(L); imp.luaV_gettable(L, t, L.top - 1, L.top - 1); lua_unlock(L); return(imp.ttnov(L, L.top - 1)); }
public static int lua_geti(lua_State L, int idx, long n) { lua_lock(L); TValue t = imp.index2addr(L, idx); imp.setivalue(L, L.top, n); imp.api_incr_top(L); imp.luaV_gettable(L, t, L.top - 1, L.top - 1); lua_unlock(L); return(imp.ttnov(L, L.top - 1)); }
public static int lua_rawgeti(lua_State L, int idx, long n) { lua_lock(L); TValue t = imp.index2addr(L, idx); imp.api_check(imp.ttistable(t), "table expected"); imp.setobj2s(L, L.top, imp.luaH_getint(imp.hvalue(t), n)); imp.api_incr_top(L); lua_unlock(L); return(imp.ttnov(L, L.top - 1)); }
public static void lua_seti(lua_State L, int idx, long n) { lua_lock(L); imp.api_checknelems(L, 1); TValue t = imp.index2addr(L, idx); imp.setivalue(L, L.top++, n); imp.luaV_settable(L, t, L.top - 1, L.top - 2); L.top -= 2; /* pop value and key */ lua_unlock(L); }
public static void lua_setfield(lua_State L, int idx, string k) { lua_lock(L); imp.api_checknelems(L, 1); TValue t = imp.index2addr(L, idx); imp.setsvalue2s(L, L.top++, imp.luaS_new(L, k)); imp.luaV_settable(L, t, L.top - 1, L.top - 2); L.top -= 2; /* pop value and key */ lua_unlock(L); }
public static void lua_setuservalue(lua_State L, int idx) { lua_lock(L); imp.api_checknelems(L, 1); TValue o = imp.index2addr(L, idx); imp.api_check(imp.ttisfulluserdata(o), "full userdata expected"); imp.setuservalue(L, imp.uvalue(o), L.top - 1); imp.luaC_barrierback(L, imp.gcvalue(o), L.top - 1); L.top--; lua_unlock(L); }
/* ** get functions (Lua -> stack) */ public static int lua_getglobal(lua_State L, string name) { Table reg = imp.hvalue(imp.G(L).l_registry); lua_lock(L); TValue gt = imp.luaH_getint(reg, LUA_RIDX_GLOBALS); imp.setsvalue2s(L, L.top++, imp.luaS_new(L, name)); imp.luaV_gettable(L, gt, L.top - 1, L.top - 1); lua_unlock(L); return(imp.ttnov(L, L.top - 1)); }
/* ** Reverse the stack segment from 'from' to 'to' ** (auxiliary to 'lua_rotate') */ public static void reverse(lua_State L, int from, int to) { TValue temp = new TValue(); for (; from < to; from++) { setobj(L, temp, from); setobjs2s(L, from, to); setobj2s(L, to, temp); to--; } }
public static long lua_tointegerx(lua_State L, int idx, ref int pisnum) { long n = 0; TValue o = imp.index2addr(L, idx); bool isnum = imp.tointeger(o, ref n); if (isnum == false) { n = 0; /* call to 'tointeger' may change 'n' even if it fails */ } pisnum = (isnum ? 1 : 0); return(n); }
public static object lua_touserdata(lua_State L, int idx) { TValue o = imp.index2addr(L, idx); switch (imp.ttnov(o)) { case LUA_TUSERDATA: return(imp.getudatamem(imp.uvalue(o))); case LUA_TLIGHTUSERDATA: return(imp.pvalue(o)); default: return(null); } }
/* ** set functions (stack -> Lua) */ public static void lua_setglobal(lua_State L, string name) { Table reg = imp.hvalue(imp.G(L).l_registry); lua_lock(L); imp.api_checknelems(L, 1); TValue gt = imp.luaH_getint(reg, LUA_RIDX_GLOBALS); /* global table */ imp.setsvalue2s(L, L.top++, imp.luaS_new(L, name)); imp.luaV_settable(L, gt, L.top - 1, L.top - 2); L.top -= 2; /* pop value and key */ lua_unlock(L); }
public static void lua_rawseti(lua_State L, int idx, long n) { lua_lock(L); imp.api_checknelems(L, 1); TValue o = imp.index2addr(L, idx); imp.api_check(imp.ttistable(o), "table expected"); Table t = imp.hvalue(o); imp.luaH_setint(L, t, n, L.top - 1); imp.luaC_barrierback(L, t, L.top - 1); L.top--; lua_unlock(L); }
public static UpVal getupvalref(lua_State L, int fidx, int n, ref LClosure pf) { TValue fi = index2addr(L, fidx); api_check(ttisLclosure(fi), "Lua function expected"); LClosure f = clLvalue(fi); api_check((1 <= n && n <= f.p.sizeupvalues), "invalid upvalue index"); if (pf != null) { pf = f; } return(f.upvals[n - 1]); /* get its upvalue pointer */ }
public static int lua_pcallk(lua_State L, int nargs, int nresults, int errfunc, long ctx, lua_KFunction k) { lua_lock(L); imp.api_check(k == null || imp.isLua(L.ci) == false, "cannot use continuations inside hooks"); imp.api_checknelems(L, nargs + 1); imp.api_check(L.status == LUA_OK, "cannot do calls on non-normal thread"); imp.checkresults(L, nargs, nresults); int func = 0; if (errfunc == 0) { func = 0; } else { TValue o = imp.index2addr(L, errfunc); imp.api_checkstackindex(errfunc, o); func = imp.savestack(L, errfunc); } CallS c = new CallS(); c.func = L.top - (nargs + 1); /* function to be called */ int status = 0; if (k == null && L.nny > 0) /* no continuation or no yieldable? */ { c.nresults = nresults; /* do a 'conventional' protected call */ status = imp.luaD_pcall(L, imp.f_call, c, imp.savestack(L, c.func), func); } else /* prepare continuation (call is already protected by 'resume') */ { CallInfo ci = L.ci; ci.u.c.k = k; /* save continuation */ ci.u.c.ctx = ctx; /* save context */ /* save information for error recovery */ ci.extra = imp.savestack(L, c.func); ci.u.c.old_errfunc = L.errfunc; L.errfunc = func; imp.setoah(ref ci.callstatus, L.allowhook); /* save value of 'allowhook' */ ci.callstatus |= imp.CIST_YPCALL; /* function can do error recovery */ imp.luaD_call(L, c.func, nresults, 1); /* do the call */ ci.callstatus = (byte)(ci.callstatus & (~imp.CIST_YPCALL)); L.errfunc = ci.u.c.old_errfunc; status = LUA_OK; /* if it is here, there were no errors */ } imp.adjustresults(L, nresults); lua_unlock(L); return(status); }
public static void lua_rawset(lua_State L, int idx) { lua_lock(L); imp.api_checknelems(L, 2); TValue o = imp.index2addr(L, idx); imp.api_check(imp.ttistable(o), "table expected"); Table t = imp.hvalue(o); imp.setobj2t(L, imp.luaH_set(L, t, L.top - 2), L.top - 1); imp.invalidateTMcache(t); imp.luaC_barrierback(L, t, L.top - 1); L.top -= 2; lua_unlock(L); }
/* ** Let x = AB, where A is a prefix of length 'n'. Then, ** rotate x n == BA. But BA == (A^r . B^r)^r. */ public static void lua_rotate(lua_State L, int idx, int n) { lua_lock(L); int t = L.top - 1; /* end of stack segment being rotated */ TValue p = imp.index2addr(L, idx); /* start of segment */ imp.api_checkstackindex(idx, p); imp.api_check((n >= 0 ? n : -n) <= (t - idx + 1), "invalid 'n'"); int m = (n >= 0 ? t - n : idx - n - 1); /* end of prefix */ imp.reverse(L, idx, m); /* reverse the prefix with length 'n' */ imp.reverse(L, m + 1, t); /* reverse the suffix */ imp.reverse(L, idx, t); /* reverse the entire segment */ lua_unlock(L); }
public static int lua_rawlen(lua_State L, int idx) { TValue o = imp.index2addr(L, idx); switch (imp.ttnov(o)) { case LUA_TSTRING: return(imp.tsvalue(o).len); case LUA_TUSERDATA: return(imp.uvalue(o).len); case LUA_TTABLE: return(imp.luaH_getn(imp.hvalue(o))); default: return(0); } }
public static string lua_getupvalue(lua_State L, int funcindex, int n) { lua_lock(L); TValue val = null; CClosure cl = null; UpVal uv = null; string name = imp.aux_upvalue(imp.index2addr(L, funcindex), n, ref val, ref cl, ref uv); if (name != null) { imp.setobj2s(L, L.top, val); imp.api_incr_top(L); } lua_unlock(L); return(name); }
public static void lua_rawsetp(lua_State L, int idx, object p) { lua_lock(L); imp.api_checknelems(L, 1); TValue o = imp.index2addr(L, idx); imp.api_check(imp.ttistable(o), "table expected"); Table t = imp.hvalue(o); TValue k = new TValue(); imp.setpvalue(k, p); imp.setobj2t(L, imp.luaH_set(L, t, k), L.top - 1); imp.luaC_barrierback(L, t, L.top - 1); L.top--; lua_unlock(L); }