OC底層-消息發(fā)送機(jī)制

方法

類(lèi)和對(duì)象篇中,我們了解到,方法存放在類(lèi)中.那么問(wèn)題來(lái)了.方法長(zhǎng)啥樣呢?

method_t

struct method_t {
    struct big {
        SEL name;
        const char *types;
        MethodListIMP imp;
    };
}

從源碼中,我們得知objc_class中有一個(gè)類(lèi)型為method_array_t的二位數(shù)組的成員methods.扒開(kāi)method_array_t的類(lèi)型,我們找到了最終的method_t,就是方法函數(shù)本身的樣子.可以看到.一個(gè)method_t中包含3個(gè)屬性

  • name:方法名
  • types:編碼(包含參數(shù),返回值類(lèi)型).TypeEncoding的方式
  • imp:方法實(shí)現(xiàn)

TypeEncoding

由蘋(píng)果定制的一系列方法返回值類(lèi)型,以及參數(shù)類(lèi)型的編碼規(guī)則.如下圖


image-20210521095538614.png

image-20210521095546866.png

OC方法調(diào)用機(jī)制

objc_msgSend

OC中,方法調(diào)用最終轉(zhuǎn)換為objc_msgSend調(diào)用.這種方式稱(chēng)為 消息機(jī)制,即發(fā)送消息給方法調(diào)用者.

  • 消息接收者(receiver):調(diào)用方法的對(duì)象

  • 消息名稱(chēng):@selector(xx)

    [a foo] //實(shí)例方法
    objc_msgSend(a, @selector(foo))
    [A foo] //類(lèi)方法
    objc_msgSend(objc_getClass("A"),@selector(foo))

objc_msgSend主要分為三個(gè)階段

  • 消息發(fā)送
  • 動(dòng)態(tài)方法解析
  • 消息轉(zhuǎn)發(fā)

消息發(fā)送

蘋(píng)果開(kāi)放的源碼中,objc_msgsend以匯編的方式實(shí)現(xiàn).匯編

    ENTRY _objc_msgSend
    UNWIND _objc_msgSend, NoFrame

    cmp p0, #0          // nil check and tagged pointer check
#if SUPPORT_TAGGED_POINTERS
    b.le    LNilOrTagged        //  (MSB tagged pointer looks negative)
#else
    b.eq    LReturnZero
#endif
    ldr p13, [x0]       // p13 = isa
    GetClassFromIsa_p16 p13, 1, x0  // p16 = class
LGetIsaDone:
    // calls imp or objc_msgSend_uncached
    CacheLookup NORMAL, _objc_msgSend, __objc_msgSend_uncached

#if SUPPORT_TAGGED_POINTERS
LNilOrTagged:
    b.eq    LReturnZero     // nil check
    GetTaggedClass
    b   LGetIsaDone
// SUPPORT_TAGGED_POINTERS
#endif

LReturnZero:
    // x0 is already zero
    mov x1, #0
    movi    d0, #0
    movi    d1, #0
    movi    d2, #0
    movi    d3, #0
    ret

    END_ENTRY _objc_msgSend
LNilOrTagged

如果消息接收者是nil,直接return

CacheLookup

確定消息接收者不為空,查找緩存.如果緩存命中,調(diào)用_objc_msgSend,如果未命中.調(diào)用__objc_msgSend_uncached再調(diào)用MethodTableLookup,再調(diào)用_lookUpImpOrForward

