Skip to content

Commit

Permalink
Stop treating NSObject as magic and do what OS X does with regard to …
Browse files Browse the repository at this point in the history
…root objects and metaclasses.
  • Loading branch information
theraven committed Feb 23, 2011
1 parent 395c5fe commit ca2c7d1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 42 deletions.
63 changes: 31 additions & 32 deletions class_table.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "magic_objects.h"
#include "objc/runtime.h"
#include "objc/hooks.h"
#include "objc/developer.h"
Expand Down Expand Up @@ -161,21 +160,6 @@ BOOL objc_resolve_class(Class cls)
}


// Give up if we can't resolve the root class yet...
static Class root_class = Nil;
if (Nil == root_class)
{
root_class = (Class)objc_getClass(ROOT_OBJECT_CLASS_NAME);
if (Nil == root_class) { return NO; }

if (cls != root_class && !objc_test_class_flag(root_class, objc_class_flag_resolved))
{
objc_resolve_class(root_class);
}
assert(root_class);
}


// Remove the class from the unresolved class list
if (Nil == cls->unresolved_class_prev)
{
Expand All @@ -194,45 +178,60 @@ BOOL objc_resolve_class(Class cls)
cls->unresolved_class_prev = Nil;
cls->unresolved_class_next = Nil;

// Resolve the superclass pointer
// The superclass for the metaclass. This is the metaclass for the
// superclass if one exists, otherwise it is the root class itself
Class superMeta = Nil;
// The metaclass for the metaclass. This is always the root class's
// metaclass.
Class metaMeta = Nil;

// If this class has no superclass, use [NS]Object
Class super = root_class;
Class superMeta = root_class;
Class meta = cls->isa;
// Resolve the superclass pointer

if (NULL != cls->super_class)
if (NULL == cls->super_class)
{
superMeta = cls;
metaMeta = cls->isa;
}
else
{
// Resolve the superclass if it isn't already resolved
super = (Class)objc_getClass((char*)cls->super_class);
Class super = (Class)objc_getClass((char*)cls->super_class);
if (!objc_test_class_flag(super, objc_class_flag_resolved))
{
objc_resolve_class(super);
}
superMeta = super->isa;
// Set the superclass pointer for the class and the superclass
cls->super_class = super;
do
{
metaMeta = super->isa;
super = super->super_class;
} while (Nil != super);
}
Class meta = cls->isa;

// Don't make the root class a subclass of itself
if (cls != super)
// Make the root class the superclass of the metaclass (e.g. NSObject is
// the superclass of all metaclasses in classes that inherit from NSObject)
meta->super_class = superMeta;
meta->isa = metaMeta;

// Don't register root classes as children of anything
if (Nil != cls->super_class)
{
// Set up the class links
cls->sibling_class = super->subclass_list;
super->subclass_list = cls;
cls->sibling_class = cls->super_class->subclass_list;
cls->super_class->subclass_list = cls;
}
// Make the root class the superclass of the metaclass (e.g. NSObject is
// the superclass of all metaclasses)
meta->super_class = superMeta;
// Set up the metaclass links
meta->sibling_class = superMeta->subclass_list;
superMeta->subclass_list = meta;

// Mark this class (and its metaclass) as resolved
objc_set_class_flag(cls, objc_class_flag_resolved);
objc_set_class_flag(cls->isa, objc_class_flag_resolved);
cls->isa->isa = (Nil == cls->isa->isa) ? root_class->isa :
((Class)objc_getClass((char*)cls->isa->isa))->isa;


// Fix up the ivar offsets
objc_compute_ivar_offsets(cls);
// Send the +load message, if required
Expand Down
7 changes: 7 additions & 0 deletions constant_string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef CONSTANT_STRING_CLASS
# ifdef GNUSTEP
# define CONSTANT_STRING_CLASS "NSConstantString"
# else
# define CONSTANT_STRING_CLASS "NXConstantString"
# endif
#endif
1 change: 0 additions & 1 deletion loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "objc/runtime.h"
#include "lock.h"
#include "loader.h"
#include "magic_objects.h"

/**
* Runtime lock. This is exposed in
Expand Down
7 changes: 0 additions & 7 deletions magic_objects.h

This file was deleted.

4 changes: 2 additions & 2 deletions statics_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <stdio.h>
#include "objc/runtime.h"
#include "module.h"
#include "magic_objects.h"
#include "constant_string.h"

#define BUFFER_TYPE struct objc_static_instance_list
#include "buffer.h"
Expand All @@ -21,7 +21,7 @@ static BOOL try_init_statics(struct objc_static_instance_list *statics)
// NXConstantString instances. This is a mess. We hack around this by
// silently assuming that the user meant NSConstantString when they said
// NXConstantString if NSConstantString is set as the constant string class
// in magic_objects.h
// in string_class.h or by an external -D flag.
if (strcmp(class_name, "NXConstantString") == 0)
{
class_name = CONSTANT_STRING_CLASS;
Expand Down

0 comments on commit ca2c7d1

Please sign in to comment.