This repository has been archived by the owner on Jan 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ObjC
instancetype
inheritance issue. (#618)
* Fix #486 * changelog and format * Fix nit
- Loading branch information
1 parent
6da03b3
commit 1e0fb4c
Showing
5 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name: InheritedInstancetypeTestObjCLibrary | ||
description: 'Regression tests for https://github.com/dart-lang/ffigen/issues/486' | ||
language: objc | ||
output: 'inherited_instancetype_bindings.dart' | ||
exclude-all-by-default: true | ||
objc-interfaces: | ||
include: | ||
- ChildClass | ||
headers: | ||
entry-points: | ||
- 'inherited_instancetype_test.m' | ||
preamble: | | ||
// ignore_for_file: camel_case_types, non_constant_identifier_names, unused_element, unused_field |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
// Objective C support is only available on mac. | ||
@TestOn('mac-os') | ||
|
||
// Regression tests for https://github.com/dart-lang/ffigen/issues/486. | ||
|
||
import 'dart:ffi'; | ||
import 'dart:io'; | ||
|
||
import 'package:test/test.dart'; | ||
import '../test_utils.dart'; | ||
import 'inherited_instancetype_bindings.dart'; | ||
import 'util.dart'; | ||
|
||
void main() { | ||
late InheritedInstancetypeTestObjCLibrary lib; | ||
|
||
group('inheritedInstancetype', () { | ||
setUpAll(() { | ||
logWarnings(); | ||
final dylib = | ||
File('test/native_objc_test/inherited_instancetype_test.dylib'); | ||
verifySetupFile(dylib); | ||
lib = InheritedInstancetypeTestObjCLibrary( | ||
DynamicLibrary.open(dylib.absolute.path)); | ||
generateBindingsForCoverage('inherited_instancetype'); | ||
}); | ||
|
||
test('Ordinary init method', () { | ||
final ChildClass child = ChildClass.alloc(lib).init(); | ||
expect(child.field, 123); | ||
final ChildClass sameChild = child.getSelf(); | ||
sameChild.field = 456; | ||
expect(child.field, 456); | ||
}); | ||
|
||
test('Custom create method', () { | ||
final ChildClass child = ChildClass.create(lib); | ||
expect(child.field, 123); | ||
final ChildClass sameChild = child.getSelf(); | ||
sameChild.field = 456; | ||
expect(child.field, 456); | ||
}); | ||
|
||
test('Polymorphism', () { | ||
final ChildClass child = ChildClass.alloc(lib).init(); | ||
final BaseClass base = child; | ||
|
||
// Calling base.getSelf() should still go through ChildClass.getSelf, so | ||
// the result will have a compile time type of BaseClass, but a runtime | ||
// type of ChildClass. | ||
final BaseClass sameChild = base.getSelf(); | ||
expect(sameChild, isA<ChildClass>()); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
#import <Foundation/NSObject.h> | ||
|
||
@interface BaseClass : NSObject {} | ||
+ (instancetype)create; | ||
- (instancetype)getSelf; | ||
@end | ||
|
||
@interface ChildClass : BaseClass {} | ||
@property int32_t field; | ||
@end | ||
|
||
@implementation BaseClass | ||
+ (instancetype)create { | ||
return [[[self class] alloc] init]; | ||
} | ||
|
||
- (instancetype)getSelf { | ||
return self; | ||
} | ||
@end | ||
|
||
@implementation ChildClass | ||
- (instancetype)init { | ||
if (self = [super init]) { | ||
self.field = 123; | ||
} | ||
return self; | ||
} | ||
@end |