Objective-C底层探究之block(二)

Objective-C底层探究之block(一)

从前面我们知道了block调用其实就是函数的调用。block本身用结构体做了一些封装。那现在又有一个疑问。block中可以无缝使用外部的变量。加上__block关键字还可以在内部修改变量又是怎么实现呢
我们可以把前面的代码改一下:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int a = 10;
        int b = 11;
        int c = 13;
        int (^block)(int,int) = ^(int a, int b) {
            return a+b+c;
        };
        block(a,b);
    }
    return 0;
}

同样使用命令解析得到C++代码

struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  int c;
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _c, int flags=0) : c(_c) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};
static int __main_block_func_0(struct __main_block_impl_0 *__cself, int a, int b) {
  int c = __cself->c; // bound by copy

            return a+b+c;
        }

static struct __main_block_desc_0 {
  size_t reserved;
  size_t Block_size;
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0)};
int main(int argc, const char * argv[]) {
    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; 
        int a = 10;
        int b = 11;
        int c = 13;
        int (*block)(int,int) = ((int (*)(int, int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, c));
        ((int (*)(__block_impl *, int, int))((__block_impl *)block)->FuncPtr)((__block_impl *)block, a, b);
    }
    return 0;
}

我们来看看与之前例子不同的部分:

struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  int c;
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _c, int flags=0) : c(_c) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};
static int __main_block_func_0(struct __main_block_impl_0 *__cself, int a, int b) {
  int c = __cself->c; // bound by copy

            return a+b+c;
        }

发现__main_block_impl_0结构体里面多了一个变量c而在block创建的时候将c变量通过构造函数传了进来

int (*block)(int,int) = ((int (*)(int, int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, c));

也就是说c变量实质上是做了一层拷贝。而在函数调用时通过__cself参数拿到block的指针并把c拷贝一份局部变量后使用。
那加了__block关键字之后是怎么实现在内部改变外部值的呢,我们修改代码如下:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int a = 10;
        int b = 11;
        __block int c = 13;
        int (^block)(int,int) = ^(int a, int b) {
            c = 14;
            return a+b+c;
        };
        block(a,b);
    }
    return 0;
}

同样使用命令解析:

struct __Block_byref_c_0 {
  void *__isa;
__Block_byref_c_0 *__forwarding;
 int __flags;
 int __size;
 int c;
};

struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  __Block_byref_c_0 *c; // by ref
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_c_0 *_c, int flags=0) : c(_c->__forwarding) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};
static int __main_block_func_0(struct __main_block_impl_0 *__cself, int a, int b) {
  __Block_byref_c_0 *c = __cself->c; // bound by ref

            (c->__forwarding->c) = 14;
            return a+b+(c->__forwarding->c);
        }
static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->c, (void*)src->c, 8/*BLOCK_FIELD_IS_BYREF*/);}

static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->c, 8/*BLOCK_FIELD_IS_BYREF*/);}

static struct __main_block_desc_0 {
  size_t reserved;
  size_t Block_size;
  void (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*);
  void (*dispose)(struct __main_block_impl_0*);
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0};
int main(int argc, const char * argv[]) {
    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; 
        int a = 10;
        int b = 11;
        __attribute__((__blocks__(byref))) __Block_byref_c_0 c = {(void*)0,(__Block_byref_c_0 *)&c, 0, sizeof(__Block_byref_c_0), 13};
        int (*block)(int,int) = ((int (*)(int, int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, (__Block_byref_c_0 *)&c, 570425344));
        ((int (*)(__block_impl *, int, int))((__block_impl *)block)->FuncPtr)((__block_impl *)block, a, b);
    }
    return 0;
}

可以发现block里面多了一个指针 __Block_byref_c_0 类型的指针。而block外面加了__block 标记的变量c被解释成 __Block_byref_c_0 类型的结构体。并在调用时将c的地址传入block的构造函数中。block解析的结构体也多了一个指向__Block_byref_c_0对象的 c 指针用于储存该地址。这样就可以在block结构体内改变c变量的值了。

你可能感兴趣的:(Objective-C底层探究之block(二))