"Core/LuaInterface/LuaHideAttribute.cs" did not exist on "75f462546757c2b70078be13a9592895d7e9d65e"
Commit b239449e authored by Isaac Brodsky's avatar Isaac Brodsky
Browse files

Fix passing null into LuaFunction

parent c1b3a950
...@@ -154,13 +154,13 @@ namespace NLua ...@@ -154,13 +154,13 @@ namespace NLua
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
} else if (paramType == typeof(LuaTable)) { } else if (paramType == typeof(LuaTable)) {
if (luatype == LuaTypes.Table) if (luatype == LuaTypes.Table || luatype == LuaTypes.Nil)
return extractValues [extractKey]; return extractValues [extractKey];
} else if (paramType == typeof(LuaUserData)) { } else if (paramType == typeof(LuaUserData)) {
if (luatype == LuaTypes.UserData) if (luatype == LuaTypes.UserData || luatype == LuaTypes.Nil)
return extractValues [extractKey]; return extractValues [extractKey];
} else if (paramType == typeof(LuaFunction)) { } else if (paramType == typeof(LuaFunction)) {
if (luatype == LuaTypes.Function) if (luatype == LuaTypes.Function || luatype == LuaTypes.Nil)
return extractValues [extractKey]; return extractValues [extractKey];
} else if (typeof(Delegate).IsAssignableFrom (paramType) && luatype == LuaTypes.Function) } else if (typeof(Delegate).IsAssignableFrom (paramType) && luatype == LuaTypes.Function)
return new ExtractValue (new DelegateGenerator (translator, paramType).ExtractGenerated); return new ExtractValue (new DelegateGenerator (translator, paramType).ExtractGenerated);
......
...@@ -830,7 +830,10 @@ namespace NLua ...@@ -830,7 +830,10 @@ namespace NLua
internal LuaTable GetTable (LuaState luaState, int index) internal LuaTable GetTable (LuaState luaState, int index)
{ {
LuaLib.LuaPushValue (luaState, index); LuaLib.LuaPushValue (luaState, index);
return new LuaTable (LuaLib.LuaRef (luaState, 1), interpreter); int reference = LuaLib.LuaRef (luaState, 1);
if (reference == -1)
return null;
return new LuaTable (reference, interpreter);
} }
/* /*
...@@ -839,7 +842,10 @@ namespace NLua ...@@ -839,7 +842,10 @@ namespace NLua
internal LuaUserData GetUserData (LuaState luaState, int index) internal LuaUserData GetUserData (LuaState luaState, int index)
{ {
LuaLib.LuaPushValue (luaState, index); LuaLib.LuaPushValue (luaState, index);
return new LuaUserData (LuaLib.LuaRef (luaState, 1), interpreter); int reference = LuaLib.LuaRef (luaState, 1);
if (reference == -1)
return null;
return new LuaUserData(reference, interpreter);
} }
/* /*
...@@ -848,7 +854,10 @@ namespace NLua ...@@ -848,7 +854,10 @@ namespace NLua
internal LuaFunction GetFunction (LuaState luaState, int index) internal LuaFunction GetFunction (LuaState luaState, int index)
{ {
LuaLib.LuaPushValue (luaState, index); LuaLib.LuaPushValue (luaState, index);
return new LuaFunction (LuaLib.LuaRef (luaState, 1), interpreter); int reference = LuaLib.LuaRef (luaState, 1);
if (reference == -1)
return null;
return new LuaFunction (reference, interpreter);
} }
/* /*
......
...@@ -153,6 +153,26 @@ namespace NLuaTest ...@@ -153,6 +153,26 @@ namespace NLuaTest
} }
} }
/*
* Tests passing a LuaFunction
*/
[Test]
public void CallLuaFunction()
{
using (Lua lua = new Lua ()) {
lua.DoString ("function someFunc(v1,v2) return v1 + v2 end");
lua ["funcObject"] = lua.GetFunction ("someFunc");
lua.DoString ("luanet.load_assembly('mscorlib')");
lua.DoString ("luanet.load_assembly('NLuaTest')");
lua.DoString ("TestClass=luanet.import_type('NLuaTest.Mock.TestClass')");
lua.DoString ("b = TestClass():TestLuaFunction(funcObject)[0]");
Assert.AreEqual (3, lua ["b"]);
lua.DoString ("a = TestClass():TestLuaFunction(nil)");
Assert.AreEqual (null, lua ["a"]);
}
}
/* /*
* Tests capturing an exception * Tests capturing an exception
*/ */
......
...@@ -374,11 +374,19 @@ namespace NLuaTest.Mock ...@@ -374,11 +374,19 @@ namespace NLuaTest.Mock
set { } set { }
} }
public TimeSpan? NullableMethod(TimeSpan? input) public TimeSpan? NullableMethod (TimeSpan? input)
{ {
return input; return input;
} }
public object[] TestLuaFunction (LuaFunction func)
{
if (func != null) {
return func.Call (1, 2);
}
return null;
}
public int sum (int x, int y) public int sum (int x, int y)
{ {
return x + y; return x + y;
......
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