Program.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Runtime.CompilerServices;
  2. using Lua.Runtime;
  3. using Lua;
  4. using Lua.Standard;
  5. using System.Text.RegularExpressions;
  6. using System;
  7. using System.IO;
  8. var state = LuaState.Create();
  9. state.OpenStandardLibraries();
  10. state.Environment["escape"] = new LuaFunction("escape",
  11. (c, _) =>
  12. {
  13. var arg = c.HasArgument(0) ? c.GetArgument<string>(0) : "";
  14. return new(c.Return(Regex.Escape(arg)));
  15. });
  16. try
  17. {
  18. var source = File.ReadAllText(GetAbsolutePath("test.lua"));
  19. Console.WriteLine("Source Code " + new string('-', 50));
  20. Console.WriteLine(source);
  21. var closure = state.Load(source, "test.lua");
  22. DebugChunk(closure.Proto, 0);
  23. Console.WriteLine("Output " + new string('-', 50));
  24. var count = await state.MainThread.RunAsync(closure);
  25. Console.WriteLine("Result " + new string('-', 50));
  26. using var results = state.MainThread.ReadReturnValues(count);
  27. for (int i = 0; i < count; i++)
  28. {
  29. Console.WriteLine(results[i]);
  30. }
  31. Console.WriteLine("End " + new string('-', 50));
  32. }
  33. catch (Exception ex)
  34. {
  35. Console.WriteLine(ex);
  36. if (ex is LuaRuntimeException { InnerException: not null } luaEx)
  37. {
  38. Console.WriteLine("Inner Exception " + new string('-', 50));
  39. Console.WriteLine(luaEx.InnerException);
  40. }
  41. }
  42. static string GetAbsolutePath(string relativePath, [CallerFilePath] string callerFilePath = "")
  43. {
  44. return Path.Combine(Path.GetDirectoryName(callerFilePath)!, relativePath);
  45. }
  46. static void DebugChunk(Prototype chunk, int id)
  47. {
  48. Console.WriteLine($"Chunk[{id}]" + new string('=', 50));
  49. Console.WriteLine($"Parameters:{chunk.ParameterCount}");
  50. Console.WriteLine("Code " + new string('-', 50));
  51. var index = 0;
  52. foreach (var inst in chunk.Code)
  53. {
  54. Console.WriteLine($"[{index}]\t{chunk.LineInfo[index]}\t\t{inst}");
  55. index++;
  56. }
  57. Console.WriteLine("LocalVariables " + new string('-', 50));
  58. index = 0;
  59. foreach (var local in chunk.LocalVariables)
  60. {
  61. Console.WriteLine($"[{index}]\t{local.Name}\t{local.StartPc}\t{local.EndPc}");
  62. index++;
  63. }
  64. Console.WriteLine("Constants " + new string('-', 50));
  65. index = 0;
  66. foreach (var constant in chunk.Constants.ToArray())
  67. {
  68. Console.WriteLine($"[{index}]\t{Regex.Escape(constant.ToString())}");
  69. index++;
  70. }
  71. Console.WriteLine("UpValues " + new string('-', 50));
  72. index = 0;
  73. foreach (var upValue in chunk.UpValues.ToArray())
  74. {
  75. Console.WriteLine($"[{index}]\t{upValue.Name}\t{(upValue.IsLocal ? 1 : 0)}\t{upValue.Index}");
  76. index++;
  77. }
  78. Console.WriteLine();
  79. var nestedChunkId = 0;
  80. foreach (var localChunk in chunk.ChildPrototypes)
  81. {
  82. DebugChunk(localChunk, nestedChunkId);
  83. nestedChunkId++;
  84. }
  85. }