1.clang -rewrite-objc 源代码文件名 -> C++源代码
void (^blk)() = ^ {
printf("Hello world");
};
void (^blk2)(int b) = ^(int b) {
printf("%d", b);
};
C++源代码
Block本质:指向函数的指针,实质 Objective-C对象
**************************** main ***************************
int main(int argc, const char * argv[]) {
void (*blk)() = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
void (*blk2)(int b) = ((void (*)(int))&__main_block_impl_1((void *)__main_block_func_1, &__main_block_desc_1_DATA));
return 0;
}
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
printf("Hello world");
}
static void __main_block_func_1(struct __main_block_impl_1 *__cself, int b) {
printf("%d", b);
}
**************************** blk ***************************
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int flags=0) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
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)};
**************************** blk2 ***************************
struct __main_block_impl_1 {
struct __block_impl impl;
struct __main_block_desc_1* Desc;
__main_block_impl_1(void *fp, struct __main_block_desc_1 *desc, int flags=0) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static struct __main_block_desc_1 {
size_t reserved;
size_t Block_size;
} __main_block_desc_1_DATA = { 0, sizeof(struct __main_block_impl_1)};
************************** __block_impl *****************************
struct __block_impl {
// block为OC对象: _NSConcreteStackBlock、_NSConcreteGlobalBlock、_NSConcreteMallocBlock
void *isa;
int Flags;
int Reserved;
void *FuncPtr;
};
自动变量会追加到结构体__main_block_impl_0 中
网友评论