示例#1
0
        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));
            }
        }
示例#2
0
            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);
            }
示例#3
0
        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);
        }
示例#4
0
        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);
            }
        }
示例#5
0
 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
 }
示例#6
0
        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);
        }
示例#7
0
 public static void PushValue(IntPtr ptr, LuaTable t)
 {
     if (t == null)
     {
         LuaNativeMethods.lua_pushnil(ptr);
     }
     else
     {
         t.Push(ptr);
     }
 }
示例#8
0
        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));
            }
        }
示例#9
0
        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);
        }
示例#10
0
        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);
        }
示例#11
0
        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);
        }