Commit 39c028da authored by Vinicius Jarina's avatar Vinicius Jarina
Browse files

Fixed #125 calling methods with params keyword

parent 627dedea
......@@ -1267,17 +1267,17 @@ namespace NLua
currentLuaParam++;
} // Type does not match, ignore if the parameter is optional
else if (IsParamsArray (luaState, currentLuaParam, currentNetParam, out extractValue)) {
else if (IsParamsArray (luaState, nLuaParams, currentLuaParam, currentNetParam, out extractValue)) {
var paramArrayType = currentNetParam.ParameterType.GetElementType ();
int count = (nLuaParams - currentLuaParam) + 1;
Type paramArrayType = currentNetParam.ParameterType.GetElementType ();
Func<int, object> extractDelegate = (currentParam) => {
currentLuaParam ++;
currentLuaParam++;
return extractValue (luaState, currentParam);
};
int count = (nLuaParams - currentLuaParam) + 1;
Array paramArray = TableToArray (extractDelegate, paramArrayType, currentLuaParam, count);
Array paramArray = TableToArray (extractDelegate, paramArrayType, currentLuaParam, count);
paramList.Add (paramArray);
int index = paramList.LastIndexOf (paramArray);
var methodArg = new MethodArgs ();
......@@ -1287,6 +1287,7 @@ namespace NLua
methodArg.paramsArrayType = paramArrayType;
argTypes.Add (methodArg);
} else if (currentLuaParam > nLuaParams) { // Adds optional parameters
if (currentNetParam.IsOptional)
paramList.Add (currentNetParam.DefaultValue);
......@@ -1333,11 +1334,14 @@ namespace NLua
}
}
private bool IsParamsArray (LuaState luaState, int currentLuaParam, ParameterInfo currentNetParam, out ExtractValue extractValue)
private bool IsParamsArray (LuaState luaState, int nLuaParams, int currentLuaParam, ParameterInfo currentNetParam, out ExtractValue extractValue)
{
extractValue = null;
bool isParamArray = false;
if (currentNetParam.GetCustomAttributes (typeof(ParamArrayAttribute), false).Any ()) {
isParamArray = nLuaParams < currentLuaParam;
LuaTypes luaType;
try {
......@@ -1374,8 +1378,7 @@ namespace NLua
}
}
Debug.WriteLine ("Type wasn't Params object.");
return false;
return isParamArray;
}
}
}
\ No newline at end of file
......@@ -2356,6 +2356,20 @@ namespace NLuaTest
}
}
[Test]
public void TestCallMethodWithParams2 ()
{
using (var l = new Lua ()) {
l.LoadCLRPackage ();
l.DoString (" import ('NLuaTest','NLuaTest.Mock') ");
l.DoString (@"
r = TestClass.MethodWithParams(2)
");
int r = (int)l.GetNumber ("r");
Assert.AreEqual (0, r, "#1");
}
}
static Lua m_lua;
}
......
......@@ -586,6 +586,16 @@ namespace NLuaTest.Mock
}
Console.WriteLine(output);
}
static public int MethodWithParams (int a, params int[] others) {
Console.WriteLine (a);
int i = 0;
foreach (int val in others) {
Console.WriteLine (val);
i++;
}
return i;
}
}
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