Skip to content

Commit

Permalink
bug fix of TryFinallyWeaver
Browse files Browse the repository at this point in the history
  • Loading branch information
sagifogel committed Jan 26, 2014
1 parent 2062ec3 commit e8fad3b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 21 deletions.
6 changes: 1 addition & 5 deletions NCop.Aspects/Weaving/EndExceptionBlockMethodScopeWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ namespace NCop.Aspects.Weaving
{
internal class EndExceptionBlockMethodScopeWeaver : IMethodScopeWeaver
{
private readonly Label endOfExceptionBlockLabel;

internal EndExceptionBlockMethodScopeWeaver(Label endOfExceptionBlockLabel) {
this.endOfExceptionBlockLabel = endOfExceptionBlockLabel;
internal EndExceptionBlockMethodScopeWeaver() {
}

public ILGenerator Weave(ILGenerator ilGenerator) {
ilGenerator.EndExceptionBlock();
ilGenerator.MarkLabel(endOfExceptionBlockLabel);

return ilGenerator;
}
Expand Down
5 changes: 1 addition & 4 deletions NCop.Aspects/Weaving/FinallyMethodScopeWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ namespace NCop.Aspects.Weaving
{
internal class FinallyMethodScopeWeaver : IMethodScopeWeaver
{
private readonly Label endOfExceptionBlockLabel;
private readonly MethodScopeWeaversQueue finallyWeaversQueue = null;

internal FinallyMethodScopeWeaver(IEnumerable<IMethodScopeWeaver> finallyWeavers, Label endOfExceptionBlockLabel) {
this.endOfExceptionBlockLabel = endOfExceptionBlockLabel;
internal FinallyMethodScopeWeaver(IEnumerable<IMethodScopeWeaver> finallyWeavers) {
finallyWeaversQueue = new MethodScopeWeaversQueue(finallyWeavers);
}

public ILGenerator Weave(ILGenerator ilGenerator) {
ilGenerator.BeginFinallyBlock();
finallyWeaversQueue.Weave(ilGenerator);
ilGenerator.Emit(OpCodes.Leave_S, endOfExceptionBlockLabel);

return ilGenerator;
}
Expand Down
7 changes: 0 additions & 7 deletions NCop.Aspects/Weaving/LocalBuilderRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ namespace NCop.Aspects.Weaving
{
public class LocalBuilderRepository : ILocalBuilderRepository
{
private int id = 0;
private static int count = 0;
private readonly Dictionary<Type, LocalBuilder> localBuilderMap = null;

internal LocalBuilderRepository() {
id = Interlocked.Increment(ref count);
localBuilderMap = new Dictionary<Type, LocalBuilder>();
}

Expand All @@ -40,9 +37,5 @@ public LocalBuilder GetOrDeclare(Type type, Func<LocalBuilder> localBuilderFacto

return localBuilder;
}

public override string ToString() {
return id.ToString();
}
}
}
5 changes: 2 additions & 3 deletions NCop.Aspects/Weaving/TryFinallyAspectWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ public TryFinallyAspectWeaver(IMethodScopeWeaver entryWeaver, IEnumerable<IMetho
public virtual ILGenerator Weave(ILGenerator ilGenerator) {
var weavers = new List<IMethodScopeWeaver>();
MethodScopeWeaversQueue methodScopeWeaversQueue = null;
var endOfExceptionBlockLabel = ilGenerator.DefineLabel();

weavers.Add(entryWeaver);
weavers.Add(new BeginExceptionBlockMethodScopeWeaver());
weavers.AddRange(tryWeavers);
weavers.Add(new FinallyMethodScopeWeaver(finallyWeavers, endOfExceptionBlockLabel));
weavers.Add(new EndExceptionBlockMethodScopeWeaver(endOfExceptionBlockLabel));
weavers.Add(new FinallyMethodScopeWeaver(finallyWeavers));
weavers.Add(new EndExceptionBlockMethodScopeWeaver());

if (returnValueWeaver.IsNotNull()) {
weavers.Add(returnValueWeaver);
Expand Down
2 changes: 1 addition & 1 deletion NCop.Mixins/Weaving/MixinsWeaverStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace NCop.Mixins.Weaving
internal class MixinsWeaverStrategy : ITypeWeaver
{
private readonly IRegistry registry = null;
private readonly IEnumerable<IMethodWeaver> methodWeavers = null;
private readonly ITypeDefinition typeDefinition = null;
private readonly IEnumerable<IMethodWeaver> methodWeavers = null;

internal MixinsWeaverStrategy(ITypeDefinition typeDefinition, IEnumerable<IMethodWeaver> methodWeavers, IRegistry registry) {
this.registry = registry;
Expand Down
48 changes: 47 additions & 1 deletion NCop.Samples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ namespace NCop.Samples
{
internal static class Aspects
{
public static CSharpDeveloperMixin mixin = null;
public static TraceAspect traceAspect = null;
public static TraceAspect2 traceAspect2 = null;
public static TraceAspect3 traceAspect3 = null;

static Aspects() {
mixin = new CSharpDeveloperMixin();
traceAspect = new TraceAspect();
traceAspect2 = new TraceAspect2();
traceAspect3 = new TraceAspect3();
Expand Down Expand Up @@ -157,7 +159,7 @@ public Person() {
public string Code(string sagi) {
var aspectArgs = new FunctionExecutionArgsImpl<CSharpDeveloperMixin, string, string>(developer, sagi);
Aspects.traceAspect3.OnEntry(aspectArgs);

try {
aspectArgs.ReturnValue = developer.Code(aspectArgs.Arg1);
Aspects.traceAspect3.OnSuccess(aspectArgs);
Expand Down Expand Up @@ -188,7 +190,51 @@ public interface IPersonComposite : IDeveloper<ILanguage>

class Program
{
public static void Create() {
AppDomain current = AppDomain.CurrentDomain;
AssemblyName myAsmName = new AssemblyName { Name = "AdderExceptionAsm" };
AssemblyBuilder myAsmBldr = current.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.RunAndSave);
ModuleBuilder myModBldr = myAsmBldr.DefineDynamicModule(myAsmName.Name, myAsmName.Name + ".dll");
TypeBuilder myTypeBldr = myModBldr.DefineType("Adder");
Type[] adderParams = new Type[] { typeof(int), typeof(int) };
MethodBuilder adderBldr = myTypeBldr.DefineMethod("DoAdd", MethodAttributes.Public | MethodAttributes.Static, typeof(string), Type.EmptyTypes);
ILGenerator ilGen = adderBldr.GetILGenerator();
var typeofFunc = typeof(FunctionExecutionArgsImpl<CSharpDeveloperMixin, string, string>);
var typeofAspects = typeof(TraceAspect3);
var local0 = ilGen.DeclareLocal(typeofFunc);

ilGen.Emit(OpCodes.Ldsfld, typeof(Aspects).GetField("mixin"));
ilGen.Emit(OpCodes.Ldarg_1);
ilGen.Emit(OpCodes.Newobj, typeofFunc.GetConstructors()[0]);
ilGen.Emit(OpCodes.Stloc_0);
ilGen.Emit(OpCodes.Ldsfld, typeof(Aspects).GetField("traceAspect3"));
ilGen.Emit(OpCodes.Ldloc_0);
ilGen.Emit(OpCodes.Callvirt, typeofAspects.GetMethod("OnEntry"));
ilGen.BeginExceptionBlock();
ilGen.Emit(OpCodes.Ldloc_0);
ilGen.Emit(OpCodes.Ldsfld, typeof(Aspects).GetField("mixin"));
ilGen.Emit(OpCodes.Ldloc_0);
ilGen.Emit(OpCodes.Callvirt, typeofFunc.GetMethod("get_Arg1"));
ilGen.Emit(OpCodes.Callvirt, typeof(CSharpDeveloperMixin).GetMethod("Code"));
ilGen.Emit(OpCodes.Callvirt, typeofFunc.GetMethod("set_ReturnValue"));
ilGen.Emit(OpCodes.Ldsfld, typeof(Aspects).GetField("traceAspect3"));
ilGen.Emit(OpCodes.Ldloc_0);
ilGen.Emit(OpCodes.Callvirt, typeofAspects.GetMethod("OnSuccess"));
ilGen.BeginFinallyBlock();
ilGen.Emit(OpCodes.Ldsfld, typeof(Aspects).GetField("traceAspect3"));
ilGen.Emit(OpCodes.Ldloc_0);
ilGen.Emit(OpCodes.Callvirt, typeofAspects.GetMethod("OnExit"));
ilGen.EndExceptionBlock();
ilGen.Emit(OpCodes.Ldloc_0);
ilGen.Emit(OpCodes.Callvirt, typeofFunc.GetMethod("get_ReturnValue"));
ilGen.Emit(OpCodes.Ret);
Type adderType = myTypeBldr.CreateType();

myAsmBldr.Save("s123.dll");
}

static void Main(string[] args) {
Create();
//new Person().Code(""); return;
var container = new CompositeContainer();
container.Configure();
Expand Down

0 comments on commit e8fad3b

Please sign in to comment.