Skip to content

Commit 6a32101

Browse files
Thomvislukaskubanek
authored andcommitted
Call SuperBuilder.addSuperInstanceMethod(to:selector:) directly
See steipete/InterposeKit#30
1 parent 2d48227 commit 6a32101

File tree

2 files changed

+6
-16
lines changed

2 files changed

+6
-16
lines changed

Sources/InterposeKit/InterposeSubclass.swift

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import SuperBuilder
23

34
class InterposeSubclass {
45

@@ -90,19 +91,14 @@ class InterposeSubclass {
9091
NSClassFromString("SuperBuilder")?.value(forKey: "isSupportedArchitecure") as? Bool ?? false
9192
}
9293

93-
private lazy var addSuperImpl: @convention(c) (AnyClass, Selector, NSErrorPointer) -> Bool = {
94-
let handle = dlopen(nil, RTLD_LAZY)
95-
let imp = dlsym(handle, "IKTAddSuperImplementationToClass")
96-
return unsafeBitCast(imp, to: (@convention(c) (AnyClass, Selector, NSErrorPointer) -> Bool).self)
97-
}()
98-
9994
func addSuperTrampoline(selector: Selector) {
100-
var error: NSError?
101-
if addSuperImpl(dynamicClass, selector, &error) == false {
102-
Interpose.log("Failed to add super implementation to -[\(dynamicClass).\(selector)]: \(error!)")
103-
} else {
95+
do {
96+
try SuperBuilder.addSuperInstanceMethod(to: dynamicClass, selector: selector)
97+
10498
let imp = class_getMethodImplementation(dynamicClass, selector)!
10599
Interpose.log("Added super for -[\(dynamicClass).\(selector)]: \(imp)")
100+
} catch {
101+
Interpose.log("Failed to add super implementation to -[\(dynamicClass).\(selector)]: \(error)")
106102
}
107103
}
108104
#else

Sources/SuperBuilder/src/ITKSuperBuilder.m

-6
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ static IMP ITKGetTrampolineForTypeEncoding(__unused const char *typeEncoding) {
3636
return requiresStructDispatch ? (IMP)msgSendSuperStretTrampoline : (IMP)msgSendSuperTrampoline;
3737
}
3838

39-
// Helper for binding with Swift
40-
BOOL IKTAddSuperImplementationToClass(Class originalClass, SEL selector, NSError **error);
41-
BOOL IKTAddSuperImplementationToClass(Class originalClass, SEL selector, NSError **error) {
42-
return [SuperBuilder addSuperInstanceMethodToClass:originalClass selector:selector error:error];
43-
}
44-
4539
#define ERROR_AND_RETURN(CODE, STRING)\
4640
if (error) { *error = [NSError errorWithDomain:SuperBuilderErrorDomain code:CODE userInfo:@{NSLocalizedDescriptionKey: STRING}];} return NO;
4741

0 commit comments

Comments
 (0)