Program.cs 4.8 KB

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