Commit e60ba816 authored by Dobosz Dariusz-FJTP67's avatar Dobosz Dariusz-FJTP67
Browse files

Fix of calling methods with 'params' array parameters when nil (null) is used as first one.

parent cdab1126
...@@ -1265,19 +1265,7 @@ namespace NLua ...@@ -1265,19 +1265,7 @@ namespace NLua
{ {
paramList.Add (null); paramList.Add (null);
outList.Add (paramList.LastIndexOf (null)); outList.Add (paramList.LastIndexOf (null));
} else if (IsTypeCorrect (luaState, currentLuaParam, currentNetParam, out extractValue)) { // Type checking
var value = extractValue (luaState, currentLuaParam);
paramList.Add (value);
int index = paramList.LastIndexOf (value);
var methodArg = new MethodArgs ();
methodArg.index = index;
methodArg.extractValue = extractValue;
argTypes.Add (methodArg);
if (currentNetParam.ParameterType.IsByRef)
outList.Add (index);
currentLuaParam++;
} // Type does not match, ignore if the parameter is optional } // Type does not match, ignore if the parameter is optional
else if (IsParamsArray (luaState, nLuaParams, currentLuaParam, currentNetParam, out extractValue)) { else if (IsParamsArray (luaState, nLuaParams, currentLuaParam, currentNetParam, out extractValue)) {
...@@ -1299,7 +1287,21 @@ namespace NLua ...@@ -1299,7 +1287,21 @@ namespace NLua
methodArg.paramsArrayType = paramArrayType; methodArg.paramsArrayType = paramArrayType;
argTypes.Add (methodArg); argTypes.Add (methodArg);
}
else if (IsTypeCorrect(luaState, currentLuaParam, currentNetParam, out extractValue))
{ // Type checking
var value = extractValue(luaState, currentLuaParam);
paramList.Add(value);
int index = paramList.LastIndexOf(value);
var methodArg = new MethodArgs();
methodArg.index = index;
methodArg.extractValue = extractValue;
argTypes.Add(methodArg);
if (currentNetParam.ParameterType.IsByRef)
outList.Add(index);
currentLuaParam++;
} else if (currentLuaParam > nLuaParams) { // Adds optional parameters } else if (currentLuaParam > nLuaParams) { // Adds optional parameters
if (currentNetParam.IsOptional) if (currentNetParam.IsOptional)
paramList.Add (currentNetParam.DefaultValue); paramList.Add (currentNetParam.DefaultValue);
......
...@@ -2490,6 +2490,51 @@ namespace NLuaTest ...@@ -2490,6 +2490,51 @@ namespace NLuaTest
} }
} }
[Test]
public void TestCallMethodWithParamsOptional()
{
using (var l = new Lua())
{
l.LoadCLRPackage();
l.DoString(" import ('NLuaTest','NLuaTest.Mock') ");
l.DoString(@"
r = TestClass.MethodWithParams(2, 7, 4)
");
int r = (int)l.GetNumber("r");
Assert.AreEqual(2, r, "#1");
}
}
[Test]
public void TestCallMethodWithObjectParams()
{
using (var l = new Lua())
{
l.LoadCLRPackage();
l.DoString(" import ('NLuaTest','NLuaTest.Mock') ");
l.DoString(@"
r = TestClass.MethodWithObjectParams(2, nil, 4, 'abc')
");
int r = (int)l.GetNumber("r");
Assert.AreEqual(4, r, "#1");
}
}
[Test]
public void TestCallMethodWithObjectParamsAndNilAsFirstArgument()
{
using (var l = new Lua())
{
l.LoadCLRPackage();
l.DoString(" import ('NLuaTest','NLuaTest.Mock') ");
l.DoString(@"
r = TestClass.MethodWithObjectParams(nil, 4, 'abc')
");
int r = (int)l.GetNumber("r");
Assert.AreEqual(3, r, "#1");
}
}
[Test] [Test]
public void TestConstructorOverload () public void TestConstructorOverload ()
{ {
......
...@@ -614,6 +614,17 @@ namespace NLuaTest.Mock ...@@ -614,6 +614,17 @@ namespace NLuaTest.Mock
} }
return i; return i;
} }
static public int MethodWithObjectParams(params object[] others)
{
int i = 0;
foreach (var val in others)
{
Console.WriteLine(val);
i++;
}
return i;
}
} }
public class TestClassWithOverloadedMethod public class TestClassWithOverloadedMethod
......
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