Commit d898c805 authored by Vinicius Jarina's avatar Vinicius Jarina
Browse files

Fixed vargs method call (params).

parent 0893940d
......@@ -828,9 +828,11 @@ namespace NLua
}
internal Array TableToArray (object luaParamValue, Type paramArrayType)
internal Array TableToArray (Func<int, object> luaParamValueExtractor, Type paramArrayType, int startIndex, int count)
{
Array paramArray;
var luaParamValue = luaParamValueExtractor (startIndex);
if (luaParamValue is LuaTable) {
LuaTable table = (LuaTable)luaParamValue;
......@@ -855,9 +857,16 @@ namespace NLua
#endif
paramArrayIndex++;
}
} else {
paramArray = Array.CreateInstance (paramArrayType, 1);
paramArray.SetValue (luaParamValue, 0);
} else {
paramArray = Array.CreateInstance (paramArrayType, count);
paramArray.SetValue (luaParamValue, 0);
for (int i = 1; i < count; i++) {
startIndex++;
var value = luaParamValueExtractor (startIndex);
paramArray.SetValue (value, i);
}
}
return paramArray;
......@@ -912,9 +921,14 @@ namespace NLua
} // Type does not match, ignore if the parameter is optional
else if (IsParamsArray (luaState, currentLuaParam, currentNetParam, out extractValue)) {
object luaParamValue = extractValue (luaState, currentLuaParam);
var paramArrayType = currentNetParam.ParameterType.GetElementType ();
Array paramArray = TableToArray (luaParamValue, paramArrayType);
var paramArrayType = currentNetParam.ParameterType.GetElementType ();
Func<int, object> extractDelegate = (currentParam) => {
currentLuaParam ++;
return extractValue (luaState, currentParam);
};
int count = (nLuaParams - currentLuaParam) + 1;
Array paramArray = TableToArray (extractDelegate, paramArrayType, currentLuaParam, count);
paramList.Add (paramArray);
int index = paramList.LastIndexOf (paramArray);
......@@ -924,7 +938,7 @@ namespace NLua
methodArg.isParamsArray = true;
methodArg.paramsArrayType = paramArrayType;
argTypes.Add (methodArg);
currentLuaParam++;
} else if (currentNetParam.IsOptional)
paramList.Add (currentNetParam.DefaultValue);
else { // No match
......
......@@ -148,15 +148,21 @@ namespace NLua.Method
try {
for (int i = 0; i < _LastCalledMethod.argTypes.Length; i++) {
MethodArgs type = _LastCalledMethod.argTypes [i];
object luaParamValue = type.extractValue (luaState, i + 1 + numStackToSkip);
if (_LastCalledMethod.argTypes [i].isParamsArray) {
Array paramArray = _Translator.TableToArray (luaParamValue, type.paramsArrayType);
MethodArgs type = _LastCalledMethod.argTypes [i];
int index = i + 1 + numStackToSkip;
Func<int, object> valueExtractor = (currentParam) => {
return type.extractValue (luaState, currentParam);
};
if (_LastCalledMethod.argTypes [i].isParamsArray) {
int count = _LastCalledMethod.argTypes.Length - index;
Array paramArray = _Translator.TableToArray (valueExtractor, type.paramsArrayType, index, count);
args [_LastCalledMethod.argTypes [i].index] = paramArray;
} else {
args [type.index] = luaParamValue;
args [type.index] = valueExtractor (index);
}
if (_LastCalledMethod.args [_LastCalledMethod.argTypes [i].index] == null &&
......
......@@ -862,8 +862,8 @@ namespace NLua
return metaFunctions.MatchParameters (luaState, method, ref methodCache);
}
internal Array TableToArray(object luaParamValue, Type paramArrayType) {
return metaFunctions.TableToArray(luaParamValue,paramArrayType);
internal Array TableToArray(Func<int, object> luaParamValue, Type paramArrayType, int startIndex, int count) {
return metaFunctions.TableToArray(luaParamValue,paramArrayType, startIndex, count);
}
}
}
\ No newline at end of file
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