lookUpImpOrForward
IMP lookUpImpOrForward(id inst, SEL sel, Class cls, int behavior)
{
    for (unsigned attempts = unreasonableClassCount();;) {
        if (curClass->cache.isConstantOptimizedCache(/* strict */true)) {
#if CONFIG_USE_PREOPT_CACHES
            imp = cache_getImp(curClass, sel);
            if (imp) goto done_unlock;
            curClass = curClass->cache.preoptFallbackClass();
#endif
        } else {
            // curClass method list.
            Method meth = getMethodNoSuper_nolock(curClass, sel);
            if (meth) {
                imp = meth->imp(false);
                goto done;
            }

            if (slowpath((curClass = curClass->getSuperclass()) == nil)) {
                // No implementation found, and method resolver didn't help.
                // Use forwarding.
                imp = forward_imp;
                break;
            }
        }

        // Halt if there is a cycle in the superclass chain.
        if (slowpath(--attempts == 0)) {
            _objc_fatal("Memory corruption in class list.");
        }

        // Superclass cache.
        imp = cache_getImp(curClass, sel);
        if (slowpath(imp == forward_imp)) {
            // Found a forward:: entry in a superclass.
            // Stop searching, but don't cache yet; call method
            // resolver for this class first.
            break;
        }
        if (fastpath(imp)) {
            // Found the method in a superclass. Cache it in this class.
            goto done;
        }
    }

    // No implementation found. Try method resolver once.
    if (slowpath(behavior & LOOKUP_RESOLVER)) {
        behavior ^= LOOKUP_RESOLVER;
        return resolveMethod_locked(inst, sel, cls, behavior);
    }
 done:
    if (fastpath((behavior & LOOKUP_NOCACHE) == 0)) {
#if CONFIG_USE_PREOPT_CACHES
        while (cls->cache.isConstantOptimizedCache(/* strict */true)) {
            cls = cls->cache.preoptFallbackClass();
        }
#endif
        log_and_fill_cache(cls, imp, sel, inst, curClass);
    }
 done_unlock:
    runtimeLock.unlock();
    if (slowpath((behavior & LOOKUP_NIL) && imp == forward_imp)) {
        return nil;
    }
    return imp;
}

這里我截取了lookUpImpOrForward的部分代碼.可以得出.

  • 1.cache_getImp從類(lèi)的緩存方法列表尋找,若命中,直接返回imp,
  • 2.未命中,調(diào)用getMethodNoSuper_nolock嘗試從類(lèi)的方法列表中查找方法.
    • 2.1命中,則調(diào)用cache_t::insert將方法緩存到消息接收者緩存列表中,并返回imp供消息接收者調(diào)用.
    • 2.2未命中,則找到父類(lèi),重新執(zhí)行步驟1,
    • 2.3若未命中,執(zhí)行2.2
  • 2.直到父類(lèi)為nil時(shí),imp未命中,則進(jìn)入動(dòng)態(tài)解析resolveMethod_locked
getMethodNoSuper_nolock
static method_t *
getMethodNoSuper_nolock(Class cls, SEL sel)
{
    runtimeLock.assertLocked();
    ASSERT(cls->isRealized());
    auto const methods = cls->data()->methods();
    for (auto mlists = methods.beginLists(),
              end = methods.endLists();
         mlists != end;
         ++mlists)
    {
        method_t *m = search_method_list_inline(*mlists, sel);
        if (m) return m;
    }
    return nil;
}

static method_t *
search_method_list_inline(const method_list_t *mlist, SEL sel)
{
    int methodListIsFixedUp = mlist->isFixedUp();
    int methodListHasExpectedSize = mlist->isExpectedSize();
    
    if (fastpath(methodListIsFixedUp && methodListHasExpectedSize)) {
        return findMethodInSortedMethodList(sel, mlist);
    } else {
        // Linear search of unsorted method list
        if (auto *m = findMethodInUnsortedMethodList(sel, mlist))
            return m;
    }
}

可以看到搜索方法列表的search_method_list_inline方法中,對(duì)已經(jīng)排好序的方法列表是進(jìn)行二分查找.而未排序的,則采用遍歷查找

動(dòng)態(tài)方法解析

resolveMethod_locked
static NEVER_INLINE IMP
resolveMethod_locked(id inst, SEL sel, Class cls, int behavior)
{
    runtimeLock.assertLocked();
    ASSERT(cls->isRealized());

    runtimeLock.unlock();

    if (! cls->isMetaClass()) {
        // try [cls resolveInstanceMethod:sel]
        resolveInstanceMethod(inst, sel, cls);
    } 
    else {
        // try [nonMetaClass resolveClassMethod:sel]
        // and [cls resolveInstanceMethod:sel]
        resolveClassMethod(inst, sel, cls);
        if (!lookUpImpOrNilTryCache(inst, sel, cls)) {
            resolveInstanceMethod(inst, sel, cls);
        }
    }
    // chances are that calling the resolver have populated the cache
    // so attempt using it
    return lookUpImpOrForwardTryCache(inst, sel, cls, behavior);
}

