コード例 #1
0
ファイル: ObjectCache.cs プロジェクト: BraedonWooding/slua
        public static ObjectCache Get(IntPtr ptr)
        {
            if (oldPtr == ptr)
            {
                return(Oldoc);
            }

            ObjectCache oc;

            if (multiState.TryGetValue(ptr, out oc))
            {
                oldPtr = ptr;
                Oldoc  = oc;
                return(oc);
            }

            LuaNativeMethods.lua_getglobal(ptr, "__main_state");
            if (LuaNativeMethods.lua_isnil(ptr, -1))
            {
                LuaNativeMethods.lua_pop(ptr, 1);
                return(null);
            }

            IntPtr nl = LuaNativeMethods.lua_touserdata(ptr, -1);

            LuaNativeMethods.lua_pop(ptr, 1);

            if (nl != ptr)
            {
                return(Get(nl));
            }

            return(null);
        }
コード例 #2
0
        public static bool CheckType(IntPtr ptr, int p, out LuaDelegate f)
        {
            LuaState state = LuaState.Get(ptr);

            p = LuaNativeMethods.lua_absindex(ptr, p);
            LuaNativeMethods.luaL_checktype(ptr, p, LuaTypes.TYPE_FUNCTION);

            LuaNativeMethods.lua_getglobal(ptr, DelgateTable);
            LuaNativeMethods.lua_pushvalue(ptr, p);
            LuaNativeMethods.lua_gettable(ptr, -2); // find function in __LuaDelegate table

            if (LuaNativeMethods.lua_isnil(ptr, -1))
            {                                     // not found
                LuaNativeMethods.lua_pop(ptr, 1); // pop nil
                f = NewDelegate(ptr, p);
            }
            else
            {
                int fref = LuaNativeMethods.lua_tointeger(ptr, -1);
                LuaNativeMethods.lua_pop(ptr, 1); // pop ref value;
                f = state.DelegateMap[fref];
                if (f == null)
                {
                    f = NewDelegate(ptr, p);
                }
            }

            LuaNativeMethods.lua_pop(ptr, 1); // pop DelgateTable
            return(true);
        }
コード例 #3
0
ファイル: LuaState.cs プロジェクト: BraedonWooding/slua
        public static LuaState Get(IntPtr ptr)
        {
            if (ptr == oldptr)
            {
                return(oldstate);
            }

            LuaState ls;

            if (statemap.TryGetValue(ptr, out ls))
            {
                oldptr   = ptr;
                oldstate = ls;
                return(ls);
            }

            LuaNativeMethods.lua_getglobal(ptr, "__main_state");
            if (LuaNativeMethods.lua_isnil(ptr, -1))
            {
                LuaNativeMethods.lua_pop(ptr, 1);
                return(null);
            }

            IntPtr nl = LuaNativeMethods.lua_touserdata(ptr, -1);

            LuaNativeMethods.lua_pop(ptr, 1);
            if (nl != ptr)
            {
                return(Get(nl));
            }

            return(null);
        }
コード例 #4
0
        public static int GetOpFunction(IntPtr ptr, string f, string tip)
        {
            int err = PushTry(ptr);

            CheckLuaObject(ptr, 1);

            while (!LuaNativeMethods.lua_isnil(ptr, -1))
            {
                LuaNativeMethods.lua_getfield(ptr, -1, f);
                if (!LuaNativeMethods.lua_isnil(ptr, -1))
                {
                    LuaNativeMethods.lua_remove(ptr, -2);
                    break;
                }

                LuaNativeMethods.lua_pop(ptr, 1);     // pop nil
                LuaNativeMethods.lua_getfield(ptr, -1, "__parent");
                LuaNativeMethods.lua_remove(ptr, -2); // pop base
            }

            if (LuaNativeMethods.lua_isnil(ptr, -1))
            {
                LuaNativeMethods.lua_pop(ptr, 1);
                throw new Exception(string.Format("No {0} operator", tip));
            }

            return(err);
        }
