Skip to content

Commit

Permalink
Workarounds for issue johnezang#19 (the clang stuff) and issue johnez…
Browse files Browse the repository at this point in the history
…ang#23.  For issue johnezang#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.
  • Loading branch information
johnezang committed May 21, 2011
1 parent 7e4d3ec commit f403357
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions JSONKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 <NSCopying, NSMutableCopying, NSFastEnumeration> {
id *objects;
Expand All @@ -618,19 +643,6 @@ @interface JKArray : NSMutableArray <NSCopying, NSMutableCopying, NSFastEnumerat

@implementation JKArray

static Class _JKArrayClass = NULL;
static size_t _JKArrayInstanceSize = 0UL;

+ (void)load
{
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));

[pool release]; pool = NULL;
}

+ (id)allocWithZone:(NSZone *)zone
{
#pragma unused(zone)
Expand Down Expand Up @@ -736,7 +748,11 @@ - (void)insertObject:(id)anObject atIndex:(NSUInteger)objectIndex
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 + 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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -836,19 +856,6 @@ @interface JKDictionary : NSMutableDictionary <NSCopying, NSMutableCopying, NSFa

@implementation JKDictionary

static Class _JKDictionaryClass = NULL;
static size_t _JKDictionaryInstanceSize = 0UL;

+ (void)load
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Though technically not required, the run time environment at +load time may be less than ideal.

_JKDictionaryClass = objc_getClass("JKDictionary");
_JKDictionaryInstanceSize = jk_max(16UL, class_getInstanceSize(_JKDictionaryClass));

[pool release]; pool = NULL;
}

+ (id)allocWithZone:(NSZone *)zone
{
#pragma unused(zone)
Expand Down

0 comments on commit f403357

Please sign in to comment.