Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit f4033578bcf16a4568ecd250d5d11e899b4bb0c3 1 parent 7e4d3ec
@johnezang authored
Showing with 33 additions and 26 deletions.
  1. +33 −26 JSONKit.m
View
59 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 <NSCopying, NSMutableCopying, NSFastEnumeration> {
id *objects;
@@ -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)
@@ -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;
}
@@ -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 <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)
Please sign in to comment.
Something went wrong with that request. Please try again.