예제 #1
0
        public static string[] DumpStack(lua_StatePtr L)
        {
            int           Top   = Lua.lua_gettop(L);
            List <string> Stack = new List <string>();

            for (int i = 1; i <= Top; i++)
            {
                int t = Lua.lua_type(L, i);

                switch (t)
                {
                case Lua.LUA_TSTRING:
                    Stack.Add(string.Format("'{0}'", Lua.lua_tostring(L, i)));
                    break;

                case Lua.LUA_TBOOLEAN:
                    Stack.Add(Lua.lua_toboolean(L, i) ? "true" : "false");
                    break;

                case Lua.LUA_TNUMBER:
                    Stack.Add(Lua.lua_tonumber(L, i).ToString());
                    break;

                default:
                    Stack.Add(Lua.lua_typename(L, t));
                    break;
                }
            }

            return(Stack.ToArray());
        }
예제 #2
0
        public static void OpenLib(lua_StatePtr L, Type StaticType, string LibName = null, int NUp = 0, bool SkipInvalid = true)
        {
            if (StaticType.IsClass && StaticType.IsAbstract && StaticType.IsSealed)
            {
                List <luaL_Reg> Regs    = new List <luaL_Reg>();
                MethodInfo[]    Methods = StaticType.GetMethods(BindingFlags.Static | BindingFlags.Public);

                if (Methods.Length == 0)
                {
                    return;
                }

                for (int i = 0; i < Methods.Length; i++)
                {
                    try {
                        Regs.Add(new luaL_Reg(Methods[i].Name, Wrap(Methods[i])));
                    } catch (Exception) {
                        continue;
                    }
                }

                Regs.Add(luaL_Reg.NULL);

                if (LibName == null)
                {
                    LibName = StaticType.Name;
                }

                Lua.luaL_openlib(L, LibName, Regs.ToArray(), NUp);
            }
            else
            {
                throw new Exception("Cannot register non static class as library");
            }
        }
예제 #3
0
        public static object Get(lua_StatePtr L, int N, Type T)
        {
            N = GetActualStackIndex(L, N);

            if (LuaToNetMarshals.ContainsKey(T))
            {
                return(LuaToNetMarshals[T](L, N));
            }
            else
            {
                foreach (var KV in LuaToNetMarshals)
                {
                    if (KV.Key.IsAssignableFrom(T))
                    {
                        try {
                            return(KV.Value(L, N));
                        } catch (NotImplementedException) {
                            continue;
                        }
                    }
                }
            }

            throw new Exception("Unsupported Lua marshal type " + T);
        }
예제 #4
0
파일: Lua.cs 프로젝트: yarligayan/libTech
        internal static void Init()
        {
            L = LL.luaL_newstate();
            LL.luaL_openlibs(L);

            DefaultEnvironment = GetEnvironment();
            GUIEnvironment     = CreateNewEnvironment(DefaultEnvironment);
        }
예제 #5
0
        public static int GetActualStackIndex(lua_StatePtr L, int Idx)
        {
            if (Idx < 0 && Idx > Lua.LUA_REGISTRYINDEX)
            {
                Idx = Lua.lua_gettop(L) + Idx + 1;
            }

            return(Idx);
        }
예제 #6
0
        public static void PrintStack(lua_StatePtr L)
        {
            string[] Stack = DumpStack(L);

            for (int i = 0; i < Stack.Length; i++)
            {
                Console.WriteLine("{0} - {1}", i + 1, Stack[i]);
            }
        }
예제 #7
0
        public LuaReference(lua_StatePtr L, int Idx = -1)
        {
            this.L = L;

            if (Idx != -1)
            {
                Lua.lua_pushvalue(L, Idx);
            }

            Reference = Lua.luaL_ref(L, Lua.LUA_REGISTRYINDEX);
        }
예제 #8
0
        public static int Push(lua_StatePtr L, object Ret)
        {
            Type T = typeof(object);

            if (Ret != null)
            {
                T = Ret.GetType();
            }

            if (T.IsArray)
            {
                Array Array = (Array)Ret;
                int   i     = 1;

                Lua.lua_createtable(L, Array.Length, 0);

                foreach (var Obj in Array)
                {
                    Push(L, Obj);
                    Lua.lua_rawseti(L, -2, i++);
                }

                return(1);
            }
            else if (NetToLuaMarshals.ContainsKey(T))
            {
                return(NetToLuaMarshals[T](L, Ret));
            }
            else
            {
                foreach (var KV in NetToLuaMarshals)
                {
                    if (KV.Key.IsAssignableFrom(T))
                    {
                        try {
                            return(KV.Value(L, Ret));
                        } catch (NotImplementedException) {
                            continue;
                        }
                    }
                }
            }

            throw new Exception("Unsupported Lua marshal type " + T);
        }
