Skip to content

Instantly share code, notes, and snippets.

@agdsdl
Created January 27, 2016 12:41
Show Gist options
  • Save agdsdl/a22666c8f64fed0dbbf5 to your computer and use it in GitHub Desktop.
Save agdsdl/a22666c8f64fed0dbbf5 to your computer and use it in GitHub Desktop.
objc-class-relations-main-code
//object.mm && objc.h
typedef struct objc_class *Class;
typedef struct objc_object *id;
//NSObject.mm
@interface NSObject <NSObject> {
Class isa OBJC_ISA_AVAILABILITY;
}
+ (Class)class {
return self;
}
- (Class)class {
return object_getClass(self);
}
//objc-class.mm
Class object_getClass(id obj)
{
if (obj) return obj->getIsa();
else return Nil;
}
//objc-private.h
struct objc_object {
private:
isa_t isa;
...
}
union isa_t
{
isa_t() { }
isa_t(uintptr_t value) : bits(value) { }
Class cls;
uintptr_t bits;
}
inline Class
objc_object::ISA()
{
assert(!isTaggedPointer());
return isa.cls;
}
//objc-object.h
inline Class
objc_object::getIsa()
{
#if SUPPORT_TAGGED_POINTERS
if (isTaggedPointer()) {
uintptr_t slot = ((uintptr_t)this >> TAG_SLOT_SHIFT) & TAG_SLOT_MASK;
return objc_tag_classes[slot];
}
#endif
return ISA();
}
//objc-runtime-old.h
struct objc_class : objc_object {
Class superclass;
const char *name;
uint32_t version;
uint32_t info;
uint32_t instance_size;
struct old_ivar_list *ivars;
struct old_method_list **methodLists;
Cache cache;
struct old_protocol_list *protocols;
// CLS_EXT only
const uint8_t *ivar_layout;
struct old_class_ext *ext;
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment