Commit c1b3a950 authored by Isaac Brodsky's avatar Isaac Brodsky
Browse files

Fix calling methods with nullable parameters (null could not be passed)

parent fb177cdf
...@@ -103,8 +103,13 @@ namespace NLua ...@@ -103,8 +103,13 @@ namespace NLua
var underlyingType = Nullable.GetUnderlyingType (paramType); var underlyingType = Nullable.GetUnderlyingType (paramType);
if (underlyingType != null) if (underlyingType != null) {
// null can always be assigned to nullable
if (luatype == LuaTypes.Nil)
return extractNetObject;
paramType = underlyingType; // Silently convert nullable types to their non null requics paramType = underlyingType; // Silently convert nullable types to their non null requics
}
var extractKey = GetExtractDictionaryKey (paramType); var extractKey = GetExtractDictionaryKey (paramType);
......
...@@ -621,6 +621,25 @@ namespace NLuaTest ...@@ -621,6 +621,25 @@ namespace NLuaTest
} }
} }
/*
* Tests passing a null object as a parameter to a
* method that accepts a nullable.
*/
[Test]
public void TestNullableParameter ()
{
using (Lua lua = new Lua ()) {
lua.DoString ("luanet.load_assembly('NLuaTest')");
lua.DoString ("TestClass=luanet.import_type('NLuaTest.Mock.TestClass')");
lua.DoString ("test=TestClass()");
lua.DoString ("a = test:NullableMethod(nil)");
lua ["timeVal"] = TimeSpan.FromSeconds (5);
lua.DoString ("b = test:NullableMethod(timeVal)");
Assert.AreEqual (null, lua ["a"]);
Assert.AreEqual (TimeSpan.FromSeconds (5), lua ["b"]);
}
}
/* /*
* Tests if DoString is correctly returning values * Tests if DoString is correctly returning values
*/ */
......
...@@ -374,6 +374,11 @@ namespace NLuaTest.Mock ...@@ -374,6 +374,11 @@ namespace NLuaTest.Mock
set { } set { }
} }
public TimeSpan? NullableMethod(TimeSpan? input)
{
return input;
}
public int sum (int x, int y) public int sum (int x, int y)
{ {
return x + y; return x + y;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment