public static void TheMain() { Console.Title = "GSharp Test"; //Console.WriteLine("GShap Test"); L = Lua.NewState(); Lua.OpenLibs(L); LuaFunc AtPanicOld = null; AtPanicOld = Lua.AtPanic(L, (LL) => { AtPanicOld(LL); throw new Exception(); }); Lua.PushCFunction(L, (LL) => { int Top = Lua.GetTop(LL); StringBuilder SB = new StringBuilder(); for (int i = 1; i < Top + 1; i++) { SB.Append(Lua.ToString(LL, i)).Append("\t"); } Console.WriteLine(SB.ToString().Trim()); return(0); }); Lua.SetGlobal(L, "print"); ErrorCheck(Lua.LoadString(L, "function helloworld() print(\"Hello World!\") return helloworld end")); ErrorCheck(Lua.PCall(L, 0, 0, 0)); ErrorCheck(Lua.LoadString(L, "function printt(t) for k,v in pairs(t) do print(tostring(k) .. \" - \" .. tostring(v)) end end")); ErrorCheck(Lua.PCall(L, 0, 0, 0)); //try { Init(); /*} catch (Exception) { * throw; * }//*/ GSharp.Dynamic.Delegates.Dump(); while (true) { Console.Write(">> "); try { ErrorCheck(Lua.LoadString(L, Console.ReadLine())); ErrorCheck(Lua.PCall(L, 0, 0, 0)); } catch (Exception E) { Console.WriteLine("exception: {0}", E.Message); } } }
public void PanicError() // PANIC: unprotected error in call to Lua API (attempt to call a string value) { state = new Lua(); LuaFunction oldPanic = state.AtPanic(MyPanicFunc); stackDump(state); LuaStatus result = state.LoadString("1"); stackDump(state); try { state.Call(0, 0); Assert.Fail("Shoudln't go so far"); } catch (MyPanicException ex) { } stackDump(state); state.Close(); }
static void Run(string In, string Out, string ChunkName) { IntPtr L = Lua.NewState(); Lua.OpenLibs(L); Lua.AtPanic(L, (LL) => { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("\n\nPANIC!"); Console.ResetColor(); Console.WriteLine(Lua.ToString(LL, -1)); Console.ReadKey(); Environment.Exit(0); return(0); }); Lua.RegisterCFunction(L, "_G", "dumpBytecode", (LL) => { if (Dumping) { int Len = 0; char *Bytecode = (char *)Lua.ToLString(L, -1, new IntPtr(&Len)).ToPointer(); string BytecodeStr = new string((sbyte *)Bytecode, 0, Len, Encoding.ASCII); File.WriteAllText(Out, BytecodeStr); } return(0); }); Lua.SetTop(L, 0); Lua.GetGlobal(L, "dumpBytecode"); Lua.GetGlobal(L, "string"); Lua.PushString(L, "dump"); // Yes, it uses string.dump, and no, i'm not gonna implement proper dump function (lazy) Lua.GetTable(L, -2); ErrorCheck(L, Lua.LoadBuffer(L, In, ChunkName)); ErrorCheck(L, Lua.PCall(L, 1, 1, 0)); Lua.Replace(L, -2); ErrorCheck(L, Lua.PCall(L, 1, 0, 0)); Lua.Close(L); }
static void Main(string[] args) { IntPtr L = Lua.NewState(); Lua.OpenLibs(L); Lua.AtPanic(L, (LL) => { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("\n\nPANIC!"); Console.ResetColor(); Console.WriteLine(Lua.ToString(LL, -1)); Console.ReadKey(); Environment.Exit(0); return(0); }); GMod.Init(L); dynamic G = new LuaObject(L); Emulator Emul = new Emulator(); FileSystemWatcher CSWatch = new FileSystemWatcher("script", "*.cs"); CSWatch.Changed += (S, E) => { ExecFile(E.FullPath, L, G, Emul); Console.WriteLine(Prompt); }; CSWatch.IncludeSubdirectories = true; CSWatch.EnableRaisingEvents = true; string[] CSFiles = Directory.GetFiles("script", "*.cs"); foreach (var CSFile in CSFiles) { ExecFile(CSFile, L, G, Emul); } if (File.Exists("script/autorun.lua")) { FileSystemWatcher FSW = new FileSystemWatcher("script", "autorun.lua"); FSW.Changed += (S, E) => { Console.Clear(); Lua.GetGlobal(L, "dofile"); Lua.PushString(L, "script/autorun.lua"); Emul.ErrorCheck(L, Lua.PCall(L, 1, 0, 0)); Console.Write(Prompt); }; FSW.EnableRaisingEvents = true; Lua.GetGlobal(L, "dofile"); Lua.PushString(L, "script/autorun.lua"); Emul.ErrorCheck(L, Lua.PCall(L, 1, 0, 0)); } while (true) { Console.Write(Prompt); if (!Emul.ErrorCheck(L, Lua.LoadString(L, Console.ReadLine()))) { try { Emul.ErrorCheck(L, Lua.PCall(L, 0, 0, 0)); } catch (Exception E) { Console.WriteLine("\n[{0}]\n{1}", E.GetType().Name, E.Message); } } } }