public static new int ToString(IntPtr ptr) { try { object o = CheckObj(ptr, 1); if (o == null) { LuaObject.PushValue(ptr, true); LuaNativeMethods.lua_pushnil(ptr); return(2); } LuaObject.PushValue(ptr, true); if (o is byte[]) { byte[] b = (byte[])o; LuaNativeMethods.lua_pushlstring(ptr, b, b.Length); } else { LuaObject.PushValue(ptr, o.ToString()); } return(2); } catch (Exception e) { return(Error(ptr, e)); } }
public bool MoveNext() { if (tableIndex < 0) { return(false); } if (iterPhase == 0) { LuaNativeMethods.lua_pushnil(table.VariablePointer); iterPhase = 1; } else { LuaNativeMethods.lua_pop(table.VariablePointer, 1); } // var ty = LuaDLL.lua_type(t.L, -1); bool ret = LuaNativeMethods.lua_next(table.VariablePointer, tableIndex) > 0; if (!ret) { iterPhase = 2; } return(ret); }
protected static void AddMember(IntPtr ptr, string name, LuaCSFunction get, LuaCSFunction set, bool instance) { CheckMethodValid(get); CheckMethodValid(set); int t = instance ? -2 : -3; LuaNativeMethods.lua_createtable(ptr, 2, 0); if (get == null) { LuaNativeMethods.lua_pushnil(ptr); } else { PushValue(ptr, get); } LuaNativeMethods.lua_rawseti(ptr, -2, 1); if (set == null) { LuaNativeMethods.lua_pushnil(ptr); } else { PushValue(ptr, set); } LuaNativeMethods.lua_rawseti(ptr, -2, 2); LuaNativeMethods.lua_setfield(ptr, t, name); }
public static void PushVar(IntPtr ptr, object o) { if (o == null) { LuaNativeMethods.lua_pushnil(ptr); return; } Type t = o.GetType(); LuaState.PushVarDelegate push; LuaState ls = LuaState.Get(ptr); if (ls.TryGetTypePusher(t, out push)) { push(ptr, o); } else if (t.IsEnum) { PushEnum(ptr, Convert.ToInt32(o)); } else if (t.IsArray) { PushObject(ptr, (Array)o); } else { PushObject(ptr, o); } }
public static void RemoveDelgate(IntPtr ptr, int r) { LuaNativeMethods.lua_getglobal(ptr, DelgateTable); LuaNativeMethods.lua_getref(ptr, r); // push key LuaNativeMethods.lua_pushnil(ptr); // push nil value LuaNativeMethods.lua_settable(ptr, -3); // remove function from __LuaDelegate table LuaNativeMethods.lua_pop(ptr, 1); // pop __LuaDelegate }
public static void GC(IntPtr ptr, int p, UnityEngine.Object o) { // set ud's metatable is nil avoid gc again LuaNativeMethods.lua_pushnil(ptr); LuaNativeMethods.lua_setmetatable(ptr, p); ObjectCache t = ObjectCache.Get(ptr); t.GC(o); }
public static void PushValue(IntPtr ptr, LuaTable t) { if (t == null) { LuaNativeMethods.lua_pushnil(ptr); } else { t.Push(ptr); } }
public static int Import(IntPtr ptr) { try { LuaNativeMethods.luaL_checktype(ptr, 1, LuaTypes.TYPE_STRING); string str = LuaNativeMethods.lua_tostring(ptr, 1); string[] ns = str.Split('.'); LuaNativeMethods.lua_pushglobaltable(ptr); for (int n = 0; n < ns.Length; n++) { LuaNativeMethods.lua_getfield(ptr, -1, ns[n]); if (!LuaNativeMethods.lua_istable(ptr, -1)) { return(LuaObject.Error(ptr, "expect {0} is type table", ns)); } LuaNativeMethods.lua_remove(ptr, -2); } LuaNativeMethods.lua_pushnil(ptr); while (LuaNativeMethods.lua_next(ptr, -2) != 0) { string key = LuaNativeMethods.lua_tostring(ptr, -2); LuaNativeMethods.lua_getglobal(ptr, key); if (!LuaNativeMethods.lua_isnil(ptr, -1)) { LuaNativeMethods.lua_pop(ptr, 1); return(LuaObject.Error(ptr, "{0} had existed, import can't overload it.", key)); } LuaNativeMethods.lua_pop(ptr, 1); LuaNativeMethods.lua_setglobal(ptr, key); } LuaNativeMethods.lua_pop(ptr, 1); LuaObject.PushValue(ptr, true); return(1); } catch (Exception e) { return(LuaObject.Error(ptr, e)); } }
public static int ToTable(IntPtr ptr, Array o) { if (o == null) { LuaNativeMethods.lua_pushnil(ptr); return(1); } LuaNativeMethods.lua_createtable(ptr, o.Length, 0); for (int n = 0; n < o.Length; n++) { LuaObject.PushVar(ptr, o.GetValue(n)); LuaNativeMethods.lua_rawseti(ptr, -2, n + 1); } return(1); }
public static int Loader(IntPtr ptr) { string fileName = LuaNativeMethods.lua_tostring(ptr, 1); byte[] bytes = LoadFile(fileName); if (bytes != null) { if (LuaNativeMethods.luaL_loadbuffer(ptr, bytes, bytes.Length, "@" + fileName) == 0) { LuaObject.PushValue(ptr, true); LuaNativeMethods.lua_insert(ptr, -2); return(2); } else { string errstr = LuaNativeMethods.lua_tostring(ptr, -1); return(LuaObject.Error(ptr, errstr)); } } LuaObject.PushValue(ptr, true); LuaNativeMethods.lua_pushnil(ptr); return(2); }
public int AllocID(IntPtr ptr, object o) { int index = -1; if (o == null) { LuaNativeMethods.lua_pushnil(ptr); return(index); } bool gco = IsGcObject(o); bool found = gco && objMap.TryGetValue(o, out index); if (found) { if (LuaNativeMethods.luaS_getcacheud(ptr, index, cacheRef) == 1) { return(-1); } } index = Add(o); return(index); }