示例#1
0
 public static int Error(IntPtr ptr, string err, params object[] args)
 {
     err = string.Format(err, args);
     LuaNativeMethods.lua_pushboolean(ptr, false);
     LuaNativeMethods.lua_pushstring(ptr, err);
     return(2);
 }
示例#2
0
        public static void CompleteTypeMeta(IntPtr ptr, LuaCSFunction con, Type self)
        {
            LuaNativeMethods.lua_pushstring(ptr, ObjectCache.GetAQName(self));
            LuaNativeMethods.lua_setfield(ptr, -3, "__fullname");

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

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

            if (con == null)
            {
                con = NoConstructor;
            }

            PushValue(ptr, con);
            LuaNativeMethods.lua_setfield(ptr, -2, "__call");

            LuaNativeMethods.lua_pushcfunction(ptr, TypeToString);
            LuaNativeMethods.lua_setfield(ptr, -2, "__tostring");

            LuaNativeMethods.lua_pushvalue(ptr, -1);
            LuaNativeMethods.lua_setmetatable(ptr, -3);

            LuaNativeMethods.lua_setfield(ptr, LuaIndexes.LUARegistryIndex, self.FullName);
        }
示例#3
0
 public ObjectCache(IntPtr ptr)
 {
     LuaNativeMethods.lua_newtable(ptr);
     LuaNativeMethods.lua_newtable(ptr);
     LuaNativeMethods.lua_pushstring(ptr, "v");
     LuaNativeMethods.lua_setfield(ptr, -2, "__mode");
     LuaNativeMethods.lua_setmetatable(ptr, -2);
     cacheRef = LuaNativeMethods.luaL_ref(ptr, LuaIndexes.LUARegistryIndex);
 }
示例#4
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);
        }
示例#5
0
        public void SetObject(string[] remainingPath, object o)
        {
            int top = LuaNativeMethods.lua_gettop(statePointer);

            for (int i = 0; i < remainingPath.Length - 1; i++)
            {
                LuaNativeMethods.lua_pushstring(statePointer, remainingPath[i]);
                LuaNativeMethods.lua_gettable(statePointer, -2);
            }

            LuaNativeMethods.lua_pushstring(statePointer, remainingPath[remainingPath.Length - 1]);
            LuaObject.PushVar(statePointer, o);
            LuaNativeMethods.lua_settable(statePointer, -3);
            LuaNativeMethods.lua_settop(statePointer, top);
        }
示例#6
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);
        }
示例#7
0
        public object GetObject(string[] remainingPath)
        {
            object returnValue = null;

            for (int i = 0; i < remainingPath.Length; i++)
            {
                LuaNativeMethods.lua_pushstring(statePointer, remainingPath[i]);
                LuaNativeMethods.lua_gettable(statePointer, -2);
                returnValue = this.GetObject(statePointer, -1);
                LuaNativeMethods.lua_remove(statePointer, -2);
                if (returnValue == null)
                {
                    break;
                }
            }

            return(returnValue);
        }
示例#8
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));
        }
示例#9
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
        }
示例#10
0
        public static int ExtractFunction(IntPtr ptr, int p)
        {
            int      op = 0;
            LuaTypes t  = LuaNativeMethods.lua_type(ptr, p);

            switch (t)
            {
            case LuaTypes.TYPE_NIL:
            case LuaTypes.TYPE_USERDATA:
                op = 0;
                break;

            case LuaTypes.TYPE_TABLE:

                LuaNativeMethods.lua_rawgeti(ptr, p, 1);
                LuaNativeMethods.lua_pushstring(ptr, "+=");
                if (LuaNativeMethods.lua_rawequal(ptr, -1, -2) == 1)
                {
                    op = 1;
                }
                else
                {
                    op = 2;
                }

                LuaNativeMethods.lua_pop(ptr, 2);
                LuaNativeMethods.lua_rawgeti(ptr, p, 2);
                break;

            case LuaTypes.TYPE_FUNCTION:
                LuaNativeMethods.lua_pushvalue(ptr, p);
                break;

            default:
                throw new Exception("expect valid Delegate");
            }

            return(op);
        }
示例#11
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);
            }
        }
