美文网首页
常见源码解析

常见源码解析

作者: 晨阳Xia | 来源:发表于2021-03-13 15:17 被阅读0次

    block源码

    
    struct __block_impl {
      void *isa;
      int Flags;
      int Reserved;
      void *FuncPtr;
    };
    
    static struct __ViewController__viewDidLoad_block_desc_0 {
      size_t reserved;
      size_t Block_size;
    } __ViewController__viewDidLoad_block_desc_0_DATA = { 0, sizeof(struct __ViewController__viewDidLoad_block_impl_0)};
    
    struct __ViewController__viewDidLoad_block_impl_0 {
      struct __block_impl impl;
      struct __ViewController__viewDidLoad_block_desc_0* Desc;
      int age;
      __ViewController__viewDidLoad_block_impl_0(void *fp, struct __ViewController__viewDidLoad_block_desc_0 *desc, int _age, int flags=0) : age(_age) {
        impl.isa = &_NSConcreteStackBlock;
        impl.Flags = flags;
        impl.FuncPtr = fp;
        Desc = desc;
      }
    };
    
    static void __ViewController__viewDidLoad_block_func_0(struct __ViewController__viewDidLoad_block_impl_0 *__cself) {
      int age = __cself->age; // bound by copy
    
            NSLog((NSString *)&__NSConstantStringImpl__var_folders_zs_r4g726_d3rd7y0hvw0_7ckym0000gn_T_ViewController_3bd104_mi_0,age);
        }
    
    
    static void _I_ViewController_viewDidLoad(ViewController * self, SEL _cmd) {
        ((void (*)(__rw_objc_super *, SEL))(void *)objc_msgSendSuper)((__rw_objc_super){(id)self, (id)class_getSuperclass(objc_getClass("ViewController"))}, sel_registerName("viewDidLoad"));
        int age = 10;
        void (*blockName)(void) = ((void (*)())&__ViewController__viewDidLoad_block_impl_0((void *)__ViewController__viewDidLoad_block_func_0, &__ViewController__viewDidLoad_block_desc_0_DATA, age));
        ((void (*)(__block_impl *))((__block_impl *)blockName)->FuncPtr)((__block_impl *)blockName);
    
    }
    

    class源码 typedef struct objc_class *Class;

    struct objc_class {    
        Class isa; 
        Class superclass;
        cache_t cache;
        class_data_bits_t bits;
    }
    
    struct class_rw_t {
        const class_ro_t * ro;
        method_array_t * methods;
        property_array_t * properties;
        protocol_array_t * protocols;
        ...
    }
    
    struct class_ro_t {
        const char * name;
        method_list_t * baseMethodList;
        protocol_list_t * baseProtocols;
        properte_list_t * baseProperties;
        ivar_list_t * ivars;
    }
    

    typedef struct objc_object *id;

    struct objc_object {
    private:m
        isa_t isa; // arm64之前是 Class *isa。arm64之后是isa_t isa。
    
    public:
        // ISA() assumes this is NOT a tagged pointer object
        Class ISA();
    };
    

    isa_t源码

    union isa_t {
        isa_t() { }
        isa_t(uintptr_t value) : bits(value) { }
    
        Class cls;
        uintptr_t bits;
    #if defined(ISA_BITFIELD)
        struct {
            ISA_BITFIELD;  // defined in isa.h
        };
    #endif
    };
    

    method_t源码

    struct method_t {
        SEL name; // 方法名
        const char *types; // 编码(返回值类型、参数类型)
        MethodListIMP imp; // 指向函数的指针
    
        struct SortBySELAddress :
            public std::binary_function<const method_t&,
                                        const method_t&, bool>
        {
            bool operator() (const method_t& lhs,
                             const method_t& rhs)
            { return lhs.name < rhs.name; }
        };
    };
    

    catch_t源码

    struct catch_t {
        struct bucket_t *_buckets; // 散列表
        mask_t _mask; // 散列表的长度-1
        mask_t _occupied; // 已经缓存的方法数量
        
    };
    

    bucket_t源码

    struct bucket_t {
        catch_key_t key; // SEL 作为key
        IMP _imp; // 函数的内存地址
    }
    

    category_t

    struct category_t {
        const char *name;
        classref_t cls;
        struct method_list_t * instanceMethods;
        strcut method_list_t * classMethods;
        struct protocol_list_t *ptotocols;
        struct property_list_t *instanceProperties;
        struct propertity_list_t *_classProperties;
        method_list_t *methodsForMeta(bool isMeta) {
            if (isMeta) return classMethods;
            else return instanceMethods;
        }
        property_list_t *propertiesForMeta(bool isMeta, struct header_info *hi);
    }
    

    相关文章

      网友评论

          本文标题:常见源码解析

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