|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// Internal storage associated with a `ClassHook`. Do not use this yourself. |
| 4 | +/// |
| 5 | +/// :nodoc: |
| 6 | +public final class _GlueClassHookStorage { |
| 7 | + let hookType: AnyClass |
| 8 | + @LazyAtomic private(set) var targetType: AnyObject.Type |
| 9 | + @LazyAtomic private(set) var group: HookGroup |
| 10 | + |
| 11 | + // additional fields may be added here in the future |
| 12 | + |
| 13 | + init( |
| 14 | + hookType: AnyClass, |
| 15 | + loadTargetType: @escaping () -> AnyObject.Type, |
| 16 | + loadGroup: @escaping () -> HookGroup |
| 17 | + ) { |
| 18 | + self.hookType = hookType |
| 19 | + _targetType = LazyAtomic(wrappedValue: loadTargetType()) |
| 20 | + _group = LazyAtomic(wrappedValue: loadGroup()) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/// A placeholder class hook glue type. Do not use this yourself. |
| 25 | +/// |
| 26 | +/// This type is the default value for the `ClassHookProtocol._Glue` constraint, |
| 27 | +/// used to satisfy the compiler until the actual glue is provided. |
| 28 | +/// |
| 29 | +/// :nodoc: |
| 30 | +public enum _GlueClassHookPlaceholder<HookType: ClassHookProtocol>: _GlueClassHook { |
| 31 | + public typealias OrigType = HookType |
| 32 | + public typealias SuprType = HookType |
| 33 | + |
| 34 | + public static var storage: _GlueClassHookStorage { error() } |
| 35 | + public static func activate(withClassHookBuilder builder: inout _GlueClassHookBuilder) { error() } |
| 36 | + |
| 37 | + private static func error() -> Never { |
| 38 | + orionError("Placeholder class hook used. Has the glue file been compiled?") |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/// A marker protocol to which `_GlueClassHook`'s orig/supr trampolines conform. Do |
| 43 | +/// not use this yourself. |
| 44 | +/// |
| 45 | +/// :nodoc: |
| 46 | +public protocol _GlueClassHookTrampoline: ClassHookProtocol {} |
| 47 | + |
| 48 | +/// A helper type used in the glue file for applying class hooks. Do not |
| 49 | +/// use this directly. |
| 50 | +/// |
| 51 | +/// :nodoc: |
| 52 | +public struct _GlueClassHookBuilder { |
| 53 | + let target: AnyClass |
| 54 | + |
| 55 | + var descriptors: [HookDescriptor] = [] |
| 56 | + |
| 57 | + public mutating func addHook<Code>( |
| 58 | + _ sel: Selector, |
| 59 | + _ replacement: Code, |
| 60 | + isClassMethod: Bool, |
| 61 | + saveOrig: @escaping (Code) -> Void |
| 62 | + ) { |
| 63 | + let cls: AnyClass = isClassMethod ? object_getClass(target)! : target |
| 64 | + descriptors.append( |
| 65 | + .method(cls: cls, sel: sel, replacement: unsafeBitCast(replacement, to: UnsafeMutableRawPointer.self)) { |
| 66 | + saveOrig(unsafeBitCast($0, to: Code.self)) |
| 67 | + } |
| 68 | + ) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/// A concrete class hook, implemented in the glue file. Do not use |
| 73 | +/// this directly. |
| 74 | +/// |
| 75 | +/// :nodoc: |
| 76 | +public protocol _GlueClassHook: _GlueAnyHook { |
| 77 | + associatedtype HookType: ClassHookProtocol |
| 78 | + associatedtype OrigType: ClassHookProtocol where OrigType.Target == HookType.Target |
| 79 | + associatedtype SuprType: ClassHookProtocol where SuprType.Target == HookType.Target |
| 80 | + |
| 81 | + static var storage: _GlueClassHookStorage { get } |
| 82 | + static func activate(withClassHookBuilder builder: inout _GlueClassHookBuilder) |
| 83 | +} |
| 84 | + |
| 85 | +/// :nodoc: |
| 86 | +extension _GlueClassHook { |
| 87 | + public static func addMethod<Code>(_ selector: Selector, _ implementation: Code, isClassMethod: Bool) { |
| 88 | + let methodDescription = { "\(isClassMethod ? "+" : "-")[\(self) \(selector)]" } |
| 89 | + guard let method = (isClassMethod ? class_getClassMethod : class_getInstanceMethod)(HookType.self, selector) |
| 90 | + else { orionError("Could not find method \(methodDescription())") } |
| 91 | + guard let types = method_getTypeEncoding(method) |
| 92 | + else { orionError("Could not get method signature for \(methodDescription())") } |
| 93 | + let cls: AnyClass = isClassMethod ? object_getClass(HookType.target)! : HookType.target |
| 94 | + guard class_addMethod(cls, selector, unsafeBitCast(implementation, to: IMP.self), types) |
| 95 | + else { orionError("Failed to add method \(methodDescription())") } |
| 96 | + } |
| 97 | + |
| 98 | + public static func activate() -> [HookDescriptor] { |
| 99 | + var classHookBuilder = _GlueClassHookBuilder(target: HookType.target) |
| 100 | + activate(withClassHookBuilder: &classHookBuilder) |
| 101 | + return classHookBuilder.descriptors |
| 102 | + } |
| 103 | + |
| 104 | + public static var groupType: HookGroup.Type { |
| 105 | + HookType.Group.self |
| 106 | + } |
| 107 | + |
| 108 | + public static func hookWillActivate() -> Bool { |
| 109 | + HookType.hookWillActivate() |
| 110 | + } |
| 111 | + |
| 112 | + public static func hookDidActivate() { |
| 113 | + HookType.hookDidActivate() |
| 114 | + } |
| 115 | + |
| 116 | + public static func initializeStorage() -> _GlueClassHookStorage { |
| 117 | + // this gives us the type of the user's hook rather than |
| 118 | + // our concrete subclass |
| 119 | + _GlueClassHookStorage( |
| 120 | + hookType: HookType.self, |
| 121 | + loadTargetType: HookType.initializeTargetType, |
| 122 | + loadGroup: HookType.loadGroup |
| 123 | + ) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +extension SubclassMode { |
| 128 | + fileprivate func subclassName(withType type: AnyClass) -> String? { |
| 129 | + switch self { |
| 130 | + case .none: |
| 131 | + return nil |
| 132 | + case .createSubclass: |
| 133 | + return "OrionSubclass.\(NSStringFromClass(type))" |
| 134 | + case .createSubclassNamed(let name): |
| 135 | + return name |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +/// :nodoc: |
| 141 | +extension ClassHookProtocol { |
| 142 | + // since `target` is referred to in `activate()`, this will deterministically be called |
| 143 | + // when a class hook is activated. |
| 144 | + fileprivate static func initializeTargetType() -> Target.Type { |
| 145 | + let targetName = self.targetName // only call getter once |
| 146 | + let baseTarget = targetName.isEmpty ? Target.self : Dynamic(targetName).as(type: Target.self) |
| 147 | + |
| 148 | + let target: Target.Type |
| 149 | + if let subclassName = subclassMode.subclassName(withType: _Glue.storage.hookType) { |
| 150 | + guard let pair: AnyClass = objc_allocateClassPair(baseTarget, subclassName, 0) |
| 151 | + else { orionError("Could not allocate subclass for \(self)") } |
| 152 | + objc_registerClassPair(pair) |
| 153 | + guard let _target = pair as? Target.Type |
| 154 | + else { orionError("Allocated invalid subclass for \(self)") } |
| 155 | + target = _target |
| 156 | + } else { |
| 157 | + target = baseTarget |
| 158 | + } |
| 159 | + |
| 160 | + protocols.forEach { |
| 161 | + guard class_addProtocol(target, $0) |
| 162 | + else { orionError("Could not add protocol \($0) to \(target)") } |
| 163 | + } |
| 164 | + return target |
| 165 | + } |
| 166 | +} |
0 commit comments