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 ...@@ -1267,17 +1267,17 @@ namespace NLua
currentLuaParam++; 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, 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) => { Func<int, object> extractDelegate = (currentParam) => {
currentLuaParam ++; currentLuaParam++;
return extractValue (luaState, currentParam); 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); paramList.Add (paramArray);
int index = paramList.LastIndexOf (paramArray); int index = paramList.LastIndexOf (paramArray);
var methodArg = new MethodArgs (); var methodArg = new MethodArgs ();
...@@ -1286,6 +1286,7 @@ namespace NLua ...@@ -1286,6 +1286,7 @@ namespace NLua
methodArg.isParamsArray = true; methodArg.isParamsArray = true;
methodArg.paramsArrayType = paramArrayType; methodArg.paramsArrayType = paramArrayType;
argTypes.Add (methodArg); argTypes.Add (methodArg);
} else if (currentLuaParam > nLuaParams) { // Adds optional parameters } else if (currentLuaParam > nLuaParams) { // Adds optional parameters
if (currentNetParam.IsOptional) if (currentNetParam.IsOptional)
...@@ -1333,11 +1334,14 @@ namespace NLua ...@@ -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; extractValue = null;
bool isParamArray = false;
if (currentNetParam.GetCustomAttributes (typeof(ParamArrayAttribute), false).Any ()) { if (currentNetParam.GetCustomAttributes (typeof(ParamArrayAttribute), false).Any ()) {
isParamArray = nLuaParams < currentLuaParam;
LuaTypes luaType; LuaTypes luaType;
try { try {
...@@ -1374,8 +1378,7 @@ namespace NLua ...@@ -1374,8 +1378,7 @@ namespace NLua
} }
} }
Debug.WriteLine ("Type wasn't Params object."); return isParamArray;
return false;
} }
} }
} }
\ No newline at end of file
...@@ -2356,6 +2356,20 @@ namespace NLuaTest ...@@ -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; static Lua m_lua;
} }
......
...@@ -586,6 +586,16 @@ namespace NLuaTest.Mock ...@@ -586,6 +586,16 @@ namespace NLuaTest.Mock
} }
Console.WriteLine(output); 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 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