From 9de1b598dca0b1da1d029f3fb4db4b84521cec4a Mon Sep 17 00:00:00 2001 From: Vinicius Jarina Date: Fri, 25 Jan 2019 00:49:00 -0500 Subject: [PATCH] Removed `Ref/Unref` integer overload, added `ToPointer, `AbsIndex` and a `LuaAlloc` `ctor`. * 99% of ref/unref will be using LuaRegistry and not int, thus removing int overload * Added `ChartSet.Ansi` to `GetGlobal`. --- src/Lua.cs | 61 +++++++++++++++++++++++++++--------------- src/NativeMethods.cs | 2 +- tests/Tests/Interop.cs | 13 +++++++++ 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/Lua.cs b/src/Lua.cs index becd43d..2e44a1a 100644 --- a/src/Lua.cs +++ b/src/Lua.cs @@ -34,14 +34,30 @@ public Lua(bool openLibs = true) _luaState = NativeMethods.luaL_newstate(); if (openLibs) - NativeMethods.luaL_openlibs(_luaState); + OpenLibs(); + + SetExtraObject(this); + } + + /// + /// Initialize Lua state with allocator function and user data value + /// This method will NOT open the default libs. + /// Creates a new thread running in a new, independent state. Returns NULL if it cannot create the thread or the state (due to lack of memory). The argument f is the allocator function; Lua does all memory allocation for this state through this function (see lua_Alloc). The second argument, ud, is an opaque pointer that Lua passes to the allocator in every call. + /// + /// LuaAlloc allocator function called to alloc/free memory + /// opaque pointer passed to allocator + public Lua(LuaAlloc allocator, IntPtr ud) + { + Encoding = Encoding.ASCII; + + _luaState = NativeMethods.lua_newstate(allocator.ToFunctionPointer(), ud); SetExtraObject(this); } private Lua(IntPtr luaThread, Lua mainState) { - this._mainState = mainState; + _mainState = mainState; _luaState = luaThread; SetExtraObject(this); GC.SuppressFinalize(this); @@ -116,6 +132,16 @@ private static T GetExtraObject(IntPtr luaState) where T : class return (T)handle.Target; } + + /// + /// Converts the acceptable index idx into an equivalent absolute index (that is, one that does not depend on the stack top). + /// + /// + /// + public int AbsIndex(int index) + { + return NativeMethods.lua_absindex(_luaState, index); + } /// /// Performs an arithmetic or bitwise operation over the two values (or one, in the case of negations) at the top of the stack, with the value at the top being the second operand, pops these values, and pushes the result of the operation. The function follows the semantics of the corresponding Lua operator (that is, it may call metamethods). /// @@ -1299,6 +1325,17 @@ public double ToNumber(int index) return null; } + /// + /// Converts the value at the given index to a generic C pointer (void*). The value can be a userdata, a table, a thread, or a function; otherwise, lua_topointer returns NULL. Different objects will give different pointers. There is no way to convert the pointer back to its original value. + /// Typically this function is used only for hashing and debug information. + /// + /// + /// + public IntPtr ToPointer(int index) + { + return NativeMethods.lua_topointer(_luaState, index); + } + /// /// Converts the value at the given index to a Lua thread @@ -1887,16 +1924,6 @@ public double OptNumber(int index, double def) return NativeMethods.luaL_optnumber(_luaState, index, def); } - /// - /// Creates and returns a reference, in the table at index t, for the object at the top of the stack (and pops the object). - /// - /// - /// - public int Ref(int tableIndex) - { - return NativeMethods.luaL_ref(_luaState, tableIndex); - } - /// /// Creates and returns a reference, in the table at index t, for the object at the top of the stack (and pops the object). /// @@ -2006,16 +2033,6 @@ public string TypeName(int index) return NativeMethods.luaL_typename(_luaState, index); } - /// - /// Releases reference ref from the table at index t (see luaL_ref). The entry is removed from the table, so that the referred object can be collected. The reference ref is also freed to be used again - /// - /// - /// - public void Unref(int tableIndex, int reference) - { - NativeMethods.luaL_unref(_luaState, tableIndex, reference); - } - /// /// Releases reference ref from the table at index t (see luaL_ref). The entry is removed from the table, so that the referred object can be collected. The reference ref is also freed to be used again /// diff --git a/src/NativeMethods.cs b/src/NativeMethods.cs index d794927..85f3ff1 100644 --- a/src/NativeMethods.cs +++ b/src/NativeMethods.cs @@ -87,7 +87,7 @@ internal static class NativeMethods [DllImport(LuaLibraryName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] internal static extern int lua_getfield(lua_State luaState, int index, string k); - [DllImport(LuaLibraryName, CallingConvention = CallingConvention.Cdecl)] + [DllImport(LuaLibraryName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] internal static extern int lua_getglobal(lua_State luaState, string name); [DllImport(LuaLibraryName, CallingConvention = CallingConvention.Cdecl)] diff --git a/tests/Tests/Interop.cs b/tests/Tests/Interop.cs index 6d80f39..b4a3764 100644 --- a/tests/Tests/Interop.cs +++ b/tests/Tests/Interop.cs @@ -285,5 +285,18 @@ public void TestLuaHookStruct() Assert.IsNull(state.Hook, "#3"); } + + [Test] + public void TestUnref() + { + var state = new Lua(); + state.DoString("function f() end"); + LuaType type = state.GetGlobal("f"); + Assert.AreEqual(LuaType.Function, type, "#1"); + + state.PushCopy(-1); + state.Ref(LuaRegistry.Index); + state.Close(); + } } }