Exemple #1
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
        }
Exemple #2
0
        public static int As(IntPtr ptr)
        {
            try
            {
                if (!LuaObject.IsTypeTable(ptr, 2))
                {
                    return(Error(ptr, "No matched type of param 2"));
                }

                string meta = LuaNativeMethods.lua_tostring(ptr, -1);
                LuaNativeMethods.luaL_getmetatable(ptr, meta);
                LuaNativeMethods.lua_setmetatable(ptr, 1);
                LuaObject.PushValue(ptr, true);
                LuaNativeMethods.lua_pushvalue(ptr, 1);
                return(2);
            }
            catch (Exception e)
            {
                return(Error(ptr, e));
            }
        }