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);
        }
Beispiel #2
0
        public static int MakeArray(IntPtr ptr)
        {
            try
            {
                Type t;
                CheckType(ptr, 1, out t);
                LuaNativeMethods.luaL_checktype(ptr, 2, LuaTypes.TYPE_TABLE);
                int   n     = LuaNativeMethods.lua_rawlen(ptr, 2);
                Array array = Array.CreateInstance(t, n);
                for (int k = 0; k < n; k++)
                {
                    LuaNativeMethods.lua_rawgeti(ptr, 2, k + 1);
                    object obj = LuaObject.CheckVar(ptr, -1);
                    array.SetValue(LuaObject.ChangeType(obj, t), k);
                    LuaNativeMethods.lua_pop(ptr, 1);
                }

                LuaObject.PushValue(ptr, true);
                LuaObject.PushValue(ptr, array);
                return(2);
            }
            catch (Exception e)
            {
                return(Error(ptr, e));
            }
        }
        public static bool CheckArray(IntPtr ptr, int p, out char[] pars)
        {
            LuaNativeMethods.luaL_checktype(ptr, p, LuaTypes.TYPE_STRING);
            string s;

            CheckType(ptr, p, out s);
            pars = s.ToCharArray();
            return(true);
        }
        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);
        }
        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);
        }
Beispiel #6
0
        public static int ProtectedCall(IntPtr ptr)
        {
            int status;

            if (LuaNativeMethods.lua_type(ptr, 1) != LuaTypes.TYPE_FUNCTION)
            {
                return(LuaObject.Error(ptr, "arg 1 expect function"));
            }

            LuaNativeMethods.luaL_checktype(ptr, 1, LuaTypes.TYPE_FUNCTION);
            status = LuaNativeMethods.lua_pcall(ptr, LuaNativeMethods.lua_gettop(ptr) - 1, LuaNativeMethods.LUAMultRet, 0);
            LuaNativeMethods.lua_pushboolean(ptr, status == 0);
            LuaNativeMethods.lua_insert(ptr, 1);
            return(LuaNativeMethods.lua_gettop(ptr));  /* return status + all results */
        }
Beispiel #7
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));
            }
        }
 public static bool CheckType(IntPtr ptr, int p, out bool v)
 {
     LuaNativeMethods.luaL_checktype(ptr, p, LuaTypes.TYPE_BOOLEAN);
     v = LuaNativeMethods.lua_toboolean(ptr, p);
     return(true);
 }