Skip to content

Commit

Permalink
Add ILuaToStringBinding so that CustomClrObjects can implement __tost…
Browse files Browse the repository at this point in the history
…ring.
  • Loading branch information
pchote committed Apr 26, 2014
1 parent 866eb48 commit 483f326
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Eluant/LuaRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ private void Initialize()

metamethodCallbacks["__newindex"] = CreateCallbackWrapper(NewindexCallback);
metamethodCallbacks["__index"] = CreateCallbackWrapper(IndexCallback);
metamethodCallbacks["__tostring"] = CreateCallbackWrapper(ToStringCallback);

metamethodCallbacks["__add"] = CreateCallbackWrapper(state => BinaryOperatorCallback<ILuaAdditionBinding>(state, (i, a, b) => i.Add(this, a, b)));
metamethodCallbacks["__sub"] = CreateCallbackWrapper(state => BinaryOperatorCallback<ILuaSubtractionBinding>(state, (i, a, b) => i.Subtract(this, a, b)));
Expand Down Expand Up @@ -835,6 +836,24 @@ private int IndexCallback(IntPtr state)
});
}

private int ToStringCallback(IntPtr state)
{
return LuaToClrBoundary(state, toDispose => {
var obj = GetClrObject<LuaClrObjectValue>(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 => {
Expand Down
6 changes: 6 additions & 0 deletions Eluant/ObjectBinding/Bindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 483f326

Please sign in to comment.