Skip to content

Commit 851f872

Browse files
committed
Implementation builder block no longer has optional return value
1 parent df600c6 commit 851f872

File tree

4 files changed

+33
-30
lines changed

4 files changed

+33
-30
lines changed

Sources/InterposeKit/ClassHook.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ 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? */
8-
/// Initialize a new hook to interpose an instance method.
9-
public init(`class`: AnyClass, selector: Selector,
10-
implementation: (ClassHook<MethodSignature, HookSignature>) -> HookSignature?) throws {
6+
7+
public init(
8+
`class`: AnyClass,
9+
selector: Selector,
10+
implementation: (ClassHook<MethodSignature, HookSignature>) -> HookSignature
11+
) throws {
1112
try super.init(class: `class`, selector: selector)
12-
replacementIMP = imp_implementationWithBlock(implementation(self) as Any)
13+
replacementIMP = imp_implementationWithBlock(implementation(self))
1314
}
1415

1516
override func replaceImplementation() throws {

Sources/InterposeKit/InterposeKit.swift

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ final public class Interpose {
6969
_ selName: String,
7070
methodSignature: MethodSignature.Type = MethodSignature.self,
7171
hookSignature: HookSignature.Type = HookSignature.self,
72-
_ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature?)
73-
throws -> TypedHook<MethodSignature, HookSignature> {
72+
_ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature
73+
) throws -> TypedHook<MethodSignature, HookSignature> {
7474
try hook(NSSelectorFromString(selName),
7575
methodSignature: methodSignature, hookSignature: hookSignature, implementation)
7676
}
@@ -80,30 +80,29 @@ final public class Interpose {
8080
_ selector: Selector,
8181
methodSignature: MethodSignature.Type = MethodSignature.self,
8282
hookSignature: HookSignature.Type = HookSignature.self,
83-
_ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature?)
84-
throws -> TypedHook<MethodSignature, HookSignature> {
85-
let hook = try prepareHook(selector, methodSignature: methodSignature,
86-
hookSignature: hookSignature, implementation)
87-
try hook.apply()
88-
return hook
89-
83+
_ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature
84+
) throws -> TypedHook<MethodSignature, HookSignature> {
85+
let hook = try prepareHook(selector, methodSignature: methodSignature,
86+
hookSignature: hookSignature, implementation)
87+
try hook.apply()
88+
return hook
9089
}
9190

9291
/// Prepares a hook, but does not call apply immediately.
9392
@discardableResult public func prepareHook<MethodSignature, HookSignature> (
9493
_ selector: Selector,
9594
methodSignature: MethodSignature.Type = MethodSignature.self,
9695
hookSignature: HookSignature.Type = HookSignature.self,
97-
_ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature?)
98-
throws -> TypedHook<MethodSignature, HookSignature> {
99-
var hook: TypedHook<MethodSignature, HookSignature>
100-
if let object = self.object {
101-
hook = try ObjectHook(object: object, selector: selector, implementation: implementation)
102-
} else {
103-
hook = try ClassHook(class: `class`, selector: selector, implementation: implementation)
104-
}
105-
hooks.append(hook)
106-
return hook
96+
_ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature
97+
) throws -> TypedHook<MethodSignature, HookSignature> {
98+
var hook: TypedHook<MethodSignature, HookSignature>
99+
if let object = self.object {
100+
hook = try ObjectHook(object: object, selector: selector, implementation: implementation)
101+
} else {
102+
hook = try ClassHook(class: `class`, selector: selector, implementation: implementation)
103+
}
104+
hooks.append(hook)
105+
return hook
107106
}
108107

109108
/// Apply all stored hooks.

Sources/InterposeKit/NSObject+InterposeKit.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ extension NSObject {
4444
for selector: Selector,
4545
methodSignature: MethodSignature.Type,
4646
hookSignature: HookSignature.Type,
47-
implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature?
47+
implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature
4848
) throws -> AnyHook {
4949
try Interpose.ObjectHook(
5050
object: self,
@@ -63,7 +63,7 @@ extension NSObject {
6363
_ selector: Selector,
6464
methodSignature: MethodSignature.Type = MethodSignature.self,
6565
hookSignature: HookSignature.Type = HookSignature.self,
66-
_ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature?
66+
_ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature
6767
) throws -> AnyHook {
6868
precondition(
6969
!(self is AnyClass),
@@ -88,7 +88,7 @@ extension NSObject {
8888
_ selector: Selector,
8989
methodSignature: MethodSignature.Type = MethodSignature.self,
9090
hookSignature: HookSignature.Type = HookSignature.self,
91-
_ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature?
91+
_ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature
9292
) throws -> AnyHook {
9393
return try Interpose.ClassHook(
9494
class: self as AnyClass,

Sources/InterposeKit/ObjectHook.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ 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,
20-
implementation: (ObjectHook<MethodSignature, HookSignature>) -> HookSignature?) throws {
19+
public init(
20+
object: AnyObject,
21+
selector: Selector,
22+
implementation: (ObjectHook<MethodSignature, HookSignature>) -> HookSignature
23+
) throws {
2124
self.object = object
2225
try super.init(class: type(of: object), selector: selector)
2326
let block = implementation(self) as AnyObject

0 commit comments

Comments
 (0)