"tests/build/vscode:/vscode.git/clone" did not exist on "019efe77b5412b0d427b11b551cc57a95c03a414"
Commit 5ce70d75 authored by Vinicius Jarina's avatar Vinicius Jarina
Browse files

[NLua] Implemented operator + handling using metatable method __add.

- Missing tests
parent 0a4d984b
......@@ -43,13 +43,16 @@ namespace ConsoleTest
{
lua.DebugHook += DebugHook;
lua.SetDebugHook (NLua.Event.EventMasks.LUA_MASKLINE, 0);
lua.DoString (@"function testing_hooks() return 10 end
val = testing_hooks()
val = val + 1
");
double res = (double)lua ["val"];
Console.WriteLine ("{0}", res);
var a = new System.Numerics.Complex (10, 0);
var b = new System.Numerics.Complex (0, 3);
var x = a + b;
// lua.LoadCLRPackage ();
lua ["a"] = a;
lua ["b"] = 1;
var res = lua.DoString (@"return a + b")[0];
}
......
......@@ -55,6 +55,18 @@ namespace NLua.Extensions
#endif
}
static class TypeExtensions
{
public static bool HasAdditionOpertator (this Type t)
{
if (t.IsPrimitive)
return true;
var op_add = t.GetMethod ("op_Addition");
return op_add != null;
}
}
static class StringExtensions
{
public static IEnumerable<string> SplitWithEscape (this string input, char separator, char escapeCharacter)
......
......@@ -273,8 +273,8 @@ end
public Lua ()
{
luaState = LuaLib.LuaLNewState (); // steffenj: Lua 5.1.1 API change (lua_open is gone)
LuaLib.LuaLOpenLibs (luaState); // steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here)
luaState = LuaLib.LuaLNewState ();
LuaLib.LuaLOpenLibs (luaState);
Init ();
// We need to keep this in a managed reference so the delegate doesn't get garbage collected
panicCallback = new LuaNativeFunction (PanicCallback);
......
......@@ -54,7 +54,7 @@ namespace NLua
public class MetaFunctions
{
internal LuaNativeFunction gcFunction, indexFunction, newindexFunction, baseIndexFunction,
classIndexFunction, classNewindexFunction, execDelegateFunction, callConstructorFunction, toStringFunction;
classIndexFunction, classNewindexFunction, execDelegateFunction, callConstructorFunction, toStringFunction, addFunction;
private Dictionary<object, object> memberCache = new Dictionary<object, object> ();
private ObjectTranslator translator;
......@@ -89,7 +89,8 @@ namespace NLua
callConstructorFunction = new LuaNativeFunction (MetaFunctions.CallConstructor);
classIndexFunction = new LuaNativeFunction (MetaFunctions.GetClassMethod);
classNewindexFunction = new LuaNativeFunction (MetaFunctions.SetClassFieldOrProperty);
execDelegateFunction = new LuaNativeFunction (MetaFunctions.RunFunctionDelegate);
execDelegateFunction = new LuaNativeFunction (MetaFunctions.RunFunctionDelegate);
addFunction = new LuaNativeFunction (MetaFunctions.AddLua);
}
/*
......@@ -158,6 +159,42 @@ namespace NLua
LuaLib.LuaPushNil (luaState);
return 1;
}
/*
* __tostring metafunction of CLR objects.
*/
#if MONOTOUCH
[MonoTouch.MonoPInvokeCallback (typeof (LuaNativeFunction))]
#endif
[System.Runtime.InteropServices.AllowReversePInvokeCalls]
static int AddLua (LuaState luaState)
{
var translator = ObjectTranslatorPool.Instance.Find (luaState);
return AddLua (luaState, translator);
}
static int AddLua (LuaState luaState, ObjectTranslator translator)
{
object obj1 = translator.GetRawNetObject (luaState, 1);
object obj2 = translator.GetRawNetObject (luaState, 2);
if (obj1 == null || obj2 == null) {
translator.ThrowError (luaState, "Cannot add a nil object");
LuaLib.LuaPushNil (luaState);
return 1;
}
Type type = obj1.GetType ();
MethodInfo opAddition = type.GetMethod ("op_Addition");
if (opAddition == null) {
translator.ThrowError (luaState, "Cannot add object (" + type.Name+ " does not overload the operator +)");
LuaLib.LuaPushNil (luaState);
return 1;
}
obj1 = opAddition.Invoke (obj1, new object[] { obj1, obj2 });
translator.Push (luaState, obj1);
return 1;
}
......
......@@ -638,7 +638,8 @@ namespace NLua
LuaLib.LuaRawSet (luaState, -3);
LuaLib.LuaPushString (luaState, "__newindex");
LuaLib.LuaPushStdCallCFunction (luaState, metaFunctions.newindexFunction);
LuaLib.LuaRawSet (luaState, -3);
LuaLib.LuaRawSet (luaState, -3);
RegisterOperatorsFunctions (luaState, o.GetType ());
}
} else
LuaLib.LuaLGetMetatable (luaState, metatable);
......@@ -655,6 +656,15 @@ namespace NLua
LuaLib.LuaRemove (luaState, -2);
}
void RegisterOperatorsFunctions (LuaState luaState, Type type)
{
if (type.HasAdditionOpertator ()) {
LuaLib.LuaPushString (luaState, "__add");
LuaLib.LuaPushStdCallCFunction (luaState, metaFunctions.addFunction);
LuaLib.LuaRawSet (luaState, -3);
}
}
/*
* Gets an object from the Lua stack with the desired type, if it matches, otherwise
* returns null.
......
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