Skip to content

Commit 7a9367e

Browse files
authored
Various fixes for Swiftlint (#18)
1 parent 4bcf98e commit 7a9367e

14 files changed

+165
-127
lines changed

.swiftlint.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ included:
22
- Sources
33
- Tests
44
analyzer_rules:
5+
disallowed_racist_terms_of_art:
6+
name: "Disallowed racist terms of art"
7+
regex: 'blacklist|whitelist|^((?<!git)[^\n])*master(?! (manifest|playlist))|slave'
8+
message: "Please, refrain from using terms which have racist connotations. Consider alternatives such as allow, disallow, primary, secondary..."
9+
severity: error
510
line_length: 120
611
identifier_name:
712
excluded:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Want to hook just a single instance? No problem!
5454
let hook = try testObj.hook(
5555
#selector(TestClass.sayHi),
5656
methodSignature: (@convention(c) (AnyObject, Selector) -> String).self,
57-
hookSignature:(@convention(block) (AnyObject) -> String).self) { store in { `self` in
57+
hookSignature: (@convention(block) (AnyObject) -> String).self) { store in { `self` in
5858
return store.original(`self`, store.selector) + "just this instance"
5959
}
6060
}

Sources/InterposeKit/AnyHook.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,54 +16,56 @@ public class AnyHook {
1616

1717
// fetched at apply time, changes late, thus class requirement
1818
var origIMP: IMP?
19-
19+
2020
/// The possible task states
2121
public enum State: Equatable {
2222
/// The task is prepared to be nterposed.
2323
case prepared
24-
24+
2525
/// The method has been successfully interposed.
2626
case interposed
27-
27+
2828
/// An error happened while interposing a method.
2929
indirect case error(InterposeError)
3030
}
31-
31+
3232
init(`class`: AnyClass, selector: Selector) throws {
3333
self.selector = selector
3434
self.class = `class`
3535

3636
// Check if method exists
3737
try validate()
3838
}
39-
39+
4040
func replaceImplementation() throws {
4141
preconditionFailure("Not implemented")
4242
}
43-
43+
4444
func resetImplementation() throws {
4545
preconditionFailure("Not implemented")
4646
}
47-
47+
4848
/// Apply the interpose hook.
4949
@discardableResult public func apply() throws -> AnyHook {
5050
try execute(newState: .interposed) { try replaceImplementation() }
5151
return self
5252
}
53-
53+
5454
/// Revert the interpose hoook.
5555
@discardableResult public func revert() throws -> AnyHook {
5656
try execute(newState: .prepared) { try resetImplementation() }
5757
return self
5858
}
59-
59+
6060
/// Validate that the selector exists on the active class.
6161
@discardableResult func validate(expectedState: State = .prepared) throws -> Method {
62-
guard let method = class_getInstanceMethod(`class`, selector) else { throw InterposeError.methodNotFound(`class`, selector)}
62+
guard let method = class_getInstanceMethod(`class`, selector) else {
63+
throw InterposeError.methodNotFound(`class`, selector)
64+
}
6365
guard state == expectedState else { throw InterposeError.invalidState(expectedState: expectedState) }
6466
return method
6567
}
66-
68+
6769
private func execute(newState: State, task: () throws -> Void) throws {
6870
do {
6971
try task()
@@ -73,7 +75,7 @@ public class AnyHook {
7375
throw error
7476
}
7577
}
76-
78+
7779
/// Release the hook block if possible.
7880
public func cleanup() {
7981
switch state {

Sources/InterposeKit/ClassHook.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import Foundation
33
extension Interpose {
44
/// A hook to an instance method and stores both the original and new implementation.
55
final public class ClassHook<MethodSignature, HookSignature>: TypedHook<MethodSignature, HookSignature> {
6+
/* HookSignature?: This must be optional or swift runtime will crash.
7+
Or swiftc may segfault. Compiler bug? */
68
/// Initialize a new hook to interpose an instance method.
7-
public init(`class`: AnyClass, selector: Selector, implementation:(ClassHook<MethodSignature, HookSignature>) -> HookSignature?) /* This must be optional or swift runtime will crash. Or swiftc may segfault. Compiler bug? */ throws {
9+
public init(`class`: AnyClass, selector: Selector,
10+
implementation: (ClassHook<MethodSignature, HookSignature>) -> HookSignature?) throws {
811
try super.init(class: `class`, selector: selector)
912
replacementIMP = imp_implementationWithBlock(implementation(self) as Any)
1013
}
@@ -20,7 +23,9 @@ extension Interpose {
2023
let method = try validate(expectedState: .interposed)
2124
precondition(origIMP != nil)
2225
let previousIMP = class_replaceMethod(`class`, selector, origIMP!, method_getTypeEncoding(method))
23-
guard previousIMP == replacementIMP else { throw InterposeError.unexpectedImplementation(`class`, selector, previousIMP) }
26+
guard previousIMP == replacementIMP else {
27+
throw InterposeError.unexpectedImplementation(`class`, selector, previousIMP)
28+
}
2429
Interpose.log("Restored -[\(`class`).\(selector)] IMP: \(origIMP!)")
2530
}
2631

Sources/InterposeKit/HookFinder.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ extension Interpose {
1919

2020
static func storeHook<HookType: AnyHook>(hook: HookType, to block: AnyObject) {
2121
// Weakly store reference to hook inside the block of the IMP.
22-
objc_setAssociatedObject(block, &AssociatedKeys.hookForBlock, WeakObjectContainer(with: hook), .OBJC_ASSOCIATION_RETAIN)
22+
objc_setAssociatedObject(block, &AssociatedKeys.hookForBlock,
23+
WeakObjectContainer(with: hook), .OBJC_ASSOCIATION_RETAIN)
2324

2425
}
2526

Sources/InterposeKit/InterposeKit.swift

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extension NSObject {
66
_ selector: Selector,
77
methodSignature: MethodSignature.Type = MethodSignature.self,
88
hookSignature: HookSignature.Type = HookSignature.self,
9-
_ implementation:(TypedHook<MethodSignature, HookSignature>) -> HookSignature?) throws -> AnyHook {
9+
_ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature?) throws -> AnyHook {
1010

1111
if let klass = self as? AnyClass {
1212
return try Interpose.ClassHook(class: klass, selector: selector, implementation: implementation).apply()
@@ -20,8 +20,9 @@ extension NSObject {
2020
_ selector: Selector,
2121
methodSignature: MethodSignature.Type = MethodSignature.self,
2222
hookSignature: HookSignature.Type = HookSignature.self,
23-
_ implementation:(TypedHook<MethodSignature, HookSignature>) -> HookSignature?) throws -> AnyHook {
24-
return try Interpose.ClassHook(class: self as AnyClass, selector: selector, implementation: implementation).apply()
23+
_ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature?) throws -> AnyHook {
24+
return try Interpose.ClassHook(class: self as AnyClass,
25+
selector: selector, implementation: implementation).apply()
2526
}
2627
}
2728

@@ -88,31 +89,47 @@ final public class Interpose {
8889
deinit {
8990
hooks.forEach({ $0.cleanup() })
9091
}
91-
92+
9293
/// Hook an `@objc dynamic` instance method via selector name on the current class.
9394
@discardableResult public func hook<MethodSignature, HookSignature>(
9495
_ selName: String,
9596
methodSignature: MethodSignature.Type = MethodSignature.self,
9697
hookSignature: HookSignature.Type = HookSignature.self,
97-
_ implementation:(TypedHook<MethodSignature, HookSignature>) -> HookSignature?) throws -> TypedHook<MethodSignature, HookSignature> {
98-
try hook(NSSelectorFromString(selName), methodSignature: methodSignature, hookSignature: hookSignature, implementation)
98+
_ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature?)
99+
throws -> TypedHook<MethodSignature, HookSignature> {
100+
try hook(NSSelectorFromString(selName),
101+
methodSignature: methodSignature, hookSignature: hookSignature, implementation)
99102
}
100103

101104
/// Hook an `@objc dynamic` instance method via selector on the current class.
102105
@discardableResult public func hook<MethodSignature, HookSignature> (
103106
_ selector: Selector,
104107
methodSignature: MethodSignature.Type = MethodSignature.self,
105108
hookSignature: HookSignature.Type = HookSignature.self,
106-
_ implementation:(TypedHook<MethodSignature, HookSignature>) -> HookSignature?) throws -> TypedHook<MethodSignature, HookSignature> {
109+
_ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature?)
110+
throws -> TypedHook<MethodSignature, HookSignature> {
111+
let hook = try prepareHook(selector, methodSignature: methodSignature,
112+
hookSignature: hookSignature, implementation)
113+
try hook.apply()
114+
return hook
107115

108-
var hook: TypedHook<MethodSignature, HookSignature>
109-
if let object = self.object {
110-
hook = try ObjectHook(object: object, selector: selector, implementation: implementation)
111-
} else {
112-
hook = try ClassHook(class: `class`, selector: selector, implementation: implementation)
113-
}
114-
hooks.append(hook)
115-
return hook
116+
}
117+
118+
/// Prepares a hook, but does not call apply immediately.
119+
@discardableResult public func prepareHook<MethodSignature, HookSignature> (
120+
_ selector: Selector,
121+
methodSignature: MethodSignature.Type = MethodSignature.self,
122+
hookSignature: HookSignature.Type = HookSignature.self,
123+
_ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature?)
124+
throws -> TypedHook<MethodSignature, HookSignature> {
125+
var hook: TypedHook<MethodSignature, HookSignature>
126+
if let object = self.object {
127+
hook = try ObjectHook(object: object, selector: selector, implementation: implementation)
128+
} else {
129+
hook = try ClassHook(class: `class`, selector: selector, implementation: implementation)
130+
}
131+
hooks.append(hook)
132+
return hook
116133
}
117134

118135
/// Apply all stored hooks.

Sources/InterposeKit/InterposeSubclass.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ class InterposeSubclass {
5656
}
5757
}
5858

59-
guard let nonnullSubclass = subclass else {
59+
guard let nnSubclass = subclass else {
6060
throw InterposeError.failedToAllocateClassPair(class: perceivedClass, subclassName: subclassName)
6161
}
6262

63-
object_setClass(object, nonnullSubclass)
64-
Interpose.log("Generated \(NSStringFromClass(nonnullSubclass)) for object (was: \(NSStringFromClass(class_getSuperclass(object_getClass(object)!)!)))")
65-
return nonnullSubclass
63+
object_setClass(object, nnSubclass)
64+
let oldName = NSStringFromClass(class_getSuperclass(object_getClass(object)!)!)
65+
Interpose.log("Generated \(NSStringFromClass(nnSubclass)) for object (was: \(oldName))")
66+
return nnSubclass
6667
}
6768

6869
/// We need to reuse a dynamic subclass if the object already has one.

Sources/InterposeKit/LinuxCompileSupport.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ func class_replaceMethod(_ cls: AnyClass?, _ name: Selector,
1818
_ imp: IMP, _ types: UnsafePointer<Int8>?) -> IMP? { IMP() }
1919
func class_addMethod(_ cls: AnyClass?, _ name: Selector,
2020
_ imp: IMP, _ types: UnsafePointer<Int8>?) -> Bool { return false }
21-
func class_copyMethodList(_ cls: AnyClass?, _ outCount: UnsafeMutablePointer<UInt32>?) -> UnsafeMutablePointer<Method>? { return nil }
21+
func class_copyMethodList(_ cls: AnyClass?,
22+
_ outCount: UnsafeMutablePointer<UInt32>?) -> UnsafeMutablePointer<Method>? { return nil }
2223
func object_getClass(_ obj: Any?) -> AnyClass? { return nil }
2324
@discardableResult func object_setClass(_ obj: Any?, _ cls: AnyClass) -> AnyClass? { return nil }
2425
func method_getName(_ method: Method) -> Selector { Selector("") }
2526
func class_getSuperclass(_ cls: AnyClass?) -> AnyClass? { return nil }
26-
// swiftlint:disable:next identifier_name
2727
func method_getTypeEncoding(_ method: Method) -> UnsafePointer<Int8>? { return nil }
28-
func method_getImplementation(_ method: Method) -> IMP { IMP() }
28+
func method_getImplementation(_ method: Method) -> IMP { IMP() }
2929
// swiftlint:disable:next identifier_name
3030
func _dyld_register_func_for_add_image(_ func:
3131
(@convention(c) (UnsafePointer<Int8>?, Int) -> Void)!) {}
@@ -60,6 +60,6 @@ enum objc_AssociationPolicy: UInt {
6060
}
6161
func objc_setAssociatedObject(_ object: Any, _ key: UnsafeRawPointer,
6262
_ value: Any?, _ policy: objc_AssociationPolicy) {}
63-
func objc_getAssociatedObject(_ object: Any, _ key: UnsafeRawPointer) -> Any?
64-
{ return nil }
63+
func objc_getAssociatedObject(_ object: Any,
64+
_ key: UnsafeRawPointer) -> Any? { return nil }
6565
#endif

Sources/InterposeKit/ObjectHook.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ extension Interpose {
1616
let generatesSuperIMP = InterposeSubclass.supportsSuperTrampolines
1717

1818
/// Initialize a new hook to interpose an instance method.
19-
public init(object: AnyObject, selector: Selector, implementation:(ObjectHook<MethodSignature, HookSignature>) -> HookSignature?) throws {
19+
public init(object: AnyObject, selector: Selector,
20+
implementation: (ObjectHook<MethodSignature, HookSignature>) -> HookSignature?) throws {
2021
self.object = object
2122
try super.init(class: type(of: object), selector: selector)
2223
let block = implementation(self) as AnyObject
@@ -64,7 +65,7 @@ extension Interpose {
6465

6566
/// Looks for an instance method in the exact class, without looking up the hierarchy.
6667
func exactClassImplementsSelector(_ klass: AnyClass, _ selector: Selector) -> Bool {
67-
var methodCount : CUnsignedInt = 0
68+
var methodCount: CUnsignedInt = 0
6869
guard let methodsInAClass = class_copyMethodList(klass, &methodCount) else { return false }
6970
defer { free(methodsInAClass) }
7071
for index in 0 ..< Int(methodCount) {
@@ -136,12 +137,13 @@ extension Interpose {
136137

137138
guard super.origIMP != nil else {
138139
// Removing methods at runtime is not supported.
139-
// https://stackoverflow.com/questions/1315169/how-do-i-remove-instance-methods-at-runtime-in-objective-c-2-0
140+
// https://stackoverflow.com/questions/1315169/
141+
// how-do-i-remove-instance-methods-at-runtime-in-objective-c-2-0
140142
//
141143
// This codepath will be hit if the super helper is missing.
142144
// We could recreate the whole class at runtime and rebuild all hooks,
143145
// but that seesm excessive when we have a trampoline at our disposal.
144-
Interpose.log("Reset of -[\(`class`).\(selector)] not supported. No Original IMP")
146+
Interpose.log("Reset of -[\(`class`).\(selector)] not supported. No IMP")
145147
throw InterposeError.resetUnsupported("No Original IMP found. SuperBuilder missing?")
146148
}
147149

@@ -151,8 +153,11 @@ extension Interpose {
151153

152154
// We are the topmost hook, replace method.
153155
if currentIMP == replacementIMP {
154-
let previousIMP = class_replaceMethod(dynamicSubclass, selector, origIMP!, method_getTypeEncoding(method))
155-
guard previousIMP == replacementIMP else { throw InterposeError.unexpectedImplementation(dynamicSubclass, selector, previousIMP) }
156+
let previousIMP = class_replaceMethod(
157+
dynamicSubclass, selector, origIMP!, method_getTypeEncoding(method))
158+
guard previousIMP == replacementIMP else {
159+
throw InterposeError.unexpectedImplementation(dynamicSubclass, selector, previousIMP)
160+
}
156161
Interpose.log("Restored -[\(`class`).\(selector)] IMP: \(origIMP!)")
157162
} else {
158163
let nextHook = Interpose.findNextHook(selfHook: self, topmostIMP: currentIMP)

0 commit comments

Comments
 (0)