-
Notifications
You must be signed in to change notification settings - Fork 514
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
Some enums have different values between x64 and arm64 on macOS and Mac Catalyst #12111
Comments
I assume this is a variation of #11498 ? |
It is a similar issue. The difference with
|
It's a breaking change to modify the values - and they differ between
The values are also baked directly into (3rd parties) assemblies that refers to them... so that limit what we can do to fix this. ProposalI suggest adding extension methods, on all affected enum types, that gives the correct value. They can be optimized/removed later (e.g. by the linker) so they won't represent a large runtime cost. The generator will also need to be aware of those types, i.e. to use the extension methods, not the enum values, e.g. void SetAlignment (NSTextAlignment alignment)
{
long value = alignment.GetArchSpecificValue ();
Pinvoke (value);
} Also manual p/invokes will need to use the (public) extensions methods to work properly. That will need to be documented. Adding a new analyzer could also help in spotting incorrect usage inside user projects. |
I have a slightly different proposal :) Proposal #2We already have some code to convert values for these enums, because they're [Native] enums. We could augment that code so that we can do something like this: [Native (ConvertToNative="NSTextAlignmentExtensions.ConvertToNative", ConvertToManaged="NSTextAlignmentExtension.ConvertToManaged")]
public static NSTextAlignment ConvertToNative (NSTextAlignment value)
{
if (Runtime.IsARM64CallingConvention) {
switch (value) {
case NSTextAlignment.Center: return NSTextAlignment.Right;
case NSTextAlignment.Right: return NSTextAlignment.Center;
}
}
return value;
} We'd have a managed enum that would define the values for managed code, and then we convert to the corresponding value whenever we enter/exit native code (exactly like we already do for the existing [Native] support). The advantage would be that we'd not have to remember to use the extension functions, it would all be automatic (the only manual code would be the extension methods themselves). It would likely be a bit more work to implement though. |
Love it! The only downside is that |
…ions. Fixes xamarin#12111. Augment the Native attribute for enums to support custom conversion functions between native values and managed values for enums. This makes it possible to have different values in managed code for an enum compared to native code. This is necessary to support different native enum values based on the architecture, because in a few cases Apple has different enum values between x86_64 and ARM64. Enum values are constants in managed code, and without this support it would be impossible to translate these correctly to native code. The updated Native attribute supports two new fields: ConvertToNative and ConvertToManaged, which are managed functions of a specific signature that the generator will emit calls to whenever needed to do the appropriate conversion. Fixes xamarin#12111.
…ions. Fixes #12111. (#12488) Augment the Native attribute for enums to support custom conversion functions between native values and managed values for enums. This makes it possible to have different values in managed code for an enum compared to native code. This is necessary to support different native enum values based on the architecture, because in a few cases Apple has different enum values between x86_64 and ARM64. Enum values are constants in managed code, and without this support it would be impossible to translate these correctly to native code. The updated Native attribute supports two new fields: ConvertToNative and ConvertToManaged, which are managed functions of a specific signature that the generator will emit calls to whenever needed to do the appropriate conversion. Fixes #12111.
Apple has defined a
TARGET_ABI_USES_IOS_VALUES
constant in their headers:which means TARGET_ABI_USES_IOS_VALUES is true on:
1
on these platforms!TARGET_CPU_X86_64
)!TARGET_CPU_X86_64
)and
TARGET_ABI_USES_IOS_VALUES
is false on:Searching Apple's headers, this shows up for the following enums:
The text was updated successfully, but these errors were encountered: