Unverified Commit 9df4990c authored by dcronqvist's avatar dcronqvist Committed by GitHub
Browse files

Registering functions now only register 1 global, fix #480 (#481)

* Registering functions now only register 1 global, fix #480

* Changed `is not null` to `!= null` for backwards compatibility

* Use `IsSubclassOf` instead of newer `IsAssignableTo`

* Explicit type for anonymous function in unit test, for pre-C#10 compatibility

* Only include test on `NETCOREAPP3_1_OR_GREATER`, added comment for test as well

* Removed `NETCOREAPP3_1_OR_GREATER` check because it is unnecessary
parent 9544136c
......@@ -654,9 +654,18 @@ namespace NLua
}
set
{
if (value != null && value.GetType().IsSubclassOf(typeof(MethodBase)))
{
// If the value is a delegate type, instead treat it as a function
this.RegisterFunction(fullPath, (MethodBase)value);
}
else
{
// Otherwise, treat it as normal
SetObjectToPath(fullPath, value);
}
}
}
/*
* Navigates a table in the top of the stack, returning
......
......@@ -100,7 +100,7 @@ namespace NLua
private void RegisterPath(string path, Type type, int recursionCounter, LuaGlobalEntry entry = null)
{
// If the type is a global method, list it directly
if (type == typeof(LuaFunction))
if (type == typeof(KeraLua.LuaFunction))
{
RegisterLuaFunction(path, entry);
}
......
......@@ -3167,6 +3167,23 @@ namespace NLuaTest
}
}
/*
* Tests registering a global function with RegisterFunction and with the indexer
* Makes sure that the amount of registered globals is correct
*/
[Test]
public void TestAmountOfRegisteredGlobals()
{
using (Lua lua = new Lua())
{
Func<string, string> testFunc = (string s) => s;
lua.RegisterFunction("testFunc1", null, testFunc.Method);
lua["testFunc2"] = testFunc.Method;
Assert.AreEqual(2, lua.Globals.Count());
}
}
[Test]
public void TestGuid()
{
......@@ -3226,8 +3243,6 @@ namespace NLuaTest
}
}
static Lua m_lua;
}
}
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