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

RuntimeHelpers::InitializeArray isn't handled (used for arrays of >=3 constants) #8

Open
zeromus opened this issue Aug 19, 2016 · 2 comments

Comments

@zeromus
Copy link

zeromus commented Aug 19, 2016

This c# will crash AlterNative: class foo { float[] bar = { 1,2,3 }; };
It's easier to see why that might be when you try this:

class foo {
    void baz() {
        float[] bar = { 1, 2, 3 };
    }
};

which results in

void foo::baz(){
    RuntimeHelpers::InitializeArray(new Array<float>(3), fieldof(<PrivateImplementationDetails>{169D9E6B-C4D0-4476-851A-DD23AA0A2F9B}::$$method0x6000001-1)->FieldHandle);
}

For 1 or 2 elements, the c# compiler will emit array stores. For the 3rd element it begins emitting that ugly hacky approach.

@zeromus
Copy link
Author

zeromus commented Aug 19, 2016

I'm sure you have more experience with this than me, but for the record, I think I would try to implement this with something as follows

Array<float> tmp1 = new Array<float>(3);
static const byte rawbuffer_from_assembly[] = {0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x40};
RuntimeHelpers::NotQuiteInitializeArray(tmp1,rawbuffer_from_assembly);

But I suppose insofar as it uses PrivateImplementationDetails, solving string switching would be a simpler introduction, and maybe another solution would manifest itself. That seems somewhat simpler, as you only have to deal with the munged up awful names

@AlexAlbala
Copy link
Owner

AlexAlbala commented Nov 3, 2016

Hi! Yes, the "uggly" approach is made by the C# IL compiler, we are not supporting Array initializators yet, I suggest you to create, in the C# side, the array as follows:

Instead of:

class foo {
    void baz() {
        float[] bar = { 1, 2, 3 };
    }
};

Use:

class foo {
    void baz() {
        float[] bar = new float[3];
        bar[0] = 1;
        bar[1] = 2;
        bar[2] = 3;
    }
};

Otherwise, C# IL compiler will generate this ugly stuff and then AlterNative will generate something strange that will not compile.

Also you can do it with a for loop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants