Skip to content

Commit e0d30a8

Browse files
authored
fix: enhance nullable type handling in method parameter validation (#367)
solves a crash introduced with #363
1 parent fb0a5f4 commit e0d30a8

4 files changed

Lines changed: 861 additions & 657 deletions

File tree

TestFixtures/Api/TNSApi.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
typedef NS_ENUM(NSInteger, TNSEnums) {
2-
TNSEnum1 = -1,
3-
TNSEnum2,
4-
TNSEnum3,
2+
TNSEnum1 = -1,
3+
TNSEnum2,
4+
TNSEnum3,
55
};
66

77
typedef NS_OPTIONS(NSInteger, TNSOptions) {
8-
TNSOption1 = 1 << 0,
9-
TNSOption2 = 1 << 1,
10-
TNSOption3 = 1 << 2,
8+
TNSOption1 = 1 << 0,
9+
TNSOption2 = 1 << 1,
10+
TNSOption3 = 1 << 2,
1111
};
1212

13-
enum {
14-
AnonymousEnumField = -1
15-
};
13+
enum { AnonymousEnumField = -1 };
1614

1715
extern NSString* const TNSConstant;
1816

@@ -27,14 +25,15 @@ void functionThrowsException();
2725
@property(getter=customGetter, setter=customSetter:) int property;
2826

2927
typedef UIColor NIKColor;
30-
@property(strong, nonatomic) NIKColor* strokeColor; // ^{UIColor=#}
28+
@property(strong, nonatomic) NIKColor* strokeColor; // ^{UIColor=#}
3129

3230
+ (void)methodThrowsException;
3331
- (void)methodThrowsException;
3432

3533
- (void)methodCalledInDealloc;
3634

3735
- (BOOL)method:(NSInteger)errorCode error:(NSError**)outError;
36+
- (BOOL)methodNullable:(NSInteger)errorCode error:(NSError* _Nullable* _Nullable)outError;
3837
@end
3938

4039
@interface TNSConflictingSelectorTypes1 : NSObject
@@ -70,5 +69,5 @@ typedef UIColor NIKColor;
7069
- (CGRect)getRect;
7170
@end
7271

73-
@interface RectClass : NSObject<RectProtocol>
72+
@interface RectClass : NSObject <RectProtocol>
7473
@end

TestFixtures/Api/TNSApi.m

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,57 @@
33
NSString* const TNSConstant = @"TNSConstant";
44

55
void functionThrowsException() {
6-
@throw [NSException exceptionWithName:NSGenericException reason:@"No reason" userInfo:nil];
6+
@throw [NSException exceptionWithName:NSGenericException reason:@"No reason" userInfo:nil];
77
}
88

99
@implementation TNSApi {
10-
int _property;
10+
int _property;
1111
}
1212

1313
- (int)customGetter {
14-
return _property;
14+
return _property;
1515
}
1616
- (void)customSetter:(int)value {
17-
_property = value;
17+
_property = value;
1818
}
1919

2020
+ (void)methodThrowsException {
21-
@throw [NSException exceptionWithName:NSGenericException reason:@"No reason" userInfo:nil];
21+
@throw [NSException exceptionWithName:NSGenericException reason:@"No reason" userInfo:nil];
2222
}
2323

2424
- (void)methodThrowsException {
25-
@throw [NSException exceptionWithName:NSGenericException reason:@"No reason" userInfo:nil];
25+
@throw [NSException exceptionWithName:NSGenericException reason:@"No reason" userInfo:nil];
2626
}
2727

2828
- (void)methodCalledInDealloc {
29-
// TODO
30-
// [TNSGetOutput() appendString:@"methodCalledInDealloc called"];
29+
// TODO
30+
// [TNSGetOutput() appendString:@"methodCalledInDealloc called"];
3131
}
3232

3333
- (void)dealloc {
34-
[self methodCalledInDealloc];
34+
[self methodCalledInDealloc];
3535
}
3636

3737
- (BOOL)method:(NSInteger)errorCode error:(NSError**)outError {
38-
if (outError) {
39-
if (errorCode != 0) {
40-
*outError = [NSError errorWithDomain:@"TNSErrorDomain" code:errorCode userInfo:nil];
41-
} else {
42-
*outError = nil;
43-
}
38+
if (outError) {
39+
if (errorCode != 0) {
40+
*outError = [NSError errorWithDomain:@"TNSErrorDomain" code:errorCode userInfo:nil];
41+
} else {
42+
*outError = nil;
4443
}
45-
return errorCode == 0;
44+
}
45+
return errorCode == 0;
46+
}
47+
48+
- (BOOL)methodNullable:(NSInteger)errorCode error:(NSError* _Nullable* _Nullable)outError {
49+
if (outError) {
50+
if (errorCode != 0) {
51+
*outError = [NSError errorWithDomain:@"TNSErrorDomain" code:errorCode userInfo:nil];
52+
} else {
53+
*outError = nil;
54+
}
55+
}
56+
return errorCode == 0;
4657
}
4758

4859
@end
@@ -56,19 +67,19 @@ - (void)method:(long long)x {
5667

5768
@implementation TNSConflictingSelectorTypes2
5869
+ (id)method:(id)x {
59-
return nil;
70+
return nil;
6071
}
6172
- (id)method:(id)x {
62-
return nil;
73+
return nil;
6374
}
6475
@end
6576

6677
@implementation TNSSwizzleKlass
6778
+ (int)staticMethod:(int)x {
68-
return x;
79+
return x;
6980
}
7081
- (int)instanceMethod:(int)x {
71-
return x;
82+
return x;
7283
}
7384
@end
7485

@@ -79,7 +90,7 @@ @implementation TNSPropertyMethodConflictClass
7990
@implementation RectClass
8091

8192
- (CGRect)getRect {
82-
return CGRectMake(1, 2, 3, 4);
93+
return CGRectMake(1, 2, 3, 4);
8394
}
8495

8596
@end

TestRunner/app/tests/Marshalling/ObjCTypesTests.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,23 @@ describe(module.id, function () {
397397
expect(typeof array.firstObject).toEqual("boolean");
398398
expect(array.firstObject).toEqual(bool);
399399
});
400+
401+
it("NSErrorOutParameterWithNullabilityAnnotations", function () {
402+
expect(function () {
403+
TNSApi.new().methodNullableError(0);
404+
}).not.toThrow();
405+
406+
var isThrown = false;
407+
try {
408+
TNSApi.new().methodNullableError(1);
409+
} catch (e) {
410+
isThrown = true;
411+
expect(e.stack).toEqual(jasmine.any(String));
412+
}
413+
expect(isThrown).toBe(true);
414+
415+
var errorRef = new interop.Reference();
416+
TNSApi.new().methodNullableError(1, errorRef);
417+
expect(errorRef.value instanceof NSError).toBe(true);
418+
});
400419
});

0 commit comments

Comments
 (0)