Commit 7dbad8a0 authored by Vinicius Jarina's avatar Vinicius Jarina
Browse files

Fixed #328 Non-existent member access always returns "invalid method" function

parent 7ff6e7c5
......@@ -216,7 +216,7 @@ namespace NLua
/// <summary>
/// The maximum number of recursive steps to take when adding global reference variables. Defaults to 2.
/// </summary>
public int MaximumRecursion { get; set;} = 2;
public int MaximumRecursion { get; set; } = 2;
#region Globals auto-complete
/// <summary>
......
......@@ -33,7 +33,6 @@ namespace NLua
public static readonly LuaNativeFunction CallConstructorFunction = CallConstructor;
public static readonly LuaNativeFunction ToStringFunction = ToStringLua;
public static readonly LuaNativeFunction CallDelegateFunction = CallDelegate;
public static readonly LuaNativeFunction CallInvalidFunction = CallInvalidMethod;
public static readonly LuaNativeFunction AddFunction = AddLua;
public static readonly LuaNativeFunction SubtractFunction = SubtractLua;
......@@ -372,11 +371,9 @@ namespace NLua
private int PushInvalidMethodCall(LuaState luaState, Type type, string name)
{
var invokeDelegate = CallInvalidFunction;
SetMemberCache(type, name, null);
SetMemberCache(type, name, invokeDelegate);
_translator.PushFunction(luaState, invokeDelegate);
_translator.Push(luaState, null);
_translator.Push(luaState, false);
return 2;
}
......@@ -1192,21 +1189,6 @@ namespace NLua
return result;
}
/*
* LuaNativeFunction called when the method wasn't found
*/
#if __IOS__ || __TVOS__ || __WATCHOS__
[MonoPInvokeCallback(typeof(LuaNativeFunction))]
#endif
static int CallInvalidMethod(IntPtr state)
{
var luaState = LuaState.FromIntPtr(state);
var translator = ObjectTranslatorPool.Instance.Find(luaState);
translator.ThrowError(luaState, "Trying to invoke invalid method or an access an invalid index");
luaState.Error();
return 1;
}
int CallDelegateInternal(LuaState luaState)
{
var del = _translator.GetRawNetObject(luaState, 1) as Delegate;
......
......@@ -2708,7 +2708,23 @@ namespace NLuaTest
lua["main"] = new Person();
object result = lua.DoString("return main[15]")[0];
object result2 = lua.DoString(
@"
function bar()
if main.foo ~= nil then
return 42
end
return 10
end
return bar()
")[0];
Assert.AreNotEqual(15, result, "#1");
Assert.AreEqual(10, result2, "#2");
Assert.IsNull(result, "#3");
}
[Test]
......@@ -2812,13 +2828,13 @@ namespace NLuaTest
lua["myTc"] = tc;
if(maxRecursion == 0)
Assert.AreEqual(1,lua.Globals.Count()); //register only the root reference
Assert.AreEqual(1,lua.Globals.Count(), "#1"); //register only the root reference
else
Assert.IsTrue(lua.Globals.Count() > 1); //many globals registered (all sub properties)
Assert.IsTrue(lua.Globals.Count() > 1, "#1"); //many globals registered (all sub properties)
// ensure even with 0 recursion we can still call properties
object o = lua.DoString(@"return myTc.LongValue")[0];
Assert.AreEqual(5, o);
Assert.AreEqual(5, o, "#2");
}
}
......
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