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

Fix passing a nullable int.

parent b239449e
......@@ -102,16 +102,34 @@ namespace NLua
paramType = paramType.GetElementType ();
var underlyingType = Nullable.GetUnderlyingType (paramType);
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
}
var extractKey = GetExtractDictionaryKey (paramType);
bool netParamIsNumeric = paramType == typeof (int) ||
paramType == typeof (uint) ||
paramType == typeof (long) ||
paramType == typeof (ulong) ||
paramType == typeof (short) ||
paramType == typeof (ushort) ||
paramType == typeof (float) ||
paramType == typeof (double) ||
paramType == typeof (decimal) ||
paramType == typeof (byte);
// If it is a nullable
if (underlyingType != null) {
// null can always be assigned to nullable
if (luatype == LuaTypes.Nil) {
// Return the correct extractor anyways
if (netParamIsNumeric)
return extractValues [extractKey];
return extractNetObject;
}
}
if (paramType.Equals (typeof(object)))
return extractValues [extractKey];
......@@ -132,15 +150,6 @@ namespace NLua
return extractValues [GetExtractDictionaryKey (typeof(double))];
}
bool netParamIsString = paramType == typeof (string) || paramType == typeof (char []);
bool netParamIsNumeric = paramType == typeof (int) ||
paramType == typeof (uint) ||
paramType == typeof (long) ||
paramType == typeof (ulong) ||
paramType == typeof (short) ||
paramType == typeof (float) ||
paramType == typeof (double) ||
paramType == typeof (decimal) ||
paramType == typeof (byte);
if (netParamIsNumeric) {
if (LuaLib.LuaIsNumber (luaState, stackPos) && !netParamIsString)
......
......@@ -653,10 +653,14 @@ namespace NLuaTest
lua.DoString ("TestClass=luanet.import_type('NLuaTest.Mock.TestClass')");
lua.DoString ("test=TestClass()");
lua.DoString ("a = test:NullableMethod(nil)");
Assert.AreEqual (null, lua ["a"]);
lua ["timeVal"] = TimeSpan.FromSeconds (5);
lua.DoString ("b = test:NullableMethod(timeVal)");
Assert.AreEqual (null, lua ["a"]);
Assert.AreEqual (TimeSpan.FromSeconds (5), lua ["b"]);
lua.DoString ("d = test:NullableMethod2(2)");
Assert.AreEqual (2, lua ["d"]);
lua.DoString ("c = test:NullableMethod2(nil)");
Assert.AreEqual (null, lua ["c"]);
}
}
......
......@@ -379,6 +379,11 @@ namespace NLuaTest.Mock
return input;
}
public int? NullableMethod2 (int? input)
{
return input;
}
public object[] TestLuaFunction (LuaFunction func)
{
if (func != null) {
......
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