Program.cs 2.8 KB

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