public static bool CheckType(IntPtr ptr, int p, out Type t)
        {
            string   tname = null;
            LuaTypes lt    = LuaNativeMethods.lua_type(ptr, p);

            switch (lt)
            {
            case LuaTypes.TYPE_USERDATA:
                object o = CheckObj(ptr, p);
                if (o.GetType() != monoType)
                {
                    throw new Exception(string.Format("{0} expect Type, got {1}", p, o.GetType().Name));
                }

                t = (Type)o;
                return(true);

            case LuaTypes.TYPE_TABLE:
                LuaNativeMethods.lua_pushstring(ptr, "__type");
                LuaNativeMethods.lua_rawget(ptr, p);
                if (!LuaNativeMethods.lua_isnil(ptr, -1))
                {
                    t = (Type)CheckObj(ptr, -1);
                    LuaNativeMethods.lua_pop(ptr, 1);
                    return(true);
                }
                else
                {
                    LuaNativeMethods.lua_pushstring(ptr, "__fullname");
                    LuaNativeMethods.lua_rawget(ptr, p);
                    tname = LuaNativeMethods.lua_tostring(ptr, -1);
                    LuaNativeMethods.lua_pop(ptr, 2);
                }

                break;

            case LuaTypes.TYPE_STRING:
                CheckType(ptr, p, out tname);
                break;
            }

            if (tname == null)
            {
                throw new Exception("expect string or type table");
            }

            t = LuaObject.FindType(tname);
            if (t != null && lt == LuaTypes.TYPE_TABLE)
            {
                LuaNativeMethods.lua_pushstring(ptr, "__type");
                PushLightObject(ptr, t);
                LuaNativeMethods.lua_rawset(ptr, p);
            }

            return(t != null);
        }
Example #2
0
        private static void CompleteInstanceMeta(IntPtr ptr, Type self)
        {
            LuaNativeMethods.lua_pushstring(ptr, "__typename");
            LuaNativeMethods.lua_pushstring(ptr, self.Name);
            LuaNativeMethods.lua_rawset(ptr, -3);

            // for instance
            indexFunction.Push(ptr);
            LuaNativeMethods.lua_setfield(ptr, -2, "__index");

            newIndexFunction.Push(ptr);
            LuaNativeMethods.lua_setfield(ptr, -2, "__newindex");

            PushValue(ptr, luaAdd);
            LuaNativeMethods.lua_setfield(ptr, -2, "__add");
            PushValue(ptr, luaSub);
            LuaNativeMethods.lua_setfield(ptr, -2, "__sub");
            PushValue(ptr, luaMul);
            LuaNativeMethods.lua_setfield(ptr, -2, "__mul");
            PushValue(ptr, luaDiv);
            LuaNativeMethods.lua_setfield(ptr, -2, "__div");
            PushValue(ptr, luaUnm);
            LuaNativeMethods.lua_setfield(ptr, -2, "__unm");
            PushValue(ptr, luaEq);
            LuaNativeMethods.lua_setfield(ptr, -2, "__eq");
            PushValue(ptr, luaLe);
            LuaNativeMethods.lua_setfield(ptr, -2, "__le");
            PushValue(ptr, luaLt);
            LuaNativeMethods.lua_setfield(ptr, -2, "__lt");
            PushValue(ptr, luaToString);
            LuaNativeMethods.lua_setfield(ptr, -2, "__tostring");

            LuaNativeMethods.lua_pushcfunction(ptr, LuaGC);
            LuaNativeMethods.lua_setfield(ptr, -2, "__gc");

            if (self.IsValueType && IsImplByLua(self))
            {
                LuaNativeMethods.lua_pushvalue(ptr, -1);
                LuaNativeMethods.lua_setglobal(ptr, self.FullName + ".Instance");
            }

            LuaNativeMethods.lua_setfield(ptr, LuaIndexes.LUARegistryIndex, ObjectCache.GetAQName(self));
        }
Example #3
0
        public static void CreateTypeMetatable(IntPtr ptr, LuaCSFunction con, Type self, Type parent)
        {
            CheckMethodValid(con);

            // set parent
            bool parentSet = false;

            LuaNativeMethods.lua_pushstring(ptr, "__parent");
            while (parent != null && parent != typeof(object) && parent != typeof(ValueType))
            {
                LuaNativeMethods.luaL_getmetatable(ptr, ObjectCache.GetAQName(parent));
                // if parentType is not exported to lua
                if (LuaNativeMethods.lua_isnil(ptr, -1))
                {
                    LuaNativeMethods.lua_pop(ptr, 1);
                    parent = parent.BaseType;
                }
                else
                {
                    LuaNativeMethods.lua_rawset(ptr, -3);

                    LuaNativeMethods.lua_pushstring(ptr, "__parent");
                    LuaNativeMethods.luaL_getmetatable(ptr, parent.FullName);
                    LuaNativeMethods.lua_rawset(ptr, -4);

                    parentSet = true;
                    break;
                }
            }

            if (!parentSet)
            {
                LuaNativeMethods.luaL_getmetatable(ptr, "__luabaseobject");
                LuaNativeMethods.lua_rawset(ptr, -3);
            }

            CompleteInstanceMeta(ptr, self);
            CompleteTypeMeta(ptr, con, self);

            LuaNativeMethods.lua_pop(ptr, 1); // pop type Table
        }
Example #4
0
        public static void NewTypeTable(IntPtr ptr, string name)
        {
            string[] subt = name.Split('.');

            LuaNativeMethods.lua_pushglobaltable(ptr);

            foreach (string t in subt)
            {
                LuaNativeMethods.lua_pushstring(ptr, t);
                LuaNativeMethods.lua_rawget(ptr, -2);
                if (LuaNativeMethods.lua_isnil(ptr, -1))
                {
                    LuaNativeMethods.lua_pop(ptr, 1);
                    LuaNativeMethods.lua_createtable(ptr, 0, 0);
                    LuaNativeMethods.lua_pushstring(ptr, t);
                    LuaNativeMethods.lua_pushvalue(ptr, -2);
                    LuaNativeMethods.lua_rawset(ptr, -4);
                }

                LuaNativeMethods.lua_remove(ptr, -2);
            }
        }