Skip to content

Commit afcf7c4

Browse files
authored
[ffigen] allow isA (null) to return false for ObjCobject (dart-lang#3059)
1 parent 2e84fb9 commit afcf7c4

12 files changed

Lines changed: 407 additions & 276 deletions

File tree

‎pkgs/ffigen/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
change to something more sensible.
2222
- __Breaking change__: Deleted the config option `Output.sort`. Sorting is now
2323
always enabled.
24+
- Fix(https://github.com/dart-lang/native/issues/2877)
25+
such that ObjCObject `isA` now accepts a nullable `ObjCObject?` and returns `false` when called with `null`, aligning its behavior with Dart’s `is`operator.
2426

2527
## 20.1.1
2628

‎pkgs/ffigen/example/objective_c/avf_audio_bindings.dart‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -563,11 +563,13 @@ extension type AVAudioPlayer._(objc.ObjCObject object$)
563563
}
564564

565565
/// Returns whether [obj] is an instance of [AVAudioPlayer].
566-
static bool isA(objc.ObjCObject obj) => _objc_msgSend_19nvye5(
567-
obj.ref.pointer,
568-
_sel_isKindOfClass_,
569-
_class_AVAudioPlayer,
570-
);
566+
static bool isA(objc.ObjCObject? obj) => obj == null
567+
? false
568+
: _objc_msgSend_19nvye5(
569+
obj.ref.pointer,
570+
_sel_isKindOfClass_,
571+
_class_AVAudioPlayer,
572+
);
571573

572574
/// alloc
573575
static AVAudioPlayer alloc() {

‎pkgs/ffigen/example/swift/swift_api_bindings.dart‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ extension type SwiftClass._(objc.ObjCObject object$)
5757
}
5858

5959
/// Returns whether [obj] is an instance of [SwiftClass].
60-
static bool isA(objc.ObjCObject obj) => _objc_msgSend_19nvye5(
61-
obj.ref.pointer,
62-
_sel_isKindOfClass_,
63-
_class_SwiftClass,
64-
);
60+
static bool isA(objc.ObjCObject? obj) => obj == null
61+
? false
62+
: _objc_msgSend_19nvye5(
63+
obj.ref.pointer,
64+
_sel_isKindOfClass_,
65+
_class_SwiftClass,
66+
);
6567

6668
/// alloc
6769
static SwiftClass alloc() {

‎pkgs/ffigen/lib/src/code_generator/objc_interface.dart‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ ${generateInstanceMethodBindings(w, this)}
152152

153153
s.write('''
154154
/// Returns whether [obj] is an instance of [$name].
155-
static bool isA($wrapObjType obj) => $isKindOfClass;
155+
static bool isA($wrapObjType? obj) => obj == null
156+
? false
157+
: $isKindOfClass;
156158
''');
157159

158160
s.write(generateStaticMethodBindings(w, this));

‎pkgs/ffigen/test/native_objc_test/is_instance_test.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,11 @@ void main() {
4848
expect(IsInstanceChildClass.isA(base), isFalse);
4949
expect(IsInstanceChildClass.isA(child), isTrue);
5050
});
51+
52+
test('Null input', () {
53+
expect(IsInstanceBaseClass.isA(null), isFalse);
54+
expect(IsInstanceChildClass.isA(null), isFalse);
55+
expect(IsInstanceUnrelatedClass.isA(null), isFalse);
56+
});
5157
});
5258
}

‎pkgs/objective_c/CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 9.4.0
2+
- Fix (https://github.com/dart-lang/native/issues/2877) such that all occurances of ObjCObject `isA` now accepts a nullable `ObjCObject?` and returns `false` when input is`null`
3+
14
## 9.3.0
25
- `autoReleasePool` now returns the value produced by its callback.
36

0 commit comments

Comments
 (0)