Skip to content

Commit

Permalink
* Release v0.1.7
Browse files Browse the repository at this point in the history
* Simplified PushObject logic
  • Loading branch information
viniciusjarina committed Jan 19, 2019
1 parent 5a3b726 commit b0b9d07
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 138 deletions.
138 changes: 10 additions & 128 deletions src/Lua.cs
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ public void PushGlobalTable()
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
public void PushReferenceData<T>(T obj) where T : class
public void PushObject<T>(T obj)
{
if(obj == null)
{
Expand All @@ -706,30 +706,6 @@ public void PushReferenceData<T>(T obj) where T : class
NativeMethods.lua_pushlightuserdata(_luaState, GCHandle.ToIntPtr(handle));
}

/// <summary>
/// Pushes a value data (C# struct) onto the stack.
/// The value must be blitable, since the content will be stored on the unmanaged memory
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
public void PushValueData<T>(T value) where T : struct
{
object box = value;
PushReferenceData<object>(box);
}

/// <summary>
/// Pushes a Nullable value data
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
public void PushValueData<T>(T? obj) where T : struct
{
if(obj.HasValue)
PushValueData(obj.Value);
else
PushNil();
}

/// <summary>
/// Pushes binary buffer onto the stack (usually UTF encoded string) or any arbitraty binary data
Expand Down Expand Up @@ -1255,14 +1231,14 @@ public Lua ToThread(int index)
/// <typeparam name="T"></typeparam>
/// <param name="index"></param>
/// <returns></returns>
public T ToReferenceData<T>(int index) where T : class
public T ToObject<T>(int index)
{
if(IsNil(index) || !IsLightUserData(index))
return null;
return default(T);

IntPtr data = NativeMethods.lua_touserdata(_luaState, index);
if(data == IntPtr.Zero)
return null;
return default(T);

var handle = GCHandle.FromIntPtr(data);
var reference = (T)handle.Target;
Expand All @@ -1271,28 +1247,6 @@ public T ToReferenceData<T>(int index) where T : class
return reference;
}

/// <summary>
/// Return a value object from index
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="index"></param>
/// <returns></returns>
public T ToValueData<T>(int index) where T : struct
{
T? value = ToValueDataNullable<T>(index);
return value ?? default(T);
}

/// <summary>
/// Return a value? from index
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="index"></param>
/// <returns></returns>
public T? ToValueDataNullable<T>(int index) where T : struct
{
return ToReferenceData<object>(index) as T?;
}

/// <summary>
/// </summary>
Expand Down Expand Up @@ -1535,14 +1489,14 @@ public void CheckType(int argument, LuaType type)
/// <param name="argument"></param>
/// <param name="typeName"></param>
/// <returns></returns>
public T CheckReferenceData<T>(int argument, string typeName) where T : class
public T CheckObject<T>(int argument, string typeName)
{
if(IsNil(argument) || !IsLightUserData(argument))
return null;
return default(T);

IntPtr data = NativeMethods.luaL_checkudata(_luaState, argument, typeName);
if(data == IntPtr.Zero)
return null;
return default(T);

var handle = GCHandle.FromIntPtr(data);
var reference = (T)handle.Target;
Expand All @@ -1551,42 +1505,6 @@ public T CheckReferenceData<T>(int argument, string typeName) where T : class
return reference;
}

/// <summary>
/// Check if the value on stack is a nullable value data and return the value
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="argument"></param>
/// <param name="typeName"></param>
/// <returns></returns>
public T CheckValueData<T>(int argument, string typeName) where T : struct
{
T? value = CheckValueDataNullable<T>(argument, typeName);
return value ?? default(T);
}

/// <summary>
/// Check if the value on stack is a nullable value data and return the value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="argument"></param>
/// <param name="typeName"></param>
/// <returns></returns>
public T? CheckValueDataNullable<T>(int argument, string typeName) where T : struct
{
if(IsNil(argument) || !IsUserData(argument))
return null;

IntPtr data = NativeMethods.luaL_checkudata(_luaState, argument, typeName);

if(data == IntPtr.Zero)
return null;
#if NETFRAMEWORK
return (T)Marshal.PtrToStructure(data, typeof(T));
#else
return Marshal.PtrToStructure<T>(data);
#endif
}

/// <summary>
/// Loads and runs the given file
/// </summary>
Expand Down Expand Up @@ -1890,14 +1808,14 @@ public void SetMetaTable(string name)
/// <param name="argument"></param>
/// <param name="typeName"></param>
/// <returns></returns>
public T TestReferenceData<T>(int argument, string typeName) where T : class
public T TestObject<T>(int argument, string typeName)
{
if (IsNil(argument) || !IsLightUserData(argument))
return null;
return default(T);

IntPtr data = NativeMethods.luaL_testudata(_luaState, argument, typeName);
if (data == IntPtr.Zero)
return null;
return default(T);

var handle = GCHandle.FromIntPtr(data);
var reference = (T)handle.Target;
Expand All @@ -1906,42 +1824,6 @@ public T TestReferenceData<T>(int argument, string typeName) where T : class
return reference;
}

/// <summary>
/// Test if the value at index is a nullable value data
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="argument"></param>
/// <param name="typeName"></param>
/// <returns></returns>
public T? TestValueDataNullable<T>(int argument, string typeName) where T : struct
{
if (IsNil(argument) || !IsUserData(argument))
return null;

IntPtr data = NativeMethods.luaL_testudata(_luaState, argument, typeName);

if (data == IntPtr.Zero)
return null;
#if NETFRAMEWORK
return (T)Marshal.PtrToStructure(data, typeof(T));
#else
return Marshal.PtrToStructure<T>(data);
#endif
}

/// <summary>
/// Test if the value at index is a value data
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="argument"></param>
/// <param name="typeName"></param>
/// <returns></returns>
public T TestValueData<T>(int argument, string typeName) where T : struct
{
T? value = TestValueDataNullable<T>(argument, typeName);
return value ?? default(T);
}

/// <summary>
/// Creates and pushes a traceback of the stack L1
/// </summary>
Expand Down
20 changes: 10 additions & 10 deletions tests/Tests/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ static int TestReferenceData(IntPtr p)
{
var state = Lua.FromIntPtr(p);

var param1 = state.ToReferenceData<XDocument>(1);
var param2 = state.ToReferenceData<XDocument>(2);
var param1 = state.ToObject<XDocument>(1);
var param2 = state.ToObject<XDocument>(2);

Assert.IsNull(param1, "#1");
Assert.AreEqual(param2, _gTempDocument, "#2");
Expand All @@ -63,9 +63,9 @@ static int TestValueData(IntPtr p)
{
var state = Lua.FromIntPtr(p);

var param1 = state.ToValueDataNullable<Rectangle>(1);
var param2 = state.ToValueData<Rectangle>(2);
var param3 = state.ToValueData<DateTime>(3);
var param1 = state.ToObject<Rectangle?>(1);
var param2 = state.ToObject<Rectangle>(2);
var param3 = state.ToObject<DateTime>(3);

Assert.IsNull(param1, "#1");
Assert.AreEqual(param2, new Rectangle(10, 10, 100, 100), "#2");
Expand Down Expand Up @@ -102,10 +102,10 @@ public void TestPushReferenceData()
</users>");
var state = new Lua();

state.PushReferenceData<XDocument>(null);
state.PushObject<XDocument>(null);
state.SetGlobal("foo");

state.PushReferenceData(document);
state.PushObject(document);
state.SetGlobal("bar");

state.PushCFunction(FuncTestReferenceData);
Expand All @@ -120,13 +120,13 @@ public void TestPushValueData()
{
var state = new Lua();

state.PushValueData<Rectangle>(null);
state.PushObject<Rectangle?>(null);
state.SetGlobal("foo");

state.PushValueData(new Rectangle(10, 10, 100, 100));
state.PushObject(new Rectangle(10, 10, 100, 100));
state.SetGlobal("bar");

state.PushValueData(new DateTime(2018, 10, 10, 0, 0 ,0));
state.PushObject(new DateTime(2018, 10, 10, 0, 0 ,0));
state.SetGlobal("date");

state.PushCFunction(FuncTestValueData);
Expand Down

0 comments on commit b0b9d07

Please sign in to comment.