iOS開發之 runtime(35) :其他的一些 section 講解

本系列博客是本人的源碼閱讀筆記,如果有 iOS 開發者在看 runtime 的,歡迎大家多多交流。爲了方便討論,本人新建了一個微信羣(iOS技術討論羣),想要加入的,請添加本人微信:zhujinhui207407,【加我前請備註:ios 】,本人博客http://www.kyson.cn 也在不停的更新中,歡迎一起討論

本文完整版詳見筆者小專欄:https://xiaozhuanlan.com/runtime

前言

今天是最後一篇講解 _read_images 的文章了,相信大家已經厭煩一直在講 _read_images 這個方法了。但畢竟這個方法確實很重要,所以筆者纔在這裏着墨最多。今天筆者將最後的幾個代碼塊貼出來,大家一起看看吧。

詳細

獲取所有的類引用

if (!noClassesRemapped()) {
    for (EACH_HEADER) {
        Class *classrefs = _getObjc2ClassRefs(hi, &count);
        for (i = 0; i < count; i++) {
//這裏筆者打印出了相關信息
            printf("ClassRefs:%s",classrefs[i]->mangledName());
            fflush(stdout);
            remapClassRef(&classrefs[i]);
        }
        // fixme why doesn't test future1 catch the absence of this?
        classrefs = _getObjc2SuperRefs(hi, &count);
        for (i = 0; i < count; i++) {
            remapClassRef(&classrefs[i]);
        }
    }
}

獲取所有的方法引用

static size_t UnfixedSelectors;
for (EACH_HEADER) {
    if (hi->isPreoptimized()) continue;
    //        printf("name:%s\n",hi->fname());
    //        fflush(stdout);
    bool isBundle = hi->isBundle();
    SEL *sels = _getObjc2SelectorRefs(hi, &count);
    UnfixedSelectors += count;
    for (i = 0; i < count; i++) {
        const char *name = sel_cname(sels[i]);
        //            printf("name:%s\n",name);
        //            fflush(stdout);
        sels[i] = sel_registerNameNoLock(name, isBundle);
    }
}

獲取所有的協議列表

for (EACH_HEADER) {
//以下兩行是筆者加的調試信息
    printf("class name:%s\n",hi->fname());
    fflush(stdout);
    extern objc_class OBJC_CLASS_$_Protocol;
    Class cls = (Class)&OBJC_CLASS_$_Protocol;
    assert(cls);
    NXMapTable *protocol_map = protocols();
    bool isPreoptimized = hi->isPreoptimized();
    bool isBundle = hi->isBundle();
    
    protocol_t **protolist = _getObjc2ProtocolList(hi, &count);
    for (i = 0; i < count; i++) {
//以下兩行是筆者加的調試信息
        printf("protocol :%s\n",protolist[i]->nameForLogging());
        fflush(stdout);
        readProtocol(protolist[i], cls, protocol_map,
                     isPreoptimized, isBundle);
    }
}

這個代碼段筆者要稍微說一下,因爲這個變量名可能大家有點不能理解:

OBJC_CLASS_$_Protocol

全局搜索,我們發現,其實是定義在 彙編文件

objc-sel-table.s

中的。筆者將打印出來的信息放到筆者的 GitHub 中了。供大家查閱,地址在這裏:
protocollist.txt
大家可能留意到文件最後有個 KysonHere 協議,其實是筆者自己加的,這裏筆者將自己寫的測試類,以及協議代碼貼出來:

@protocol KysonHere <NSObject>
@optional
-(void) kysonIsHer;
@end

也算是筆者的一段測試代碼了。

獲取所有的協議引用列表

for (EACH_HEADER) {
    protocol_t **protolist = _getObjc2ProtocolRefs(hi, &count);
    for (i = 0; i < count; i++) {
        //以下兩行爲筆者的測試代碼
        printf("ref protocol :%s\n",protolist[i]->nameForLogging());
        fflush(stdout);
        remapProtocolRef(&protolist[i]);
    }
}

返回的是空。

獲取所有的分類列表

// Discover categories.
for (EACH_HEADER) {
    category_t **catlist = _getObjc2CategoryList(hi, &count);
    bool hasClassProperties = hi->info()->hasCategoryClassProperties();
    
    for (i = 0; i < count; i++) {
        category_t *cat = catlist[i];
        Class cls = remapClass(cat->cls);
        
        if (!cls) {
            catlist[i] = nil;
            continue;
        }
        
        bool classExists = NO;
        if (cat->instanceMethods ||  cat->protocols
            ||  cat->instanceProperties)
        {
            addUnattachedCategoryForClass(cat, cls, hi);
            if (cls->isRealized()) {
                remethodizeClass(cls);
                classExists = YES;
            }
        }
        
        if (cat->classMethods  ||  cat->protocols
            ||  (hasClassProperties && cat->_classProperties))
        {
            addUnattachedCategoryForClass(cat, cls->ISA(), hi);
            if (cls->ISA()->isRealized()) {
                remethodizeClass(cls->ISA());
            }
        }
    }
}

總結

_read_images 方法終於告一段落了,希望大家有所收穫!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章