简单地Block变形C函数调用代码(用作理解)

struct _block_impl {
    void *isa;
    int Flags;
    int Reserved;
    void *FuncPtr;
};

struct _main_block_desc_0 {
    unsigned long reserved;
    unsigned long Block_size;
};

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;
    }
};

struct _main_block_desc_0 _main_block_desc_0_DATA = {
    0,
    sizeof(struct _main_block_impl_0)
};

static void _main_block_func_0(struct _main_block_impl_0 *c_self) {
    printf("Block => C \n");
}



#pragma mark 运行入口

void runBlockFunc() {
    
    struct _main_block_impl_0 impl_0 = _main_block_impl_0((void*)_main_block_func_0,&_main_block_desc_0_DATA,0);
    void (*blk)(void) = (void(*)(void))&impl_0;
    ((void(*)(struct _block_impl *))((struct _block_impl*)blk) -> FuncPtr)((struct _block_impl *)blk);
}

int main(int argc, char * argv[]) {
    @autoreleasepool {
        runBlockFunc();
        void(^blk)(void) = ^{
            printf("Block => OC \n");
        };
        blk();
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

 https://blog.csdn.net/snsn1984/article/details/17716261  关于clang的理解

 

 通过clang编译oc文件的配置

clang -x objective-c -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk main.m


作者:Billlin
链接:https://www.jianshu.com/p/f4d0c145fb09
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
posted @ 2019-08-01 13:49  雨筱逸悠  阅读(166)  评论(0编辑  收藏  举报