simple_block

simple_test.m 源码

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        void (^blockTest)(void) = ^{ printf("BlockTest\n"); };
        blockTest();
    }
    return 0;
}

simple_test.cpp 编译后

说明此处只展示与BLock实现相关的重要代码,想看编译后完整内容,可以到Block项目中找simple_test.cpp即可

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

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 void __main_block_func_0(struct __main_block_impl_0 *__cself) {
    printf("BlockTest\n"); }

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;
        void (*blockTest)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
        ((void (*)(__block_impl *))((__block_impl *)blockTest)->FuncPtr)((__block_impl *)blockTest);
        return 0;
    }
    return 0;
}

你可能感兴趣的:(simple_block)