Program.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. string source = "";
  17. try
  18. {
  19. source = File.ReadAllText(GetAbsolutePath("test.lua"));
  20. Console.WriteLine("Source Code " + new string('-', 50));
  21. Console.WriteLine(source);
  22. var closure = state.Load(source, "@test.lua");
  23. DebugChunk(closure.Proto, 0);
  24. Console.WriteLine("Output " + new string('-', 50));
  25. var count = await state.MainThread.RunAsync(closure);
  26. Console.WriteLine("Result " + new string('-', 50));
  27. using var results = state.MainThread.ReadReturnValues(count);
  28. for (int i = 0; i < count; i++)
  29. {
  30. Console.WriteLine(results[i]);
  31. }
  32. Console.WriteLine("End " + new string('-', 50));
  33. }
  34. catch (Exception ex)
  35. {
  36. if (ex is LuaCompileException luaCompileException)
  37. {
  38. var linOffset = luaCompileException.OffSet - luaCompileException.Position.Column + 1;
  39. var length = 0;
  40. foreach (var c in source.AsSpan(linOffset))
  41. {
  42. if (c is '\n' or '\r')
  43. {
  44. break;
  45. }
  46. length++;
  47. }
  48. Console.WriteLine("CompileError " + new string('-', 50));
  49. Console.WriteLine(luaCompileException.ChunkName + ":"+luaCompileException.Position.Line + ":" + luaCompileException.Position.Column);
  50. var line = source.Substring(linOffset, length);
  51. var lineNumString = luaCompileException.Position.Line.ToString();
  52. Console.WriteLine(new string(' ', lineNumString.Length) + " |");
  53. Console.WriteLine(lineNumString + " | " + line);
  54. Console.WriteLine(new string(' ', lineNumString.Length) + " | " +
  55. new string(' ', luaCompileException.Position.Column - 1) +
  56. "^ " + luaCompileException.MainMessage);
  57. Console.WriteLine(new string('-', 55));
  58. }
  59. Console.WriteLine(ex);
  60. if (ex is LuaRuntimeException { InnerException: not null } luaEx)
  61. {
  62. Console.WriteLine("Inner Exception " + new string('-', 50));
  63. Console.WriteLine(luaEx.InnerException);
  64. }
  65. }
  66. static string GetAbsolutePath(string relativePath, [CallerFilePath] string callerFilePath = "")
  67. {
  68. return Path.Combine(Path.GetDirectoryName(callerFilePath)!, relativePath);
  69. }
  70. static void DebugChunk(Prototype chunk, int id)
  71. {
  72. Console.WriteLine($"Chunk[{id}]" + new string('=', 50));
  73. Console.WriteLine($"Parameters:{chunk.ParameterCount}");
  74. Console.WriteLine("Code " + new string('-', 50));
  75. var index = 0;
  76. foreach (var inst in chunk.Code)
  77. {
  78. Console.WriteLine($"[{index}]\t{chunk.LineInfo[index]}\t\t{inst}");
  79. index++;
  80. }
  81. Console.WriteLine("LocalVariables " + new string('-', 50));
  82. index = 0;
  83. foreach (var local in chunk.LocalVariables)
  84. {
  85. Console.WriteLine($"[{index}]\t{local.Name}\t{local.StartPc}\t{local.EndPc}");
  86. index++;
  87. }
  88. Console.WriteLine("Constants " + new string('-', 50));
  89. index = 0;
  90. foreach (var constant in chunk.Constants.ToArray())
  91. {
  92. Console.WriteLine($"[{index}]\t{Regex.Escape(constant.ToString())}");
  93. index++;
  94. }
  95. Console.WriteLine("UpValues " + new string('-', 50));
  96. index = 0;
  97. foreach (var upValue in chunk.UpValues.ToArray())
  98. {
  99. Console.WriteLine($"[{index}]\t{upValue.Name}\t{(upValue.IsLocal ? 1 : 0)}\t{upValue.Index}");
  100. index++;
  101. }
  102. Console.WriteLine();
  103. var nestedChunkId = 0;
  104. foreach (var localChunk in chunk.ChildPrototypes)
  105. {
  106. DebugChunk(localChunk, nestedChunkId);
  107. nestedChunkId++;
  108. }
  109. }