Skip to content

Commit

Permalink
Avoid generating constructor-linking code for static constructors. Fi…
Browse files Browse the repository at this point in the history
…xes issue #14.
  • Loading branch information
ssimek committed May 24, 2010
1 parent d0ef2c8 commit ea3f501
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
56 changes: 56 additions & 0 deletions Examples/Bugs/X14_StaticConstructor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2010, Stefan Simek
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/

// Google Code Issue 14: static ctor generated badly
// Reported by royosherove, May 12, 2010

using System;
using System.Collections.Generic;
using System.Text;

namespace TriAxis.RunSharp.Examples.Bugs
{
static class X14_StaticConstructor
{
public static void GenStaticCtor(AssemblyGen ag)
{
TypeGen Test = ag.Class("Test");
{
FieldGen a = Test.Static.Field(typeof(int), "a");

CodeGen g = Test.StaticConstructor();
{
g.WriteLine("Hello from .cctor!");
g.Assign(a, 3);
}

g = Test.Static.Method(typeof(void), "Main");
{
g.Invoke(typeof(System.Diagnostics.Debug), "Assert", a == 3);
g.WriteLine(".cctor works now...");
}
}
}
}
}
1 change: 1 addition & 0 deletions Examples/Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Compile Include="A2_Attributes.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Bugs\X14_StaticConstructor.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Bugs\X1864084_ValueTypeVirtual.cs" />
Expand Down
5 changes: 5 additions & 0 deletions RunSharp/CodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ internal CodeGen(ICodeGenContext context)
{
this.context = context;
this.cg = context as ConstructorGen;

if (cg != null && cg.IsStatic)
// #14 - cg is relevant for instance constructors - it wreaks havoc in a static constructor
cg = null;

il = context.GetILGenerator();
}

Expand Down
4 changes: 2 additions & 2 deletions RunSharp/Properties/Messages.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion RunSharp/Properties/Messages.resx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
<value>Specific chained constructor can be invoked only as the first statement in a constructor</value>
</data>
<data name="ErrConstructorOnlyCall" xml:space="preserve">
<value>This method can be called only for constructor code</value>
<value>This method can be called only for instance constructor code</value>
</data>
<data name="ErrCustomEventFieldAccess" xml:space="preserve">
<value>Event with custom accessors cannot be accessed as a field</value>
Expand Down

0 comments on commit ea3f501

Please sign in to comment.