Browse Source

Add: LuaState.LoadedModules

AnnulusGames 1 year ago
parent
commit
b49820e2be
2 changed files with 10 additions and 3 deletions
  1. 2 0
      src/Lua/LuaState.cs
  2. 8 3
      src/Lua/Standard/OpenLibExtensions.cs

+ 2 - 0
src/Lua/LuaState.cs

@@ -13,6 +13,7 @@ public sealed class LuaState
     readonly LuaMainThread mainThread = new();
     readonly LuaMainThread mainThread = new();
     FastListCore<UpValue> openUpValues;
     FastListCore<UpValue> openUpValues;
     FastStackCore<LuaThread> threadStack;
     FastStackCore<LuaThread> threadStack;
+    readonly LuaTable packages = new();
     readonly LuaTable environment;
     readonly LuaTable environment;
     readonly UpValue envUpValue;
     readonly UpValue envUpValue;
     bool isRunning;
     bool isRunning;
@@ -22,6 +23,7 @@ public sealed class LuaState
     internal ref FastListCore<UpValue> OpenUpValues => ref openUpValues;
     internal ref FastListCore<UpValue> OpenUpValues => ref openUpValues;
 
 
     public LuaTable Environment => environment;
     public LuaTable Environment => environment;
+    public LuaTable LoadedModules => packages;
     public LuaMainThread MainThread => mainThread;
     public LuaMainThread MainThread => mainThread;
     public LuaThread CurrentThread
     public LuaThread CurrentThread
     {
     {

+ 8 - 3
src/Lua/Standard/OpenLibExtensions.cs

@@ -176,14 +176,14 @@ public static class OpenLibExtensions
         math["huge"] = double.PositiveInfinity;
         math["huge"] = double.PositiveInfinity;
 
 
         state.Environment["math"] = math;
         state.Environment["math"] = math;
+        state.LoadedModules["math"] = math;
     }
     }
 
 
     public static void OpenModuleLibrary(this LuaState state)
     public static void OpenModuleLibrary(this LuaState state)
     {
     {
-        var package = new LuaTable(0, 1);
-        package["loaded"] = new LuaTable();
+        var package = new LuaTable();
+        package["loaded"] = state.LoadedModules;
         state.Environment["package"] = package;
         state.Environment["package"] = package;
-
         state.Environment[RequireFunction.Instance.Name] = RequireFunction.Instance;
         state.Environment[RequireFunction.Instance.Name] = RequireFunction.Instance;
     }
     }
 
 
@@ -196,6 +196,7 @@ public static class OpenLibExtensions
         }
         }
 
 
         state.Environment["table"] = table;
         state.Environment["table"] = table;
+        state.LoadedModules["table"] = table;
     }
     }
 
 
     public static void OpenStringLibrary(this LuaState state)
     public static void OpenStringLibrary(this LuaState state)
@@ -207,6 +208,7 @@ public static class OpenLibExtensions
         }
         }
 
 
         state.Environment["string"] = @string;
         state.Environment["string"] = @string;
+        state.LoadedModules["string"] = @string;
 
 
         // set __index
         // set __index
         var key = new LuaValue("");
         var key = new LuaValue("");
@@ -232,6 +234,7 @@ public static class OpenLibExtensions
         io["stderr"] = new FileHandle(Console.OpenStandardError());
         io["stderr"] = new FileHandle(Console.OpenStandardError());
 
 
         state.Environment["io"] = io;
         state.Environment["io"] = io;
+        state.LoadedModules["io"] = io;
     }
     }
 
 
     public static void OpenOperatingSystemLibrary(this LuaState state)
     public static void OpenOperatingSystemLibrary(this LuaState state)
@@ -243,6 +246,7 @@ public static class OpenLibExtensions
         }
         }
 
 
         state.Environment["os"] = os;
         state.Environment["os"] = os;
+        state.LoadedModules["os"] = os;
     }
     }
 
 
     public static void OpenBitwiseLibrary(this LuaState state)
     public static void OpenBitwiseLibrary(this LuaState state)
@@ -254,6 +258,7 @@ public static class OpenLibExtensions
         }
         }
 
 
         state.Environment["bit32"] = bit32;
         state.Environment["bit32"] = bit32;
+        state.LoadedModules["bit32"] = bit32;
     }
     }
 
 
     public static void OpenStandardLibraries(this LuaState state)
     public static void OpenStandardLibraries(this LuaState state)