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