Skip to content

Commit

Permalink
Merge pull request #529 from stakx/proxygenerationoptions
Browse files Browse the repository at this point in the history
Minimize passing around of `ProxyGenerationOptions`
  • Loading branch information
jonorossi committed Jul 3, 2020
2 parents b6c3be6 + b6ebeda commit a7454e5
Show file tree
Hide file tree
Showing 29 changed files with 105 additions and 139 deletions.
Expand Up @@ -54,7 +54,7 @@ protected override Reference GetTargetReference(ClassEmitter emitter)
return SelfReference.Self;
}

public override void Generate(ClassEmitter @class, ProxyGenerationOptions options)
public override void Generate(ClassEmitter @class)
{
var interceptors = @class.GetField("__interceptors");
#if FEATURE_SERIALIZATION
Expand Down
Expand Up @@ -57,7 +57,6 @@ protected override IEnumerable<MembersCollector> CollectElementsToProxyInternal(
}

protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class,
ProxyGenerationOptions options,
OverrideMethodDelegate overrideMethod)
{
if (methodsToSkip.Contains(method.Method))
Expand All @@ -73,10 +72,10 @@ protected override IEnumerable<MembersCollector> CollectElementsToProxyInternal(

if (ExplicitlyImplementedInterfaceMethod(method))
{
return ExplicitlyImplementedInterfaceMethodGenerator(method, @class, options, overrideMethod);
return ExplicitlyImplementedInterfaceMethodGenerator(method, @class, overrideMethod);
}

var invocation = GetInvocationType(method, @class, options);
var invocation = GetInvocationType(method, @class);

GetTargetExpressionDelegate getTargetTypeExpression = (c, m) => new TypeTokenExpression(targetType);

Expand All @@ -89,22 +88,22 @@ protected override IEnumerable<MembersCollector> CollectElementsToProxyInternal(
null);
}

private Type BuildInvocationType(MetaMethod method, ClassEmitter @class, ProxyGenerationOptions options)
private Type BuildInvocationType(MetaMethod method, ClassEmitter @class)
{
var methodInfo = method.Method;
if (!method.HasTarget)
{
return new InheritanceInvocationTypeGenerator(targetType,
method,
null, null)
.Generate(@class, options, namingScope)
.Generate(@class, namingScope)
.BuildType();
}
var callback = CreateCallbackMethod(@class, methodInfo, method.MethodOnTarget);
return new InheritanceInvocationTypeGenerator(callback.DeclaringType,
method,
callback, null)
.Generate(@class, options, namingScope)
.Generate(@class, namingScope)
.BuildType();
}

Expand Down Expand Up @@ -141,13 +140,12 @@ private bool ExplicitlyImplementedInterfaceMethod(MetaMethod method)
}

private MethodGenerator ExplicitlyImplementedInterfaceMethodGenerator(MetaMethod method, ClassEmitter @class,
ProxyGenerationOptions options,
OverrideMethodDelegate overrideMethod)
{
var @delegate = GetDelegateType(method, @class, options);
var @delegate = GetDelegateType(method, @class);
var contributor = GetContributor(@delegate, method);
var invocation = new InheritanceInvocationTypeGenerator(targetType, method, null, contributor)
.Generate(@class, options, namingScope)
.Generate(@class, namingScope)
.BuildType();
return new MethodWithInvocationGenerator(method,
@class.GetField("__interceptors"),
Expand All @@ -168,7 +166,7 @@ private IInvocationCreationContributor GetContributor(Type @delegate, MetaMethod
new FieldReference(InvocationMethods.ProxyObject));
}

private Type GetDelegateType(MetaMethod method, ClassEmitter @class, ProxyGenerationOptions options)
private Type GetDelegateType(MetaMethod method, ClassEmitter @class)
{
var scope = @class.ModuleScope;
var key = new CacheKey(
Expand All @@ -181,14 +179,14 @@ private Type GetDelegateType(MetaMethod method, ClassEmitter @class, ProxyGenera

return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ =>
new DelegateTypeGenerator(method, targetType)
.Generate(@class, options, namingScope)
.Generate(@class, namingScope)
.BuildType());
}

private Type GetInvocationType(MetaMethod method, ClassEmitter @class, ProxyGenerationOptions options)
private Type GetInvocationType(MetaMethod method, ClassEmitter @class)
{
// NOTE: No caching since invocation is tied to this specific proxy type via its invocation method
return BuildInvocationType(method, @class, options);
return BuildInvocationType(method, @class);
}
}
}
Expand Up @@ -56,7 +56,6 @@ protected override IEnumerable<MembersCollector> CollectElementsToProxyInternal(
}

protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class,
ProxyGenerationOptions options,
OverrideMethodDelegate overrideMethod)
{
if (methodsToSkip.Contains(method.Method))
Expand All @@ -72,10 +71,10 @@ protected override IEnumerable<MembersCollector> CollectElementsToProxyInternal(

if (IsDirectlyAccessible(method) == false)
{
return IndirectlyCalledMethodGenerator(method, @class, options, overrideMethod);
return IndirectlyCalledMethodGenerator(method, @class, overrideMethod);
}

var invocation = GetInvocationType(method, @class, options);
var invocation = GetInvocationType(method, @class);

return new MethodWithInvocationGenerator(method,
@class.GetField("__interceptors"),
Expand All @@ -85,22 +84,22 @@ protected override IEnumerable<MembersCollector> CollectElementsToProxyInternal(
null);
}

private Type BuildInvocationType(MetaMethod method, ClassEmitter @class, ProxyGenerationOptions options)
private Type BuildInvocationType(MetaMethod method, ClassEmitter @class)
{
if (!method.HasTarget)
{
return new InheritanceInvocationTypeGenerator(targetType,
method,
null, null)
.Generate(@class, options, namingScope)
.Generate(@class, namingScope)
.BuildType();
}
return new CompositionInvocationTypeGenerator(method.Method.DeclaringType,
method,
method.Method,
false,
null)
.Generate(@class, options, namingScope)
.Generate(@class, namingScope)
.BuildType();
}

Expand All @@ -115,7 +114,7 @@ private IInvocationCreationContributor GetContributor(Type @delegate, MetaMethod
new FieldReference(InvocationMethods.CompositionInvocationTarget));
}

private Type GetDelegateType(MetaMethod method, ClassEmitter @class, ProxyGenerationOptions options)
private Type GetDelegateType(MetaMethod method, ClassEmitter @class)
{
var scope = @class.ModuleScope;
var key = new CacheKey(
Expand All @@ -128,11 +127,11 @@ private Type GetDelegateType(MetaMethod method, ClassEmitter @class, ProxyGenera

return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ =>
new DelegateTypeGenerator(method, targetType)
.Generate(@class, options, namingScope)
.Generate(@class, namingScope)
.BuildType());
}

private Type GetInvocationType(MetaMethod method, ClassEmitter @class, ProxyGenerationOptions options)
private Type GetInvocationType(MetaMethod method, ClassEmitter @class)
{
var scope = @class.ModuleScope;
var invocationInterfaces = new[] { typeof(IInvocation) };
Expand All @@ -141,17 +140,16 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter @class, ProxyGene

// no locking required as we're already within a lock

return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ => BuildInvocationType(method, @class, options));
return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ => BuildInvocationType(method, @class));
}

private MethodGenerator IndirectlyCalledMethodGenerator(MetaMethod method, ClassEmitter proxy,
ProxyGenerationOptions options,
OverrideMethodDelegate overrideMethod)
{
var @delegate = GetDelegateType(method, proxy, options);
var @delegate = GetDelegateType(method, proxy);
var contributor = GetContributor(@delegate, method);
var invocation = new CompositionInvocationTypeGenerator(targetType, method, null, false, contributor)
.Generate(proxy, options, namingScope)
.Generate(proxy, namingScope)
.BuildType();
return new MethodWithInvocationGenerator(method,
proxy.GetField("__interceptors"),
Expand Down
Expand Up @@ -70,7 +70,7 @@ public void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model)

protected abstract IEnumerable<MembersCollector> CollectElementsToProxyInternal(IProxyGenerationHook hook);

public virtual void Generate(ClassEmitter @class, ProxyGenerationOptions options)
public virtual void Generate(ClassEmitter @class)
{
foreach (var method in methods)
{
Expand All @@ -81,18 +81,17 @@ public virtual void Generate(ClassEmitter @class, ProxyGenerationOptions options

ImplementMethod(method,
@class,
options,
@class.CreateMethod);
}

foreach (var property in properties)
{
ImplementProperty(@class, property, options);
ImplementProperty(@class, property);
}

foreach (var @event in events)
{
ImplementEvent(@class, @event, options);
ImplementEvent(@class, @event);
}
}

Expand All @@ -106,41 +105,40 @@ public void AddInterfaceToProxy(Type @interface)
interfaces.Add(@interface);
}

private void ImplementEvent(ClassEmitter emitter, MetaEvent @event, ProxyGenerationOptions options)
private void ImplementEvent(ClassEmitter emitter, MetaEvent @event)
{
@event.BuildEventEmitter(emitter);
ImplementMethod(@event.Adder, emitter, options, @event.Emitter.CreateAddMethod);
ImplementMethod(@event.Remover, emitter, options, @event.Emitter.CreateRemoveMethod);
ImplementMethod(@event.Adder, emitter, @event.Emitter.CreateAddMethod);
ImplementMethod(@event.Remover, emitter, @event.Emitter.CreateRemoveMethod);
}

private void ImplementProperty(ClassEmitter emitter, MetaProperty property, ProxyGenerationOptions options)
private void ImplementProperty(ClassEmitter emitter, MetaProperty property)
{
property.BuildPropertyEmitter(emitter);
if (property.CanRead)
{
ImplementMethod(property.Getter, emitter, options, property.Emitter.CreateGetMethod);
ImplementMethod(property.Getter, emitter, property.Emitter.CreateGetMethod);
}

if (property.CanWrite)
{
ImplementMethod(property.Setter, emitter, options, property.Emitter.CreateSetMethod);
ImplementMethod(property.Setter, emitter, property.Emitter.CreateSetMethod);
}
}

protected abstract MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class,
ProxyGenerationOptions options,
OverrideMethodDelegate overrideMethod);

