美文网首页
weak 的实现

weak 的实现

作者: forping | 来源:发表于2020-11-20 13:50 被阅读0次

    代码如下

    - (void)testWeak{
        __weak id weaSelf = self;
    }
    

    转成汇编

    "-[A testWeak]":                        ; @"\01-[A testWeak]"
        sub sp, sp, #48             ; =48
        stp x29, x30, [sp, #32]     ; 16-byte Folded Spill
        add x29, sp, #32            ; =32
        stur    x0, [x29, #-8]
        str x1, [sp, #16]
        ldur    x1, [x29, #-8]
        add x8, sp, #8              ; =8
        mov x0, x8
        str x8, [sp]                ; 8-byte Folded Spill
        bl  _objc_initWeak
        ldr x8, [sp]                ; 8-byte Folded Reload
        mov x0, x8
        bl  _objc_destroyWeak
        ldp x29, x30, [sp, #32]     ; 16-byte Folded Reload
        add sp, sp, #48             ; =48
        ret
    

    调用了 _objc_initWeak 和 _objc_destroyWeak 函数
    去runtime里去找对应的实现

    id
    objc_initWeak(id *location, id newObj)
    {
        if (!newObj) {
            *location = nil;
            return nil;
        }
    
        return storeWeak<DontHaveOld, DoHaveNew, DoCrashIfDeallocating>
            (location, (objc_object*)newObj);
    }
    
    void
    objc_destroyWeak(id *location)
    {
        (void)storeWeak<DoHaveOld, DontHaveNew, DontCrashIfDeallocating>
            (location, nil);
    }
    

    未完待续~

    相关文章

      网友评论

          本文标题:weak 的实现

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