From 44c20b565a026767d006e3a34c5958cc07f74152 Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Fri, 17 Feb 2023 20:00:44 +0100 Subject: [PATCH] Add methods to individually open standard libraries. --- src/KeraLua.Core.projitems | 1 + src/Lua.cs | 104 ++++++++++++++++++++++++++++++++++++- src/LuaLibraryName.cs | 23 ++++++++ src/NativeMethods.cs | 30 +++++++++++ 4 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 src/LuaLibraryName.cs diff --git a/src/KeraLua.Core.projitems b/src/KeraLua.Core.projitems index 04de032..9e39806 100644 --- a/src/KeraLua.Core.projitems +++ b/src/KeraLua.Core.projitems @@ -9,6 +9,7 @@ KeraLua + diff --git a/src/Lua.cs b/src/Lua.cs index ef75c3a..f75aaf2 100644 --- a/src/Lua.cs +++ b/src/Lua.cs @@ -124,8 +124,8 @@ public void Close() NativeMethods.lua_close(_luaState); _luaState = IntPtr.Zero; GC.SuppressFinalize(this); - } - + } + /// /// Dispose the lua context (calling Close) /// @@ -2013,6 +2013,106 @@ public void OpenLibs() NativeMethods.luaL_openlibs(_luaState); } + /// + /// Open the standard base library into the given state. + /// + /// + /// + public void OpenBase(string moduleName = LuaLibraryName.Base, bool global = true) + { + RequireF(moduleName, NativeMethods.luaopen_base, global); + } + + /// + /// Open the standard package library into the given state. + /// + /// + /// + public void OpenPackage(string moduleName = LuaLibraryName.Package, bool global = true) + { + RequireF(moduleName, NativeMethods.luaopen_package, global); + } + + /// + /// Open the standard coroutine library into the given state. + /// + /// + /// + public void OpenCoroutine(string moduleName = LuaLibraryName.Coroutine, bool global = true) + { + RequireF(moduleName, NativeMethods.luaopen_coroutine, global); + } + + /// + /// Open the standard table library into the given state. + /// + /// + /// + public void OpenTable(string moduleName = LuaLibraryName.Table, bool global = true) + { + RequireF(moduleName, NativeMethods.luaopen_table, global); + } + + /// + /// Open the standard IO library into the given state. + /// + /// + /// + public void OpenIO(string moduleName = LuaLibraryName.IO, bool global = true) + { + RequireF(moduleName, NativeMethods.luaopen_io, global); + } + + /// + /// Open the standard OS library into the given state. + /// + /// + /// + public void OpenOS(string moduleName = LuaLibraryName.OS, bool global = true) + { + RequireF(moduleName, NativeMethods.luaopen_os, global); + } + + /// + /// Open the standard string library into the given state. + /// + /// + /// + public void OpenString(string moduleName = LuaLibraryName.String, bool global = true) + { + RequireF(moduleName, NativeMethods.luaopen_string, global); + } + + /// + /// Open the standard math library into the given state. + /// + /// + /// + public void OpenMath(string moduleName = LuaLibraryName.Math, bool global = true) + { + RequireF(moduleName, NativeMethods.luaopen_math, global); + } + + /// + /// Open the standard UTF8 library into the given state. + /// + /// + /// + public void OpenUTF8(string moduleName = LuaLibraryName.UTF8, bool global = true) + { + RequireF(moduleName, NativeMethods.luaopen_utf8, global); + } + + /// + /// Open the standard debug library into the given state. + /// + /// + /// + public void OpenDebug(string moduleName = LuaLibraryName.Debug, bool global = true) + { + RequireF(moduleName, NativeMethods.luaopen_debug, global); + } + /// /// 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/LuaLibraryName.cs b/src/LuaLibraryName.cs new file mode 100644 index 0000000..99add55 --- /dev/null +++ b/src/LuaLibraryName.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace KeraLua +{ + /// + /// Lua library names + /// + public static class LuaLibraryName + { + public const string Base = "_G"; + public const string Package = "package"; + public const string Coroutine = "coroutine"; + public const string Table = "table"; + public const string IO = "io"; + public const string OS = "os"; + public const string String = "string"; + public const string Math = "math"; + public const string UTF8 = "utf8"; + public const string Debug = "debug"; + } +} diff --git a/src/NativeMethods.cs b/src/NativeMethods.cs index dc7316e..1b4c7ee 100644 --- a/src/NativeMethods.cs +++ b/src/NativeMethods.cs @@ -331,6 +331,36 @@ internal static extern int lua_yieldk(lua_State luaState, lua_KContext ctx, lua_KFunction k); + [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_math(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_debug(lua_State luaState); + [DllImport(LuaLibraryName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] internal static extern int luaL_argerror(lua_State luaState, int arg, string message);