private void ImplementMethod(MetaMethod method, ClassEmitter @class, ProxyGenerationOptions options,
private void ImplementMethod(MetaMethod method, ClassEmitter @class,
OverrideMethodDelegate overrideMethod)
{
{
var generator = GetMethodGenerator(method, @class, options, overrideMethod);
var generator = GetMethodGenerator(method, @class, overrideMethod);
if (generator == null)
{
return;
}
var proxyMethod = generator.Generate(@class, options, namingScope);
var proxyMethod = generator.Generate(@class, namingScope);
foreach (var attribute in method.Method.GetNonInheritableAttributes())
{
proxyMethod.DefineCustomAttribute(attribute.Builder);
Expand Down
Expand Up @@ -24,6 +24,6 @@ internal interface ITypeContributor
{
void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model);

void Generate(ClassEmitter @class, ProxyGenerationOptions options);
void Generate(ClassEmitter @class);
}
}
Expand Up @@ -54,7 +54,6 @@ protected virtual MembersCollector GetCollectorForInterface(Type @interface)
}

protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class,
ProxyGenerationOptions options,
OverrideMethodDelegate overrideMethod)
{
if (!method.Proxyable)
Expand All @@ -64,7 +63,7 @@ protected virtual MembersCollector GetCollectorForInterface(Type @interface)
(c, m) => c.GetField("__target"));
}

var invocation = GetInvocationType(method, @class, options);
var invocation = GetInvocationType(method, @class);

return new MethodWithInvocationGenerator(method,
@class.GetField("__interceptors"),
Expand All @@ -74,7 +73,7 @@ protected virtual MembersCollector GetCollectorForInterface(Type @interface)
null);
}

private Type GetInvocationType(MetaMethod method, ClassEmitter @class, ProxyGenerationOptions options)
private Type GetInvocationType(MetaMethod method, ClassEmitter @class)
{
var scope = @class.ModuleScope;

Expand All @@ -98,7 +97,7 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter @class, ProxyGene
method.Method,
canChangeTarget,
null)
.Generate(@class, options, namingScope)
.Generate(@class, namingScope)
.BuildType());
}
}
Expand Down
Expand Up @@ -30,15 +30,14 @@ internal class InterfaceProxyWithOptionalTargetContributor : InterfaceProxyWitho
}

protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class,
ProxyGenerationOptions options,
OverrideMethodDelegate overrideMethod)
{
if (!method.Proxyable)
{
return new OptionallyForwardingMethodGenerator(method, overrideMethod, getTargetReference);
}

return base.GetMethodGenerator(method, @class, options, overrideMethod);
return base.GetMethodGenerator(method, @class, overrideMethod);
}
}
}
Expand Up @@ -44,15 +44,14 @@ protected override IEnumerable<MembersCollector> CollectElementsToProxyInternal(
}

protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class,
ProxyGenerationOptions options,
OverrideMethodDelegate overrideMethod)
{
if (!method.Proxyable)
{
return new MinimialisticMethodGenerator(method, overrideMethod);
}

var invocation = GetInvocationType(method, @class, options);
var invocation = GetInvocationType(method, @class);
return new MethodWithInvocationGenerator(method,
@class.GetField("__interceptors"),
invocation,
Expand All @@ -61,7 +60,7 @@ protected override IEnumerable<MembersCollector> CollectElementsToProxyInternal(
null);
}

private Type GetInvocationType(MetaMethod method, ClassEmitter emitter, ProxyGenerationOptions options)
private Type GetInvocationType(MetaMethod method, ClassEmitter emitter)
{
var scope = emitter.ModuleScope;
Type[] invocationInterfaces;
Expand All @@ -83,7 +82,7 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter emitter, ProxyGen
method.Method,
canChangeTarget,
null)
.Generate(emitter, options, namingScope)
.Generate(emitter, namingScope)
.BuildType());
}
}
Expand Down

0 comments on commit a7454e5

Please sign in to comment.