Skip to content

Commit

Permalink
Avoid calling ILGenerator.Begin/EndScope on dynamic methods. Fixes is…
Browse files Browse the repository at this point in the history
…sue #10.
  • Loading branch information
ssimek committed Aug 22, 2010
1 parent 5ce497c commit 37f117f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
13 changes: 12 additions & 1 deletion Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,18 @@ static void Main(string[] args)
static void DynamicMethodExamples()
{
DynamicMethodGen dmg = DynamicMethodGen.Static(typeof(Program)).Method(typeof(void)).Parameter(typeof(string), "name");
dmg.GetCode().WriteLine("Hello {0}!", dmg.GetCode().Arg("name"));
CodeGen g = dmg.GetCode();
g.Try();
{
Operand name = g.Local(typeof(string), g.Arg("name"));
g.WriteLine("Hello {0}!", name);
}
g.CatchAll();
{
g.WriteLine("Error");
}
g.End();

DynamicMethod dm = dmg.GetCompletedDynamicMethod(true);

// reflection-style invocation
Expand Down
6 changes: 4 additions & 2 deletions RunSharp/CodeGen.Statements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,8 @@ public void EnsureScope()
{
if (!hasScope)
{
g.il.BeginScope();
if (g.context.SupportsScopes)
g.il.BeginScope();
hasScope = true;
}
}
Expand All @@ -624,7 +625,8 @@ protected void EndScope()
{
if (hasScope)
{
g.il.EndScope();
if (g.context.SupportsScopes)
g.il.EndScope();
hasScope = false;
}
}
Expand Down
1 change: 1 addition & 0 deletions RunSharp/CodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ interface ICodeGenContext : IMemberInfo, ISignatureGen, IDelayedDefinition, IDel
ILGenerator GetILGenerator();

Type OwnerType { get; }
bool SupportsScopes { get; }
}

public partial class CodeGen
Expand Down
11 changes: 10 additions & 1 deletion RunSharp/DynamicMethodGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace TriAxis.RunSharp
{
public sealed class DynamicMethodGen : RoutineGen<DynamicMethodGen>
public sealed class DynamicMethodGen : RoutineGen<DynamicMethodGen>, ICodeGenContext
{
Attributes attrs;
DynamicMethod dm;
Expand Down Expand Up @@ -173,5 +173,14 @@ protected override void SetCustomAttribute(CustomAttributeBuilder cab)
}

#endregion

#region ICodeGenContext Members

bool ICodeGenContext.SupportsScopes
{
get { return false; }
}

#endregion
}
}
9 changes: 9 additions & 0 deletions RunSharp/RoutineGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,14 @@ ILGenerator ICodeGenContext.GetILGenerator()
}

#endregion

#region ICodeGenContext Members

bool ICodeGenContext.SupportsScopes
{
get { return true; }
}

#endregion
}
}

0 comments on commit 37f117f

Please sign in to comment.