Commit 27e4f04c authored by Vinicius Jarina's avatar Vinicius Jarina
Browse files

Fixed call delegate functions.

Automatically register metamethod __call for delegate types.
+ Unit tests.
parent d7f1849b
......@@ -10,14 +10,17 @@ using NLuaTest;
namespace ConsoleTest
{
public class Program
{
static void Main (string [] args)
{
Core c = new Core ();
c.Setup ();
c.Sieve ();
using (var l = new Lua ()) {
Action c = () => { Console.WriteLine ("Ola"); };
l ["d"] = c;
l.DoString (" d () ");
}
}
}
}
This diff is collapsed.
......@@ -571,6 +571,7 @@ namespace NLua
PushObject (luaState, func, "luaNet_function");
}
/*
* Pushes a CLR object into the Lua stack as an userdata
* with the provided metatable
......@@ -651,6 +652,7 @@ namespace NLua
LuaLib.LuaRawSet (luaState, -3);
// Bind C# operator with Lua metamethods (__add, __sub, __mul)
RegisterOperatorsFunctions (luaState, o.GetType ());
RegisterCallMethodForDelegate (luaState, o);
}
} else
LuaLib.LuaLGetMetatable (luaState, metatable);
......@@ -667,6 +669,16 @@ namespace NLua
LuaLib.LuaRemove (luaState, -2);
}
void RegisterCallMethodForDelegate (LuaState luaState, object o)
{
if (!(o is Delegate))
return;
LuaLib.LuaPushString (luaState, "__call");
LuaLib.LuaPushStdCallCFunction (luaState, metaFunctions.CallDelegateFunction);
LuaLib.LuaRawSet (luaState, -3);
}
void RegisterOperatorsFunctions (LuaState luaState, Type type)
{
if (type.HasAdditionOpertator ()) {
......@@ -930,8 +942,8 @@ namespace NLua
if (o == null)
LuaLib.LuaPushNil (luaState);
else if (o is sbyte || o is byte || o is short || o is ushort ||
o is int || o is uint || o is long || o is float ||
o is ulong || o is decimal || o is double) {
o is int || o is uint || o is long || o is float ||
o is ulong || o is decimal || o is double) {
double d = Convert.ToDouble (o);
LuaLib.LuaPushNumber (luaState, d);
} else if (o is char) {
......
This diff is collapsed.
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