Skip to content

Commit 8733b52

Browse files
committed
Add OpenSwiftUIShims
1 parent 95c081b commit 8733b52

File tree

5 files changed

+85
-109
lines changed

5 files changed

+85
-109
lines changed

Sources/OpenSwiftUI_SPI/Shims/CoreAnimation/CoreAnimation_Private.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
OPENSWIFTUI_ASSUME_NONNULL_BEGIN
1515

1616
@interface CALayer (OpenSwiftUI_SPI)
17-
@property (nonatomic, assign, readonly) BOOL openSwiftUI_hasBeenCommitted OPENSWIFTUI_SWIFT_NAME(hasBeenCommitted);
17+
@property (nonatomic, assign, readonly) BOOL hasBeenCommitted_openswiftui_safe_wrapper OPENSWIFTUI_SWIFT_NAME(hasBeenCommitted);
18+
1819
@property (nonatomic, assign) uint64_t openSwiftUI_viewTestProperties;
1920
@end
2021

Sources/OpenSwiftUI_SPI/Shims/CoreAnimation/CoreAnimation_Private.m

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,27 @@
33
// OpenSwiftUI_SPI
44

55
#import "CoreAnimation_Private.h"
6+
#import "../OpenSwiftUIShims.h"
67

78
#if __has_include(<QuartzCore/CoreAnimation.h>)
89

910
#import <objc/runtime.h>
1011

1112
@implementation CALayer (OpenSwiftUI_SPI)
1213