예제 #9
0
        static void Main(string[] args)
        {
            Console.Title = "Test";
            Console.WriteLine("Running {0} bit {1}", IntPtr.Size * 8, Lua.VERSION);
            lua_StatePtr L = Lua.lua_newstate((UD, Ptr, OSize, NSize) => {
                if ((int)NSize == 0)
                {
                    Marshal.FreeHGlobal(Ptr);
                    return(IntPtr.Zero);
                }
                if (Ptr == IntPtr.Zero)
                {
                    return(Marshal.AllocHGlobal(NSize));
                }
                return(Marshal.ReAllocHGlobal(Ptr, NSize));
            }, IntPtr.Zero);

            Lua.luaL_openlibs(L);

            Lua.lua_register(L, "test", Test);

            /*if (Lua.VERSION == LuaVersion.LuaJIT) { // LuaJIT print doesn't work :V
             *      Advanced.SetGlobal(L, "write", new Action<string>(Console.Write));
             *      Lua.luaL_dostring(L, "local _write = write write = nil function print(...) for _,v in pairs({...}) do _write(tostring(v)) _write('\\t') end _write('\\n') end");
             * }
             * Lua.luaL_dostring(L, "function printt(t) for k,v in pairs(t) do print(k, ' - ', v) end end");
             */

            string Str;

            while ((Str = ReadLine("> ")).Length > 0)
            {
                try {
                    if (Lua.luaL_dostring(L, Str) != 0)
                    {
                        Console.WriteLine(Lua.lua_tostring(L, -1));
                    }
                } catch (Exception E) {
                    Console.WriteLine(E.Message);
                }
            }

            Console.WriteLine("Done!");
            Console.ReadLine();
        }
예제 #10
0
 public static extern int lua_gc(lua_StatePtr L, int what, int data);
예제 #11
0
		public static void OpenLib(lua_StatePtr L, Type StaticType, int NUp = 0, bool SkipInvalid = true) {
			if (StaticType.IsClass && StaticType.IsAbstract && StaticType.IsSealed) {
				List<luaL_Reg> Regs = new List<luaL_Reg>();
				MethodInfo[] Methods = StaticType.GetMethods(BindingFlags.Static | BindingFlags.Public);
				if (Methods.Length == 0)
					return;
				for (int i = 0; i < Methods.Length; i++) {
					try {
						Regs.Add(new luaL_Reg(Methods[i].Name, Wrap(Methods[i])));
					} catch (Exception) {
						continue;
					}
				}
				Regs.Add(luaL_Reg.NULL);
				Lua.luaL_openlib(L, StaticType.Name, Regs.ToArray(), NUp);
			} else
				throw new Exception("Cannot register non static class as library");
		}
예제 #12
0
		static object Pop(lua_StatePtr L, int N, Type T) {
			if (LuaToNetMarshals.ContainsKey(T))
				return LuaToNetMarshals[T](L, N);
			else
				foreach (var KV in LuaToNetMarshals)
					if (KV.Key.IsAssignableFrom(T))
						try {
							return KV.Value(L, N);
						} catch (NotImplementedException) {
							continue;
						}

			throw new Exception("Unsupported Lua marshal type " + T);
		}
예제 #13
0
 public static extern void lua_close(lua_StatePtr L);
예제 #14
0
 public static extern void lua_setallocf(lua_StatePtr L, lua_Alloc f, IntPtr ud);
예제 #15
0
 public static extern void lua_concat(lua_StatePtr L, int n);
예제 #16
0
 public static extern int lua_pcall(lua_StatePtr L, int nargs, int nresults, int errfunc);
예제 #17
0
 public static extern void lua_call(lua_StatePtr L, int nargs, int nresults);
예제 #18
0
 public static extern int lua_setfenv(lua_StatePtr L, int idx);
예제 #19
0
 public static extern int lua_setmetatable(lua_StatePtr L, int objindex);
예제 #20
0
 public static extern void lua_rawseti(lua_StatePtr L, int idx, int n);
예제 #21
0
 public static extern void lua_rawset(lua_StatePtr L, int idx);
예제 #22
0
 public static extern void lua_setfield(lua_StatePtr L, int idx, string k);
예제 #23
0
 public static extern int lua_error(lua_StatePtr L);
예제 #24
0
 public static extern int lua_next(lua_StatePtr L, int idx);
예제 #25
0
 public static extern int lua_cpcall(lua_StatePtr L, lua_CFunction func, IntPtr ud);
예제 #26
0
 public static extern lua_Alloc lua_getallocf(lua_StatePtr L, IntPtr ud);
예제 #27
0
		static int Test(lua_StatePtr S) {
			return Lua.LUA_MULTRET;
		}
예제 #28
0
 public static extern int luaJIT_setmode(lua_StatePtr L, int idx, int mode);
예제 #29
0
 public static extern int lua_dump(lua_StatePtr L, lua_Writer writer, IntPtr data);
예제 #30
0
		static int Push(lua_StatePtr L, object Ret) {
			Type T = typeof(object);
			if (Ret != null)
				T = Ret.GetType();

			if (NetToLuaMarshals.ContainsKey(T))
				return NetToLuaMarshals[T](L, Ret);
			else
				foreach (var KV in NetToLuaMarshals)
					if (KV.Key.IsAssignableFrom(T))
						try {
							return KV.Value(L, Ret);
						} catch (NotImplementedException) {
							continue;
						}

			throw new Exception("Unsupported Lua marshal type " + T);
		}
예제 #31
0
 public static extern int lua_yield(lua_StatePtr L, int nresults);
예제 #32
0
		public static void SetGlobal(lua_StatePtr L, string Name, object Obj) {
			Push(L, Obj);
			Lua.lua_setglobal(L, Name);
		}
예제 #33
0
 public static extern int lua_resume(lua_StatePtr L, int narg);
예제 #34
0
 public static extern int lua_load(lua_StatePtr L, lua_Reader reader, IntPtr dt,
                                   string chunkname);
예제 #35
0
 public static extern int lua_status(lua_StatePtr L);