1.截获自动变量
int value = 1;
void (^blk)(void) = ^{
printf("Block function :%d",value);
};
value = 2;
blk();
将上面代码进行clang转换
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
int value;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _value, int flags=0) : value(_value) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
int value = __cself->value; // bound by copy
printf("Block function :%d",value);
}
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, char * argv[]) {
int value = 1;
void (*blk)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, value));
value = 2;
((void (*)(__block_impl *))((__block_impl *)blk)->FuncPtr)((__block_impl *)blk);
return 0;
}
我们可以看到 value 被捕获到__main_block_impl_0结构体中了
2.__block 说明符
我们先看一下 静态全局变量 全局变量 和静态变量在clang中的表示
int globalValue = 0;
static int staticGlobalValue = 1;
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
int *staticValue;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int *_staticValue, int flags=0) : staticValue(_staticValue) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
int *staticValue = __cself->staticValue; // bound by copy
globalValue += 1;
staticGlobalValue += 1;
(*staticValue) += 1;
printf("Block function :%d",(*staticValue));
}
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, char * argv[]) {
static int staticValue = 2;
void (*blk)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, &staticValue));
((void (*)(__block_impl *))((__block_impl *)blk)->FuncPtr)((__block_impl *)blk);
return 0;
}
我们看到只有静态变量进行了转换,全局和动态变量都和之前完全相同,我们再来看把静态变量转成__block
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
__Block_byref_staticValue_0 *staticValue; // by ref
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_staticValue_0 *_staticValue, int flags=0) : staticValue(_staticValue->__forwarding) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
__Block_byref_staticValue_0 *staticValue = __cself->staticValue; // bound by ref
globalValue += 1;
staticGlobalValue += 1;
(staticValue->__forwarding->staticValue) += 1;
printf("Block function :%d",(staticValue->__forwarding->staticValue));
}
static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->staticValue, (void*)src->staticValue, 8/*BLOCK_FIELD_IS_BYREF*/);}
static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->staticValue, 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, char * argv[]) {
__attribute__((__blocks__(byref))) __Block_byref_staticValue_0 staticValue = {(void*)0,(__Block_byref_staticValue_0 *)&staticValue, 0, sizeof(__Block_byref_staticValue_0), 2};
void (*blk)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, (__Block_byref_staticValue_0 *)&staticValue, 570425344));
((void (*)(__block_impl *))((__block_impl *)blk)->FuncPtr)((__block_impl *)blk);
return 0;
}
此时__block 竟然变成__Block_byref_staticValue_0结构体,给staticValue静态变量赋值的时候,使用了指向该静态变量的指针,而向__block变量赋值的时候__Block_byref_staticValue_0 结构体的成员变量__forwarding 持有该实例自身的指针
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
__Block_byref_staticValue_0 *staticValue = __cself->staticValue; // bound by ref
globalValue += 1;
staticGlobalValue += 1;
(staticValue->__forwarding->staticValue) += 1;
printf("Block function :%d",(staticValue->__forwarding->staticValue));
}