Slide 51
Slide 51 text
Class cls = objc_allocateClassPair(NSClassFromString(parentClassName), className.UTF8String, 0);
objc_registerClassPair(cls);
!
Class superClass = class_getSuperclass(cls);
if (superClass) {
setupForwardingImplementations(cls, superClass, instanceMembers, staticMembers);
}
!
NSString *types;
BOOL result;
!
Class metaClass = objc_getMetaClass(className.UTF8String);
!
types = [NSString stringWithFormat: @"%s%s%s%s", @encode(NSMethodSignature), @encode(id), @encode(SEL), @encode(SEL)];
result = class_addMethod(cls, @selector(methodSignatureForSelector:), (IMP)methodSignatureForSelector, types.UTF8String);
result = class_addMethod(metaClass, @selector(methodSignatureForSelector:), (IMP)methodSignatureForSelector, types.UTF8String);
!
types = [NSString stringWithFormat: @"%s%s%s%s", @encode(void), @encode(id), @encode(SEL), @encode(NSInvocation)];
result = class_addMethod(cls, @selector(forwardInvocation:), (IMP)forwardInvocation, types.UTF8String);
result = class_addMethod(metaClass, @selector(forwardInvocation:), (IMP)forwardInvocation, types.UTF8String);
!
types = [NSString stringWithFormat: @"%s%s%s%s", @encode(BOOL), @encode(id), @encode(SEL), @encode(SEL)];
result = class_addMethod(cls, @selector(respondsToSelector:), (IMP)respondsToSelector, types.UTF8String);
result = class_addMethod(metaClass, @selector(respondsToSelector:), (IMP)respondsToSelector, types.UTF8String);
!
for (NSString *protocol in [protocols componentsSeparatedByString:@","]) {
class_addProtocol(cls, NSProtocolFromString([protocol stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]));
}
!
class_addProtocol(cls, @protocol(JSBNSObject));
!
NSString *key = mangledNameFromClass(cls);
globalContext[key] = cls;
globalContext[key][JSBInstanceMembersKey] = instanceMembers;
globalContext[key][JSBStaticMembersKey] = staticMembers;
!
return cls;