From a09878dcb4f8f8063ab36f9a9f70ca8e24f295fe Mon Sep 17 00:00:00 2001 From: Derek Hammer Date: Mon, 6 Sep 2010 12:07:50 -0500 Subject: [PATCH] Adding self checks in toll free bridged objects so that they are subclassable. --- Foundation/CPArray.j | 1 + Foundation/CPDictionary.j | 1 + Foundation/CPException.j | 1 + Foundation/CPNumber.j | 1 + Foundation/CPString.j | 1 + Tests/Foundation/SubclassTollFreeTest.j | 59 +++++++++++++++++++++++++ 6 files changed, 64 insertions(+) create mode 100644 Tests/Foundation/SubclassTollFreeTest.j diff --git a/Foundation/CPArray.j b/Foundation/CPArray.j index 6ee14b6af..f6b6566de 100755 --- a/Foundation/CPArray.j +++ b/Foundation/CPArray.j @@ -108,6 +108,7 @@ CPEnumerationReverse = 1 << 1; */ + (id)alloc { + if ([self class] !== CPArray && [self class] !== CPMutableArray) return [super alloc]; return []; } diff --git a/Foundation/CPDictionary.j b/Foundation/CPDictionary.j index acb4cca1e..c352c1afa 100755 --- a/Foundation/CPDictionary.j +++ b/Foundation/CPDictionary.j @@ -84,6 +84,7 @@ */ + (id)alloc { + if ([self class] !== CPDictionary && [self class] !== CPMutableDictionary) return [super alloc]; return new CFMutableDictionary(); } diff --git a/Foundation/CPException.j b/Foundation/CPException.j index 770c6ad63..7a9c8c959 100755 --- a/Foundation/CPException.j +++ b/Foundation/CPException.j @@ -54,6 +54,7 @@ if (input == nil) */ + (id)alloc { + if ([self class] !== CPException) return [super alloc]; return new Error(); } diff --git a/Foundation/CPNumber.j b/Foundation/CPNumber.j index a0bbef39e..e6efff8fb 100644 --- a/Foundation/CPNumber.j +++ b/Foundation/CPNumber.j @@ -43,6 +43,7 @@ var __placeholder = new Number(), + (id)alloc { + if ([self class] !== CPNumber) return [super alloc]; return __placeholder; } diff --git a/Foundation/CPString.j b/Foundation/CPString.j index cb5b23917..87831a567 100644 --- a/Foundation/CPString.j +++ b/Foundation/CPString.j @@ -91,6 +91,7 @@ var CPStringRegexSpecialCharacters = [ */ + (id)alloc { + if ([self class] !== CPString) return [super alloc]; return new String; } diff --git a/Tests/Foundation/SubclassTollFreeTest.j b/Tests/Foundation/SubclassTollFreeTest.j new file mode 100644 index 000000000..effa6835f --- /dev/null +++ b/Tests/Foundation/SubclassTollFreeTest.j @@ -0,0 +1,59 @@ +@implementation SubclassTollFreeTest : OJTestCase + +- (void)testThatSubclassTollFreeDoesAllowForSubclassing +{ + [OJAssert assert:@"a" equals:[[[MyDict alloc] init] newMessage]]; + [OJAssert assert:@"a" equals:[[[MyString alloc] init] newMessage]]; + [OJAssert assert:@"a" equals:[[[MyNum alloc] init] newMessage]]; + [OJAssert assert:@"a" equals:[[[MyException alloc] init] newMessage]]; + [OJAssert assert:@"a" equals:[[[MyArray alloc] init] newMessage]]; +} + +@end + +@import + +@implementation MyDict : CPDictionary + +- (id)newMessage +{ + return "a"; +} + +@end + +@implementation MyNum : CPNumber + +- (id)newMessage +{ + return "a"; +} + +@end + +@implementation MyString : CPString + +- (id)newMessage +{ + return "a"; +} + +@end + +@implementation MyException : CPException + +- (id)newMessage +{ + return "a"; +} + +@end + +@implementation MyArray : CPArray + +- (id)newMessage +{ + return "a"; +} + +@end