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