block_test_with_external_variable

block_test_with_external_variable.m 源码

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        __block int b_external_variable = 1;
        int external_variable = 1;
        
        void (^block_test_with_external_variable)(int) = ^(int b_form_variable){
            b_external_variable += 1;
            b_form_variable += 1;
            
            NSLog(@"Input:b_external_variable=%d",b_external_variable);
            NSLog(@"Input:external_variable=%d",external_variable);
            NSLog(@"Input:external_variable=%d",b_form_variable);
            
        };
        external_variable += 1;
        block_test_with_external_variable(1);
    }
    return 0;
}

block_test_with_external_variable.cpp 编译后

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

struct __block_impl
{
    void *isa;
    int Flags;
    int Reserved;
    void *FuncPtr;
};
struct __Block_byref_b_external_variable_0
{
  void *__isa;
__Block_byref_b_external_variable_0 *__forwarding;
 int __flags;
 int __size;
 int b_external_variable;
};

struct __main_block_impl_0
{
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  int external_variable;
  __Block_byref_b_external_variable_0 *b_external_variable; // by ref
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _external_variable, __Block_byref_b_external_variable_0 *_b_external_variable, int flags=0) : external_variable(_external_variable), b_external_variable(_b_external_variable->__forwarding) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

static void __main_block_func_0(struct __main_block_impl_0 *__cself, int b_form_variable) {
  __Block_byref_b_external_variable_0 *b_external_variable = __cself->b_external_variable; // bound by ref
  int external_variable = __cself->external_variable; // bound by copy

            (b_external_variable->__forwarding->b_external_variable) += 1;
            b_form_variable += 1;

            NSLog((NSString *)&__NSConstantStringImpl__var_folders_92_0j5cq12s6cx44km6rp1q00ww0000gn_T_main_888b43_mi_0,(b_external_variable->__forwarding->b_external_variable));
            NSLog((NSString *)&__NSConstantStringImpl__var_folders_92_0j5cq12s6cx44km6rp1q00ww0000gn_T_main_888b43_mi_1,external_variable);
            NSLog((NSString *)&__NSConstantStringImpl__var_folders_92_0j5cq12s6cx44km6rp1q00ww0000gn_T_main_888b43_mi_2,b_form_variable);

        }

static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->b_external_variable, (void*)src->b_external_variable, 8/*BLOCK_FIELD_IS_BYREF*/);}

static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->b_external_variable, 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, const char * argv[]) {
    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
        __attribute__((__blocks__(byref))) __Block_byref_b_external_variable_0 b_external_variable = {(void*)0,(__Block_byref_b_external_variable_0 *)&b_external_variable, 0, sizeof(__Block_byref_b_external_variable_0), 1};
        int external_variable = 1;

        void (*block_test_with_external_variable)(int) = ((void (*)(int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, external_variable, (__Block_byref_b_external_variable_0 *)&b_external_variable, 570425344));
        external_variable += 1;
        ((void (*)(__block_impl *, int))((__block_impl *)block_test_with_external_variable)->FuncPtr)((__block_impl *)block_test_with_external_variable, 1);
    }
    return 0;
}

你可能感兴趣的:(block_test_with_external_variable)