新类和Block

#ifndef _REWRITER_typedef_TestA
#define _REWRITER_typedef_TestA
typedef struct objc_object TestA;
typedef struct {} _objc_exc_TestA;
#endif

struct TestA_IMPL {
    struct NSObject_IMPL NSObject_IVARS;
};


/* @end */


// @implementation TestA


struct __TestA__funcitonBlock_block_impl_0 {
  struct __block_impl impl;
  struct __TestA__funcitonBlock_block_desc_0* Desc;
  __TestA__funcitonBlock_block_impl_0(void *fp, struct __TestA__funcitonBlock_block_desc_0 *desc, int flags=0) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};
static int __TestA__funcitonBlock_block_func_0(struct __TestA__funcitonBlock_block_impl_0 *__cself, int index) {

        int addIndex = index + 1;
        printf("====> %d\n",addIndex);
        return addIndex;
    }

static struct __TestA__funcitonBlock_block_desc_0 {
  size_t reserved;
  size_t Block_size;
} __TestA__funcitonBlock_block_desc_0_DATA = { 0, sizeof(struct __TestA__funcitonBlock_block_impl_0)};

static void _I_TestA_funcitonBlock(TestA * self, SEL _cmd) {
    int i = 10;
    int (*testBlk)(int) = ((int (*)(int))&__TestA__funcitonBlock_block_impl_0((void *)__TestA__funcitonBlock_block_func_0, &__TestA__funcitonBlock_block_desc_0_DATA));
    printf("---> %d\n",((int (*)(__block_impl *, int))((__block_impl *)testBlk)->FuncPtr)((__block_impl *)testBlk, i));
}

// @end

