Program.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. using System.Text;
  9. var state = LuaState.Create();
  10. var globalState = state.GlobalState;
  11. state.OpenStandardLibraries();
  12. globalState.Environment["escape"] = new LuaFunction("escape",
  13. (c, _) =>
  14. {
  15. var arg = c.HasArgument(0) ? c.GetArgument<string>(0) : "";
  16. return new(c.Return(Regex.Escape(arg)));
  17. });
  18. var source = "";
  19. try
  20. {
  21. source = File.ReadAllText(GetAbsolutePath("test.lua"));
  22. //Console.WriteLine("Source Code " + new string('-', 50));
  23. // Console.WriteLine(source);
  24. var closure = state.Load(source, "@test.lua");
  25. DebugChunk(closure.Proto, 0);
  26. Console.WriteLine("Output " + new string('-', 50));
  27. //Console.Read();
  28. var timer = new System.Diagnostics.Stopwatch();
  29. timer.Start();
  30. for (var i = 0; i < 1000; i++)
  31. {
  32. var count = await state.RunAsync(closure);
  33. state.Pop(count);
  34. if (i % 100 == 0)
  35. {
  36. Console.WriteLine($"Iteration {i} completed. Time elapsed: {timer.ElapsedMilliseconds} ms");
  37. Thread.Sleep(100);
  38. }
  39. }
  40. // Console.WriteLine("Result " + new string('-', 50));
  41. // using var results = state.RootAccess.ReadTopValues(count);
  42. // for (var i = 0; i < count; i++)
  43. // {
  44. // Console.WriteLine(results[i]);
  45. // }
  46. Console.WriteLine("End " + new string('-', 50));
  47. }
  48. catch (Exception ex)
  49. {
  50. if (ex is LuaCompileException luaCompileException)
  51. {
  52. Console.WriteLine("CompileError " + new string('-', 50));
  53. Console.WriteLine(RustLikeExceptionHook.OnCatch(source, luaCompileException));
  54. ;
  55. Console.WriteLine(new string('-', 55));
  56. }
  57. Console.WriteLine(ex);
  58. if (ex is LuaRuntimeException { InnerException: not null } luaEx)
  59. {
  60. Console.WriteLine("Inner Exception " + new string('-', 50));
  61. Console.WriteLine(luaEx.InnerException);
  62. }
  63. }
  64. static string GetAbsolutePath(string relativePath, [CallerFilePath] string callerFilePath = "")
  65. {
  66. return Path.Combine(Path.GetDirectoryName(callerFilePath)!, relativePath);
  67. }
  68. static void DebugChunk(Prototype chunk, int id)
  69. {
  70. Console.WriteLine($"Chunk[{id}]" + new string('=', 50));
  71. Console.WriteLine($"Parameters:{chunk.ParameterCount}");
  72. Console.WriteLine("Code " + new string('-', 50));
  73. var index = 0;
  74. foreach (var inst in chunk.Code)
  75. {
  76. Console.WriteLine($"[{index}]\t{chunk.LineInfo[index]}\t\t{inst}");
  77. index++;
  78. }
  79. Console.WriteLine("LocalVariables " + new string('-', 50));
  80. index = 0;
  81. foreach (var local in chunk.LocalVariables)
  82. {
  83. Console.WriteLine($"[{index}]\t{local.Name}\t{local.StartPc}\t{local.EndPc}");
  84. index++;
  85. }
  86. Console.WriteLine("Constants " + new string('-', 50));
  87. index = 0;
  88. foreach (var constant in chunk.Constants.ToArray())
  89. {
  90. Console.WriteLine($"[{index}]\t{Regex.Escape(constant.ToString())}");
  91. index++;
  92. }
  93. Console.WriteLine("UpValues " + new string('-', 50));
  94. index = 0;
  95. foreach (var upValue in chunk.UpValues.ToArray())
  96. {
  97. Console.WriteLine($"[{index}]\t{upValue.Name}\t{(upValue.IsLocal ? 1 : 0)}\t{upValue.Index}");
  98. index++;
  99. }
  100. Console.WriteLine();
  101. var nestedChunkId = 0;
  102. foreach (var localChunk in chunk.ChildPrototypes)
  103. {
  104. DebugChunk(localChunk, nestedChunkId);
  105. nestedChunkId++;
  106. }
  107. }
  108. public class LuaRustLikeException(string message, Exception? innerException) : Exception(message, innerException);
  109. class RustLikeExceptionHook //: ILuaCompileHook
  110. {
  111. public static string OnCatch(ReadOnlySpan<char> source, LuaCompileException exception)
  112. {
  113. var lineOffset = exception.OffSet - exception.Position.Column + 1;
  114. var length = 0;
  115. if (lineOffset < 0)
  116. {
  117. lineOffset = 0;
  118. }
  119. foreach (var c in source[lineOffset..])
  120. {
  121. if (c is '\n' or '\r')
  122. {
  123. break;
  124. }
  125. length++;
  126. }
  127. var builder = new StringBuilder();
  128. builder.AppendLine();
  129. builder.AppendLine("[error]: " + exception.MessageWithNearToken);
  130. builder.AppendLine("-->" + exception.ChunkName + ":" + exception.Position.Line + ":" + exception.Position.Column);
  131. var line = source.Slice(lineOffset, length).ToString();
  132. var lineNumString = exception.Position.Line.ToString();
  133. builder.AppendLine(new string(' ', lineNumString.Length) + " |");
  134. builder.AppendLine(lineNumString + " | " + line);
  135. builder.AppendLine(new string(' ', lineNumString.Length) + " | " +
  136. new string(' ', exception.Position.Column - 1) +
  137. "^ " + exception.MainMessage);
  138. return builder.ToString();
  139. }
  140. }