Skip to content

Commit

Permalink
Removed Ref/Unref integer overload, added ToPointer, AbsIndex` an…
Browse files Browse the repository at this point in the history
…d a `LuaAlloc` `ctor`.

* 99% of ref/unref will be using LuaRegistry and not int, thus removing int overload
* Added `ChartSet.Ansi` to `GetGlobal`.
  • Loading branch information
viniciusjarina committed Jan 25, 2019
1 parent ca303ba commit 9de1b59
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 23 deletions.
61 changes: 39 additions & 22 deletions src/Lua.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,30 @@ public Lua(bool openLibs = true)
_luaState = NativeMethods.luaL_newstate();

if (openLibs)
NativeMethods.luaL_openlibs(_luaState);
OpenLibs();

SetExtraObject(this);
}

/// <summary>
/// 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.
/// </summary>
/// <param name="allocator">LuaAlloc allocator function called to alloc/free memory</param>
/// <param name="ud">opaque pointer passed to allocator</param>
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);
Expand Down Expand Up @@ -116,6 +132,16 @@ private static T GetExtraObject<T>(IntPtr luaState) where T : class

return (T)handle.Target;
}

/// <summary>
/// Converts the acceptable index idx into an equivalent absolute index (that is, one that does not depend on the stack top).
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public int AbsIndex(int index)
{
return NativeMethods.lua_absindex(_luaState, index);
}
/// <summary>
/// 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).
/// </summary>
Expand Down Expand Up @@ -1299,6 +1325,17 @@ public double ToNumber(int index)
return null;
}

/// <summary>
/// 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.
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public IntPtr ToPointer(int index)
{
return NativeMethods.lua_topointer(_luaState, index);
}


/// <summary>
/// Converts the value at the given index to a Lua thread
Expand Down Expand Up @@ -1887,16 +1924,6 @@ public double OptNumber(int index, double def)
return NativeMethods.luaL_optnumber(_luaState, index, def);
}

/// <summary>
/// Creates and returns a reference, in the table at index t, for the object at the top of the stack (and pops the object).
/// </summary>
/// <param name="tableIndex"></param>
/// <returns></returns>
public int Ref(int tableIndex)
{
return NativeMethods.luaL_ref(_luaState, tableIndex);
}

/// <summary>
/// Creates and returns a reference, in the table at index t, for the object at the top of the stack (and pops the object).
/// </summary>
Expand Down Expand Up @@ -2006,16 +2033,6 @@ public string TypeName(int index)
return NativeMethods.luaL_typename(_luaState, index);
}

/// <summary>
/// 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
/// </summary>
/// <param name="tableIndex"></param>
/// <param name="reference"></param>
public void Unref(int tableIndex, int reference)
{
NativeMethods.luaL_unref(_luaState, tableIndex, reference);
}

/// <summary>
/// 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
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
13 changes: 13 additions & 0 deletions tests/Tests/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

0 comments on commit 9de1b59

Please sign in to comment.