動(dòng)態(tài)解析時(shí),根據(jù)類(lèi)或者元類(lèi)屬性來(lái)分別調(diào)用resolveInstanceMethod,resolveClassMethod

實(shí)現(xiàn)動(dòng)態(tài)方法解析

每一個(gè)NSObject類(lèi),都存在下述方法供我們處理動(dòng)態(tài)方法解析.

+ (BOOL)resolveClassMethod:(SEL)sel OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
+ (BOOL)resolveInstanceMethod:(SEL)sel OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

那么怎么處理呢?上代碼吧

@interface Test : NSObject
+ (void)test;
- (void)test;
@end

@implementation Test

+ (BOOL)resolveClassMethod:(SEL)sel {
    if (sel == @selector(test)) {
        Method method = class_getClassMethod(self, @selector(handleResolveClassMethod));
        class_addMethod(object_getClass(self), sel, method_getImplementation(method), method_getTypeEncoding(method));
        return YES;
    }
    return [super resolveClassMethod:sel];
}

+ (void)handleResolveClassMethod {
    NSLog(@"%s",__func__);
}

+ (BOOL)resolveInstanceMethod:(SEL)sel {
    if (sel == @selector(test)) {
        Method method = class_getInstanceMethod(self, @selector(handleResolveInstanceMethod));
        class_addMethod(self, sel, method_getImplementation(method), method_getTypeEncoding(method));
        return YES;
    }
    return [super resolveInstanceMethod:sel];
}

- (void)handleResolveInstanceMethod {
    NSLog(@"%s",__func__);
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        [Test test];
        [[Test new] test];
    }
    return 0;
}

值得注意的是.類(lèi)方法存在元類(lèi)里面.所以用runtime動(dòng)態(tài)添加類(lèi)方法時(shí),記得要找到類(lèi)的元類(lèi)進(jìn)行添加.

而如果是添加實(shí)例方法,則直接傳入self即可.

當(dāng)我們動(dòng)態(tài)為類(lèi)添加了對(duì)應(yīng)的方法實(shí)現(xiàn)后.會(huì)重新走一次objc_msgSend

_lookUpImpTryCache
ALWAYS_INLINE
static IMP _lookUpImpTryCache(id inst, SEL sel, Class cls, int behavior)
{
    runtimeLock.assertUnlocked();

    if (slowpath(!cls->isInitialized())) {
        // see comment in lookUpImpOrForward
        return lookUpImpOrForward(inst, sel, cls, behavior);
    }

    IMP imp = cache_getImp(cls, sel);
    if (imp != NULL) goto done;
#if CONFIG_USE_PREOPT_CACHES
    if (fastpath(cls->cache.isConstantOptimizedCache(/* strict */true))) {
        imp = cache_getImp(cls->cache.preoptFallbackClass(), sel);
    }
#endif
    if (slowpath(imp == NULL)) {
        return lookUpImpOrForward(inst, sel, cls, behavior);
    }

done:
    if ((behavior & LOOKUP_NIL) && imp == (IMP)_objc_msgForward_impcache) {
        return nil;
    }
    return imp;
}

從方法可以看出.實(shí)現(xiàn)動(dòng)態(tài)方法解析后,也是重新走消息發(fā)送的流程.先從方法緩存列表找起.再走lookUpImpOrForward

到這一步,動(dòng)態(tài)方法解析基本走完.如果我們沒(méi)有實(shí)現(xiàn)動(dòng)態(tài)方法解析的話(huà),就會(huì)進(jìn)入第三階段.消息轉(zhuǎn)發(fā)

消息轉(zhuǎn)發(fā)

消息轉(zhuǎn)發(fā)

