Runtime浅谈
2019-07-11 本文已影响0人
木易林1
oc/swift是一种类面向对象语言
c是一种结构化的语言
1:为什么我们使用oc和c很容易融合一起编写代码?下面可以直观理解。
2:oc经过LLVM编译器编译会转换为c++代码,转换过程是怎样的?
可以通过下面思考下。
3:一切皆为对象,类也是对象(可理解为类是元类的对象)
4:那我们可以通过下面理解下objc_msgSend是如何实现的?
下章会为大家谈谈我的理解
Class 定义
typedef struct objc_class *Class;
Object定义
struct objc_object {
Class _Nonnull isa OBJC_ISA_AVAILABILITY;
};
id定义
typedef struct objc_object *id;
SEL定义
typedef struct objc_selector *SEL;
IMP定义
typedef void (*IMP)(void /* id, SEL, ... */ );
or
typedef id _Nullable (*IMP)(id _Nonnull, SEL _Nonnull, ...);
objc_class结构体里内容相关
/// An opaque type that represents a method in a class definition.
typedef struct objc_method *Method;
/// An opaque type that represents an instance variable.
typedef struct objc_ivar *Ivar;
/// An opaque type that represents a category.
typedef struct objc_category *Category;
/// An opaque type that represents an Objective-C declared property.
typedef struct objc_property *objc_property_t;
objc_class (objc2)
struct objc_class {
Class _Nonnull isa OBJC_ISA_AVAILABILITY;
#if !__OBJC2__
Class _Nullable super_class OBJC2_UNAVAILABLE;
const char * _Nonnull name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list * _Nullable ivars OBJC2_UNAVAILABLE;
struct objc_method_list * _Nullable * _Nullable methodLists OBJC2_UNAVAILABLE;
struct objc_cache * _Nonnull cache OBJC2_UNAVAILABLE;
struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE;
#endif
} OBJC2_UNAVAILABLE;
Protocol
typedef struct objc_object Protocol;
method
/// Defines a method
struct objc_method_description {
SEL _Nullable name; /**< The name of the method */
char * _Nullable types; /**< The types of the method arguments */
};
property
/// Defines a property attribute
typedef struct {
const char * _Nonnull name; /**< The name of the attribute */
const char * _Nonnull value; /**< The value of the attribute (usually empty) */
} objc_property_attribute_t;
protocol_list
struct objc_protocol_list {
struct objc_protocol_list * _Nullable next;
long count;
__unsafe_unretained Protocol * _Nullable list[1];
};
category
struct objc_category {
char * _Nonnull category_name OBJC2_UNAVAILABLE;
char * _Nonnull class_name OBJC2_UNAVAILABLE;
struct objc_method_list * _Nullable instance_methods OBJC2_UNAVAILABLE;
struct objc_method_list * _Nullable class_methods OBJC2_UNAVAILABLE;
struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE;
} OBJC2_UNAVAILABLE;
method_description_list
struct objc_method_description_list {
int count;
struct objc_method_description list[1];
};
ivar
struct objc_ivar {
char * _Nullable ivar_name OBJC2_UNAVAILABLE;
char * _Nullable ivar_type OBJC2_UNAVAILABLE;
int ivar_offset OBJC2_UNAVAILABLE;
#ifdef __LP64__
int space OBJC2_UNAVAILABLE;
#endif
} OBJC2_UNAVAILABLE;
ivar_list
struct objc_ivar_list {
int ivar_count OBJC2_UNAVAILABLE;
#ifdef __LP64__
int space OBJC2_UNAVAILABLE;
#endif
/* variable length structure */
struct objc_ivar ivar_list[1] OBJC2_UNAVAILABLE;
} OBJC2_UNAVAILABLE;
method
struct objc_method {
SEL _Nonnull method_name OBJC2_UNAVAILABLE;
char * _Nullable method_types OBJC2_UNAVAILABLE;
IMP _Nonnull method_imp OBJC2_UNAVAILABLE;
} OBJC2_UNAVAILABLE;
method_list
struct objc_method_list {
struct objc_method_list * _Nullable obsolete OBJC2_UNAVAILABLE;
int method_count OBJC2_UNAVAILABLE;
#ifdef __LP64__
int space OBJC2_UNAVAILABLE;
#endif
/* variable length structure */
struct objc_method method_list[1] OBJC2_UNAVAILABLE;
} OBJC2_UNAVAILABLE;
symtab 符号表
typedef struct objc_symtab *Symtab OBJC2_UNAVAILABLE;
struct objc_symtab {
unsigned long sel_ref_cnt OBJC2_UNAVAILABLE;
SEL _Nonnull * _Nullable refs OBJC2_UNAVAILABLE;
unsigned short cls_def_cnt OBJC2_UNAVAILABLE;
unsigned short cat_def_cnt OBJC2_UNAVAILABLE;
void * _Nullable defs[1] /* variable size */ OBJC2_UNAVAILABLE;
} OBJC2_UNAVAILABLE;
cache
struct objc_cache {
unsigned int mask /* total = mask + 1 */ OBJC2_UNAVAILABLE;
unsigned int occupied OBJC2_UNAVAILABLE;
Method _Nullable buckets[1] OBJC2_UNAVAILABLE;
};
module
typedef struct objc_module *Module OBJC2_UNAVAILABLE;
struct objc_module {
unsigned long version OBJC2_UNAVAILABLE;
unsigned long size OBJC2_UNAVAILABLE;
const char * _Nullable name OBJC2_UNAVAILABLE;
Symtab _Nullable symtab OBJC2_UNAVAILABLE;
} OBJC2_UNAVAILABLE;