美文网首页
3、聊聊iOS中的Runtime

3、聊聊iOS中的Runtime

作者: 宁远_1号 | 来源:发表于2016-10-03 11:42 被阅读0次

    1、Runtime是什么?

    我们知道啊,在oc 中 之所以称之为动态语言,是因为有Runtime这一运行库,它使一套底层的C语言框架库,如果你想了解更多的运行时的底层实现,您可以移步Runtime源码,这里面有更多关于Runtime的底层实现,如果有C , C++ ,汇编基础建议你查看,其中objc_msgSend就是使用汇编实现的,如下

    
    /////////////////////////////////////////////////////////////////////
    //
    // CacheLookup  return-type
    //
    // Locate the implementation for a selector in a class's method cache.
    //
    // Takes: 
    //    $0 = NORMAL, STRET, SUPER, SUPER_STRET, SUPER2, SUPER2_STRET, GETIMP
    //    r0 or r1 (STRET) = receiver
    //    r1 or r2 (STRET) = selector
    //    r9 = class to search in
    //
    // On exit: r9 and r12 clobbered
    //      (found) calls or returns IMP, eq/ne/r9 set for forwarding
    //      (not found) jumps to LCacheMiss
    //
    /////////////////////////////////////////////////////////////////////
        
    .macro CacheHit
    
    .if $0 == GETIMP //将0 与GETIMP变量比较
        ldr r0, [r9, #4]        // r0 = bucket->imp ,从r9寄存器中取出后四位地址值,放到 r0中
        MI_GET_ADDRESS(r1, __objc_msgSend_uncached_impcache) ;这里会传递一个函数指针的地址近来
        teq r0, r1 //测试比较 r0 和 r1 是否相等,可以理解为 if ( r0 == r1) 测试是否等于
        it  eq // it 是if than 的缩写 , eq 是equal 缩写
        moveq   r0, #0          // don't return msgSend_uncached , 拆开理解 ,move equal 将 r0 寄存器中的值 与 0 进行比较,如果不为0  ,则设置为0
        bx  lr          // return imp //跳转到lr 连接寄存器(Link Register, LR)
    .elseif $0 == NORMAL
        ldr r12, [r9, #4]       // r12 = bucket->imp
                        // eq already set for nonstret forward
        MESSENGER_END_FAST
        bx  r12         // call imp
    .elseif $0 == STRET
        ldr r12, [r9, #4]       // r12 = bucket->imp
        movs    r9, #1          // r9=1, Z=0 for stret forwarding
        MESSENGER_END_FAST
        bx  r12         // call imp
    .elseif $0 == SUPER
        ldr r12, [r9, #4]       // r12 = bucket->imp
        ldr r9, [r0, #CLASS]    // r9 = class to search for forwarding
        ldr r0, [r0, #RECEIVER] // fetch real receiver
        tst r12, r12        // set ne for forwarding (r12!=0)
        MESSENGER_END_FAST
        bx  r12         // call imp
    .elseif $0 == SUPER2
        ldr r12, [r9, #4]       // r12 = bucket->imp
        ldr r9, [r0, #CLASS]
        ldr r9, [r9, #SUPERCLASS]   // r9 = class to search for forwarding
        ldr r0, [r0, #RECEIVER] // fetch real receiver
        tst r12, r12        // set ne for forwarding (r12!=0)
        MESSENGER_END_FAST
        bx  r12         // call imp
    .elseif $0 == SUPER_STRET
        ldr r12, [r9, #4]       // r12 = bucket->imp
        ldr r9, [r1, #CLASS]    // r9 = class to search for forwarding
        orr r9, r9, #1      // r9 = class|1 for super_stret forward
        ldr r1, [r1, #RECEIVER] // fetch real receiver
        tst r12, r12        // set ne for forwarding (r12!=0)
        MESSENGER_END_FAST
        bx  r12         // call imp
    .elseif $0 == SUPER2_STRET
        ldr r12, [r9, #4]       // r12 = bucket->imp
        ldr r9, [r1, #CLASS]    // r9 = class to search for forwarding
        ldr r9, [r9, #SUPERCLASS]   // r9 = class to search for forwarding
        orr r9, r9, #1      // r9 = class|1 for super_stret forward
        ldr r1, [r1, #RECEIVER] // fetch real receiver
        tst r12, r12        // set ne for forwarding (r12!=0)
        MESSENGER_END_FAST
        bx  r12         // call imp
    .else
    .abort oops
    .endif
    
    .endmacro
    
    
    
    • 2、上面的汇编代码,能看懂大概就行,没必要深究,配上消息机制图或许好理解些
    4196_131216091606_1.png
    • 3 消息机制详解请移步这里

    2、Runtime 头文件 简单介绍

    因为头文件篇幅比较长,所以没有再此展示,请移步runtime头文件简要翻译

    3 Runtime 用途

    • 1、字典转模型
    • 2 、解档归档优化代码
    • 3 、给category添加属性
    • 4、获取系统私有成员变量及其方法
    • 5、使用Method Swizzling 实现偷梁换柱,怎么使用您可以移步Method Swizzling

    Runtime 相关学习链接

    1、南峰子的技术博客
    2、runtime 装逼指南

    相关文章

      网友评论

          本文标题:3、聊聊iOS中的Runtime

          本文链接:https://www.haomeiwen.com/subject/potjyttx.html