Skip to content

Commit

Permalink
Merge pull request #1 from RoosterDragon/cache-metamethods
Browse files Browse the repository at this point in the history
Cache metamethods, finalize references
  • Loading branch information
Mailaender committed Nov 27, 2015
2 parents 483f326 + 46d53a8 commit 2630107
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 10 deletions.
15 changes: 14 additions & 1 deletion Eluant/LuaClrObjectValue.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//
// LuaClrObjectValue.cs
//
// Author:
// Authors:
// Chris Howie <[email protected]>
// Tom Roostan <[email protected]>
//
// Copyright (c) 2013 Chris Howie
// Copyright (c) 2015 Tom Roostan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,6 +27,8 @@
// THE SOFTWARE.

using System;
using System.Linq;
using Eluant.ObjectBinding;

namespace Eluant
{
Expand Down Expand Up @@ -54,6 +58,15 @@ public override string ToString()

internal abstract object BackingCustomObject { get; }

internal abstract MetamethodAttribute[] BackingCustomObjectMetamethods { get; }

static internal MetamethodAttribute[] Metamethods(Type backingCustomObjectType)
{
return backingCustomObjectType.GetInterfaces()
.SelectMany(iface => iface.GetCustomAttributes(typeof(MetamethodAttribute), false).Cast<MetamethodAttribute>())
.ToArray();
}

internal override object ToClrType(Type type)
{
if (type == null) { throw new ArgumentNullException("type"); }
Expand Down
12 changes: 11 additions & 1 deletion Eluant/LuaCustomClrObject.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//
// LuaCustomClrObject.cs
//
// Author:
// Authors:
// Chris Howie <[email protected]>
// Tom Roostan <[email protected]>
//
// Copyright (c) 2013 Chris Howie
// Copyright (c) 2015 Tom Roostan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,6 +27,7 @@
// THE SOFTWARE.

using System;
using Eluant.ObjectBinding;

namespace Eluant
{
Expand All @@ -51,6 +54,13 @@ internal override object BackingCustomObject
{
get { return ClrObject; }
}

private MetamethodAttribute[] metamethods;

internal override MetamethodAttribute[] BackingCustomObjectMetamethods
{
get { return metamethods ?? (metamethods = Metamethods(BackingCustomObject.GetType())); }
}
}
}

10 changes: 9 additions & 1 deletion Eluant/LuaOpaqueClrObject.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//
// LuaOpaqueClrObject.cs
//
// Author:
// Authors:
// Chris Howie <[email protected]>
// Tom Roostan <[email protected]>
//
// Copyright (c) 2013 Chris Howie
// Copyright (c) 2015 Tom Roostan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,6 +27,7 @@
// THE SOFTWARE.

using System;
using Eluant.ObjectBinding;

namespace Eluant
{
Expand All @@ -51,6 +54,11 @@ internal override object BackingCustomObject
{
get { return null; }
}

internal override MetamethodAttribute[] BackingCustomObjectMetamethods
{
get { return null; }
}
}
}

5 changes: 4 additions & 1 deletion Eluant/LuaReference.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//
// LuaReference.cs
//
// Author:
// Authors:
// Chris Howie <[email protected]>
// Tom Roostan <[email protected]>
//
// Copyright (c) 2013 Chris Howie
// Copyright (c) 2015 Tom Roostan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -50,6 +52,7 @@ internal LuaReference(LuaRuntime runtime, int reference)
public sealed override void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
Expand Down
9 changes: 4 additions & 5 deletions Eluant/LuaRuntime.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//
// LuaRuntime.cs
//
// Author:
// Authors:
// Chris Howie <[email protected]>
// Tom Roostan <[email protected]>
//
// Copyright (c) 2013 Chris Howie
// Copyright (c) 2015 Tom Roostan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -776,10 +778,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.
var metamethods = obj.BackingCustomObject.GetType().GetInterfaces()
.SelectMany(iface => iface.GetCustomAttributes(typeof(MetamethodAttribute), false).Cast<MetamethodAttribute>());

foreach (var metamethod in metamethods) {
foreach (var metamethod in obj.BackingCustomObjectMetamethods) {
LuaApi.lua_pushstring(LuaState, metamethod.MethodName);
Push(metamethodCallbacks[metamethod.MethodName]);
LuaApi.lua_settable(LuaState, -3);
Expand Down
11 changes: 10 additions & 1 deletion Eluant/LuaTransparentClrObject.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//
// LuaTransparentClrObject.cs
//
// Author:
// Authors:
// Chris Howie <[email protected]>
// Tom Roostan <[email protected]>
//
// Copyright (c) 2013 Chris Howie
// Copyright (c) 2015 Tom Roostan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -71,8 +73,15 @@ internal override object BackingCustomObject
get { return proxy; }
}

internal override MetamethodAttribute[] BackingCustomObjectMetamethods
{
get { return TransparentClrObjectProxy.Metamethods; }
}

private class TransparentClrObjectProxy : ILuaTableBinding, ILuaEqualityBinding
{
public static readonly MetamethodAttribute[] Metamethods = LuaClrObjectValue.Metamethods(typeof(TransparentClrObjectProxy));

private LuaTransparentClrObject clrObject;

public TransparentClrObjectProxy(LuaTransparentClrObject obj)
Expand Down

0 comments on commit 2630107

Please sign in to comment.