Commit 10f4d0ce authored by Vinicius Jarina's avatar Vinicius Jarina
Browse files

Fixed #103 Overloaded functions bug + Unit tests

parent 6ee56d59
...@@ -12,54 +12,11 @@ namespace ConsoleTest ...@@ -12,54 +12,11 @@ namespace ConsoleTest
public class Program public class Program
{ {
public static void func()
{
KeraLua.LuaDebug info = new KeraLua.LuaDebug ();
int level = 0;
StringBuilder sb = new StringBuilder ();
while (m_lua.GetStack (level,ref info) != 0) {
m_lua.GetInfo ("nSl", ref info);
string name = "<unknow>";
if (info.name != null && !string.IsNullOrEmpty(info.name.ToString()))
name = info.name.ToString ();
sb.AppendFormat ("[{0}] {1}:{2} -- {3} [{4}]\n",
level, info.shortsrc, info.currentline,
name, info.namewhat);
++level;
}
string expected = "[0] [C]:-1 -- func [field]\n[1] [string \"chunk\"]:12 -- f3 [global]\n[2] [string \"chunk\"]:8 -- f2 [global]\n[3] [string \"chunk\"]:4 -- f1 [global]\n[4] [string \"chunk\"]:15 -- <unknow> []\n";
string x = sb.ToString ();
if (x == expected)
Console.WriteLine ("OK");
Console.Write (x);
}
static Lua m_lua;
static void Main (string [] args) static void Main (string [] args)
{ {
Core c = new Core ();
using (Lua lua = new Lua ()) { c.Setup ();
lua.LoadCLRPackage (); c.Sieve ();
m_lua = lua;
lua.DoString (@"
import ('ConsoleTest')
function f1 ()
f2 ()
end
function f2()
f3()
end
function f3()
Program.func()
end
f1 ()
");
}
} }
} }
......
Subproject commit 223df8de506ad4f5e256c3a756f38ec723400b97 Subproject commit 900d27bdbdf24dd0cf1ad7111e15e907cf4ef553
Subproject commit 9ca352bd9b1bfe64576cda42bff059a3495b04dd Subproject commit ca139f2bb0ce5eb1af342a2017896a1bfd8edca4
...@@ -130,15 +130,25 @@ namespace NLua ...@@ -130,15 +130,25 @@ namespace NLua
else if (luatype == LuaTypes.Number) else if (luatype == LuaTypes.Number)
return extractValues [GetExtractDictionaryKey (typeof(double))]; return extractValues [GetExtractDictionaryKey (typeof(double))];
} }
bool netParamIsString = paramType == typeof (string) || paramType == typeof (char []);
if (LuaLib.LuaIsNumber (luaState, stackPos)) bool netParamIsNumeric = paramType == typeof (int) ||
return extractValues [extractKey]; paramType == typeof (uint) ||
paramType == typeof (long) ||
if (paramType == typeof(bool)) { paramType == typeof (ulong) ||
paramType == typeof (short) ||
paramType == typeof (float) ||
paramType == typeof (double) ||
paramType == typeof (decimal) ||
paramType == typeof (byte);
if (netParamIsNumeric) {
if (LuaLib.LuaIsNumber (luaState, stackPos) && !netParamIsString)
return extractValues [extractKey];
} else if (paramType == typeof(bool)) {
if (LuaLib.LuaIsBoolean (luaState, stackPos)) if (LuaLib.LuaIsBoolean (luaState, stackPos))
return extractValues [extractKey]; return extractValues [extractKey];
} else if (paramType == typeof (string) || paramType == typeof (char [])) { } else if (netParamIsString) {
if (LuaLib.LuaIsString (luaState, stackPos)) if (LuaLib.LuaNetIsStringStrict (luaState, stackPos))
return extractValues [extractKey]; return extractValues [extractKey];
else if (luatype == LuaTypes.Nil) else if (luatype == LuaTypes.Nil)
return extractNetObject; // kevinh - silently convert nil to a null string pointer return extractNetObject; // kevinh - silently convert nil to a null string pointer
...@@ -230,10 +240,10 @@ namespace NLua ...@@ -230,10 +240,10 @@ namespace NLua
private object GetAsInt (LuaState luaState, int stackPos) private object GetAsInt (LuaState luaState, int stackPos)
{ {
int retVal = (int)LuaLib.LuaToNumber (luaState, stackPos); if (!LuaLib.LuaIsNumber (luaState, stackPos))
if (retVal == 0 && !LuaLib.LuaIsNumber (luaState, stackPos))
return null; return null;
int retVal = (int)LuaLib.LuaToNumber (luaState, stackPos);
return retVal; return retVal;
} }
...@@ -309,19 +319,17 @@ namespace NLua ...@@ -309,19 +319,17 @@ namespace NLua
private object GetAsCharArray (LuaState luaState, int stackPos) private object GetAsCharArray (LuaState luaState, int stackPos)
{ {
string retVal = LuaLib.LuaToString (luaState, stackPos).ToString (); if (!LuaLib.LuaNetIsStringStrict (luaState, stackPos))
if (string.IsNullOrEmpty(retVal) && !LuaLib.LuaIsString (luaState, stackPos))
return null; return null;
string retVal = LuaLib.LuaToString (luaState, stackPos).ToString ();
return retVal.ToCharArray(); return retVal.ToCharArray();
} }
private object GetAsString (LuaState luaState, int stackPos) private object GetAsString (LuaState luaState, int stackPos)
{ {
string retVal = LuaLib.LuaToString (luaState, stackPos).ToString (); if (!LuaLib.LuaNetIsStringStrict (luaState, stackPos))
if (string.IsNullOrEmpty(retVal) && !LuaLib.LuaIsString (luaState, stackPos))
return null; return null;
string retVal = LuaLib.LuaToString (luaState, stackPos).ToString ();
return retVal; return retVal;
} }
......
...@@ -272,6 +272,11 @@ namespace NLua ...@@ -272,6 +272,11 @@ namespace NLua
public static bool LuaIsString (LuaState luaState, int index) public static bool LuaIsString (LuaState luaState, int index)
{ {
return LuaCore.LuaIsString (luaState, index) != 0; return LuaCore.LuaIsString (luaState, index) != 0;
}
public static bool LuaNetIsStringStrict (LuaState luaState, int index)
{
return LuaCore.LuaNetIsStringStrict (luaState, index) != 0;
} }
public static bool LuaIsCFunction (LuaState luaState, int index) public static bool LuaIsCFunction (LuaState luaState, int index)
......
...@@ -48,8 +48,15 @@ namespace NLuaTest ...@@ -48,8 +48,15 @@ namespace NLuaTest
[SetUp] [SetUp]
public void Setup() public void Setup()
{ {
lua = new Lua (); lua = new Lua ();
lua.RegisterFunction ("print", typeof (Console).GetMethod ("WriteLine", new Type [] { typeof(String) }));
lua.DoString (@"
luanet.load_assembly ('System')
Console = luanet.import_type ('System.Console')
function print (...)
Console.WriteLine (...)
end
");
} }
[TearDown] [TearDown]
......
...@@ -2131,6 +2131,24 @@ namespace NLuaTest ...@@ -2131,6 +2131,24 @@ namespace NLuaTest
} }
} }
[Test]
public void TestOverloadedMethods ()
{
using (Lua lua = new Lua ()) {
var obj = new TestClassWithOverloadedMethod ();
lua ["obj"] = obj;
lua.DoString (@"
obj:Func (10)
obj:Func ('10')
obj:Func (10)
obj:Func ('10')
obj:Func (10)
");
Assert.AreEqual (3, obj.CallsToIntFunc,"#integer");
Assert.AreEqual (2, obj.CallsToStringFunc, "#string");
}
}
[Test] [Test]
public void TestGetStack () public void TestGetStack ()
{ {
......
...@@ -577,4 +577,20 @@ namespace NLuaTest.Mock ...@@ -577,4 +577,20 @@ namespace NLuaTest.Mock
Console.WriteLine(output); Console.WriteLine(output);
} }
} }
public class TestClassWithOverloadedMethod
{
public int CallsToStringFunc {get;set;}
public int CallsToIntFunc {get;set;}
public void Func (string param)
{
CallsToStringFunc++;
}
public void Func (int param)
{
CallsToIntFunc++;
}
}
} }
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