Unverified Commit 8e868c0c authored by Vinicius Jarina's avatar Vinicius Jarina Committed by GitHub
Browse files

Fixed #276 Overload issue. (#278)

LuaState.ToString calls luaL_tolstring which can add something to the stack. using lua_tostring.
parent 469c964b
......@@ -16,25 +16,25 @@ namespace NLua
public CheckType(ObjectTranslator translator)
{
_translator = translator;
_extractValues.Add(GetExtractDictionaryKey(typeof(object)), GetAsObject);
_extractValues.Add(GetExtractDictionaryKey(typeof(sbyte)), GetAsSbyte);
_extractValues.Add(GetExtractDictionaryKey(typeof(byte)), GetAsByte);
_extractValues.Add(GetExtractDictionaryKey(typeof(short)), GetAsShort);
_extractValues.Add(GetExtractDictionaryKey(typeof(ushort)), GetAsUshort);
_extractValues.Add(GetExtractDictionaryKey(typeof(int)), GetAsInt);
_extractValues.Add(GetExtractDictionaryKey(typeof(uint)), GetAsUint);
_extractValues.Add(GetExtractDictionaryKey(typeof(long)), GetAsLong);
_extractValues.Add(GetExtractDictionaryKey(typeof(ulong)), GetAsUlong);
_extractValues.Add(GetExtractDictionaryKey(typeof(double)), GetAsDouble);
_extractValues.Add(GetExtractDictionaryKey(typeof(char)), GetAsChar);
_extractValues.Add(GetExtractDictionaryKey(typeof(float)), GetAsFloat);
_extractValues.Add(GetExtractDictionaryKey(typeof(decimal)), GetAsDecimal);
_extractValues.Add(GetExtractDictionaryKey(typeof(bool)), GetAsBoolean);
_extractValues.Add(GetExtractDictionaryKey(typeof(string)), GetAsString);
_extractValues.Add(GetExtractDictionaryKey(typeof(char[])), GetAsCharArray);
_extractValues.Add(GetExtractDictionaryKey(typeof(LuaFunction)), GetAsFunction);
_extractValues.Add(GetExtractDictionaryKey(typeof(LuaTable)), GetAsTable);
_extractValues.Add(GetExtractDictionaryKey(typeof(LuaUserData)), GetAsUserdata);
_extractValues.Add(typeof(object), GetAsObject);
_extractValues.Add(typeof(sbyte), GetAsSbyte);
_extractValues.Add(typeof(byte), GetAsByte);
_extractValues.Add(typeof(short), GetAsShort);
_extractValues.Add(typeof(ushort), GetAsUshort);
_extractValues.Add(typeof(int), GetAsInt);
_extractValues.Add(typeof(uint), GetAsUint);
_extractValues.Add(typeof(long), GetAsLong);
_extractValues.Add(typeof(ulong), GetAsUlong);
_extractValues.Add(typeof(double), GetAsDouble);
_extractValues.Add(typeof(char), GetAsChar);
_extractValues.Add(typeof(float), GetAsFloat);
_extractValues.Add(typeof(decimal), GetAsDecimal);
_extractValues.Add(typeof(bool), GetAsBoolean);
_extractValues.Add(typeof(string), GetAsString);
_extractValues.Add(typeof(char[]), GetAsCharArray);
_extractValues.Add(typeof(LuaFunction), GetAsFunction);
_extractValues.Add(typeof(LuaTable), GetAsTable);
_extractValues.Add(typeof(LuaUserData), GetAsUserdata);
_extractNetObject = GetAsNetObject;
}
......@@ -52,8 +52,7 @@ namespace NLua
if (paramType.IsByRef)
paramType = paramType.GetElementType();
var extractKey = GetExtractDictionaryKey(paramType);
return _extractValues.ContainsKey(extractKey) ? _extractValues[extractKey] : _extractNetObject;
return _extractValues.ContainsKey(paramType) ? _extractValues[paramType] : _extractNetObject;
}
internal ExtractValue CheckLuaType(LuaState luaState, int stackPos, Type paramType)
......@@ -70,7 +69,6 @@ namespace NLua
paramType = underlyingType; // Silently convert nullable types to their non null requics
}
var extractKey = GetExtractDictionaryKey(paramType);
bool netParamIsNumeric = paramType == typeof(int) ||
paramType == typeof(uint) ||
......@@ -91,63 +89,63 @@ namespace NLua
{
// Return the correct extractor anyways
if (netParamIsNumeric || paramType == typeof(bool))
return _extractValues[extractKey];
return _extractValues[paramType];
return _extractNetObject;
}
}
if (paramType.Equals(typeof(object)))
return _extractValues[extractKey];
if (paramType == typeof(object))
return _extractValues[paramType];
//CP: Added support for generic parameters
if (paramType.IsGenericParameter)
{
if (luatype == LuaType.Boolean)
return _extractValues[GetExtractDictionaryKey(typeof(bool))];
return _extractValues[typeof(bool)];
if (luatype == LuaType.String)
return _extractValues[GetExtractDictionaryKey(typeof(string))];
return _extractValues[typeof(string)];
if (luatype == LuaType.Table)
return _extractValues[GetExtractDictionaryKey(typeof(LuaTable))];
return _extractValues[typeof(LuaTable)];
if (luatype == LuaType.UserData)
return _extractValues[GetExtractDictionaryKey(typeof(object))];
return _extractValues[typeof(object)];
if (luatype == LuaType.Function)
return _extractValues[GetExtractDictionaryKey(typeof(LuaFunction))];
return _extractValues[typeof(LuaFunction)];
if (luatype == LuaType.Number)
return _extractValues[GetExtractDictionaryKey(typeof(double))];
return _extractValues[typeof(double)];
}
bool netParamIsString = paramType == typeof(string) || paramType == typeof(char[]);
if (netParamIsNumeric)
{
if (luaState.IsNumber(stackPos) && !netParamIsString)
return _extractValues[extractKey];
return _extractValues[paramType];
}
else if (paramType == typeof(bool))
{
if (luaState.IsBoolean(stackPos))
return _extractValues[extractKey];
return _extractValues[paramType];
}
else if (netParamIsString)
{
if (luaState.IsString(stackPos))
return _extractValues[extractKey];
return _extractValues[paramType];
if (luatype == LuaType.Nil)
return _extractNetObject; // kevinh - silently convert nil to a null string pointer
}
else if (paramType == typeof(LuaTable))
{
if (luatype == LuaType.Table || luatype == LuaType.Nil)
return _extractValues[extractKey];
return _extractValues[paramType];
}
else if (paramType == typeof(LuaUserData))
{
if (luatype == LuaType.UserData || luatype == LuaType.Nil)
return _extractValues[extractKey];
return _extractValues[paramType];
}
else if (paramType == typeof(LuaFunction))
{
if (luatype == LuaType.Function || luatype == LuaType.Nil)
return _extractValues[extractKey];
return _extractValues[paramType];
}
else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaType.Function)
return new DelegateGenerator(_translator, paramType).ExtractGenerated;
......@@ -164,7 +162,7 @@ namespace NLua
{
object obj = _translator.GetNetObject(luaState, -1);
luaState.SetTop(-2);
if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
if (obj != null && paramType.IsInstanceOfType(obj))
return _extractNetObject;
}
else
......@@ -173,17 +171,13 @@ namespace NLua
else
{
object obj = _translator.GetNetObject(luaState, stackPos);
if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
if (obj != null && paramType.IsInstanceOfType(obj))
return _extractNetObject;
}
return null;
}
Type GetExtractDictionaryKey(Type targetType)
{
return targetType;
}
/*
* The following functions return the value in the Lua stack
......@@ -309,7 +303,7 @@ namespace NLua
{
if (!luaState.IsString(stackPos))
return null;
string retVal = luaState.ToString(stackPos);
string retVal = luaState.ToString(stackPos, false);
return retVal.ToCharArray();
}
......@@ -317,7 +311,7 @@ namespace NLua
{
if (!luaState.IsString(stackPos))
return null;
string retVal = luaState.ToString(stackPos);
string retVal = luaState.ToString(stackPos, false);
return retVal;
}
......
......@@ -309,7 +309,7 @@ namespace NLua
static int PanicCallback(IntPtr state)
{
var luaState = LuaState.FromIntPtr(state);
string reason = string.Format("Unprotected error in call to Lua API ({0})", luaState.ToString(-1));
string reason = string.Format("Unprotected error in call to Lua API ({0})", luaState.ToString(-1, false));
throw new LuaException(reason);
}
......
......@@ -313,7 +313,7 @@ namespace NLua
var type = luaState.Type(i);
// we dump stacks when deep in calls, calling typename while the stack is in flux can fail sometimes, so manually check for key types
string typestr = (type == LuaType.Table) ? "table" : luaState.TypeName(type);
string strrep = luaState.ToString(i);
string strrep = luaState.ToString(i, false);
if (type == LuaType.UserData)
{
......@@ -483,7 +483,7 @@ namespace NLua
return 2;
}
string methodName = luaState.ToString(2);
string methodName = luaState.ToString(2, false);
if (string.IsNullOrEmpty(methodName))
{
......@@ -855,7 +855,7 @@ namespace NLua
}
// We only look up property names by string
string fieldName = luaState.ToString(2);
string fieldName = luaState.ToString(2, false);
if (string.IsNullOrEmpty(fieldName) || !(char.IsLetter(fieldName[0]) || fieldName[0] == '_'))
{
detailMessage = "Invalid property name";
......@@ -978,7 +978,7 @@ namespace NLua
return 1;
}
string methodName = luaState.ToString(2);
string methodName = luaState.ToString(2, false);
if (string.IsNullOrEmpty(methodName))
{
......@@ -1305,7 +1305,7 @@ namespace NLua
{ // Type checking
var value = extractValue(luaState, currentLuaParam);
paramList.Add(value);
int index = paramList.LastIndexOf(value);
int index = paramList.Count - 1;
var methodArg = new MethodArgs();
methodArg.Index = index;
methodArg.ExtractValue = extractValue;
......@@ -1348,16 +1348,8 @@ namespace NLua
/// <returns></returns>
private bool IsTypeCorrect(LuaState luaState, int currentLuaParam, ParameterInfo currentNetParam, out ExtractValue extractValue)
{
try
{
return (extractValue = translator.typeChecker.CheckLuaType(luaState, currentLuaParam, currentNetParam.ParameterType)) != null;
}
catch
{
extractValue = null;
Debug.WriteLine("Type wasn't correct");
return false;
}
extractValue = translator.typeChecker.CheckLuaType(luaState, currentLuaParam, currentNetParam.ParameterType);
return extractValue != null;
}
private bool IsParamsArray(LuaState luaState, int nLuaParams, int currentLuaParam, ParameterInfo currentNetParam, out ExtractValue extractValue)
......
......@@ -205,8 +205,7 @@ namespace NLua.Method
foreach (var member in _members)
{
candidateName = member.ReflectedType.Name + "." + member.Name;
var m = (MethodInfo)member;
bool isMethod = _translator.MatchParameters(luaState, m, ref _lastCalledMethod);
bool isMethod = _translator.MatchParameters(luaState, member, ref _lastCalledMethod);
if (isMethod)
{
......
......@@ -265,7 +265,7 @@ namespace NLua
{
try
{
string assemblyName = luaState.ToString(1);
string assemblyName = luaState.ToString(1, false);
Assembly assembly = null;
Exception exception = null;
......@@ -357,7 +357,7 @@ namespace NLua
private int ImportTypeInternal(LuaState luaState)
{
string className = luaState.ToString(1);
string className = luaState.ToString(1, false);
var klass = FindType(className);
if (klass != null)
......@@ -392,7 +392,7 @@ namespace NLua
}
LuaTable luaTable = GetTable(luaState, 1);
string superclassName = luaState.ToString(2);
string superclassName = luaState.ToString(2, false);
if (string.IsNullOrEmpty(superclassName))
{
......@@ -567,7 +567,7 @@ namespace NLua
var signature = new Type[luaState.GetTop() - 1];
for (int i = 0; i < signature.Length; i++)
signature[i] = FindType(luaState.ToString(i + 2));
signature[i] = FindType(luaState.ToString(i + 2, false));
try
{
......@@ -828,7 +828,7 @@ namespace NLua
case LuaType.Number:
return luaState.ToNumber(index);
case LuaType.String:
return luaState.ToString(index);
return luaState.ToString(index, false);
case LuaType.Boolean:
return luaState.ToBoolean(index);
case LuaType.Table:
......@@ -1081,7 +1081,7 @@ namespace NLua
}
else if (lt == LuaType.String)
{
string sflags = luaState.ToString(2);
string sflags = luaState.ToString(2, false);
string err = null;
try
{
......
......@@ -2048,9 +2048,25 @@ namespace NLuaTest
");
Assert.AreEqual(3, obj.CallsToIntFunc, "#integer");
Assert.AreEqual(2, obj.CallsToStringFunc, "#string");
obj.CallsToIntFunc = 0;
obj.CallsToStringFunc = 0;
lua.DoString(@"
obj:Func2('foo','10')
obj:Func2('foo', 10)
obj:Func2('foo','10')
obj:Func2('foo',10)
obj:Func2('foo','10')
");
Assert.AreEqual(2, obj.CallsToIntFunc, "#integer");
Assert.AreEqual(3, obj.CallsToStringFunc, "#string");
}
}
[Test]
public void TestGetStack()
{
......
......@@ -14,5 +14,15 @@
CallsToIntFunc++;
}
public void Func2(string param, string param2)
{
CallsToStringFunc++;
}
public void Func2(string param, int param2)
{
CallsToIntFunc++;
}
}
}
\ 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