From 67216d686101112f6b04621b37e31ae52e39277e Mon Sep 17 00:00:00 2001 From: Ricardo Reis Date: Wed, 8 Apr 2026 14:26:41 -0300 Subject: [PATCH 1/3] Fix for PR#86 Added support for selectively loading Lua standard libraries using luaL_requiref, instead of relying solely on luaL_openlibs. --- src/Lua.cs | 148 ++++++++++++++++++++++++++++++++++++++++++- src/NativeMethods.cs | 30 +++++++++ 2 files changed, 175 insertions(+), 3 deletions(-) diff --git a/src/Lua.cs b/src/Lua.cs index ef75c3a..921f72e 100644 --- a/src/Lua.cs +++ b/src/Lua.cs @@ -906,9 +906,9 @@ public void PushString(string value, params object[] args) /// Pushes a double with value n onto the stack. /// /// - public void PushNumber(double number) => NativeMethods.lua_pushnumber(_luaState, number); - - + public void PushNumber(double number) => NativeMethods.lua_pushnumber(_luaState, number); + + /// /// Pushes the thread represented by L onto the stack. Returns true if this thread is the main thread of its state. /// @@ -2005,14 +2005,156 @@ public bool NewMetaTable(string name) return NativeMethods.luaL_newmetatable(_luaState, name) != 0; } + private static int OpenBase(IntPtr luaState) + { + return KeraLua.NativeMethods.luaopen_base(luaState); + } + + private static int OpenPackage(IntPtr luaState) + { + return KeraLua.NativeMethods.luaopen_package(luaState); + } + + private static int OpenCoroutine(IntPtr luaState) + { + return KeraLua.NativeMethods.luaopen_coroutine(luaState); + } + + private static int OpenTable(IntPtr luaState) + { + return KeraLua.NativeMethods.luaopen_table(luaState); + } + + private static int OpenIO(IntPtr luaState) + { + return KeraLua.NativeMethods.luaopen_io(luaState); + } + + private static int OpenOS(IntPtr luaState) + { + return KeraLua.NativeMethods.luaopen_os(luaState); + } + + private static int OpenString(IntPtr luaState) + { + return KeraLua.NativeMethods.luaopen_string(luaState); + } + + private static int OpenUtf8(IntPtr luaState) + { + return KeraLua.NativeMethods.luaopen_utf8(luaState); + } + + private static int OpenMath(IntPtr luaState) + { + return KeraLua.NativeMethods.luaopen_math(luaState); + } + + private static int OpenDebug(IntPtr luaState) + { + return KeraLua.NativeMethods.luaopen_debug(luaState); + } + /// /// Opens all standard Lua libraries into the given state. + /// Equivalent to calling luaL_openlibs /// public void OpenLibs() { NativeMethods.luaL_openlibs(_luaState); } + /// + /// Opens the base library (_G), registering core global functions + /// such as print, pairs, and type + /// + public void OpenBasicLibrary() + { + RequireF("_G", OpenBase, true); + Pop(1); + } + + /// + /// Opens the package library, enabling module loading via require + /// + public void OpenPackageLibrary() + { + RequireF("package", OpenPackage, true); + Pop(1); + } + + /// + /// Opens the coroutine library for cooperative multitasking + /// + public void OpenCoroutineLibrary() + { + RequireF("coroutine", OpenCoroutine, true); + Pop(1); + } + + /// + /// Opens the table library for table manipulation utilities + /// + public void OpenTableLibrary() + { + RequireF("table", OpenTable, true); + Pop(1); + } + + /// + /// Opens the I/O library for file operations + /// + public void OpenIOLibrary() + { + RequireF("io", OpenIO, true); + Pop(1); + } + + /// + /// Opens the OS library for operating system interactions + /// + public void OpenOSLibrary() + { + RequireF("os", OpenOS, true); + Pop(1); + } + + /// + /// Opens the string library for string manipulation functions + /// + public void OpenStringLibrary() + { + RequireF("string", OpenString, true); + Pop(1); + } + + /// + /// Opens the UTF-8 library for Unicode string handling + /// + public void OpenUTF8Library() + { + RequireF("utf8", OpenUtf8, true); + Pop(1); + } + + /// + /// Opens the math library for mathematical operations + /// + public void OpenMathLibrary() + { + RequireF("math", OpenMath, true); + Pop(1); + } + + /// + /// Opens the debug library for advanced debugging and introspection + /// + public void OpenDebugLibrary() + { + RequireF("debug", OpenDebug, true); + Pop(1); + } + /// /// If the function argument arg is an integer (or convertible to an integer), returns this integer. If this argument is absent or is nil, returns d /// diff --git a/src/NativeMethods.cs b/src/NativeMethods.cs index dc7316e..42d44ef 100644 --- a/src/NativeMethods.cs +++ b/src/NativeMethods.cs @@ -442,6 +442,36 @@ internal static extern charptr_t luaL_traceback [DllImport(LuaLibraryName, CallingConvention = CallingConvention.Cdecl)] internal static extern void luaL_where(lua_State luaState, int level); + [DllImport(LuaLibraryName, CallingConvention = CallingConvention.Cdecl)] + internal static extern int luaopen_base(lua_State luaState); + + [DllImport(LuaLibraryName, CallingConvention = CallingConvention.Cdecl)] + internal static extern int luaopen_package(lua_State luaState); + + [DllImport(LuaLibraryName, CallingConvention = CallingConvention.Cdecl)] + internal static extern int luaopen_coroutine(lua_State luaState); + + [DllImport(LuaLibraryName, CallingConvention = CallingConvention.Cdecl)] + internal static extern int luaopen_table(lua_State luaState); + + [DllImport(LuaLibraryName, CallingConvention = CallingConvention.Cdecl)] + internal static extern int luaopen_io(lua_State luaState); + + [DllImport(LuaLibraryName, CallingConvention = CallingConvention.Cdecl)] + internal static extern int luaopen_os(lua_State luaState); + + [DllImport(LuaLibraryName, CallingConvention = CallingConvention.Cdecl)] + internal static extern int luaopen_string(lua_State luaState); + + [DllImport(LuaLibraryName, CallingConvention = CallingConvention.Cdecl)] + internal static extern int luaopen_utf8(lua_State luaState); + + [DllImport(LuaLibraryName, CallingConvention = CallingConvention.Cdecl)] + internal static extern int luaopen_math(lua_State luaState); + + [DllImport(LuaLibraryName, CallingConvention = CallingConvention.Cdecl)] + internal static extern int luaopen_debug(lua_State luaState); + #pragma warning restore CA2101 // Bug on CA + VS2017 #pragma warning restore IDE1006 // Naming Styles From fc88ecc09c79efc15962707e7810f62878c8f7b5 Mon Sep 17 00:00:00 2001 From: Ricardo Reis Date: Wed, 8 Apr 2026 14:48:59 -0300 Subject: [PATCH 2/3] Update Lua.cs Removed extra line --- src/Lua.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Lua.cs b/src/Lua.cs index 921f72e..8903142 100644 --- a/src/Lua.cs +++ b/src/Lua.cs @@ -908,7 +908,6 @@ public void PushString(string value, params object[] args) /// public void PushNumber(double number) => NativeMethods.lua_pushnumber(_luaState, number); - /// /// Pushes the thread represented by L onto the stack. Returns true if this thread is the main thread of its state. /// From 2eae0c073bf96ff25a9e965ed388300e4a8b69c5 Mon Sep 17 00:00:00 2001 From: Ricardo Reis Date: Wed, 8 Apr 2026 14:53:30 -0300 Subject: [PATCH 3/3] Update Lua.cs OpenUTF8 method name normalization --- src/Lua.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Lua.cs b/src/Lua.cs index 8903142..61cfcc7 100644 --- a/src/Lua.cs +++ b/src/Lua.cs @@ -2039,7 +2039,7 @@ private static int OpenString(IntPtr luaState) return KeraLua.NativeMethods.luaopen_string(luaState); } - private static int OpenUtf8(IntPtr luaState) + private static int OpenUTF8(IntPtr luaState) { return KeraLua.NativeMethods.luaopen_utf8(luaState); } @@ -2132,7 +2132,7 @@ public void OpenStringLibrary() /// public void OpenUTF8Library() { - RequireF("utf8", OpenUtf8, true); + RequireF("utf8", OpenUTF8, true); Pop(1); }