Program.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // See https://aka.ms/new-console-template for more information
  2. using System.Reflection;
  3. using System.Runtime.CompilerServices;
  4. using JitInspect;
  5. using Lua;
  6. using Lua.Runtime;
  7. using Lua.Standard;
  8. // dotnet run --configuration Release /p:DefineConstants="CASE_MARKER"
  9. // to activate the CASE_MARKER
  10. // JitInspect can be run in Windows and Linux (MacOS is not supported yet)
  11. var luaState = LuaState.Create();
  12. luaState.OpenStandardLibraries();
  13. {
  14. await luaState.DoFileAsync((GetAbsolutePath("db.lua")));
  15. }
  16. var closure = luaState.Load(File.ReadAllBytes(GetAbsolutePath("test.lua")),"test.lua");
  17. for (int i = 0; i < 1000; i++)
  18. {
  19. await luaState.MainThread.RunAsync(closure);
  20. luaState.MainThread.Stack.Clear();
  21. }
  22. var savePath = GetAbsolutePath("history");
  23. var thisDir = GetThisDirectoryName();
  24. var newJIitPath = Path.Join(thisDir, $"jit_{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.txt");
  25. var lastJitPaths = Directory.GetFiles(thisDir).Where(x=>x.Contains("jit_"));
  26. if (!Directory.Exists(savePath))
  27. {
  28. Directory.CreateDirectory(savePath);
  29. }
  30. if (lastJitPaths.Any())
  31. {
  32. Console.WriteLine("Last:" + File.ReadAllLines(lastJitPaths.First())[^1]);
  33. foreach (var jitPath in lastJitPaths)
  34. {
  35. var last = jitPath;
  36. var dest = Path.Join(savePath, Path.GetFileName(jitPath));
  37. File.Move(last, dest);
  38. }
  39. }
  40. var method = typeof(LuaVirtualMachine).GetMethod("MoveNext", BindingFlags.Static | BindingFlags.NonPublic)!;
  41. using var disassembler = JitDisassembler.Create();
  42. var nextJitText = disassembler.Disassemble(method);
  43. File.WriteAllText(newJIitPath, nextJitText);
  44. Console.WriteLine("New:" + nextJitText.Split("\n")[^1]);
  45. static string GetThisDirectoryName([CallerFilePath] string callerFilePath = "")
  46. {
  47. return Path.GetDirectoryName(callerFilePath)!;
  48. }
  49. static string GetAbsolutePath(string relativePath, [CallerFilePath] string callerFilePath = "")
  50. {
  51. return Path.Join(Path.GetDirectoryName(callerFilePath)!, relativePath);
  52. }