Tanner Bennett лет назад: 5
Родитель
Сommit
515806e194

+ 9 - 9
Classes/ObjectExplorers/Sections/Shortcuts/FLEXShortcutsFactory+Defaults.m

@@ -15,7 +15,7 @@
 
 @implementation FLEXShortcutsFactory (UIApplication)
 
-+ (void)load { FLEX_EXIT_IF_TESTING()
++ (void)load { FLEX_EXIT_IF_NO_CTORS()
     // sharedApplication class property possibly not added
     // as a literal class property until iOS 10
     FLEXRuntimeUtilityTryAddObjectProperty(
@@ -40,7 +40,7 @@
 
 @implementation FLEXShortcutsFactory (Views)
 
-+ (void)load { FLEX_EXIT_IF_TESTING()
++ (void)load { FLEX_EXIT_IF_NO_CTORS()
     // A quirk of UIView and some other classes: a lot of the `@property`s are
     // not actually properties from the perspective of the runtime.
     //
@@ -131,7 +131,7 @@
 
 @implementation FLEXShortcutsFactory (ViewControllers)
 
-+ (void)load { FLEX_EXIT_IF_TESTING()
++ (void)load { FLEX_EXIT_IF_NO_CTORS()
     // toolbarItems is not really a property, make it one 
     FLEXRuntimeUtilityTryAddObjectProperty(3, toolbarItems, UIViewController.class, NSArray);
     
@@ -151,7 +151,7 @@
 
 @implementation FLEXShortcutsFactory (UIImage)
 
-+ (void)load { FLEX_EXIT_IF_TESTING()
++ (void)load { FLEX_EXIT_IF_NO_CTORS()
     self.append.methods(@[
         @"CGImage", @"CIImage"
     ]).properties(@[
@@ -171,7 +171,7 @@
 
 @implementation FLEXShortcutsFactory (NSBundle)
 
-+ (void)load { FLEX_EXIT_IF_TESTING()
++ (void)load { FLEX_EXIT_IF_NO_CTORS()
     self.append.properties(@[
         @"bundleIdentifier", @"principalClass",
         @"infoDictionary", @"bundlePath",
@@ -186,7 +186,7 @@
 
 @implementation FLEXShortcutsFactory (Classes)
 
-+ (void)load { FLEX_EXIT_IF_TESTING()
++ (void)load { FLEX_EXIT_IF_NO_CTORS()
     self.append.classMethods(@[@"new", @"alloc"]).forClass(NSObject.flex_metaclass);
 }
 
@@ -197,7 +197,7 @@
 
 @implementation FLEXShortcutsFactory (Activities)
 
-+ (void)load { FLEX_EXIT_IF_TESTING()
++ (void)load { FLEX_EXIT_IF_NO_CTORS()
     // Property was added in iOS 10 but we want it on iOS 9 too
     FLEXRuntimeUtilityTryAddNonatomicProperty(9, item, UIActivityItemProvider.class, id, PropertyKey(ReadOnly));
     
@@ -217,7 +217,7 @@
 
 @implementation FLEXShortcutsFactory (Blocks)
 
-+ (void)load { FLEX_EXIT_IF_TESTING()
++ (void)load { FLEX_EXIT_IF_NO_CTORS()
     self.append.methods(@[@"invoke"]).forClass(NSClassFromString(@"NSBlock"));
 }
 
@@ -227,7 +227,7 @@
 
 @implementation FLEXShortcutsFactory (Foundation)
 
-+ (void)load { FLEX_EXIT_IF_TESTING()
++ (void)load { FLEX_EXIT_IF_NO_CTORS()
     self.append.properties(@[
         @"configuration", @"delegate", @"delegateQueue", @"sessionDescription",
     ]).methods(@[

+ 1 - 1
Classes/Utility/Categories/NSObject+FLEX_Reflection.m

@@ -146,7 +146,7 @@ NSArray<FLEXMethod *> *FLEXGetAllMethods(_Nullable Class cls, BOOL instance) {
 @interface NSProxy (AnyObjectAdditions) @end
 @implementation NSProxy (AnyObjectAdditions)
 
-+ (void)load { FLEX_EXIT_IF_TESTING()
++ (void)load { FLEX_EXIT_IF_NO_CTORS()
     // We need to get all of the methods in this file and add them to NSProxy. 
     // To do this we we need the class itself and it's metaclass.
     // Edit: also add them to Swift._SwiftObject

+ 12 - 2
Classes/Utility/FLEXMacros.h

@@ -9,8 +9,18 @@
 #ifndef FLEXMacros_h
 #define FLEXMacros_h
 
-// Used to prevent loading of pre-registered shortcuts and runtime categories in a test environment
-#define FLEX_EXIT_IF_TESTING() if (NSClassFromString(@"XCTest")) return;
+#define flex_keywordify class NSObject;
+#define ctor flex_keywordify __attribute__((constructor)) void __flex_ctor_##__LINE__()
+#define dtor flex_keywordify __attribute__((destructor)) void __flex_dtor_##__LINE__()
+
+// A macro to check if we are running in a test environment
+#define FLEX_IS_TESTING() (NSClassFromString(@"XCTest") != nil)
+
+/// Whether we want the majority of constructors to run upon load or not.
+extern BOOL FLEXConstructorsShouldRun();
+
+/// A macro to return from the current procedure if we don't want to run constructors
+#define FLEX_EXIT_IF_NO_CTORS() if (!FLEXConstructorsShouldRun()) return;
 
 /// Rounds down to the nearest "point" coordinate
 NS_INLINE CGFloat FLEXFloor(CGFloat x) {

+ 15 - 1
Classes/Utility/FLEXUtility.m

@@ -11,8 +11,22 @@
 #import "FLEXResources.h"
 #import "FLEXWindow.h"
 #import <ImageIO/ImageIO.h>
-#import <zlib.h>
 #import <objc/runtime.h>
+#import <zlib.h>
+
+BOOL FLEXConstructorsShouldRun() {
+    static BOOL _FLEXConstructorsShouldRun_storage = YES;
+    
+    static dispatch_once_t onceToken;
+    dispatch_once(&onceToken, ^{
+        NSString *key = @"FLEX_SKIP_INIT";
+        if (getenv(key.UTF8String) || [NSUserDefaults.standardUserDefaults boolForKey:key]) {
+            _FLEXConstructorsShouldRun_storage = NO;
+        }
+    });
+    
+    return _FLEXConstructorsShouldRun_storage;
+}
 
 @implementation FLEXUtility