示例#12
0
        public static int SetData(IntPtr ptr)
        {
            try
            {
                int argc = LuaNativeMethods.lua_gettop(ptr);
                if (argc == 2)
                {
                    ByteArray self = (ByteArray)LuaObject.CheckSelf(ptr);
                    byte[]    a1;
                    LuaObject.CheckArray(ptr, 2, out a1);
                    self.SetData(a1);
                    LuaObject.PushValue(ptr, true);
                    return(1);
                }
                else if (argc == 4)
                {
                    ByteArray self = (ByteArray)LuaObject.CheckSelf(ptr);
                    byte[]    a1;
                    LuaObject.CheckArray(ptr, 2, out a1);
                    int a2;
                    CheckType(ptr, 3, out a2);
                    int a3;
                    CheckType(ptr, 4, out a3);
                    self.SetData(a1, a2, a3);
                    LuaObject.PushValue(ptr, true);
                    return(1);
                }

                LuaObject.PushValue(ptr, false);
                LuaNativeMethods.lua_pushstring(ptr, "No matched override function SetData to call");
                return(2);
            }
            catch (Exception e)
            {
                return(Error(ptr, e));
            }
        }
示例#13
0
 public static void PushValue(IntPtr ptr, string s)
 {
     LuaNativeMethods.lua_pushstring(ptr, s);
 }
示例#14
0
        public void SetupPushVar()
        {
            typePushMap[typeof(float)] = (IntPtr ptr, object o) =>
            {
                LuaNativeMethods.lua_pushnumber(ptr, (float)o);
            };

            typePushMap[typeof(double)] = (IntPtr ptr, object o) =>
            {
                LuaNativeMethods.lua_pushnumber(ptr, (double)o);
            };

            typePushMap[typeof(int)] =
                (IntPtr ptr, object o) =>
            {
                LuaNativeMethods.lua_pushinteger(ptr, (int)o);
            };

            typePushMap[typeof(uint)] =
                (IntPtr ptr, object o) =>
            {
                LuaNativeMethods.lua_pushnumber(ptr, Convert.ToUInt32(o));
            };

            typePushMap[typeof(short)] =
                (IntPtr ptr, object o) =>
            {
                LuaNativeMethods.lua_pushinteger(ptr, (short)o);
            };

            typePushMap[typeof(ushort)] =
                (IntPtr ptr, object o) =>
            {
                LuaNativeMethods.lua_pushinteger(ptr, (ushort)o);
            };

            typePushMap[typeof(sbyte)] =
                (IntPtr ptr, object o) =>
            {
                LuaNativeMethods.lua_pushinteger(ptr, (sbyte)o);
            };

            typePushMap[typeof(byte)] =
                (IntPtr ptr, object o) =>
            {
                LuaNativeMethods.lua_pushinteger(ptr, (byte)o);
            };

            typePushMap[typeof(long)]      =
                typePushMap[typeof(ulong)] =
                    (IntPtr ptr, object o) =>
            {
#if LUA_5_3
                LuaNativeMethods.lua_pushinteger(ptr, (long)o);
#else
                LuaNativeMethods.lua_pushnumber(ptr, System.Convert.ToDouble(o));
#endif
            };

            typePushMap[typeof(string)] = (IntPtr ptr, object o) =>
            {
                LuaNativeMethods.lua_pushstring(ptr, (string)o);
            };

            typePushMap[typeof(bool)] = (IntPtr ptr, object o) =>
            {
                LuaNativeMethods.lua_pushboolean(ptr, (bool)o);
            };

            typePushMap[typeof(LuaTable)]          =
                typePushMap[typeof(LuaFunction)]   =
                    typePushMap[typeof(LuaThread)] =
                        (IntPtr ptr, object o) =>
            {
                ((LuaVar)o).Push(ptr);
            };

            typePushMap[typeof(LuaCSFunction)] = (IntPtr ptr, object o) =>
            {
                LuaObject.PushValue(ptr, (LuaCSFunction)o);
            };
        }
示例#15
0
 public static int Error(IntPtr ptr, Exception e)
 {
     LuaNativeMethods.lua_pushboolean(ptr, false);
     LuaNativeMethods.lua_pushstring(ptr, e.ToString());
     return(2);
 }
示例#16
0
 public static int Error(IntPtr ptr, string err)
 {
     LuaNativeMethods.lua_pushboolean(ptr, false);
     LuaNativeMethods.lua_pushstring(ptr, err);
     return(2);
 }
示例#17
0
 public static void lua_setglobal(IntPtr luaState, string name)
 {
     LuaNativeMethods.lua_pushstring(luaState, name);
     LuaNativeMethods.lua_insert(luaState, -2);
     LuaNativeMethods.lua_settable(luaState, LuaIndexes.LUAGlobalIndex);
 }
示例#18
0
 public static int TypeToString(IntPtr ptr)
 {
     LuaNativeMethods.lua_pushstring(ptr, "__fullname");
     LuaNativeMethods.lua_rawget(ptr, -2);
     return(1);
 }