面试专题

01--方法本质04--lookUpImpOrForward 介

2020-06-15  本文已影响0人  修_远

对方法的探索,全篇分六个章节

01-方法本质-方法初探

02-方法本质-objc_msgSend的使用

03-方法本质-面试题分析

04-方法本质-lookUpImpOrForward 介绍

05-方法本质-消息查找流程

06-方法本质-消息转发流程

lookUpImpOrForward 方法介绍

前言

前面三个章节中讲了很多关于oc中方法的使用,以及一些常用方法的分析。但那都只是停留在上层oc代码的使用。

本章节主要介绍 lookUpImpOrForward 方法的注释和参数

从后面两个章节将从源码的角度分析消息到底是如何发送的。

[toc]

4.1 lookUpImpOrForward 方法来源

最终跟踪到了 lookUpImpOrForward 方法,所以这个方法是我们后面的重点。从方法名的含义可以猜出来是 查找方法和转发,用中文语法润滑一下就是我们的主题--方法的查找和转发流程。这篇文章主要讲方法的查找流程。

4.1.1 从汇编调试找到 lookUpImpOrForward 方法

在oc工程中查看lookUpImpOrForward流程

  1. 断点入口,打开汇编调试开关


    image
  2. 找到 objc_msgSend 流程,按住 ctrl+stepin,进入流程


    image
  3. 看到我们熟悉的 _objc_msgSend_uncached 流程,继续下一步


    image
  4. 来到熟悉的 lookUpImpOrForward 流程,继续下一步


    image
  5. 这里就是 lookUpImpOrForward 流程分析了,后面的过程跟之前源码分析流程一致


    image

4.2 lookUpImpOrForward 方法源码初识(重点)

虽然这个方法的源码会很长,但这个方法很重要,所以笔者思考之后还是选择了将代码贴出来,没有做任何删减,保留了苹果的原始代码。

4.2.1 lookUpImpOrForward 方法注释和参数

看一个方法肯定是从注释先看起

/***********************************************************************
* lookUpImpOrForward。
* The standard IMP lookup。
* initialize==NO tries to avoid +initialize (but sometimes fails)
* cache==NO skips optimistic unlocked lookup (but uses cache elsewhere)
* Most callers should use initialize==YES and cache==YES。
* inst is an instance of cls or a subclass thereof,or nil if none is known。
*   If cls is an un-initialized metaclass then a non-nil inst is faster。
* May return _objc_msgForward_impcache。IMPs destined for external use 
*   must be converted to _objc_msgForward or _objc_msgForward_stret。
*   If you don't want forwarding at all,use lookUpImpOrNil() instead。
**********************************************************************/
IMP lookUpImpOrForward(Class cls,SEL sel,id inst,
                       bool initialize,bool cache,bool resolver)

注释内容

  1. 标准的方法查找函数
  2. 大多数时间的调用使用
    initialize==YES
    cache==YES
  3. 可以返回_objc_msgForward_impcache。用于外部使用的imp必须转换为_objc_msgForward_objc_msgForward_stret
  4. 如果不想转发,可以使用 lookUpImpOrNil() 方法

参数说明

  1. 参数一:Class cls
    指定查找的类
  2. 参数二:SEL sel
    指定查找的方法
  3. 参数三:id inst
    cls或其子类的一个实例,如果不知道,则为nil;如果cls是一个未初始化的元类,那么非空的inst会更快。
  4. 参数四:bool initialize
    initialize==NO,视图避免 +initialize (有时会失败)
  5. 参数五:bool cache
    cache==NO,指定为NO的时候跳过 Optimistic cache lookup 流程
    参数六:bool resolver
    注释中没有做解释,但是是另一个流程的关键控制变量

不管是注释还是参数说明,这里都无法详尽解释,只要看完后面的流程在回过头来看才会有恍然大悟的感觉,废话不多说了,先来看看方法查找流程吧。

4.2.2 lookUpImpOrForward 方法源码

读者可以快速略过,后面会详细讲解这段代码。

