Skip to content

Commit 963155d

Browse files
authored
[ffigen] Fix bug in NSRange import (#1602)
1 parent 1aae9da commit 963155d

File tree

5 files changed

+66
-11
lines changed

5 files changed

+66
-11
lines changed

pkgs/ffigen/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
ObjC but before the invocation was received by Dart:
1515
https://github.com/dart-lang/native/issues/1571
1616
- `sort:` config option now affects ObjC interface/protocol methods.
17+
- Fix a bug where `NSRange` was not being imported from package:objective_c:
18+
https://github.com/dart-lang/native/issues/1180
1719

1820
## 14.0.1
1921

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ abstract class Compound extends BindingType {
105105
}
106106

107107
bool get _isBuiltIn =>
108-
objCBuiltInFunctions?.isBuiltInCompound(originalName) ?? false;
108+
objCBuiltInFunctions?.getBuiltInCompoundName(originalName) != null;
109109

110110
@override
111111
BindingString toBindingString(Writer w) {
@@ -188,7 +188,11 @@ abstract class Compound extends BindingType {
188188
bool get isIncompleteCompound => isIncomplete;
189189

190190
@override
191-
String getCType(Writer w) => _isBuiltIn ? '${w.objcPkgPrefix}.$name' : name;
191+
String getCType(Writer w) {
192+
final builtInName =
193+
objCBuiltInFunctions?.getBuiltInCompoundName(originalName);
194+
return builtInName != null ? '${w.objcPkgPrefix}.$builtInName' : name;
195+
}
192196

193197
@override
194198
String getNativeType({String varName = ''}) => '$nativeType $varName';

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ObjCBuiltInFunctions {
4343
ObjCImport('UnimplementedOptionalMethodException');
4444

4545
// Keep in sync with pkgs/objective_c/ffigen_objc.yaml.
46-
static const builtInInterfaces = {
46+
static const _builtInInterfaces = {
4747
'DartProxy',
4848
'DartProxyBuilder',
4949
'NSArray',
@@ -78,11 +78,11 @@ class ObjCBuiltInFunctions {
7878
'NSValue',
7979
'Protocol',
8080
};
81-
static const builtInCompounds = {
82-
'NSFastEnumerationState',
83-
'NSRange',
81+
static const _builtInCompounds = {
82+
'NSFastEnumerationState': 'NSFastEnumerationState',
83+
'_NSRange': 'NSRange',
8484
};
85-
static const builtInEnums = {
85+
static const _builtInEnums = {
8686
'NSBinarySearchingOptions',
8787
'NSComparisonResult',
8888
'NSDataBase64DecodingOptions',
@@ -110,11 +110,11 @@ class ObjCBuiltInFunctions {
110110
// TODO(https://github.com/dart-lang/native/issues/1173): Ideally this check
111111
// would be based on more than just the name.
112112
bool isBuiltInInterface(String name) =>
113-
!generateForPackageObjectiveC && builtInInterfaces.contains(name);
114-
bool isBuiltInCompound(String name) =>
115-
!generateForPackageObjectiveC && builtInCompounds.contains(name);
113+
!generateForPackageObjectiveC && _builtInInterfaces.contains(name);
114+
String? getBuiltInCompoundName(String name) =>
115+
generateForPackageObjectiveC ? null : _builtInCompounds[name];
116116
bool isBuiltInEnum(String name) =>
117-
!generateForPackageObjectiveC && builtInEnums.contains(name);
117+
!generateForPackageObjectiveC && _builtInEnums.contains(name);
118118
bool isNSObject(String name) => name == 'NSObject';
119119

120120
// We need to load a separate instance of objc_msgSend for each signature. If
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Objective C support is only available on mac.
6+
@TestOn('mac-os')
7+
8+
import 'dart:ffi';
9+
import 'dart:io';
10+
11+
import 'package:ffi/ffi.dart';
12+
import 'package:ffigen/ffigen.dart';
13+
import 'package:ffigen/src/config_provider/config.dart';
14+
import 'package:ffigen/src/config_provider/config_types.dart';
15+
import 'package:logging/logging.dart';
16+
import 'package:pub_semver/pub_semver.dart';
17+
import 'package:test/test.dart';
18+
import '../test_utils.dart';
19+
import 'util.dart';
20+
21+
void main() {
22+
group('NSRange', () {
23+
late final String bindings;
24+
setUpAll(() {
25+
final config = Config(
26+
wrapperName: 'NSRangeTestObjCLibrary',
27+
language: Language.objc,
28+
output: Uri.file('test/native_objc_test/ns_range_bindings.dart'),
29+
entryPoints: [Uri.file('test/native_objc_test/ns_range_test.m')],
30+
formatOutput: false,
31+
objcInterfaces: DeclarationFilters.include({'SFTranscriptionSegment'}),
32+
);
33+
FfiGen(logLevel: Level.SEVERE).run(config);
34+
bindings = File('test/native_objc_test/ns_range_bindings.dart')
35+
.readAsStringSync();
36+
});
37+
38+
test('interfaces', () {
39+
// Regression test for https://github.com/dart-lang/native/issues/1180.
40+
expect(bindings.split('\n'),
41+
isNot(contains(matches(RegExp(r'class.*NSRange.*Struct')))));
42+
});
43+
});
44+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
#import <Speech/Speech.h>

0 commit comments

Comments
 (0)