Skip to content

Commit

Permalink
Merge pull request #2 from RoosterDragon/runtime-metamethod-cache
Browse files Browse the repository at this point in the history
Cache metamethods in each LuaRuntime.
  • Loading branch information
Mailaender committed Jan 24, 2016
2 parents 2630107 + 2c0ef03 commit ca89baa
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Eluant/LuaClrObjectValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public override string ToString()

internal abstract object BackingCustomObject { get; }

internal abstract MetamethodAttribute[] BackingCustomObjectMetamethods { get; }
internal abstract MetamethodAttribute[] BackingCustomObjectMetamethods(LuaRuntime runtime);

static internal MetamethodAttribute[] Metamethods(Type backingCustomObjectType)
{
Expand Down
4 changes: 2 additions & 2 deletions Eluant/LuaCustomClrObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ internal override object BackingCustomObject

private MetamethodAttribute[] metamethods;

internal override MetamethodAttribute[] BackingCustomObjectMetamethods
internal override MetamethodAttribute[] BackingCustomObjectMetamethods(LuaRuntime runtime)
{
get { return metamethods ?? (metamethods = Metamethods(BackingCustomObject.GetType())); }
return metamethods ?? (metamethods = runtime.CachedMetamethods(BackingCustomObject.GetType()));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Eluant/LuaOpaqueClrObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ internal override object BackingCustomObject
get { return null; }
}

internal override MetamethodAttribute[] BackingCustomObjectMetamethods
internal override MetamethodAttribute[] BackingCustomObjectMetamethods(LuaRuntime runtime)
{
get { return null; }
return null;
}
}
}
Expand Down
16 changes: 15 additions & 1 deletion Eluant/LuaRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ protected GCHandle SelfHandle
private const string OPAQUECLROBJECT_METATABLE = "eluant_opaqueclrobject";

private Dictionary<string, LuaFunction> metamethodCallbacks = new Dictionary<string, LuaFunction>();
private Dictionary<Type, MetamethodAttribute[]> metamethodAttributes = new Dictionary<Type, MetamethodAttribute[]>();

private LuaFunction createManagedCallWrapper;

Expand Down Expand Up @@ -778,7 +779,7 @@ internal void PushCustomClrObject(LuaClrObjectValue obj)
LuaApi.lua_settable(LuaState, -3);

// For all others, we use MetamethodAttribute on the interface to make this code less repetitive.
foreach (var metamethod in obj.BackingCustomObjectMetamethods) {
foreach (var metamethod in obj.BackingCustomObjectMetamethods(this)) {
LuaApi.lua_pushstring(LuaState, metamethod.MethodName);
Push(metamethodCallbacks[metamethod.MethodName]);
LuaApi.lua_settable(LuaState, -3);
Expand All @@ -791,6 +792,19 @@ internal void PushCustomClrObject(LuaClrObjectValue obj)
}
}

internal MetamethodAttribute[] CachedMetamethods(Type backingCustomObjectType)
{
MetamethodAttribute[] metamethods;
if (metamethodAttributes.TryGetValue(backingCustomObjectType, out metamethods)) {
return metamethods;
}

metamethods = LuaClrObjectValue.Metamethods(backingCustomObjectType);
metamethodAttributes.Add(backingCustomObjectType, metamethods);

return metamethods;
}

private int NewindexCallback(IntPtr state)
{
return LuaToClrBoundary(state, toDispose => {
Expand Down
4 changes: 2 additions & 2 deletions Eluant/LuaTransparentClrObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ internal override object BackingCustomObject
get { return proxy; }
}

internal override MetamethodAttribute[] BackingCustomObjectMetamethods
internal override MetamethodAttribute[] BackingCustomObjectMetamethods(LuaRuntime runtime)
{
get { return TransparentClrObjectProxy.Metamethods; }
return TransparentClrObjectProxy.Metamethods;
}

private class TransparentClrObjectProxy : ILuaTableBinding, ILuaEqualityBinding
Expand Down

0 comments on commit ca89baa

Please sign in to comment.