Skip to content

Commit

Permalink
Added some logging to debug stuff at runtime
Browse files Browse the repository at this point in the history
Implemented custom responds to selector that works fine with x64 simulators
  • Loading branch information
anton-matosov committed Nov 15, 2015
1 parent e7b1b29 commit 514db44
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
55 changes: 50 additions & 5 deletions src/Kernel/Categories/NSObject+PXSubclass.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,58 @@ + (void)subclassInstance:(id)object
object_setClass(object, newClass);
}

static BOOL classRespondsToSelectorRAW(Class class, SEL selector)
{
if (class != Nil)
{
return class_getInstanceMethod(class, selector) != NULL;
}
return NO;
}

static BOOL respondsToSelectorRAW(id self, SEL selector)
{
if (self)
{
return classRespondsToSelectorRAW(object_getClass(self), selector);
}
return NO;
}

static BOOL classHierarchyRespondsToSelector(Class class, SEL selector)
{
if (class)
{
if (classRespondsToSelectorRAW(class, selector))
{
return YES;
}
else
{
return classHierarchyRespondsToSelector(class_getSuperclass(class), selector);
}
}

return NO;
}

static BOOL respondsToSelectorIMP(id self, SEL _cmd, SEL selector)
{
return
#if !(TARGET_IPHONE_SIMULATOR && TARGET_CPU_X86_64)
((BOOL)callSuper1v(self, [self pxClass], _cmd, selector)) ||
#endif
(class_getInstanceMethod(object_getClass(self), selector) != NULL);
BOOL result1 = classHierarchyRespondsToSelector([self pxClass], selector);
BOOL result1Old = ((BOOL)callSuper1v(self, [self pxClass], _cmd, selector));

NSCAssert(result1 == result1Old, @"classHierarchyRespondsToSelector gives a false result");
BOOL result2 = respondsToSelectorRAW(self, selector);

printf("respondsToSelectorIMP(%s):, 1:%s 2:%s | self %p, self.pxClass: %p [%s] | self.class: %p [%s]\n",
sel_getName(selector),
(result1 ? "YES" : "NO"),
(result2 ? "YES" : "NO"),
(__bridge void*)self,
(__bridge void*)[self pxClass], object_getClassName([self pxClass]),
(__bridge void*)object_getClass(self), object_getClassName(object_getClass(self)));

return result1 || result2;
}

@end
14 changes: 8 additions & 6 deletions src/Kernel/Utils/objc.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "objc.h"
#include <string.h>
#include <stdio.h>

#define __bridge

Expand Down Expand Up @@ -50,10 +51,10 @@ void* callSuper1b(id self, Class superClass, SEL _cmd, BOOL arg1)
struct objc_super super;
super.receiver = (__bridge void *)self;
super.class = superClass != NULL ? superClass : class_getSuperclass(object_getClass(self));

void* (*objc_msgSendSuperTyped)(id self, SEL _cmd, BOOL arg1) = (void*)objc_msgSendSuper;

return objc_msgSendSuperTyped((id) &super, _cmd, arg1);
// void* (*objc_msgSendSuperTyped)(id self, SEL _cmd, BOOL arg1) = (void*)objc_msgSendSuper;

return objc_msgSendSuper(&super, _cmd, arg1);
}


Expand All @@ -62,10 +63,11 @@ void* callSuper1v(id self, Class superClass, SEL _cmd, void *arg1)
struct objc_super super;
super.receiver = (__bridge void *)self;
super.class = superClass != NULL ? superClass : class_getSuperclass(object_getClass(self));

void* (*objc_msgSendSuperTyped)(id self, SEL _cmd, void *arg1) = (void*)objc_msgSendSuper;

return objc_msgSendSuperTyped((id) &super, _cmd, arg1);
printf("superClass: %p [%s]| self.class: %p [%s]\n", (void*)superClass, object_getClassName(superClass), (void*)object_getClass(self), object_getClassName((void*)object_getClass(self)));
// void* (*objc_msgSendSuperTyped)(id self, SEL _cmd, void *arg1) = (void*)objc_msgSendSuper;

return objc_msgSendSuper(&super, _cmd, arg1);
}

void* callSuper2(id self, Class superClass, SEL _cmd, id arg1, id arg2)
Expand Down

0 comments on commit 514db44

Please sign in to comment.