找遍了源碼.也找不到消息轉(zhuǎn)發(fā)相關(guān)的東西.但是從最終崩潰的調(diào)用棧來(lái)看.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[Test test]: unrecognized selector sent to class 0x100008208'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff206206af __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00000001002fbb80 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff206a2bdd __CFExceptionProem + 0
    3   CoreFoundation                      0x00007fff2058807d ___forwarding___ + 1467
    4   CoreFoundation                      0x00007fff20587a38 _CF_forwarding_prep_0 + 120
    5   KCObjcBuild                         0x0000000100003ee5 main + 53
    6   libdyld.dylib                       0x00007fff204c9621 start + 1
)

看到了forwarding,進(jìn)入該調(diào)用棧的匯編.發(fā)現(xiàn)有一句注釋講到未實(shí)現(xiàn)methodSignatureForSelector,那我們嘗試實(shí)現(xiàn)一下.

0x7fff20587fbe <+1276>: leaq   0x5febb1ab(%rip), %rsi    ; @"*** NSForwarding: warning: object %p of class '%s' does not implement methodSignatureForSelector: -- did you forget to declare the superclass of '%s'?"

methodSignatureForSelector

+ (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector

可以看到這個(gè)方法需要返回一個(gè)NSMethodSignature.查看該類(lèi)的初始化方法,我們需要傳入方法實(shí)現(xiàn)的Method_t的types參數(shù).也就是typeEncoding.那我們?cè)囋噷?shí)現(xiàn)一下.我們?cè)赥est類(lèi)中添加如下代碼,然后運(yùn)行

+ (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    if (aSelector == @selector(test)) {
        return [NSMethodSignature signatureWithObjCTypes:"v16@0:8"];
    }
    return [super methodSignatureForSelector:aSelector];
}

運(yùn)行過(guò)后繼續(xù)報(bào)錯(cuò).繼續(xù)輸出調(diào)用棧.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[Test test]: unrecognized selector sent to class 0x100008258'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff206206af __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00000001002fbb80 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff206a2bdd __CFExceptionProem + 0
    3   libobjc.A.dylib                     0x0000000100350957 +[NSObject forwardInvocation:] + 103
    4   CoreFoundation                      0x00007fff20587e07 ___forwarding___ + 837
    5   CoreFoundation                      0x00007fff20587a38 _CF_forwarding_prep_0 + 120
    6   KCObjcBuild                         0x0000000100003ea3 main + 51
    7   libdyld.dylib                       0x00007fff204c9621 start + 1
)

forwardInvocation

此時(shí)可以看到.多了一個(gè)[NSObject forwardInvocation:].

+ (void)forwardInvocation:(NSInvocation *)invocation {
    [self doesNotRecognizeSelector:(invocation ? [invocation selector] : 0)];
}

- (void)forwardInvocation:(NSInvocation *)invocation {
    [self doesNotRecognizeSelector:(invocation ? [invocation selector] : 0)];
}

// Replaced by CF (throws an NSException)
+ (void)doesNotRecognizeSelector:(SEL)sel {
    _objc_fatal("+[%s %s]: unrecognized selector sent to instance %p", 
                class_getName(self), sel_getName(sel), self);
}

進(jìn)入NSObject的源碼中發(fā)現(xiàn).調(diào)用了doesNotRecognizeSelector方法.最終發(fā)現(xiàn)了萬(wàn)惡之源...

就是每次崩潰時(shí)輸出的unrecognized selector sent to class錯(cuò)誤

NSInvocation

@interface NSInvocation : NSObject

+ (NSInvocation *)invocationWithMethodSignature:(NSMethodSignature *)sig;

@property (readonly, retain) NSMethodSignature *methodSignature;

- (void)retainArguments;
@property (readonly) BOOL argumentsRetained;

@property (nullable, assign) id target;
@property SEL selector;

- (void)getReturnValue:(void *)retLoc;
- (void)setReturnValue:(void *)retLoc;

- (void)getArgument:(void *)argumentLocation atIndex:(NSInteger)idx;
- (void)setArgument:(void *)argumentLocation atIndex:(NSInteger)idx;

- (void)invoke;
- (void)invokeWithTarget:(id)target;

@end

我們?cè)趂orwardInvocation中加入斷點(diǎn).看看anInvocation放了什么東西.