/***********************************************************************
* lookUpImpOrForward。
* The standard IMP lookup。
* initialize==NO tries to avoid +initialize (but sometimes fails)
* cache==NO skips optimistic unlocked lookup (but uses cache elsewhere)
* Most callers should use initialize==YES and cache==YES。
* inst is an instance of cls or a subclass thereof,or nil if none is known。
*   If cls is an un-initialized metaclass then a non-nil inst is faster。
* May return _objc_msgForward_impcache。IMPs destined for external use 
*   must be converted to _objc_msgForward or _objc_msgForward_stret。
*   If you don't want forwarding at all,use lookUpImpOrNil() instead。
**********************************************************************/
IMP lookUpImpOrForward(Class cls,SEL sel,id inst,
                       bool initialize,bool cache,bool resolver)
{
    IMP imp = nil;
    bool triedResolver = NO;

    runtimeLock。assertUnlocked();

    // Optimistic cache lookup
    if (cache) {
        imp = cache_getImp(cls,sel);
        if (imp) return imp;
    }

    // runtimeLock is held during isRealized and isInitialized checking
    // to prevent races against concurrent realization。

    // runtimeLock is held during method search to make
    // method-lookup + cache-fill atomic with respect to method addition。
    // Otherwise,a category could be added but ignored indefinitely because
    // the cache was re-filled with the old value after the cache flush on
    // behalf of the category。

    runtimeLock。lock();
    checkIsKnownClass(cls);

    if (!cls->isRealized()) {
        realizeClass(cls);
    }

    if (initialize  &&  !cls->isInitialized()) {
        runtimeLock。unlock();
        _class_initialize (_class_getNonMetaClass(cls,inst));
        runtimeLock。lock();
        // If sel == initialize,_class_initialize will send +initialize and 
        // then the messenger will send +initialize again after this 
        // procedure finishes。Of course,if this is not being called 
        // from the messenger then it won't happen。2778172
    }

    
 retry:    
    runtimeLock。assertLocked();

    // Try this class's cache。

    imp = cache_getImp(cls,sel);
    if (imp) goto done;

    // Try this class's method lists。
    {
        Method meth = getMethodNoSuper_nolock(cls,sel);
        if (meth) {
            log_and_fill_cache(cls,meth->imp,sel,inst,cls);
            imp = meth->imp;
            goto done;
        }
    }

    // Try superclass caches and method lists。
    {
        unsigned attempts = unreasonableClassCount();
        for (Class curClass = cls->superclass;
             curClass != nil;
             curClass = curClass->superclass)
        {
            // Halt if there is a cycle in the superclass chain。
            if (--attempts == 0) {
                _objc_fatal("Memory corruption in class list。");
            }
            
            // Superclass cache。
            imp = cache_getImp(curClass,sel);
            if (imp) {
                if (imp != (IMP)_objc_msgForward_impcache) {
                    // Found the method in a superclass。Cache it in this class。
                    log_and_fill_cache(cls,imp,sel,inst,curClass);
                    goto done;
                }
                else {
                    // Found a forward:: entry in a superclass。
                    // Stop searching,but don't cache yet; call method 
                    // resolver for this class first。
                    break;
                }
            }
            
            // Superclass method list。
            Method meth = getMethodNoSuper_nolock(curClass,sel);
            if (meth) {
                log_and_fill_cache(cls,meth->imp,sel,inst,curClass);
                imp = meth->imp;
                goto done;
            }
        }
    }

    // No implementation found。Try method resolver once。

    if (resolver  &&  !triedResolver) {
        runtimeLock。unlock();
        _class_resolveMethod(cls,sel,inst);
        runtimeLock。lock();
        // Don't cache the result; we don't hold the lock so it may have 
        // changed already。Re-do the search from scratch instead。
        triedResolver = YES;
        goto retry;
    }

    // No implementation found,and method resolver didn't help。
    // Use forwarding。

    imp = (IMP)_objc_msgForward_impcache;
    cache_fill(cls,sel,imp,inst);

 done:
    runtimeLock。unlock();

    return imp;
}
上一篇 下一篇

猜你喜欢

热点阅读