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()); }
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"); } }
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); }
internal static void Init() { L = LL.luaL_newstate(); LL.luaL_openlibs(L); DefaultEnvironment = GetEnvironment(); GUIEnvironment = CreateNewEnvironment(DefaultEnvironment); }
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); }
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]); } }
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); }
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); }
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(); }
public static extern int lua_gc(lua_StatePtr L, int what, int data);
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"); }
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); }
public static extern void lua_close(lua_StatePtr L);
public static extern void lua_setallocf(lua_StatePtr L, lua_Alloc f, IntPtr ud);
public static extern void lua_concat(lua_StatePtr L, int n);
public static extern int lua_pcall(lua_StatePtr L, int nargs, int nresults, int errfunc);
public static extern void lua_call(lua_StatePtr L, int nargs, int nresults);
public static extern int lua_setfenv(lua_StatePtr L, int idx);
public static extern int lua_setmetatable(lua_StatePtr L, int objindex);
public static extern void lua_rawseti(lua_StatePtr L, int idx, int n);
public static extern void lua_rawset(lua_StatePtr L, int idx);
public static extern void lua_setfield(lua_StatePtr L, int idx, string k);
public static extern int lua_error(lua_StatePtr L);
public static extern int lua_next(lua_StatePtr L, int idx);
public static extern int lua_cpcall(lua_StatePtr L, lua_CFunction func, IntPtr ud);
public static extern lua_Alloc lua_getallocf(lua_StatePtr L, IntPtr ud);
static int Test(lua_StatePtr S) { return Lua.LUA_MULTRET; }
public static extern int luaJIT_setmode(lua_StatePtr L, int idx, int mode);
public static extern int lua_dump(lua_StatePtr L, lua_Writer writer, IntPtr data);
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); }
public static extern int lua_yield(lua_StatePtr L, int nresults);
public static void SetGlobal(lua_StatePtr L, string Name, object Obj) { Push(L, Obj); Lua.lua_setglobal(L, Name); }
public static extern int lua_resume(lua_StatePtr L, int narg);
public static extern int lua_load(lua_StatePtr L, lua_Reader reader, IntPtr dt, string chunkname);
public static extern int lua_status(lua_StatePtr L);