From f4033578bcf16a4568ecd250d5d11e899b4bb0c3 Mon Sep 17 00:00:00 2001 From: John Engelhart Date: Sat, 21 May 2011 00:14:43 -0400 Subject: [PATCH] Workarounds for issue #19 (the clang stuff) and issue #23. For issue #23, the code in the collection classes `+load` was removed and placed in a function with the `__attribute__ ((constructor))` attribute. This is to work around an apparent bug when building JSONKit as a static library for iOS targets. @ohhorob also opened a bug with apple- # 9461567. --- JSONKit.m | 59 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/JSONKit.m b/JSONKit.m index 9ce1ce7..2875c44 100644 --- a/JSONKit.m +++ b/JSONKit.m @@ -609,6 +609,31 @@ - (void)releaseState; // classes receive the mutating methods, but this is handled by having those methods throw an exception when the ivar bit is set to immutable. // We adopt the same strategy here. It's both cleaner and gets rid of the method swizzling hackery used in JSONKit v1.4. + +// This is a workaround for issue #23 https://github.com/johnezang/JSONKit/pull/23 +// Basically, there seem to be a problem with using +load in static libraries on iOS. However, __attribute__ ((constructor)) does work correctly. +// Since we do not require anything "special" that +load provides, and we can accomplish the same thing using __attribute__ ((constructor)), the +load logic was moved here. + +static Class _JKArrayClass = NULL; +static size_t _JKArrayInstanceSize = 0UL; +static Class _JKDictionaryClass = NULL; +static size_t _JKDictionaryInstanceSize = 0UL; + +extern void jk_collectionClassLoadTimeInitialization(void) __attribute__ ((constructor)); + +void jk_collectionClassLoadTimeInitialization(void) { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Though technically not required, the run time environment at +load time may be less than ideal. + + _JKArrayClass = objc_getClass("JKArray"); + _JKArrayInstanceSize = jk_max(16UL, class_getInstanceSize(_JKArrayClass)); + + _JKDictionaryClass = objc_getClass("JKDictionary"); + _JKDictionaryInstanceSize = jk_max(16UL, class_getInstanceSize(_JKDictionaryClass)); + + [pool release]; pool = NULL; +} + + #pragma mark - @interface JKArray : NSMutableArray { id *objects; @@ -618,19 +643,6 @@ @interface JKArray : NSMutableArray count) { [NSException raise:NSRangeException format:@"*** -[%@ %@]: index (%lu) beyond bounds (%lu)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), objectIndex, count + 1UL]; } +#ifdef __clang_analyzer__ + [anObject retain]; // Stupid clang analyzer... Issue #19. +#else anObject = [anObject retain]; +#endif _JKArrayInsertObjectAtIndex(self, anObject, objectIndex); mutations = (mutations == NSUIntegerMax) ? 1UL : mutations + 1UL; } @@ -754,7 +770,11 @@ - (void)replaceObjectAtIndex:(NSUInteger)objectIndex withObject:(id)anObject if(mutations == 0UL) { [NSException raise:NSInternalInconsistencyException format:@"*** -[%@ %@]: mutating method sent to immutable object", NSStringFromClass([self class]), NSStringFromSelector(_cmd)]; } if(anObject == NULL) { [NSException raise:NSInvalidArgumentException format:@"*** -[%@ %@]: attempt to insert nil", NSStringFromClass([self class]), NSStringFromSelector(_cmd)]; } if(objectIndex >= count) { [NSException raise:NSRangeException format:@"*** -[%@ %@]: index (%lu) beyond bounds (%lu)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), objectIndex, count]; } +#ifdef __clang_analyzer__ + [anObject retain]; // Stupid clang analyzer... Issue #19. +#else anObject = [anObject retain]; +#endif _JKArrayReplaceObjectAtIndexWithObject(self, objectIndex, anObject); mutations = (mutations == NSUIntegerMax) ? 1UL : mutations + 1UL; } @@ -836,19 +856,6 @@ @interface JKDictionary : NSMutableDictionary