Skip to content

Commit

Permalink
Use a symbol-based check for imp_implementationWithBlock and property…
Browse files Browse the repository at this point in the history
…_copyAttributeValue for some funky implementation business on < 4.3.

Signed-off-by: Zachary Waldowski <zwaldowski@gmail.com>
  • Loading branch information
zwaldowski committed Mar 16, 2012
1 parent 4669e57 commit 35bd534
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions A2BlockDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#import "A2BlockDelegate.h"
#import "A2DynamicDelegate.h"

#import <dlfcn.h>

#if __has_attribute(objc_arc)
#error "At present, 'A2BlockDelegate.m' may not be compiled with ARC. This is a limitation of the Obj-C runtime library. See here: http://j.mp/tJsoOV"
#endif
Expand All @@ -23,6 +25,9 @@
void *A2BlockDelegateProtocolsKey;
void *A2BlockDelegateMapKey;

static BOOL a2_hasImplementationWithBlock(void);
static BOOL a2_hasCopyAttributeValue(void);

static BOOL a2_resolveInstanceMethod(id self, SEL _cmd, SEL selector);

// Block Property Accessors
Expand Down Expand Up @@ -253,6 +258,22 @@ + (void) linkProtocol: (Protocol *) protocol methods: (NSDictionary *) dictionar

#pragma mark - Functions

static BOOL a2_hasImplementationWithBlock(void) {
static dispatch_once_t onceToken;
static BOOL hasImplementationWithBlock;
dispatch_once(&onceToken, ^{
hasImplementationWithBlock = dlsym(RTLD_DEFAULT, "imp_implementationWithBlock") ? YES : NO;
});
}

static BOOL a2_hasCopyAttributeValue(void) {
static dispatch_once_t onceToken;
static BOOL hasCopyAttributeValue;
dispatch_once(&onceToken, ^{
hasCopyAttributeValue = dlsym(RTLD_DEFAULT, "property_copyAttributeValue") ? YES : NO;
});
}

static BOOL a2_resolveInstanceMethod(id self, SEL _cmd, SEL selector)
{
// Check for existence of `-a2_protocols` and `-a2_mapForProtocol:`, respectively
Expand All @@ -269,7 +290,7 @@ static BOOL a2_resolveInstanceMethod(id self, SEL _cmd, SEL selector)

if (argc == 1)
{
if (imp_implementationWithBlock != NULL)
if (a2_hasImplementationWithBlock())
{
implementation = imp_implementationWithBlock([[^(NSObject *obj, id block) {
[[obj dynamicDelegateForProtocol: protocol] implementMethod: representedSelector withBlock: block];
Expand All @@ -284,7 +305,7 @@ static BOOL a2_resolveInstanceMethod(id self, SEL _cmd, SEL selector)
}
else
{
if (imp_implementationWithBlock != NULL)
if (a2_hasImplementationWithBlock())
{
implementation = imp_implementationWithBlock([[^id (NSObject *obj) {
return [[obj dynamicDelegateForProtocol: protocol] blockImplementationForMethod: representedSelector];
Expand Down Expand Up @@ -441,7 +462,7 @@ static BOOL a2_findOneAttribute(__unused unsigned int index, void *ctxa, void *c
}
char *a2_property_copyAttributeValue(objc_property_t property, const char *name)
{
if (&property_copyAttributeValue)
if (a2_hasCopyAttributeValue())
{
return property_copyAttributeValue(property, name);
}
Expand Down

0 comments on commit 35bd534

Please sign in to comment.