OpenLibsExtensions.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using Lua.Runtime;
  2. namespace Lua.Standard;
  3. public static class OpenLibsExtensions
  4. {
  5. public static void OpenBasicLibrary(this LuaState state)
  6. {
  7. var globalState = state.GlobalState;
  8. globalState.Environment["_G"] = globalState.Environment;
  9. globalState.Environment["_VERSION"] = "Lua 5.2";
  10. foreach (var func in BasicLibrary.Instance.Functions)
  11. {
  12. globalState.Environment[func.Name] = func;
  13. }
  14. }
  15. public static void OpenBitwiseLibrary(this LuaState state)
  16. {
  17. var globalState = state.GlobalState;
  18. LuaTable bit32 = new(0, BitwiseLibrary.Instance.Functions.Length);
  19. foreach (var func in BitwiseLibrary.Instance.Functions)
  20. {
  21. bit32[func.Name] = func.Func;
  22. }
  23. globalState.Environment["bit32"] = bit32;
  24. globalState.LoadedModules["bit32"] = bit32;
  25. }
  26. public static void OpenCoroutineLibrary(this LuaState state)
  27. {
  28. var globalState = state.GlobalState;
  29. LuaTable coroutine = new(0, CoroutineLibrary.Instance.Functions.Length);
  30. foreach (var func in CoroutineLibrary.Instance.Functions)
  31. {
  32. coroutine[func.Name] = func.Func;
  33. }
  34. globalState.Environment["coroutine"] = coroutine;
  35. }
  36. public static void OpenIOLibrary(this LuaState state)
  37. {
  38. var globalState = state.GlobalState;
  39. LuaTable io = new(0, IOLibrary.Instance.Functions.Length);
  40. foreach (var func in IOLibrary.Instance.Functions)
  41. {
  42. io[func.Name] = func.Func;
  43. }
  44. var registry = globalState.Registry;
  45. var standardIO = globalState.Platform.StandardIO;
  46. LuaValue stdin = new(new FileHandle(standardIO.Input));
  47. LuaValue stdout = new(new FileHandle(standardIO.Output));
  48. LuaValue stderr = new(new FileHandle(standardIO.Error));
  49. registry["_IO_input"] = stdin;
  50. registry["_IO_output"] = stdout;
  51. io["stdin"] = stdin;
  52. io["stdout"] = stdout;
  53. io["stderr"] = stderr;
  54. globalState.Environment["io"] = io;
  55. globalState.LoadedModules["io"] = io;
  56. }
  57. public static void OpenMathLibrary(this LuaState state)
  58. {
  59. var globalState = state.GlobalState;
  60. globalState.Environment[MathematicsLibrary.RandomInstanceKey] = new(new MathematicsLibrary.RandomUserData(new()));
  61. LuaTable math = new(0, MathematicsLibrary.Instance.Functions.Length);
  62. foreach (var func in MathematicsLibrary.Instance.Functions)
  63. {
  64. math[func.Name] = func.Func;
  65. }
  66. math["pi"] = Math.PI;
  67. math["huge"] = double.PositiveInfinity;
  68. globalState.Environment["math"] = math;
  69. globalState.LoadedModules["math"] = math;
  70. }
  71. public static void OpenModuleLibrary(this LuaState state)
  72. {
  73. var globalState = state.GlobalState;
  74. LuaTable package = new(0, 8);
  75. package["loaded"] = globalState.LoadedModules;
  76. package["preload"] = globalState.PreloadModules;
  77. var moduleLibrary = ModuleLibrary.Instance;
  78. LuaTable searchers = new();
  79. searchers[1] = new LuaFunction("preload", moduleLibrary.SearcherPreload);
  80. searchers[2] = new LuaFunction("searcher_Lua", moduleLibrary.SearcherLua);
  81. package["searchers"] = searchers;
  82. package["path"] = "?.lua";
  83. package["searchpath"] = moduleLibrary.SearchPathFunction;
  84. package["config"] = $"{Path.DirectorySeparatorChar}\n;\n?\n!\n-";
  85. globalState.Environment["package"] = package;
  86. globalState.Environment["require"] = moduleLibrary.RequireFunction;
  87. }
  88. public static void OpenOperatingSystemLibrary(this LuaState state)
  89. {
  90. var globalState = state.GlobalState;
  91. LuaTable os = new(0, OperatingSystemLibrary.Instance.Functions.Length);
  92. foreach (var func in OperatingSystemLibrary.Instance.Functions)
  93. {
  94. os[func.Name] = func.Func;
  95. }
  96. globalState.Environment["os"] = os;
  97. globalState.LoadedModules["os"] = os;
  98. }
  99. public static void OpenStringLibrary(this LuaState state)
  100. {
  101. var globalState = state.GlobalState;
  102. LuaTable @string = new(0, StringLibrary.Instance.Functions.Length);
  103. foreach (var func in StringLibrary.Instance.Functions)
  104. {
  105. @string[func.Name] = func.Func;
  106. }
  107. globalState.Environment["string"] = @string;
  108. globalState.LoadedModules["string"] = @string;
  109. // set __index
  110. LuaValue key = new("");
  111. if (!globalState.TryGetMetatable(key, out var metatable))
  112. {
  113. metatable = new();
  114. globalState.SetMetatable(key, metatable);
  115. }
  116. metatable[Metamethods.Index] = new LuaFunction("index", (context, cancellationToken) =>
  117. {
  118. context.GetArgument<string>(0);
  119. var key = context.GetArgument(1);
  120. return new(context.Return(@string[key]));
  121. });
  122. }
  123. public static void OpenTableLibrary(this LuaState state)
  124. {
  125. var globalState = state.GlobalState;
  126. LuaTable table = new(0, TableLibrary.Instance.Functions.Length);
  127. foreach (var func in TableLibrary.Instance.Functions)
  128. {
  129. table[func.Name] = func.Func;
  130. }
  131. globalState.Environment["table"] = table;
  132. globalState.LoadedModules["table"] = table;
  133. }
  134. public static void OpenDebugLibrary(this LuaState state)
  135. {
  136. var globalState = state.GlobalState;
  137. LuaTable debug = new(0, DebugLibrary.Instance.Functions.Length);
  138. foreach (var func in DebugLibrary.Instance.Functions)
  139. {
  140. debug[func.Name] = func.Func;
  141. }
  142. globalState.Environment["debug"] = debug;
  143. globalState.LoadedModules["debug"] = debug;
  144. }
  145. public static void OpenStandardLibraries(this LuaState state)
  146. {
  147. state.OpenBasicLibrary();
  148. state.OpenBitwiseLibrary();
  149. state.OpenCoroutineLibrary();
  150. state.OpenIOLibrary();
  151. state.OpenMathLibrary();
  152. state.OpenModuleLibrary();
  153. state.OpenOperatingSystemLibrary();
  154. state.OpenStringLibrary();
  155. state.OpenTableLibrary();
  156. state.OpenDebugLibrary();
  157. }
  158. }