From 77217d9c580bdad82e8bce899f660305819664ea Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Saenz Date: Wed, 26 Feb 2025 12:41:14 -0500 Subject: [PATCH 1/4] [RGen] Add the new added methods to smart enums. In order to simplify the BindAs/BindFrom code, nes methods were added to the smart enums generated by bgen. We have to do the same in rgen. --- .../Emitters/EnumEmitter.cs | 11 +++++++++++ .../SmartEnum/Data/ExpectedAVCaptureDeviceTypeEnum.cs | 6 ++++++ .../Data/ExpectedAVCaptureSystemPressureLevel.cs | 6 ++++++ .../SmartEnum/Data/ExpectedCustomLibraryEnum.cs | 6 ++++++ .../Data/ExpectedCustomLibraryEnumInternal.cs | 6 ++++++ .../Data/ExpectedMacOSAVMediaCharacteristics.cs | 6 ++++++ .../Data/ExpectediOSAVMediaCharacteristics.cs | 6 ++++++ 7 files changed, 47 insertions(+) diff --git a/src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs b/src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs index b547025f7942..5eb7f31e5e23 100644 --- a/src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs +++ b/src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs @@ -95,6 +95,17 @@ void EmitExtensionMethods (TabbedWriter classBlock, in Binding bin getValueBlock.WriteLine ( "throw new NotSupportedException ($\"The constant {constant} has no associated enum value on this platform.\");"); } + classBlock.WriteLine (); + + // get value from a handle, this is a helper method used in the BindAs bindings. + using (var getValueFromHandle = + classBlock.CreateBlock ($"public static {binding.Name} GetValueFromHandle (NativeHandle handle)", + true)) { + getValueFromHandle.WriteRaw ( +@"using var str = Runtime.GetNSObject (handle); +return GetValue (str); +"); + } classBlock.WriteLine (); // To ConstantArray diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureDeviceTypeEnum.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureDeviceTypeEnum.cs index 137621264fd4..62661b507731 100644 --- a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureDeviceTypeEnum.cs +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureDeviceTypeEnum.cs @@ -196,6 +196,12 @@ public static AVCaptureDeviceType GetValue (NSString constant) throw new NotSupportedException ($"The constant {constant} has no associated enum value on this platform."); } + public static AVCaptureDeviceType GetValueFromHandle (NativeHandle handle) + { + using var str = Runtime.GetNSObject (handle); + return GetValue (str); + } + internal static NSString?[]? ToConstantArray (this AVCaptureDeviceType[]? values) { if (values is null) diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureSystemPressureLevel.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureSystemPressureLevel.cs index ecdd60b7b700..d98c4022df9c 100644 --- a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureSystemPressureLevel.cs +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureSystemPressureLevel.cs @@ -106,6 +106,12 @@ public static AVCaptureSystemPressureLevel GetValue (NSString constant) throw new NotSupportedException ($"The constant {constant} has no associated enum value on this platform."); } + public static AVCaptureSystemPressureLevel GetValueFromHandle (NativeHandle handle) + { + using var str = Runtime.GetNSObject (handle); + return GetValue (str); + } + internal static NSString?[]? ToConstantArray (this AVCaptureSystemPressureLevel[]? values) { if (values is null) diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnum.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnum.cs index f729baab9ef7..cb8dd2096880 100644 --- a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnum.cs +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnum.cs @@ -77,6 +77,12 @@ public static CustomLibraryEnum GetValue (NSString constant) throw new NotSupportedException ($"The constant {constant} has no associated enum value on this platform."); } + public static CustomLibraryEnum GetValueFromHandle (NativeHandle handle) + { + using var str = Runtime.GetNSObject (handle); + return GetValue (str); + } + internal static NSString?[]? ToConstantArray (this CustomLibraryEnum[]? values) { if (values is null) diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnumInternal.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnumInternal.cs index f30d1af5f75b..09d01d2f2e7d 100644 --- a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnumInternal.cs +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnumInternal.cs @@ -77,6 +77,12 @@ public static CustomLibraryEnumInternal GetValue (NSString constant) throw new NotSupportedException ($"The constant {constant} has no associated enum value on this platform."); } + public static CustomLibraryEnumInternal GetValueFromHandle (NativeHandle handle) + { + using var str = Runtime.GetNSObject (handle); + return GetValue (str); + } + internal static NSString?[]? ToConstantArray (this CustomLibraryEnumInternal[]? values) { if (values is null) diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedMacOSAVMediaCharacteristics.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedMacOSAVMediaCharacteristics.cs index d49b2def67d6..3c8769cdecbc 100644 --- a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedMacOSAVMediaCharacteristics.cs +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedMacOSAVMediaCharacteristics.cs @@ -359,6 +359,12 @@ public static AVMediaCharacteristics GetValue (NSString constant) throw new NotSupportedException ($"The constant {constant} has no associated enum value on this platform."); } + public static AVMediaCharacteristics GetValueFromHandle (NativeHandle handle) + { + using var str = Runtime.GetNSObject (handle); + return GetValue (str); + } + internal static NSString?[]? ToConstantArray (this AVMediaCharacteristics[]? values) { if (values is null) diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectediOSAVMediaCharacteristics.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectediOSAVMediaCharacteristics.cs index d66462b9bfbb..a39a20c42577 100644 --- a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectediOSAVMediaCharacteristics.cs +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectediOSAVMediaCharacteristics.cs @@ -378,6 +378,12 @@ public static AVMediaCharacteristics GetValue (NSString constant) throw new NotSupportedException ($"The constant {constant} has no associated enum value on this platform."); } + public static AVMediaCharacteristics GetValueFromHandle (NativeHandle handle) + { + using var str = Runtime.GetNSObject (handle); + return GetValue (str); + } + internal static NSString?[]? ToConstantArray (this AVMediaCharacteristics[]? values) { if (values is null) From 022e35eb4efb5863227c54ad38f5f799fe422c15 Mon Sep 17 00:00:00 2001 From: GitHub Actions Autoformatter Date: Wed, 26 Feb 2025 17:45:54 +0000 Subject: [PATCH 2/4] Auto-format source code --- src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs b/src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs index 5eb7f31e5e23..a6f09ebfa6c5 100644 --- a/src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs +++ b/src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs @@ -96,11 +96,11 @@ void EmitExtensionMethods (TabbedWriter classBlock, in Binding bin "throw new NotSupportedException ($\"The constant {constant} has no associated enum value on this platform.\");"); } classBlock.WriteLine (); - + // get value from a handle, this is a helper method used in the BindAs bindings. using (var getValueFromHandle = - classBlock.CreateBlock ($"public static {binding.Name} GetValueFromHandle (NativeHandle handle)", - true)) { + classBlock.CreateBlock ($"public static {binding.Name} GetValueFromHandle (NativeHandle handle)", + true)) { getValueFromHandle.WriteRaw ( @"using var str = Runtime.GetNSObject (handle); return GetValue (str); From d6db2e7b919aa4ae9ca603f63c5b027314ec39cd Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Date: Wed, 26 Feb 2025 13:56:30 -0500 Subject: [PATCH 3/4] Ignore null. --- src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs b/src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs index a6f09ebfa6c5..127bf2832312 100644 --- a/src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs +++ b/src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs @@ -102,7 +102,7 @@ void EmitExtensionMethods (TabbedWriter classBlock, in Binding bin classBlock.CreateBlock ($"public static {binding.Name} GetValueFromHandle (NativeHandle handle)", true)) { getValueFromHandle.WriteRaw ( -@"using var str = Runtime.GetNSObject (handle); +@"using var str = Runtime.GetNSObject (handle)!; return GetValue (str); "); } From 0dd0b987633ec9e111e2757705c00bd1641b3a77 Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Saenz Date: Thu, 27 Feb 2025 16:21:35 -0500 Subject: [PATCH 4/4] Fix failing tests. --- tests/cecil-tests/Documentation.KnownFailures.txt | 1 + .../SmartEnum/Data/ExpectedAVCaptureDeviceTypeEnum.cs | 2 +- .../SmartEnum/Data/ExpectedAVCaptureSystemPressureLevel.cs | 2 +- .../SmartEnum/Data/ExpectedCustomLibraryEnum.cs | 2 +- .../SmartEnum/Data/ExpectedCustomLibraryEnumInternal.cs | 2 +- .../SmartEnum/Data/ExpectedMacOSAVMediaCharacteristics.cs | 2 +- .../SmartEnum/Data/ExpectediOSAVMediaCharacteristics.cs | 2 +- 7 files changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/cecil-tests/Documentation.KnownFailures.txt b/tests/cecil-tests/Documentation.KnownFailures.txt index b5fb51df6bcc..2421bbc7ce54 100644 --- a/tests/cecil-tests/Documentation.KnownFailures.txt +++ b/tests/cecil-tests/Documentation.KnownFailures.txt @@ -24425,6 +24425,7 @@ M:AVFoundation.AVCapturePhotoSettingsThumbnailFormat.#ctor M:AVFoundation.AVCapturePhotoSettingsThumbnailFormat.#ctor(Foundation.NSDictionary) M:AVFoundation.AVCaptureReactionTypeExtensions.GetConstant(AVFoundation.AVCaptureReactionType) M:AVFoundation.AVCaptureReactionTypeExtensions.GetValue(Foundation.NSString) +M:AVFoundation.AVCaptureReactionTypeExtensions.GetValueFromHandle(ObjCRuntime.NativeHandle) M:AVFoundation.AVCaptureSessionRuntimeErrorEventArgs.#ctor(Foundation.NSNotification) M:AVFoundation.AVCaptureStillImageOutput.CaptureStillImageTaskAsync(AVFoundation.AVCaptureConnection) M:AVFoundation.AVCaptureVideoDataOutput.GetRecommendedVideoSettings(AVFoundation.AVVideoCodecType,AVFoundation.AVFileTypes,Foundation.NSUrl) diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureDeviceTypeEnum.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureDeviceTypeEnum.cs index 62661b507731..5a96840f2d01 100644 --- a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureDeviceTypeEnum.cs +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureDeviceTypeEnum.cs @@ -198,7 +198,7 @@ public static AVCaptureDeviceType GetValue (NSString constant) public static AVCaptureDeviceType GetValueFromHandle (NativeHandle handle) { - using var str = Runtime.GetNSObject (handle); + using var str = Runtime.GetNSObject (handle)!; return GetValue (str); } diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureSystemPressureLevel.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureSystemPressureLevel.cs index d98c4022df9c..6febb9052524 100644 --- a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureSystemPressureLevel.cs +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedAVCaptureSystemPressureLevel.cs @@ -108,7 +108,7 @@ public static AVCaptureSystemPressureLevel GetValue (NSString constant) public static AVCaptureSystemPressureLevel GetValueFromHandle (NativeHandle handle) { - using var str = Runtime.GetNSObject (handle); + using var str = Runtime.GetNSObject (handle)!; return GetValue (str); } diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnum.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnum.cs index cb8dd2096880..3047f86b6e7d 100644 --- a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnum.cs +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnum.cs @@ -79,7 +79,7 @@ public static CustomLibraryEnum GetValue (NSString constant) public static CustomLibraryEnum GetValueFromHandle (NativeHandle handle) { - using var str = Runtime.GetNSObject (handle); + using var str = Runtime.GetNSObject (handle)!; return GetValue (str); } diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnumInternal.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnumInternal.cs index 09d01d2f2e7d..5574d2b486ee 100644 --- a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnumInternal.cs +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedCustomLibraryEnumInternal.cs @@ -79,7 +79,7 @@ public static CustomLibraryEnumInternal GetValue (NSString constant) public static CustomLibraryEnumInternal GetValueFromHandle (NativeHandle handle) { - using var str = Runtime.GetNSObject (handle); + using var str = Runtime.GetNSObject (handle)!; return GetValue (str); } diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedMacOSAVMediaCharacteristics.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedMacOSAVMediaCharacteristics.cs index 3c8769cdecbc..fc5529fec48f 100644 --- a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedMacOSAVMediaCharacteristics.cs +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectedMacOSAVMediaCharacteristics.cs @@ -361,7 +361,7 @@ public static AVMediaCharacteristics GetValue (NSString constant) public static AVMediaCharacteristics GetValueFromHandle (NativeHandle handle) { - using var str = Runtime.GetNSObject (handle); + using var str = Runtime.GetNSObject (handle)!; return GetValue (str); } diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectediOSAVMediaCharacteristics.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectediOSAVMediaCharacteristics.cs index a39a20c42577..33d0e5245c99 100644 --- a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectediOSAVMediaCharacteristics.cs +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/Data/ExpectediOSAVMediaCharacteristics.cs @@ -380,7 +380,7 @@ public static AVMediaCharacteristics GetValue (NSString constant) public static AVMediaCharacteristics GetValueFromHandle (NativeHandle handle) { - using var str = Runtime.GetNSObject (handle); + using var str = Runtime.GetNSObject (handle)!; return GetValue (str); }