zoukankan      html  css  js  c++  java
  • 通过runtime替换系统类实现的代码(从github开源库fdstackview中摘录)

    其中部分代码为汇编;由此可见oc的runtime的灵活性和能力。此代码仅供参考


    // ----------------------------------------------------

    // Runtime injection start.
    // Assemble codes below are based on:
    // https://github.com/0xced/NSUUID/blob/master/NSUUID.m
    // ----------------------------------------------------

    #pragma mark - Runtime Injection

    __asm(
          ".section        __DATA,__objc_classrefs,regular,no_dead_strip "
    #if    TARGET_RT_64_BIT
          ".align          3 "
          "L_OBJC_CLASS_UIStackView: "
          ".quad           _OBJC_CLASS_$_UIStackView "
    #else
          ".align          2 "
          "_OBJC_CLASS_UIStackView: "
          ".long           _OBJC_CLASS_$_UIStackView "
    #endif
          ".weak_reference _OBJC_CLASS_$_UIStackView "
          );

    // Constructors are called after all classes have been loaded.
    __attribute__((constructor)) static void FDStackViewPatchEntry(void) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            @autoreleasepool {
                
                // >= iOS9.
                if (objc_getClass("UIStackView")) {
                    return;
                }
                
                Class *stackViewClassLocation = NULL;
                
    #if TARGET_CPU_ARM
                __asm("movw %0, :lower16:(_OBJC_CLASS_UIStackView-(LPC0+4)) "
                      "movt %0, :upper16:(_OBJC_CLASS_UIStackView-(LPC0+4)) "
                      "LPC0: add %0, pc" : "=r"(stackViewClassLocation));
    #elif TARGET_CPU_ARM64
                __asm("adrp %0, L_OBJC_CLASS_UIStackView@PAGE "
                      "add  %0, %0, L_OBJC_CLASS_UIStackView@PAGEOFF" : "=r"(stackViewClassLocation));
    #elif TARGET_CPU_X86_64
                __asm("leaq L_OBJC_CLASS_UIStackView(%%rip), %0" : "=r"(stackViewClassLocation));
    #elif TARGET_CPU_X86
                void *pc = NULL;
                __asm("calll L0 "
                      "L0: popl %0 "
                      "leal _OBJC_CLASS_UIStackView-L0(%0), %1" : "=r"(pc), "=r"(stackViewClassLocation));
    #else
    #error Unsupported CPU
    #endif
                
                if (stackViewClassLocation && !*stackViewClassLocation) {
                    Class class = objc_allocateClassPair(FDStackView.class, "UIStackView", 0);
                    if (class) {
                        objc_registerClassPair(class);
                        *stackViewClassLocation = class;
                    }
                }
            }
        });
    }

  • 相关阅读:
    Spring Boot 结合 Redis 序列化配置的一些问题
    基于SpringBoot的代码在线运行的简单实现
    将Spring实战第5版中Spring HATEOAS部分代码迁移到Spring HATEOAS 1.0
    用Spring Security, JWT, Vue实现一个前后端分离无状态认证Demo
    使用最新AndroidStudio编写Android编程权威指南(第3版)中的代码会遇到的一些问题
    C# 内存管理优化畅想----前言
    C# 内存管理优化实践
    C# 内存管理优化畅想(三)---- 其他方法&结语
    C# 内存管理优化畅想(二)---- 巧用堆栈
    C# 内存管理优化畅想(一)---- 大对象堆(LOH)的压缩
  • 原文地址:https://www.cnblogs.com/fengju/p/6173603.html
Copyright © 2011-2022 走看看