diff --git a/Eluant/LuaRuntime.cs b/Eluant/LuaRuntime.cs index f215294..1b6d219 100644 --- a/Eluant/LuaRuntime.cs +++ b/Eluant/LuaRuntime.cs @@ -212,6 +212,7 @@ private void Initialize() metamethodCallbacks["__newindex"] = CreateCallbackWrapper(NewindexCallback); metamethodCallbacks["__index"] = CreateCallbackWrapper(IndexCallback); + metamethodCallbacks["__tostring"] = CreateCallbackWrapper(ToStringCallback); metamethodCallbacks["__add"] = CreateCallbackWrapper(state => BinaryOperatorCallback(state, (i, a, b) => i.Add(this, a, b))); metamethodCallbacks["__sub"] = CreateCallbackWrapper(state => BinaryOperatorCallback(state, (i, a, b) => i.Subtract(this, a, b))); @@ -835,6 +836,24 @@ private int IndexCallback(IntPtr state) }); } + private int ToStringCallback(IntPtr state) + { + return LuaToClrBoundary(state, toDispose => { + var obj = GetClrObject(1).BackingCustomObject as ILuaToStringBinding; + + if (obj == null) { + throw new LuaException("CLR object does not support indexing."); + } + + var value = obj.ToString(this); + toDispose.Add(value); + + Push(value); + + return 1; + }); + } + private int CallCallback(IntPtr state) { return LuaToClrBoundary(state, toDispose => { diff --git a/Eluant/ObjectBinding/Bindings.cs b/Eluant/ObjectBinding/Bindings.cs index 36bdaf2..949e524 100644 --- a/Eluant/ObjectBinding/Bindings.cs +++ b/Eluant/ObjectBinding/Bindings.cs @@ -41,6 +41,12 @@ public interface ILuaTableBinding LuaValue this[LuaRuntime runtime, LuaValue key] { get; set; } } + [Metamethod("__tostring")] + public interface ILuaToStringBinding + { + LuaValue ToString(LuaRuntime runtime); + } + [Metamethod("__add")] public interface ILuaAdditionBinding {