Object-c Runtime 从代码到代码

关于Object C 中object, Class, Meta Class 的关系,在objc4的源代码中有如下的调用:

LibObjc 动态库被调用后,下面的函数会依次调用

asm

_map_images:

...

jmp _map_2_images

map_2_images ->

map_images_nolock ->

_read_images ->

_objc_connect_classes_from_image ->

connect_class ->

really_connect_class ->

set_superclass 

 

/***********************************************************************

* objc_allocateClass.

**********************************************************************/

void set_superclass(Class cls, Class supercls, bool cls_is_new)

{

    Class meta = cls->ISA();

 

    if (supercls) {

        cls->superclass = supercls;

        meta->superclass = supercls->ISA();

        meta->initIsa(supercls->ISA()->ISA());

 

        // Propagate C++ cdtors from superclass.

        if (supercls->info & CLS_HAS_CXX_STRUCTORS) {

            if (cls_is_new) cls->info |= CLS_HAS_CXX_STRUCTORS;

            else cls->setInfo(CLS_HAS_CXX_STRUCTORS);

        }

 

        // Superclass is no longer a leaf for cache flushing

        if (supercls->info & CLS_LEAF) {

            supercls->clearInfo(CLS_LEAF);

            supercls->ISA()->clearInfo(CLS_LEAF);

        }

    } else {

        cls->superclass = Nil// superclass of root class is nil

        meta->superclass = cls; // superclass of root metaclass is root class

        meta->initIsa(meta);    // metaclass of root metaclass is root metaclass

 

        // Root class is never a leaf for cache flushing, because the 

        // root metaclass is a subclass. (This could be optimized, but 

        // is too uncommon to bother.)

        cls->clearInfo(CLS_LEAF);

        meta->clearInfo(CLS_LEAF);

    }    

}

 

set_superclass 中的注释明确的说明了Class, meta Class,  Class Isa,  meta Isa, Class Super Class, meta super Class 之间的关系和赋值。

你可能感兴趣的:(Object-c Runtime 从代码到代码)