kekyo / IL2C

IL2C - A translator for ECMA-335 CIL/MSIL to C language.
Apache License 2.0
401 stars 36 forks source link

static constructors are ommited to process alltogether #98

Closed cyborgyn closed 2 years ago

cyborgyn commented 2 years ago

In the following static class initialization:

    static class Foo
    {
        public static string Bar;
        static Foo()
        {
            Bar = "foobar";
        }
    }

Foo.Bar will never contain a value, the string "foobar" is not enlisted between constant strings, and related to #97 issue, no static constructor is generated, but called.

cyborgyn commented 2 years ago

Actually, it seems to be that easy to fix, as the following patch suggests. Is there a not so obvious reason why static constructors were filtered out during C code generation?

diff --git "a/IL2C.Core/AssemblyPreparer.cs" "b/IL2C.Core/AssemblyPreparer.cs"
index 12389a6..49d824f 100644
--- "a/IL2C.Core/AssemblyPreparer.cs"
+++ "b/IL2C.Core/AssemblyPreparer.cs"
@@ -322,7 +322,7 @@ namespace IL2C
                 // All types
                 type => true,
                 // The methods except type initializer.
-                method => !(method.IsConstructor && method.IsStatic));
+                method => true);
         }
     }
 }
kekyo commented 2 years ago

@cyborgyn Sorry for the very delay. I understood #100 work and imported it with cherry-pick. 2cbb6f1da780acb289fb9f98d7b4adda99fd06c3

For the life of me, I can't remember why I excluded the Type initializer; it may have been due to the fact that I had not checked the initialization order of the Type initializer. (I still have not been able to confirm this.) If the initialization order is affected by the ECMA335 specification, I may need to rework this area, but for now, I'll leave it at that.