Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix try/catch/finally il code generation #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Pose/IL/MethodRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,6 @@ private void EmitILForExceptionHandlers(ILGenerator ilGenerator, Instruction ins
ilGenerator.BeginExceptionBlock();
}

foreach (var handler in handlers.Where(h => h.HandlerStart == instruction.Offset))
{
if (handler.Flag == "Clause")
ilGenerator.BeginCatchBlock(handler.CatchType);
else if (handler.Flag == "Finally")
ilGenerator.BeginFinallyBlock();
}

foreach (var handler in handlers.Where(h => h.HandlerEnd == instruction.Offset))
{
if (handler.Flag == "Clause")
Expand All @@ -190,6 +182,14 @@ private void EmitILForExceptionHandlers(ILGenerator ilGenerator, Instruction ins

ilGenerator.EndExceptionBlock();
}

foreach (var handler in handlers.Where(h => h.HandlerStart == instruction.Offset))
{
if (handler.Flag == "Clause")
ilGenerator.BeginCatchBlock(handler.CatchType);
else if (handler.Flag == "Finally")
ilGenerator.BeginFinallyBlock();
}
}

private void EmitILForInlineNone(ILGenerator ilGenerator, Instruction instruction)
Expand Down
24 changes: 24 additions & 0 deletions test/Pose.Tests/IL/MethodRewriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,29 @@ public void TestConstructorRewrite()
Assert.AreEqual(typeof(void), dynamicMethod.ReturnType);
Assert.AreEqual(typeof(List<string>), dynamicMethod.GetParameters()[0].ParameterType);
}

[TestMethod]
public void TestExceptionHandlersRewrite()
{
MethodInfo methodInfo = typeof(MethodRewriterTests).GetMethod("ExceptionHandlersRewriteMethod");
MethodRewriter methodRewriter = MethodRewriter.CreateRewriter(methodInfo);
DynamicMethod dynamicMethod = methodRewriter.Rewrite() as DynamicMethod;

Delegate func = dynamicMethod.CreateDelegate(typeof(Func<int>));
Assert.AreEqual(1, (int)func.DynamicInvoke());
}

public static int ExceptionHandlersRewriteMethod()
{
try
{
return 1;
}
catch
{
return 0;
}
finally {}
}
}
}