(lldb) po anInvocation
<NSInvocation: 0x10070f420>
return value: {v} void
target: {@} 0x100008298
selector: {:} test

(lldb) po 0x100008298
Test

可以看到.anInvocation里面放了要執(zhí)行的方法名以及執(zhí)行它的target.如果我們此時(shí)調(diào)用invoke.那還是會(huì)繼續(xù)報(bào)找不到方法錯(cuò)誤.那么怎么解決呢.如果我們讓其他實(shí)現(xiàn)了test方法的類(lèi)來(lái)作為target.是不是就可以呢.讓我們?cè)囋?/p>

@interface Test1 : NSObject
@end

@implementation Test1

+ (void)test {
    NSLog(@"%s",__func__);
}

@end

@interface Test : NSObject
+ (void)test;
@end

@implementation Test
+ (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    if (aSelector == @selector(test)) {
        return [NSMethodSignature signatureWithObjCTypes:"v16@0:8"];
    }
    return [super methodSignatureForSelector:aSelector];
}

+ (void)forwardInvocation:(NSInvocation *)anInvocation {
    [anInvocation invokeWithTarget:[Test1 class]];
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        [Test test];
    }
    return 0;
}

我們發(fā)現(xiàn).最終通過(guò)消息轉(zhuǎn)發(fā).Test1成為了消息接收者并完成了方法的調(diào)用.到此消息轉(zhuǎn)發(fā)就結(jié)束了.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末费封,一起剝皮案震驚了整個(gè)濱河市替废,隨后出現(xiàn)的幾起案子瘾境,更是在濱河造成了極大的恐慌砸泛,老刑警劉巖威彰,帶你破解...
    沈念sama閱讀 206,126評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件狗准,死亡現(xiàn)場(chǎng)離奇詭異购笆,居然都是意外死亡拢切,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén)猎物,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)虎囚,“玉大人,你說(shuō)我怎么就攤上這事蔫磨√约ィ” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 152,445評(píng)論 0 341
  • 文/不壞的土叔 我叫張陵堤如,是天一觀(guān)的道長(zhǎng)蒲列。 經(jīng)常有香客問(wèn)我,道長(zhǎng)搀罢,這世上最難降的妖魔是什么蝗岖? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,185評(píng)論 1 278
  • 正文 為了忘掉前任,我火速辦了婚禮榔至,結(jié)果婚禮上抵赢,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好铅鲤,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,178評(píng)論 5 371
  • 文/花漫 我一把揭開(kāi)白布划提。 她就那樣靜靜地躺著,像睡著了一般邢享。 火紅的嫁衣襯著肌膚如雪鹏往。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 48,970評(píng)論 1 284
  • 那天骇塘,我揣著相機(jī)與錄音掸犬,去河邊找鬼。 笑死绪爸,一個(gè)胖子當(dāng)著我的面吹牛湾碎,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播奠货,決...
    沈念sama閱讀 38,276評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼介褥,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了递惋?” 一聲冷哼從身側(cè)響起柔滔,我...
    開(kāi)封第一講書(shū)人閱讀 36,927評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎萍虽,沒(méi)想到半個(gè)月后睛廊,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,400評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡杉编,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,883評(píng)論 2 323
  • 正文 我和宋清朗相戀三年超全,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片邓馒。...
    茶點(diǎn)故事閱讀 37,997評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡嘶朱,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出光酣,到底是詐尸還是另有隱情疏遏,我是刑警寧澤,帶...
    沈念sama閱讀 33,646評(píng)論 4 322
  • 正文 年R本政府宣布救军,位于F島的核電站财异,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏唱遭。R本人自食惡果不足惜戳寸,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,213評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望胆萧。 院中可真熱鬧庆揩,春花似錦俐东、人聲如沸跌穗。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,204評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蚌吸。三九已至锈拨,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間羹唠,已是汗流浹背奕枢。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,423評(píng)論 1 260
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留佩微,地道東北人缝彬。 一個(gè)月前我還...
    沈念sama閱讀 45,423評(píng)論 2 352
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像哺眯,于是被迫代替她去往敵國(guó)和親谷浅。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,722評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容