Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

[Interpreter] Static constructors and field access #8370

Merged
merged 27 commits into from
Oct 30, 2020

Conversation

tonerdo
Copy link
Contributor

@tonerdo tonerdo commented Oct 22, 2020

Opening this here, since I don't yet have a branch in the runtimelabs repo.

This PR adds support for instance and static fields on a reference type. It interprets the following opcodes:

  • ldfld
  • ldsfld
  • stfld
  • stsfld

As a consequence of adding static field support, I also added support for interpreting static constructors on dynamically loaded types. The following scenario is now supported:

class DynamicClass1
{
    public string Field1;
    public int Field2;
    public float Field3;
    public Random Field4 = new Random();

    public static string StaticField1;
    public static int StaticField2;
    public static float StaticField3;

    static DynamicClass1()
    {
        StaticField1 = "statically nice!";
        StaticField2 = 10;
        StaticField3 = 3.56f;
    }
}

.....

public static void DoFields()
{
    DynamicClass1 class1 = new DynamicClass1();
    class1.Field1 = "nice";
    class1.Field2 = 23;
    class1.Field3 = 2.5f;

    Console.WriteLine(class1.Field1);
    Console.WriteLine(class1.Field2);
    Console.WriteLine(class1.Field3);
    Console.WriteLine(class1.Field4);
}

public static void DoStaticFields()
{
    Console.WriteLine(DynamicClass1.StaticField1);
    Console.WriteLine(string.Empty);
    Console.WriteLine(DynamicClass1.StaticField2);
    Console.WriteLine(DynamicClass1.StaticField3);
}

The interpreter will happily load instance and static fields on both dynamically loaded types and types compiled into the native executable, while ensuring that static constructors have been run in both cases.

Note: Thread statics on dynamically loaded types are currently unsupported

cc @jkotas @MichalStrehovsky

@tonerdo
Copy link
Contributor Author

tonerdo commented Oct 30, 2020

I'm guessing this won't be able to make the cut before the archive?

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@jkotas jkotas merged commit cb82940 into dotnet:master Oct 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants