huangyirong 4 years ago
parent
commit
7b1a54c27d

BIN
.DS_Store


BIN
Objc4_Study/.DS_Store


+ 6 - 4
Objc4_Study/Objc4_Study.xcodeproj/project.pbxproj

@@ -7,7 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
-		40F628AF2476DB0900110F74 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 40F628AE2476DB0900110F74 /* main.m */; };
+		40F628AF2476DB0900110F74 /* main.mm in Sources */ = {isa = PBXBuildFile; fileRef = 40F628AE2476DB0900110F74 /* main.mm */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXCopyFilesBuildPhase section */
@@ -24,8 +24,9 @@
 
 /* Begin PBXFileReference section */
 		40F628AB2476DB0900110F74 /* Objc4_Study */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Objc4_Study; sourceTree = BUILT_PRODUCTS_DIR; };
-		40F628AE2476DB0900110F74 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
+		40F628AE2476DB0900110F74 /* main.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = main.mm; sourceTree = "<group>"; };
 		40F628D3247C1E4000110F74 /* main-arm64.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = "main-arm64.cpp"; sourceTree = "<group>"; };
+		6EDCFF2A24A77F100094F1D8 /* custom_objc_class.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = custom_objc_class.h; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -58,7 +59,8 @@
 		40F628AD2476DB0900110F74 /* Objc4_Study */ = {
 			isa = PBXGroup;
 			children = (
-				40F628AE2476DB0900110F74 /* main.m */,
+				40F628AE2476DB0900110F74 /* main.mm */,
+				6EDCFF2A24A77F100094F1D8 /* custom_objc_class.h */,
 				40F628D3247C1E4000110F74 /* main-arm64.cpp */,
 			);
 			path = Objc4_Study;
@@ -121,7 +123,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
-				40F628AF2476DB0900110F74 /* main.m in Sources */,
+				40F628AF2476DB0900110F74 /* main.mm in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 148 - 0
Objc4_Study/Objc4_Study/custom_objc_class.h

@@ -0,0 +1,148 @@
+//
+//  custom_objc_class.h
+//  Objc4_Study
+//
+//  Created by huangyirong on 2020/6/27.
+//  Copyright © 2020 Sogou. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+# if __arm64__
+#   define ISA_MASK        0x0000000ffffffff8ULL
+# elif __x86_64__
+#   define ISA_MASK        0x00007ffffffffff8ULL
+# endif
+
+#ifndef custom_objc_class_h
+#define custom_objc_class_h
+
+
+
+#if __LP64__
+typedef uint32_t mask_t;
+#else
+typedef uint16_t mask_t;
+#endif
+typedef uintptr_t cache_key_t;
+
+struct bucket_t {
+    cache_key_t _key;
+    IMP _imp;
+};
+
+struct cache_t {
+    bucket_t *_buckets;
+    mask_t _mask;
+    mask_t _occupied;
+};
+
+struct entsize_list_tt {
+    uint32_t entsizeAndFlags;
+    uint32_t count;
+};
+
+struct method_t {
+    SEL name;
+    const char *types;
+    IMP imp;
+};
+
+struct method_list_t : entsize_list_tt {
+    method_t first;
+};
+
+struct ivar_t {
+    int32_t *offset;
+    const char *name;
+    const char *type;
+    uint32_t alignment_raw;
+    uint32_t size;
+};
+
+struct ivar_list_t : entsize_list_tt {
+    ivar_t first;
+};
+
+struct property_t {
+    const char *name;
+    const char *attributes;
+};
+
+struct property_list_t : entsize_list_tt {
+    property_t first;
+};
+
+struct chained_property_list {
+    chained_property_list *next;
+    uint32_t count;
+    property_t list[0];
+};
+
+typedef uintptr_t protocol_ref_t;
+struct protocol_list_t {
+    uintptr_t count;
+    protocol_ref_t list[0];
+};
+
+struct class_ro_t {
+    uint32_t flags;
+    uint32_t instanceStart;
+    uint32_t instanceSize;  // instance对象占用的内存空间
+#ifdef __LP64__
+    uint32_t reserved;
+#endif
+    const uint8_t * ivarLayout;
+    const char * name;  // 类名
+    method_list_t * baseMethodList;
+    protocol_list_t * baseProtocols;
+    const ivar_list_t * ivars;  // 成员变量列表
+    const uint8_t * weakIvarLayout;
+    property_list_t *baseProperties;
+};
+
+struct class_rw_t {
+    uint32_t flags;
+    uint32_t version;
+    const class_ro_t *ro;
+    method_list_t * methods;    // 方法列表
+    property_list_t *properties;    // 属性列表
+    const protocol_list_t * protocols;  // 协议列表
+    Class firstSubclass;
+    Class nextSiblingClass;
+    char *demangledName;
+};
+
+#define FAST_DATA_MASK          0x00007ffffffffff8UL
+struct class_data_bits_t {
+    uintptr_t bits;
+public:
+    class_rw_t* data() {
+        return (class_rw_t *)(bits & FAST_DATA_MASK);
+    }
+};
+
+/* OC对象 */
+struct custom_objc_object {
+//    void *isa;
+    Class _Nonnull isa  OBJC_ISA_AVAILABILITY;
+
+};
+
+/* 类对象 */
+struct custom_objc_class : custom_objc_object {
+    Class superclass;
+    cache_t cache;
+    class_data_bits_t bits;
+public:
+    class_rw_t* data() {
+        return bits.data();
+    }
+    
+    custom_objc_class* metaClass() {
+        return (custom_objc_class *)((long long)isa & ISA_MASK);
+    }
+};
+
+
+#endif /* custom_objc_class_h */

File diff suppressed because it is too large
+ 131 - 5
Objc4_Study/Objc4_Study/main.m


BIN
objc4-750/.DS_Store


BIN
objc4-779.1/.DS_Store


+ 5 - 1
objc4-779.1/runtime/objc-runtime-new.mm

@@ -2443,12 +2443,14 @@ static Class realizeClassWithoutSwift(Class cls, Class previously)
     Class metacls;
     bool isMeta;
 
+    // 如果类已经实现了,直接返回
     if (!cls) return nil;
     if (cls->isRealized()) return cls;
     ASSERT(cls == remapClass(cls));
 
     // fixme verify class is not in an un-dlopened part of the shared cache?
-
+    // 编译期间,cls->data指向的是class_ro_t结构体
+    // 因此可以强制转成class_ro_t
     ro = (const class_ro_t *)cls->data();
     if (ro->flags & RO_FUTURE) {
         // This was a future class. rw data is already allocated.
@@ -2457,6 +2459,7 @@ static Class realizeClassWithoutSwift(Class cls, Class previously)
         cls->changeInfo(RW_REALIZED|RW_REALIZING, RW_FUTURE);
     } else {
         // Normal class. Allocate writeable class data.
+        // 初始化class_rw_t结构体
         rw = (class_rw_t *)calloc(sizeof(class_rw_t), 1);
         rw->ro = ro;
         rw->flags = RW_REALIZED|RW_REALIZING;
@@ -2565,6 +2568,7 @@ static Class realizeClassWithoutSwift(Class cls, Class previously)
     } else {
         addRootClass(cls);
     }
+    // 将类实现的方法(包括分类)、属性和遵循的协议添加到class_rw_t结构体中的methods、properties、protocols列表中
 
     // Attach categories
     methodizeClass(cls, previously);