Unverified Commit 2a620d52 authored by Vinicius Jarina's avatar Vinicius Jarina Committed by GitHub
Browse files

* Fixed issue with default paramers. (#274)

* The default parameter check was after typecheck
* Fixes https://github.com/NLua/NLua/issues/273
parent 006fe41a
...@@ -1314,6 +1314,16 @@ namespace NLua ...@@ -1314,6 +1314,16 @@ namespace NLua
argTypes.Add(methodArg); argTypes.Add(methodArg);
} }
else if (currentLuaParam > nLuaParams)
{ // Adds optional parameters
if (currentNetParam.IsOptional)
paramList.Add(currentNetParam.DefaultValue);
else
{
isMethod = false;
break;
}
}
else if (IsTypeCorrect(luaState, currentLuaParam, currentNetParam, out extractValue)) else if (IsTypeCorrect(luaState, currentLuaParam, currentNetParam, out extractValue))
{ // Type checking { // Type checking
var value = extractValue(luaState, currentLuaParam); var value = extractValue(luaState, currentLuaParam);
...@@ -1329,16 +1339,6 @@ namespace NLua ...@@ -1329,16 +1339,6 @@ namespace NLua
currentLuaParam++; currentLuaParam++;
} }
else if (currentLuaParam > nLuaParams)
{ // Adds optional parameters
if (currentNetParam.IsOptional)
paramList.Add(currentNetParam.DefaultValue);
else
{
isMethod = false;
break;
}
}
else if (currentNetParam.IsOptional) else if (currentNetParam.IsOptional)
paramList.Add(currentNetParam.DefaultValue); paramList.Add(currentNetParam.DefaultValue);
else else
......
...@@ -2702,6 +2702,52 @@ namespace NLuaTest ...@@ -2702,6 +2702,52 @@ namespace NLuaTest
} }
} }
[Test]
public void TestDefaultParameter()
{
using (var l = new Lua())
{
var obj = new TestClassWithMethodDefaultParameter();
l["obj"] = obj;
// Use both functions to avoid linker to remove.
obj.Func("param1");
obj.Func2("param1");
obj.x = 0;
l.DoString("obj:Func('param1')");
Assert.AreEqual(1, obj.x, "#1");
l.DoString("obj:Func('param1', 0, 0,'foo')");
Assert.AreEqual(3, obj.x, "#2");
l.DoString("obj:Func('param1', 0, 0,'')");
Assert.AreEqual(7, obj.x, "#3");
obj.x = 0;
l.DoString("obj:Func('param1', 0, 0,nil)");
Assert.AreEqual(1, obj.x, "#4");
obj.x = 0;
l.DoString("obj:Func2('param1')");
Assert.AreEqual(4, obj.x, "#2.1");
l.DoString("obj:Func2('param1', 0, 0,'foo')");
Assert.AreEqual(6, obj.x, "#2.2");
l.DoString("obj:Func2('param1', 0, 0,'')");
Assert.AreEqual(14, obj.x, "#2.3");
l.DoString("obj:Func2('param1', 0, 0,nil)");
Assert.AreEqual(15, obj.x, "#2.4");
}
}
static Lua m_lua; static Lua m_lua;
} }
......
...@@ -657,4 +657,30 @@ namespace NLuaTest.Mock ...@@ -657,4 +657,30 @@ namespace NLuaTest.Mock
} }
} }
public class TestClassWithMethodDefaultParameter
{
public int x;
public void Func(string param1, int param2 = 0, int param3 = 0, string param = null)
{
if (param == null)
x += 1;
else if (param == "foo")
x += 2;
else if (param == "")
x += 4;
}
public void Func2(string param1, int param2 = 0, int param3 = 0, string param = "default")
{
if (param == null)
x += 1;
else if (param == "foo")
x += 2;
else if (param == "default")
x += 4;
else if (param == "")
x += 8;
}
}
} }
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