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
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)
{
using (Lua lua = new Lua ()) {
lua.LoadCLRPackage ();
m_lua = lua;
lua.DoString (@"
import ('ConsoleTest')
function f1 ()
f2 ()
end
function f2()
f3()
end
function f3()
Program.func()
end
f1 ()
");
}
Core c = new Core ();
c.Setup ();
c.Sieve ();
}
}
......
Subproject commit 223df8de506ad4f5e256c3a756f38ec723400b97
Subproject commit 900d27bdbdf24dd0cf1ad7111e15e907cf4ef553
Subproject commit 9ca352bd9b1bfe64576cda42bff059a3495b04dd
Subproject commit ca139f2bb0ce5eb1af342a2017896a1bfd8edca4
......@@ -130,15 +130,25 @@ namespace NLua
else if (luatype == LuaTypes.Number)
return extractValues [GetExtractDictionaryKey (typeof(double))];
}
if (LuaLib.LuaIsNumber (luaState, stackPos))
bool netParamIsString = paramType == typeof (string) || paramType == typeof (char []);
bool netParamIsNumeric = paramType == typeof (int) ||
paramType == typeof (uint) ||
paramType == typeof (long) ||
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];
if (paramType == typeof(bool)) {
} else if (paramType == typeof(bool)) {
if (LuaLib.LuaIsBoolean (luaState, stackPos))
return extractValues [extractKey];
} else if (paramType == typeof (string) || paramType == typeof (char [])) {
if (LuaLib.LuaIsString (luaState, stackPos))
} else if (netParamIsString) {
if (LuaLib.LuaNetIsStringStrict (luaState, stackPos))
return extractValues [extractKey];
else if (luatype == LuaTypes.Nil)
return extractNetObject; // kevinh - silently convert nil to a null string pointer
......@@ -230,10 +240,10 @@ namespace NLua
private object GetAsInt (LuaState luaState, int stackPos)
{
int retVal = (int)LuaLib.LuaToNumber (luaState, stackPos);
if (retVal == 0 && !LuaLib.LuaIsNumber (luaState, stackPos))
if (!LuaLib.LuaIsNumber (luaState, stackPos))
return null;
int retVal = (int)LuaLib.LuaToNumber (luaState, stackPos);
return retVal;
}
......@@ -309,19 +319,17 @@ namespace NLua
private object GetAsCharArray (LuaState luaState, int stackPos)
{
string retVal = LuaLib.LuaToString (luaState, stackPos).ToString ();
if (string.IsNullOrEmpty(retVal) && !LuaLib.LuaIsString (luaState, stackPos))
if (!LuaLib.LuaNetIsStringStrict (luaState, stackPos))
return null;
string retVal = LuaLib.LuaToString (luaState, stackPos).ToString ();
return retVal.ToCharArray();
}
private object GetAsString (LuaState luaState, int stackPos)
{
string retVal = LuaLib.LuaToString (luaState, stackPos).ToString ();
if (string.IsNullOrEmpty(retVal) && !LuaLib.LuaIsString (luaState, stackPos))
if (!LuaLib.LuaNetIsStringStrict (luaState, stackPos))
return null;
string retVal = LuaLib.LuaToString (luaState, stackPos).ToString ();
return retVal;
}
......
......@@ -274,6 +274,11 @@ namespace NLua
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)
{
return LuaCore.LuaIsCFunction (luaState, index);
......
......@@ -49,7 +49,14 @@ namespace NLuaTest
public void Setup()
{
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]
......
......@@ -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]
public void TestGetStack ()
{
......
......@@ -577,4 +577,20 @@ namespace NLuaTest.Mock
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