Skip to content

Commit 52424e1

Browse files
authored
[ffigen] Fix bug in imported enums (#2786)
1 parent dd7125d commit 52424e1

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

pkgs/ffigen/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
`Declarations.includeMemberSet`, `Declarations.useOriginalName`,
1010
`Declarations.renameWithMap`, `Declarations.useMemberOriginalName`, and
1111
`Declarations.renameMemberWithMap`.
12+
- Fix [a bug](https://github.com/dart-lang/native/issues/2761) in imported
13+
NS_OPTIONS enums.
1214
- Fix [a bug](https://github.com/dart-lang/native/issues/2760) in the internal
1315
variables generated by function bindings.
1416

pkgs/ffigen/lib/src/code_generator/enum_class.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,10 @@ class EnumClass extends BindingType with HasLocalScope {
235235

236236
@override
237237
String getDartType(Context context) {
238-
if (isObjCImport) {
239-
return '${context.libs.prefix(objcPkgImport)}.$name';
240-
} else if (style == EnumStyle.intConstants) {
238+
if (style == EnumStyle.intConstants) {
241239
return nativeType.getDartType(context);
240+
} else if (isObjCImport) {
241+
return '${context.libs.prefix(objcPkgImport)}.$name';
242242
} else {
243243
return name;
244244
}

pkgs/ffigen/test/native_objc_test/enum_config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ enums:
77
include:
88
- Fruit
99
- CoffeeOptions
10+
objc-interfaces:
11+
include:
12+
- EnumTestInterface
1013
headers:
1114
entry-points:
1215
- 'enum_test.m'

pkgs/ffigen/test/native_objc_test/enum_test.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ import 'util.dart';
1818
void main() {
1919
group('enum', () {
2020
setUpAll(() {
21+
// TODO(https://github.com/dart-lang/native/issues/1068): Remove this.
22+
DynamicLibrary.open(
23+
path.join(
24+
packagePathForTests,
25+
'..',
26+
'objective_c',
27+
'test',
28+
'objective_c.dylib',
29+
),
30+
);
31+
final dylib = File(
32+
path.join(
33+
packagePathForTests,
34+
'test',
35+
'native_objc_test',
36+
'objc_test.dylib',
37+
),
38+
);
39+
verifySetupFile(dylib);
40+
DynamicLibrary.open(dylib.absolute.path);
2141
generateBindingsForCoverage('enum');
2242
});
2343

@@ -33,5 +53,22 @@ void main() {
3353
5,
3454
);
3555
});
56+
57+
test('Imported enum', () {
58+
// Regression test for https://github.com/dart-lang/native/issues/2761
59+
expect(
60+
EnumTestInterface.useImportedNSEnum(
61+
NSQualityOfService.NSQualityOfServiceUtility,
62+
),
63+
17,
64+
);
65+
expect(
66+
EnumTestInterface.useImportedNSOptions(
67+
NSOrderedCollectionDifferenceCalculationOptions
68+
.NSOrderedCollectionDifferenceCalculationOmitInsertedObjects,
69+
),
70+
1,
71+
);
72+
});
3673
});
3774
}

pkgs/ffigen/test/native_objc_test/enum_test.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
#import <Foundation/NSArray.h>
56
#import <Foundation/NSObject.h>
67

78
typedef NS_ENUM(NSInteger, Fruit) {
@@ -17,3 +18,24 @@ typedef NS_OPTIONS(NSUInteger, CoffeeOptions) {
1718
CoffeeOptionsSugar = 1 << 1,
1819
CoffeeOptionsIced = 1 << 2,
1920
};
21+
22+
@interface EnumTestInterface : NSObject {}
23+
24+
+(int32_t)useImportedNSEnum:(NSQualityOfService)x;
25+
+(int32_t)useImportedNSOptions:
26+
(NSOrderedCollectionDifferenceCalculationOptions)x;
27+
28+
@end
29+
30+
@implementation EnumTestInterface
31+
32+
+(int32_t)useImportedNSEnum:(NSQualityOfService)x {
33+
return (int32_t)x;
34+
}
35+
36+
+(int32_t)useImportedNSOptions:
37+
(NSOrderedCollectionDifferenceCalculationOptions)x {
38+
return (int32_t)x;
39+
}
40+
41+
@end

0 commit comments

Comments
 (0)