Skip to content

Commit

Permalink
Moved the +load logic from JSONDecoder in to jk_collectionClassLoadTi…
Browse files Browse the repository at this point in the history
…meInitialization(). Missed the JSONDecoder +load stuff on the last commit. Related to issue johnezang#23.
  • Loading branch information
johnezang committed May 21, 2011
1 parent 353b124 commit cab1ea8
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions JSONKit.m
Expand Up @@ -614,22 +614,37 @@ - (void)releaseState;
// 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;
static Class _JKArrayClass = NULL;
static size_t _JKArrayInstanceSize = 0UL;
static Class _JKDictionaryClass = NULL;
static size_t _JKDictionaryInstanceSize = 0UL;

// For JSONDecoder...
static Class _jk_NSNumberClass = NULL;
static NSNumberAllocImp _jk_NSNumberAllocImp = NULL;
static NSNumberInitWithUnsignedLongLongImp _jk_NSNumberInitWithUnsignedLongLongImp = NULL;

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.
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Though technically not required, the run time environment at load time initialization 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));

// For JSONDecoder...
_jk_NSNumberClass = [NSNumber class];
_jk_NSNumberAllocImp = (NSNumberAllocImp)[NSNumber methodForSelector:@selector(alloc)];

// Hacktacular. Need to do it this way due to the nature of class clusters.
id temp_NSNumber = [NSNumber alloc];
_jk_NSNumberInitWithUnsignedLongLongImp = (NSNumberInitWithUnsignedLongLongImp)[temp_NSNumber methodForSelector:@selector(initWithUnsignedLongLong:)];
[[temp_NSNumber init] release];
temp_NSNumber = NULL;

[pool release]; pool = NULL;
}

Expand Down Expand Up @@ -2052,26 +2067,6 @@ JK_STATIC_INLINE void jk_cache_age(JKParseState *parseState) {
#pragma mark -
@implementation JSONDecoder

static Class _jk_NSNumberClass;
static NSNumberAllocImp _jk_NSNumberAllocImp;
static NSNumberInitWithUnsignedLongLongImp _jk_NSNumberInitWithUnsignedLongLongImp;

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

_jk_NSNumberClass = [NSNumber class];
_jk_NSNumberAllocImp = (NSNumberAllocImp)[NSNumber methodForSelector:@selector(alloc)];

// Hacktacular. Need to do it this way due to the nature of class clusters.
id temp_NSNumber = [NSNumber alloc];
_jk_NSNumberInitWithUnsignedLongLongImp = (NSNumberInitWithUnsignedLongLongImp)[temp_NSNumber methodForSelector:@selector(initWithUnsignedLongLong:)];
[[temp_NSNumber init] release];
temp_NSNumber = NULL;

[pool release]; pool = NULL;
}

+ (id)decoder
{
return([self decoderWithParseOptions:JKParseOptionStrict]);
Expand Down

0 comments on commit cab1ea8

Please sign in to comment.