コード例 #5
0
 public static void CheckLuaObject(IntPtr ptr, int p)
 {
     LuaNativeMethods.lua_getmetatable(ptr, p);
     if (LuaNativeMethods.lua_isnil(ptr, -1))
     {
         LuaNativeMethods.lua_pop(ptr, 1);
         throw new Exception("expect luaobject as first argument");
     }
 }
コード例 #6
0
        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);
        }
コード例 #7
0
ファイル: LuaState.cs プロジェクト: BraedonWooding/slua
        public static int LoadFile(IntPtr ptr)
        {
            Loader(ptr);

            if (LuaNativeMethods.lua_isnil(ptr, -1))
            {
                string fileName = LuaNativeMethods.lua_tostring(ptr, 1);
                return(LuaObject.Error(ptr, "Can't find {0}", fileName));
            }

            return(2);
        }
コード例 #8
0
        public static bool CheckType(IntPtr ptr, int p, out LuaTable t)
        {
            if (LuaNativeMethods.lua_isnil(ptr, p))
            {
                t = null;
                return(true);
            }

            LuaNativeMethods.luaL_checktype(ptr, p, LuaTypes.TYPE_TABLE);
            LuaNativeMethods.lua_pushvalue(ptr, p);
            int fref = LuaNativeMethods.luaL_ref(ptr, LuaIndexes.LUARegistryIndex);

            t = new LuaTable(ptr, fref);
            return(true);
        }
コード例 #9
0
        public static bool CheckType(IntPtr ptr, int p, out LuaFunction f)
        {
            if (LuaNativeMethods.lua_isnil(ptr, p))
            {
                f = null;
                return(true);
            }

            LuaNativeMethods.luaL_checktype(ptr, p, LuaTypes.TYPE_FUNCTION);
            LuaNativeMethods.lua_pushvalue(ptr, p);
            int fref = LuaNativeMethods.luaL_ref(ptr, LuaIndexes.LUARegistryIndex);

            f = new LuaFunction(ptr, fref);
            return(true);
        }
コード例 #10
0
ファイル: LuaState.cs プロジェクト: BraedonWooding/slua
        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));
            }
        }
コード例 #11
0
        public static bool IsTypeTable(IntPtr ptr, int p)
        {
            if (LuaNativeMethods.lua_type(ptr, p) != LuaTypes.TYPE_TABLE)
            {
                return(false);
            }

            LuaNativeMethods.lua_pushstring(ptr, "__fullname");
            LuaNativeMethods.lua_rawget(ptr, p);
            if (LuaNativeMethods.lua_isnil(ptr, -1))
            {
                LuaNativeMethods.lua_pop(ptr, 1);
                return(false);
            }

            return(true);
        }
コード例 #12
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
        }
コード例 #13
0
        public static bool CheckNullable <T>(IntPtr ptr, int p, out T?v) where T : struct
        {
            if (LuaNativeMethods.lua_isnil(ptr, p))
            {
                v = null;
            }
            else
            {
                object o = CheckVar(ptr, p, typeof(T));
                if (o == null)
                {
                    v = null;
                }
                else
                {
                    v = new T?((T)o);
                }
            }

            return(true);
        }
コード例 #14
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);
            }
        }
コード例 #15
0
ファイル: LuaState.cs プロジェクト: BraedonWooding/slua
        public static int DoFile(IntPtr ptr)
        {
            int n = LuaNativeMethods.lua_gettop(ptr);

            Loader(ptr);
            if (!LuaNativeMethods.lua_toboolean(ptr, -2))
            {
                return(2);
            }
            else
            {
                if (LuaNativeMethods.lua_isnil(ptr, -1))
                {
                    string fileName = LuaNativeMethods.lua_tostring(ptr, 1);
                    return(LuaObject.Error(ptr, "Can't find {0}", fileName));
                }

                int k = LuaNativeMethods.lua_gettop(ptr);
                LuaNativeMethods.lua_call(ptr, 0, LuaNativeMethods.LUAMultRet);
                k = LuaNativeMethods.lua_gettop(ptr);
                return(k - n);
            }
        }