1.截獲自動(dòng)變量
int value = 1;
void (^blk)(void) = ^{
printf("Block function :%d",value);
};
value = 2;
blk();
將上面代碼進(jìn)行clang轉(zhuǎn)換
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結(jié)構(gòu)體中了
2.__block 說明符
我們先看一下 靜態(tài)全局變量 全局變量 和靜態(tài)變量在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;
}
我們看到只有靜態(tài)變量進(jìn)行了轉(zhuǎn)換免胃,全局和動(dòng)態(tài)變量都和之前完全相同寄症,我們?cè)賮砜窗鸯o態(tài)變量轉(zhuǎn)成__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;
}
此時(shí)__block 竟然變成__Block_byref_staticValue_0結(jié)構(gòu)體,給staticValue靜態(tài)變量賦值的時(shí)候,使用了指向該靜態(tài)變量的指針弄跌,而向__block變量賦值的時(shí)候__Block_byref_staticValue_0 結(jié)構(gòu)體的成員變量__forwarding 持有該實(shí)例自身的指針
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));
}