block

2021-05-06  本文已影响0人  Berning

block

block 原理

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
            
        void(^block)(void) = ^{
            NSLog(@"hello world");
        };
        
        block();
    }
    return 0;
}

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) {

            NSLog((NSString *)&__NSConstantStringImpl__var_folders_p8_kz1xzn596q746q_0rv80p9zc0000gn_T_main_1e5739_mi_0);
}

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(*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));

        ((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);

    }
    return 0;
}

block捕获变量

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int a = 10;
        static int b = 11;
        void(^block)(void) = ^{
            NSLog(@"a = %d , b = %d",a,b);
        };
        a = 20;
        b = 21;
        block();
    }
    return 0;
}

struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  int a;        //auto 值传递
  int *b;      //static 指针传递
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _a, int *_b, int flags=0) : a(_a), b(_b) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
//局部变量需要进行捕获  
int a = __cself->a; // bound by copy
  int *b = __cself->b; // bound by copy

    NSLog((NSString *)&__NSConstantStringImpl__var_folders_p8_kz1xzn596q746q_0rv80p9zc0000gn_T_main_e37aef_mi_0,a,(*b));
 }

#import <Foundation/Foundation.h>

int g_a = 10;
static int g_b = 11;

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        void(^block)(void) = ^{
            NSLog(@"a = %d , b = %d",g_a,g_b);
        };
        g_a = 20;
        g_b = 21;
        block();
    }
    return 0;
}

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) {
//全局变量直接访问       
NSLog((NSString *)&__NSConstantStringImpl__var_folders_p8_kz1xzn596q746q_0rv80p9zc0000gn_T_main_0c6a2d_mi_0,g_a,g_b);   
 }


上一篇 下一篇

猜你喜欢

热点阅读