int main () {
    ((void (*)(id, SEL))(void *)objc_msgSend)((id)((TestA *(*)(id, SEL))(void *)objc_msgSend)((id)((TestA *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("TestA"), sel_registerName("alloc")), sel_registerName("init")), sel_registerName("funcitonBlock"));
    return 0;
}

struct _prop_t {
    const char *name;
    const char *attributes;
};

struct _protocol_t;

struct _objc_method {
    struct objc_selector * _cmd;
    const char *method_type;
    void  *_imp;
};

struct _protocol_t {
    void * isa;  // NULL
    const char *protocol_name;
    const struct _protocol_list_t * protocol_list; // super protocols
    const struct method_list_t *instance_methods;
    const struct method_list_t *class_methods;
    const struct method_list_t *optionalInstanceMethods;
    const struct method_list_t *optionalClassMethods;
    const struct _prop_list_t * properties;
    const unsigned int size;  // sizeof(struct _protocol_t)
    const unsigned int flags;  // = 0
    const char ** extendedMethodTypes;
};

struct _ivar_t {
    unsigned long int *offset;  // pointer to ivar offset location
    const char *name;
    const char *type;
    unsigned int alignment;
    unsigned int  size;
};

struct _class_ro_t {
    unsigned int flags;
    unsigned int instanceStart;
    unsigned int instanceSize;
    unsigned int reserved;
    const unsigned char *ivarLayout;
    const char *name;
    const struct _method_list_t *baseMethods;
    const struct _objc_protocol_list *baseProtocols;
    const struct _ivar_list_t *ivars;
    const unsigned char *weakIvarLayout;
    const struct _prop_list_t *properties;
};

struct _class_t {
    struct _class_t *isa;
    struct _class_t *superclass;
    void *cache;
    void *vtable;
    struct _class_ro_t *ro;
};

struct _category_t {
    const char *name;
    struct _class_t *cls;
    const struct _method_list_t *instance_methods;
    const struct _method_list_t *class_methods;
    const struct _protocol_list_t *protocols;
    const struct _prop_list_t *properties;
};
extern "C" __declspec(dllimport) struct objc_cache _objc_empty_cache;
#pragma warning(disable:4273)

static struct /*_method_list_t*/ {
    unsigned int entsize;  // sizeof(struct _objc_method)
    unsigned int method_count;
    struct _objc_method method_list[1];
} _OBJC_$_INSTANCE_METHODS_TestA __attribute__ ((used, section ("__DATA,__objc_const"))) = {
    sizeof(_objc_method),
    1,
    {{(struct objc_selector *)"funcitonBlock", "v16@0:8", (void *)_I_TestA_funcitonBlock}}
};

static struct _class_ro_t _OBJC_METACLASS_RO_$_TestA __attribute__ ((used, section ("__DATA,__objc_const"))) = {
    1, sizeof(struct _class_t), sizeof(struct _class_t), 
    (unsigned int)0, 
    0, 
    "TestA",
    0, 
    0, 
    0, 
    0, 
    0, 
};

static struct _class_ro_t _OBJC_CLASS_RO_$_TestA __attribute__ ((used, section ("__DATA,__objc_const"))) = {
    0, sizeof(struct TestA_IMPL), sizeof(struct TestA_IMPL), 
    (unsigned int)0, 
    0, 
    "TestA",
    (const struct _method_list_t *)&_OBJC_$_INSTANCE_METHODS_TestA,
    0, 
    0, 
    0, 
    0, 
};

extern "C" __declspec(dllimport) struct _class_t OBJC_METACLASS_$_NSObject;

extern "C" __declspec(dllexport) struct _class_t OBJC_METACLASS_$_TestA __attribute__ ((used, section ("__DATA,__objc_data"))) = {
    0, // &OBJC_METACLASS_$_NSObject,
    0, // &OBJC_METACLASS_$_NSObject,
    0, // (void *)&_objc_empty_cache,
    0, // unused, was (void *)&_objc_empty_vtable,
    &_OBJC_METACLASS_RO_$_TestA,
};

extern "C" __declspec(dllimport) struct _class_t OBJC_CLASS_$_NSObject;

extern "C" __declspec(dllexport) struct _class_t OBJC_CLASS_$_TestA __attribute__ ((used, section ("__DATA,__objc_data"))) = {
    0, // &OBJC_METACLASS_$_TestA,
    0, // &OBJC_CLASS_$_NSObject,
    0, // (void *)&_objc_empty_cache,
    0, // unused, was (void *)&_objc_empty_vtable,
    &_OBJC_CLASS_RO_$_TestA,
};
static void OBJC_CLASS_SETUP_$_TestA(void ) {
    OBJC_METACLASS_$_TestA.isa = &OBJC_METACLASS_$_NSObject;
    OBJC_METACLASS_$_TestA.superclass = &OBJC_METACLASS_$_NSObject;
    OBJC_METACLASS_$_TestA.cache = &_objc_empty_cache;
    OBJC_CLASS_$_TestA.isa = &OBJC_METACLASS_$_TestA;
    OBJC_CLASS_$_TestA.superclass = &OBJC_CLASS_$_NSObject;
    OBJC_CLASS_$_TestA.cache = &_objc_empty_cache;
}
#pragma section(".objc_inithooks$B", long, read, write)
__declspec(allocate(".objc_inithooks$B")) static void *OBJC_CLASS_SETUP[] = {
    (void *)&OBJC_CLASS_SETUP_$_TestA,
};
static struct _class_t *L_OBJC_LABEL_CLASS_$ [1] __attribute__((used, section ("__DATA, __objc_classlist,regular,no_dead_strip")))= {
    &OBJC_CLASS_$_TestA,
};
#import <Foundation/Foundation.h>

@interface TestA : NSObject

@end

@implementation TestA

-(void)funcitonBlock {
    int i = 10;
    int (^testBlk)(int) = ^int(int index){
        int addIndex = index + 1;
        printf("====> %d\n",addIndex);
        return addIndex;
    };
    printf("---> %d\n",testBlk(i));
}

@end

int main () {
    [[[TestA alloc] init] funcitonBlock];
    return 0;
}

 

https://www.jianshu.com/p/dcaaed1d47f2  什么是否Block会从栈复制到堆?答:当Block被当成对象进行拷贝传递赋值时(原来的内存空间会被销毁,对象方法生成Block,给到另一个对象之后该制造者就销毁了?直接赋值复制从会被销毁的对象中获取等方式均没有写出会复制到堆的代码,实际上这个复制操作是有了的,它是一个函数0.0)

posted @ 2019-08-01 14:44  雨筱逸悠  阅读(257)  评论(0编辑  收藏  举报