13-
- (BOOL)openSwiftUI_hasBeenCommitted {
14-
typedef BOOL (*Func)(CALayer *, SEL);
15-
SEL selector = NSSelectorFromString(@"hasBeenCommitted");
16-
Func func = nil;
17-
if ([self respondsToSelector:selector]) {
18-
IMP impl = class_getMethodImplementation([self class], selector);
19-
func = (Func)impl;
20-
}
21-
if (func == nil) {
22-
return NO;
23-
}
14+
- (BOOL)hasBeenCommitted_openswiftui_safe_wrapper {
15+
OPENSWIFTUI_SAFE_WRAPPER_IMP(BOOL, @"_performBlockAfterCATransactionCommits:", NO);
2416
return func(self, selector);
2517
}
2618

2719
- (uint64_t)openSwiftUI_viewTestProperties {
28-
typedef uint64_t (*Func)(CALayer *, SEL);
29-
SEL selector = NSSelectorFromString(@"swiftUI_viewTestProperties");
30-
Func func = nil;
31-
if ([self respondsToSelector:selector]) {
32-
IMP impl = class_getMethodImplementation([self class], selector);
33-
func = (Func)impl;
34-
}
35-
if (func == nil) {
36-
return 0;
37-
}
38-
return func(self, selector);
20+
NSNumber *properties = [self valueForKey:@"_viewTestProperties"];
21+
return properties.integerValue;
3922
}
4023

41-
- (void)setOpenSwiftUI_viewTestProperties:(uint64_t)viewTestProperties {
42-
typedef void (*Func)(CALayer *, SEL, uint64_t);
43-
SEL selector = NSSelectorFromString(@"setSwiftUI_viewTestProperties:");
44-
Func func = nil;
45-
if ([self respondsToSelector:selector]) {
46-
IMP impl = class_getMethodImplementation([self class], selector);
47-
func = (Func)impl;
48-
}
49-
if (func == nil) {
50-
return;
51-
}
52-
return func(self, selector, viewTestProperties);
24+
- (void)setOpenSwiftUI_viewTestProperties:(uint64_t)properties {
25+
[self setValue:[NSNumber numberWithUnsignedLongLong:properties] forKey:@"_viewTestProperties"];
5326
}
54-
5527
@end
5628

5729
#endif /* CoreAnimation.h */
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// OpenSwiftUIShims.h
3+
// OpenSwiftUI_SPI
4+
//
5+
6+
#ifndef OpenSwiftUIShims_h
7+
#define OpenSwiftUIShims_h
8+
9+
#include "OpenSwiftUIBase.h"
10+
11+
#if OPENSWIFTUI_TARGET_OS_DARWIN
12+
13+
#include <os/log.h>
14+
15+
OPENSWIFTUI_ASSUME_NONNULL_BEGIN
16+
17+
OPENSWIFTUI_EXPORT
18+
os_log_t openSwiftUIShimsLog(void);
19+
20+
#define OPENSWIFTUI_SHIMS_LOG_ERROR(fmt, ...) os_log_error(openSwiftUIShimsLog(), fmt, ##__VA_ARGS__)
21+
22+
#define OPENSWIFTUI_SAFE_WRAPPER_IMP(ReturnType, SelectorName, DefaultReturnValue, ...) \
23+
typedef ReturnType (*Func)(id, SEL, ##__VA_ARGS__); \
24+
SEL selector = NSSelectorFromString(SelectorName); \
25+
Func func = nil; \
26+
if ([self respondsToSelector:selector]) { \
27+
IMP impl = class_getMethodImplementation([self class], selector); \
28+
func = (Func)impl; \
29+
} else { \
30+
OPENSWIFTUI_SHIMS_LOG_ERROR("%@ can't respond to selector %@", NSStringFromClass([self class]), NSStringFromSelector(selector)); \
31+
} \
32+
if (func == nil) { \
33+
OPENSWIFTUI_SHIMS_LOG_ERROR("%@ can't get method implementation for selector %@", NSStringFromClass([self class]), NSStringFromSelector(selector)); \
34+
return DefaultReturnValue; \
35+
}
36+
37+
38+
OPENSWIFTUI_ASSUME_NONNULL_END
39+
40+
#endif /* OPENSWIFTUI_TARGET_OS_DARWIN */
41+
42+
#endif /* OpenSwiftUIShims_h */
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// OpenSwiftUIShims.m
3+
// OpenSwiftUI_SPI
4+
//
5+
6+
#import "OpenSwiftUIShims.h"
7+
8+
#if OPENSWIFTUI_TARGET_OS_DARWIN
9+
10+
os_log_t openSwiftUIShimsLog(void) {
11+
static dispatch_once_t onceToken;
12+
static os_log_t log = NULL;
13+
dispatch_once(&onceToken, ^{
14+
log = os_log_create("org.OpenSwiftUIProject.OpenSwiftUI", "Shims");
15+
});
16+
return log;
17+
}
18+
19+
#endif /* OPENSWIFTUI_TARGET_OS_DARWIN */

Sources/OpenSwiftUI_SPI/Shims/UIKit/UIKit_Private.m

Lines changed: 15 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,108 +5,50 @@
55
// Status: WIP
66

77
#import "UIKit_Private.h"
8+
#import "../OpenSwiftUIShims.h"
89

910
#if __has_include(<UIKit/UIKit.h>)
1011

1112
#import <objc/runtime.h>
1213

1314
@implementation UIApplication (OpenSwiftUI_SPI)
1415
- (void)_performBlockAfterCATransactionCommits_openswiftui_safe_wrapper:(void (^)(void))block {
15-
typedef void (*Func)(UIApplication *, SEL, void (^)(void));
16-
SEL selector = NSSelectorFromString(@"_performBlockAfterCATransactionCommits:");
17-
Func func = nil;
18-
if ([self respondsToSelector:selector]) {
19-
IMP impl = class_getMethodImplementation([self class], selector);
20-
func = (Func)impl;
21-
}
22-
if (func != nil) {
23-
func(self, selector, block);
24-
}
16+
OPENSWIFTUI_SAFE_WRAPPER_IMP(void, @"_performBlockAfterCATransactionCommits:", block(),void (^)(void));
17+
func(self, selector, block);
2518
}
2619

2720
- (void)startedTest_openswiftui_safe_wrapper:(NSString *)name {
28-
typedef void (*Func)(UIApplication *, SEL, NSString *);
29-
SEL selector = NSSelectorFromString(@"startedTest:");
30-
Func func = nil;
31-
if ([self respondsToSelector:selector]) {
32-
IMP impl = class_getMethodImplementation([self class], selector);
33-
func = (Func)impl;
34-
}
35-
if (func != nil) {
36-
func(self, selector, name);
37-
}
21+
OPENSWIFTUI_SAFE_WRAPPER_IMP(void, @"startedTest:", , NSString *);
22+
func(self, selector, name);
3823
}
3924

4025
- (void)finishedTest_openswiftui_safe_wrapper:(NSString *)name {
41-
typedef void (*Func)(UIApplication *, SEL, NSString *);
42-
SEL selector = NSSelectorFromString(@"finishedTest:");
43-
Func func = nil;
44-
if ([self respondsToSelector:selector]) {
45-
IMP impl = class_getMethodImplementation([self class], selector);
46-
func = (Func)impl;
47-
}
48-
if (func != nil) {
49-
func(self, selector, name);
50-
}
26+
OPENSWIFTUI_SAFE_WRAPPER_IMP(void, @"finishedTest:", , NSString *);
27+
func(self, selector, name);
5128
}
5229

5330
- (void)failedTest_openswiftui_safe_wrapper:(NSString *)name withFailure:(NSError *)failure {
54-
typedef void (*Func)(UIApplication *, SEL, NSString *, NSError *);
55-
SEL selector = NSSelectorFromString(@"failedTest:withFailure:");
56-
Func func = nil;
57-
if ([self respondsToSelector:selector]) {
58-
IMP impl = class_getMethodImplementation([self class], selector);
59-
func = (Func)impl;
60-
}
61-
if (func != nil) {
62-
func(self, selector, name, failure);
63-
}
31+
OPENSWIFTUI_SAFE_WRAPPER_IMP(void, @"failedTest:withFailure:", , NSString *, NSError *);
32+
func(self, selector, name, failure);
6433
}
6534

6635
- (NSString *)_launchTestName_openswiftui_safe_wrapper {
67-
typedef NSString* (*Func)(UIApplication *, SEL);
68-
SEL selector = NSSelectorFromString(@"_launchTestName");
69-
Func func = nil;
70-
if ([self respondsToSelector:selector]) {
71-
IMP impl = class_getMethodImplementation([self class], selector);
72-
func = (Func)impl;
73-
}
74-
if (func != nil) {
75-
return func(self, selector);
76-
}
77-
return nil;
36+
OPENSWIFTUI_SAFE_WRAPPER_IMP(NSString *, @"_launchTestName", nil);
37+
return func(self, selector);
7838
}
7939
@end
8040

8141
@implementation UIView (OpenSwiftUI_SPI)
8242
- (BOOL)_shouldAnimatePropertyWithKey_openswiftui_safe_wrapper:(NSString *)key {
83-
typedef BOOL (*Func)(UIView *, SEL, NSString *);
84-
SEL selector = NSSelectorFromString(@"_shouldAnimatePropertyWithKey:");
85-
Func func = nil;
86-
if ([self respondsToSelector:selector]) {
87-
IMP impl = class_getMethodImplementation([self class], selector);
88-
func = (Func)impl;
89-
}
90-
if (func != nil) {
91-
return func(self, selector, key);
92-
}
93-
return NO;
43+
OPENSWIFTUI_SAFE_WRAPPER_IMP(BOOL, @"_shouldAnimatePropertyWithKey:", NO, NSString *);
44+
return func(self, selector, key);
9445
}
9546
@end
9647

9748
@implementation UIViewController (OpenSwiftUI_SPI)
9849
- (BOOL)_canShowWhileLocked_openswiftui_safe_wrapper {
99-
typedef BOOL (*Func)(UIViewController *, SEL);
100-
SEL selector = NSSelectorFromString(@"_canShowWhileLocked");
101-
Func func = nil;
102-
if ([self respondsToSelector:selector]) {
103-
IMP impl = class_getMethodImplementation([self class], selector);
104-
func = (Func)impl;
105-
}
106-
if (func != nil) {
107-
return func(self, selector);
108-
}
109-
return NO;
50+
OPENSWIFTUI_SAFE_WRAPPER_IMP(BOOL, @"_canShowWhileLocked", NO);
51+
return func(self, selector);
11052
}
11153
@end
11254

0 commit comments

Comments
 (0)