OpenLibsExtensions.cs 7.2 KB

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