FLEXRuntimeUtility.m 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. //
  2. // FLEXRuntimeUtility.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/8/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import "FLEXRuntimeUtility.h"
  10. #import "FLEXObjcInternal.h"
  11. #import "FLEXTypeEncodingParser.h"
  12. static NSString *const FLEXRuntimeUtilityErrorDomain = @"FLEXRuntimeUtilityErrorDomain";
  13. typedef NS_ENUM(NSInteger, FLEXRuntimeUtilityErrorCode) {
  14. FLEXRuntimeUtilityErrorCodeDoesNotRecognizeSelector = 0,
  15. FLEXRuntimeUtilityErrorCodeInvocationFailed = 1,
  16. FLEXRuntimeUtilityErrorCodeArgumentTypeMismatch = 2
  17. };
  18. @implementation FLEXRuntimeUtility
  19. #pragma mark - General Helpers (Public)
  20. + (BOOL)pointerIsValidObjcObject:(const void *)pointer {
  21. return FLEXPointerIsValidObjcObject(pointer);
  22. }
  23. + (id)potentiallyUnwrapBoxedPointer:(id)returnedObjectOrNil type:(const FLEXTypeEncoding *)returnType {
  24. if (!returnedObjectOrNil) {
  25. return nil;
  26. }
  27. NSInteger i = 0;
  28. if (returnType[i] == FLEXTypeEncodingConst) {
  29. i++;
  30. }
  31. BOOL returnsObjectOrClass = returnType[i] == FLEXTypeEncodingObjcObject ||
  32. returnType[i] == FLEXTypeEncodingObjcClass;
  33. BOOL returnsVoidPointer = returnType[i] == FLEXTypeEncodingPointer &&
  34. returnType[i+1] == FLEXTypeEncodingVoid;
  35. BOOL returnsCString = returnType[i] == FLEXTypeEncodingCString;
  36. // If we got back an NSValue and the return type is not an object,
  37. // we check to see if the pointer is of a valid object. If not,
  38. // we just display the NSValue.
  39. if (!returnsObjectOrClass) {
  40. // Skip NSNumber instances
  41. if ([returnedObjectOrNil isKindOfClass:[NSNumber class]]) {
  42. return returnedObjectOrNil;
  43. }
  44. // Can only be NSValue since return type is not an object,
  45. // so we bail if this doesn't add up
  46. if (![returnedObjectOrNil isKindOfClass:[NSValue class]]) {
  47. return returnedObjectOrNil;
  48. }
  49. NSValue *value = (NSValue *)returnedObjectOrNil;
  50. if (returnsCString) {
  51. // Wrap char * in NSString
  52. const char *string = (const char *)value.pointerValue;
  53. returnedObjectOrNil = string ? [NSString stringWithCString:string encoding:NSUTF8StringEncoding] : NULL;
  54. } else if (returnsVoidPointer) {
  55. // Cast valid objects disguised as void * to id
  56. if ([FLEXRuntimeUtility pointerIsValidObjcObject:value.pointerValue]) {
  57. returnedObjectOrNil = (__bridge id)value.pointerValue;
  58. }
  59. }
  60. }
  61. return returnedObjectOrNil;
  62. }
  63. + (NSUInteger)fieldNameOffsetForTypeEncoding:(const FLEXTypeEncoding *)typeEncoding {
  64. NSUInteger beginIndex = 0;
  65. while (typeEncoding[beginIndex] == FLEXTypeEncodingQuote) {
  66. NSUInteger endIndex = beginIndex + 1;
  67. while (typeEncoding[endIndex] != FLEXTypeEncodingQuote) {
  68. ++endIndex;
  69. }
  70. beginIndex = endIndex + 1;
  71. }
  72. return beginIndex;
  73. }
  74. + (NSArray<Class> *)classHierarchyOfObject:(id)objectOrClass {
  75. NSMutableArray<Class> *superClasses = [NSMutableArray new];
  76. id cls = [objectOrClass class];
  77. do {
  78. [superClasses addObject:cls];
  79. } while ((cls = [cls superclass]));
  80. return superClasses;
  81. }
  82. /// Could be nil
  83. + (NSString *)safeDescriptionForObject:(id)object {
  84. // Don't assume that we have an NSObject subclass.
  85. // Check to make sure the object responds to the description method
  86. if ([object respondsToSelector:@selector(description)]) {
  87. return [object description];
  88. }
  89. return nil;
  90. }
  91. /// Never nil
  92. + (NSString *)safeDebugDescriptionForObject:(id)object {
  93. NSString *description = nil;
  94. // Don't assume that we have an NSObject subclass.
  95. // Check to make sure the object responds to the description method
  96. if ([object respondsToSelector:@selector(debugDescription)]) {
  97. description = [object debugDescription];
  98. } else {
  99. description = [self safeDescriptionForObject:object];
  100. }
  101. if (!description.length) {
  102. NSString *cls = NSStringFromClass(object_getClass(object));
  103. if (object_isClass(object)) {
  104. description = [cls stringByAppendingString:@" class (no description)"];
  105. } else {
  106. description = [cls stringByAppendingString:@" instance (no description)"];
  107. }
  108. }
  109. return description;
  110. }
  111. + (NSString *)summaryForObject:(id)value {
  112. NSString *description = nil;
  113. // Special case BOOL for better readability.
  114. if ([value isKindOfClass:[NSValue class]]) {
  115. const char *type = [value objCType];
  116. if (strcmp(type, @encode(BOOL)) == 0) {
  117. BOOL boolValue = NO;
  118. [value getValue:&boolValue];
  119. return boolValue ? @"YES" : @"NO";
  120. } else if (strcmp(type, @encode(SEL)) == 0) {
  121. SEL selector = NULL;
  122. [value getValue:&selector];
  123. return NSStringFromSelector(selector);
  124. }
  125. }
  126. @try {
  127. // Single line display - replace newlines and tabs with spaces.
  128. description = [[self safeDescriptionForObject:value] stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
  129. description = [description stringByReplacingOccurrencesOfString:@"\t" withString:@" "];
  130. } @catch (NSException *e) {
  131. description = [@"Thrown: " stringByAppendingString:e.reason ?: @"(nil exception reason)"];
  132. }
  133. if (!description) {
  134. description = @"nil";
  135. }
  136. return description;
  137. }
  138. #pragma mark - Property Helpers (Public)
  139. + (BOOL)tryAddPropertyWithName:(const char *)name
  140. attributes:(NSDictionary<NSString *, NSString *> *)attributePairs
  141. toClass:(__unsafe_unretained Class)theClass {
  142. objc_property_t property = class_getProperty(theClass, name);
  143. if (!property) {
  144. unsigned int totalAttributesCount = (unsigned int)attributePairs.count;
  145. objc_property_attribute_t *attributes = malloc(sizeof(objc_property_attribute_t) * totalAttributesCount);
  146. if (attributes) {
  147. unsigned int attributeIndex = 0;
  148. for (NSString *attributeName in attributePairs.allKeys) {
  149. objc_property_attribute_t attribute;
  150. attribute.name = attributeName.UTF8String;
  151. attribute.value = attributePairs[attributeName].UTF8String;
  152. attributes[attributeIndex++] = attribute;
  153. }
  154. BOOL success = class_addProperty(theClass, name, attributes, totalAttributesCount);
  155. free(attributes);
  156. return success;
  157. } else {
  158. return NO;
  159. }
  160. }
  161. return YES;
  162. }
  163. + (NSArray<NSString *> *)allPropertyAttributeKeys {
  164. static NSArray<NSString *> *allPropertyAttributeKeys = nil;
  165. static dispatch_once_t onceToken;
  166. dispatch_once(&onceToken, ^{
  167. allPropertyAttributeKeys = @[
  168. kFLEXPropertyAttributeKeyTypeEncoding,
  169. kFLEXPropertyAttributeKeyBackingIvarName,
  170. kFLEXPropertyAttributeKeyReadOnly,
  171. kFLEXPropertyAttributeKeyCopy,
  172. kFLEXPropertyAttributeKeyRetain,
  173. kFLEXPropertyAttributeKeyNonAtomic,
  174. kFLEXPropertyAttributeKeyCustomGetter,
  175. kFLEXPropertyAttributeKeyCustomSetter,
  176. kFLEXPropertyAttributeKeyDynamic,
  177. kFLEXPropertyAttributeKeyWeak,
  178. kFLEXPropertyAttributeKeyGarbageCollectable,
  179. kFLEXPropertyAttributeKeyOldStyleTypeEncoding,
  180. ];
  181. });
  182. return allPropertyAttributeKeys;
  183. }
  184. #pragma mark - Method Helpers (Public)
  185. + (NSArray<NSString *> *)prettyArgumentComponentsForMethod:(Method)method {
  186. NSMutableArray<NSString *> *components = [NSMutableArray new];
  187. NSString *selectorName = NSStringFromSelector(method_getName(method));
  188. NSMutableArray<NSString *> *selectorComponents = [selectorName componentsSeparatedByString:@":"].mutableCopy;
  189. // this is a workaround cause method_getNumberOfArguments() returns wrong number for some methods
  190. if (selectorComponents.count == 1) {
  191. return @[];
  192. }
  193. if ([selectorComponents.lastObject isEqualToString:@""]) {
  194. [selectorComponents removeLastObject];
  195. }
  196. for (unsigned int argIndex = 0; argIndex < selectorComponents.count; argIndex++) {
  197. char *argType = method_copyArgumentType(method, argIndex + kFLEXNumberOfImplicitArgs);
  198. NSString *readableArgType = (argType != NULL) ? [self readableTypeForEncoding:@(argType)] : nil;
  199. free(argType);
  200. NSString *prettyComponent = [NSString
  201. stringWithFormat:@"%@:(%@) ",
  202. selectorComponents[argIndex],
  203. readableArgType
  204. ];
  205. [components addObject:prettyComponent];
  206. }
  207. return components;
  208. }
  209. #pragma mark - Method Calling/Field Editing (Public)
  210. + (id)performSelector:(SEL)selector onObject:(id)object {
  211. return [self performSelector:selector onObject:object withArguments:@[] error:nil];
  212. }
  213. + (id)performSelector:(SEL)selector
  214. onObject:(id)object
  215. withArguments:(NSArray *)arguments
  216. error:(NSError * __autoreleasing *)error {
  217. static dispatch_once_t onceToken;
  218. static SEL stdStringExclusion = nil;
  219. dispatch_once(&onceToken, ^{
  220. stdStringExclusion = NSSelectorFromString(@"stdString");
  221. });
  222. // Bail if the object won't respond to this selector.
  223. if (![object respondsToSelector:selector]) {
  224. if (error) {
  225. NSString *msg = [NSString
  226. stringWithFormat:@"%@ does not respond to the selector %@",
  227. object, NSStringFromSelector(selector)
  228. ];
  229. NSDictionary<NSString *, id> *userInfo = @{ NSLocalizedDescriptionKey : msg };
  230. *error = [NSError
  231. errorWithDomain:FLEXRuntimeUtilityErrorDomain
  232. code:FLEXRuntimeUtilityErrorCodeDoesNotRecognizeSelector
  233. userInfo:userInfo
  234. ];
  235. }
  236. return nil;
  237. }
  238. NSMethodSignature *methodSignature = [NSMethodSignature signatureWithObjCTypes:({
  239. Method method;
  240. if (object_isClass(object)) {
  241. method = class_getClassMethod(object, selector);
  242. } else {
  243. method = class_getInstanceMethod(object_getClass(object), selector);
  244. }
  245. method_getTypeEncoding(method);
  246. })];
  247. // Probably an unsupported type encoding, like bitfields.
  248. // In the future, we could calculate the return length
  249. // on our own. For now, we abort.
  250. //
  251. // For future reference, the code here will get the true type encoding.
  252. // NSMethodSignature will convert {?=b8b4b1b1b18[8S]} to {?}
  253. //
  254. // returnType = method_getTypeEncoding(class_getInstanceMethod([object class], selector));
  255. if (!methodSignature.methodReturnLength &&
  256. methodSignature.methodReturnType[0] != FLEXTypeEncodingVoid) {
  257. return nil;
  258. }
  259. // Build the invocation
  260. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
  261. [invocation setSelector:selector];
  262. [invocation setTarget:object];
  263. [invocation retainArguments];
  264. // Always self and _cmd
  265. NSUInteger numberOfArguments = [methodSignature numberOfArguments];
  266. for (NSUInteger argumentIndex = kFLEXNumberOfImplicitArgs; argumentIndex < numberOfArguments; argumentIndex++) {
  267. NSUInteger argumentsArrayIndex = argumentIndex - kFLEXNumberOfImplicitArgs;
  268. id argumentObject = arguments.count > argumentsArrayIndex ? arguments[argumentsArrayIndex] : nil;
  269. // NSNull in the arguments array can be passed as a placeholder to indicate nil.
  270. // We only need to set the argument if it will be non-nil.
  271. if (argumentObject && ![argumentObject isKindOfClass:[NSNull class]]) {
  272. const char *typeEncodingCString = [methodSignature getArgumentTypeAtIndex:argumentIndex];
  273. if (typeEncodingCString[0] == FLEXTypeEncodingObjcObject ||
  274. typeEncodingCString[0] == FLEXTypeEncodingObjcClass ||
  275. [self isTollFreeBridgedValue:argumentObject forCFType:typeEncodingCString]) {
  276. // Object
  277. [invocation setArgument:&argumentObject atIndex:argumentIndex];
  278. } else if (strcmp(typeEncodingCString, @encode(CGColorRef)) == 0 &&
  279. [argumentObject isKindOfClass:[UIColor class]]) {
  280. // Bridging UIColor to CGColorRef
  281. CGColorRef colorRef = [argumentObject CGColor];
  282. [invocation setArgument:&colorRef atIndex:argumentIndex];
  283. } else if ([argumentObject isKindOfClass:[NSValue class]]) {
  284. // Primitive boxed in NSValue
  285. NSValue *argumentValue = (NSValue *)argumentObject;
  286. // Ensure that the type encoding on the NSValue matches the type encoding of the argument in the method signature
  287. if (strcmp([argumentValue objCType], typeEncodingCString) != 0) {
  288. if (error) {
  289. NSString *msg = [NSString
  290. stringWithFormat:@"Type encoding mismatch for argument at index %lu. "
  291. "Value type: %s; Method argument type: %s.",
  292. (unsigned long)argumentsArrayIndex, argumentValue.objCType, typeEncodingCString
  293. ];
  294. NSDictionary<NSString *, id> *userInfo = @{ NSLocalizedDescriptionKey : msg };
  295. *error = [NSError
  296. errorWithDomain:FLEXRuntimeUtilityErrorDomain
  297. code:FLEXRuntimeUtilityErrorCodeArgumentTypeMismatch
  298. userInfo:userInfo
  299. ];
  300. }
  301. return nil;
  302. }
  303. @try {
  304. NSUInteger bufferSize = 0;
  305. FLEXGetSizeAndAlignment(typeEncodingCString, &bufferSize, NULL);
  306. if (bufferSize > 0) {
  307. void *buffer = alloca(bufferSize);
  308. [argumentValue getValue:buffer];
  309. [invocation setArgument:buffer atIndex:argumentIndex];
  310. }
  311. } @catch (NSException *exception) { }
  312. }
  313. }
  314. }
  315. // Try to invoke the invocation but guard against an exception being thrown.
  316. id returnObject = nil;
  317. @try {
  318. [invocation invoke];
  319. // Retrieve the return value and box if necessary.
  320. const char *returnType = methodSignature.methodReturnType;
  321. if (returnType[0] == FLEXTypeEncodingObjcObject || returnType[0] == FLEXTypeEncodingObjcClass) {
  322. // Return value is an object.
  323. __unsafe_unretained id objectReturnedFromMethod = nil;
  324. [invocation getReturnValue:&objectReturnedFromMethod];
  325. returnObject = objectReturnedFromMethod;
  326. } else if (returnType[0] != FLEXTypeEncodingVoid) {
  327. NSAssert(methodSignature.methodReturnLength, @"Memory corruption lies ahead");
  328. if (returnType[0] == FLEXTypeEncodingStructBegin) {
  329. if (selector == stdStringExclusion && [object isKindOfClass:[NSString class]]) {
  330. // stdString is a C++ object and we will crash if we try to access it
  331. if (error) {
  332. *error = [NSError
  333. errorWithDomain:FLEXRuntimeUtilityErrorDomain
  334. code:FLEXRuntimeUtilityErrorCodeInvocationFailed
  335. userInfo:@{ NSLocalizedDescriptionKey : @"Skipping -[NSString stdString]" }
  336. ];
  337. }
  338. return nil;
  339. }
  340. }
  341. // Will use arbitrary buffer for return value and box it.
  342. void *returnValue = malloc(methodSignature.methodReturnLength);
  343. [invocation getReturnValue:returnValue];
  344. returnObject = [self valueForPrimitivePointer:returnValue objCType:returnType];
  345. free(returnValue);
  346. }
  347. } @catch (NSException *exception) {
  348. // Bummer...
  349. if (error) {
  350. // "… on <class>" / "… on instance of <class>"
  351. NSString *class = NSStringFromClass([object class]);
  352. NSString *calledOn = object == [object class] ? class : [@"an instance of " stringByAppendingString:class];
  353. NSString *message = [NSString
  354. stringWithFormat:@"Exception '%@' thrown while performing selector '%@' on %@.\nReason:\n\n%@",
  355. exception.name, NSStringFromSelector(selector), calledOn, exception.reason
  356. ];
  357. *error = [NSError
  358. errorWithDomain:FLEXRuntimeUtilityErrorDomain
  359. code:FLEXRuntimeUtilityErrorCodeInvocationFailed
  360. userInfo:@{ NSLocalizedDescriptionKey : message }
  361. ];
  362. }
  363. }
  364. return returnObject;
  365. }
  366. + (BOOL)isTollFreeBridgedValue:(id)value forCFType:(const char *)typeEncoding {
  367. // See https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/Toll-FreeBridgin/Toll-FreeBridgin.html
  368. #define CASE(cftype, foundationClass) \
  369. if (strcmp(typeEncoding, @encode(cftype)) == 0) { \
  370. return [value isKindOfClass:[foundationClass class]]; \
  371. }
  372. CASE(CFArrayRef, NSArray);
  373. CASE(CFAttributedStringRef, NSAttributedString);
  374. CASE(CFCalendarRef, NSCalendar);
  375. CASE(CFCharacterSetRef, NSCharacterSet);
  376. CASE(CFDataRef, NSData);
  377. CASE(CFDateRef, NSDate);
  378. CASE(CFDictionaryRef, NSDictionary);
  379. CASE(CFErrorRef, NSError);
  380. CASE(CFLocaleRef, NSLocale);
  381. CASE(CFMutableArrayRef, NSMutableArray);
  382. CASE(CFMutableAttributedStringRef, NSMutableAttributedString);
  383. CASE(CFMutableCharacterSetRef, NSMutableCharacterSet);
  384. CASE(CFMutableDataRef, NSMutableData);
  385. CASE(CFMutableDictionaryRef, NSMutableDictionary);
  386. CASE(CFMutableSetRef, NSMutableSet);
  387. CASE(CFMutableStringRef, NSMutableString);
  388. CASE(CFNumberRef, NSNumber);
  389. CASE(CFReadStreamRef, NSInputStream);
  390. CASE(CFRunLoopTimerRef, NSTimer);
  391. CASE(CFSetRef, NSSet);
  392. CASE(CFStringRef, NSString);
  393. CASE(CFTimeZoneRef, NSTimeZone);
  394. CASE(CFURLRef, NSURL);
  395. CASE(CFWriteStreamRef, NSOutputStream);
  396. #undef CASE
  397. return NO;
  398. }
  399. + (NSString *)editableJSONStringForObject:(id)object {
  400. NSString *editableDescription = nil;
  401. if (object) {
  402. // This is a hack to use JSON serialization for our editable objects.
  403. // NSJSONSerialization doesn't allow writing fragments - the top level object must be an array or dictionary.
  404. // We always wrap the object inside an array and then strip the outer square braces off the final string.
  405. NSArray *wrappedObject = @[object];
  406. if ([NSJSONSerialization isValidJSONObject:wrappedObject]) {
  407. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:wrappedObject options:0 error:NULL];
  408. NSString *wrappedDescription = [NSString stringWithUTF8String:jsonData.bytes];
  409. editableDescription = [wrappedDescription substringWithRange:NSMakeRange(1, wrappedDescription.length - 2)];
  410. }
  411. }
  412. return editableDescription;
  413. }
  414. + (id)objectValueFromEditableJSONString:(NSString *)string {
  415. id value = nil;
  416. // nil for empty string/whitespace
  417. if ([string stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet].length) {
  418. value = [NSJSONSerialization
  419. JSONObjectWithData:[string dataUsingEncoding:NSUTF8StringEncoding]
  420. options:NSJSONReadingAllowFragments
  421. error:NULL
  422. ];
  423. }
  424. return value;
  425. }
  426. + (NSValue *)valueForNumberWithObjCType:(const char *)typeEncoding fromInputString:(NSString *)inputString {
  427. NSNumberFormatter *formatter = [NSNumberFormatter new];
  428. [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
  429. NSNumber *number = [formatter numberFromString:inputString];
  430. // Is the type encoding longer than one character?
  431. if (strlen(typeEncoding) > 1) {
  432. NSString *type = @(typeEncoding);
  433. // Is it NSDecimalNumber or NSNumber?
  434. if ([type isEqualToString:@FLEXEncodeClass(NSDecimalNumber)]) {
  435. return [NSDecimalNumber decimalNumberWithString:inputString];
  436. } else if ([type isEqualToString:@FLEXEncodeClass(NSNumber)]) {
  437. return number;
  438. }
  439. return nil;
  440. }
  441. // Type encoding is one character, switch on the type
  442. FLEXTypeEncoding type = typeEncoding[0];
  443. uint8_t value[32];
  444. void *bufferStart = &value[0];
  445. // Make sure we box the number with the correct type encoding
  446. // so it can be properly unboxed later via getValue:
  447. switch (type) {
  448. case FLEXTypeEncodingChar:
  449. *(char *)bufferStart = number.charValue; break;
  450. case FLEXTypeEncodingInt:
  451. *(int *)bufferStart = number.intValue; break;
  452. case FLEXTypeEncodingShort:
  453. *(short *)bufferStart = number.shortValue; break;
  454. case FLEXTypeEncodingLong:
  455. *(long *)bufferStart = number.longValue; break;
  456. case FLEXTypeEncodingLongLong:
  457. *(long long *)bufferStart = number.longLongValue; break;
  458. case FLEXTypeEncodingUnsignedChar:
  459. *(unsigned char *)bufferStart = number.unsignedCharValue; break;
  460. case FLEXTypeEncodingUnsignedInt:
  461. *(unsigned int *)bufferStart = number.unsignedIntValue; break;
  462. case FLEXTypeEncodingUnsignedShort:
  463. *(unsigned short *)bufferStart = number.unsignedShortValue; break;
  464. case FLEXTypeEncodingUnsignedLong:
  465. *(unsigned long *)bufferStart = number.unsignedLongValue; break;
  466. case FLEXTypeEncodingUnsignedLongLong:
  467. *(unsigned long long *)bufferStart = number.unsignedLongLongValue; break;
  468. case FLEXTypeEncodingFloat:
  469. *(float *)bufferStart = number.floatValue; break;
  470. case FLEXTypeEncodingDouble:
  471. *(double *)bufferStart = number.doubleValue; break;
  472. case FLEXTypeEncodingLongDouble:
  473. // NSNumber does not support long double
  474. default:
  475. return nil;
  476. }
  477. return [NSValue value:value withObjCType:typeEncoding];
  478. }
  479. + (void)enumerateTypesInStructEncoding:(const char *)structEncoding
  480. usingBlock:(void (^)(NSString *structName,
  481. const char *fieldTypeEncoding,
  482. NSString *prettyTypeEncoding,
  483. NSUInteger fieldIndex,
  484. NSUInteger fieldOffset))typeBlock {
  485. if (structEncoding && structEncoding[0] == FLEXTypeEncodingStructBegin) {
  486. const char *equals = strchr(structEncoding, '=');
  487. if (equals) {
  488. const char *nameStart = structEncoding + 1;
  489. NSString *structName = [@(structEncoding)
  490. substringWithRange:NSMakeRange(nameStart - structEncoding, equals - nameStart)
  491. ];
  492. NSUInteger fieldAlignment = 0, structSize = 0;
  493. if (FLEXGetSizeAndAlignment(structEncoding, &structSize, &fieldAlignment)) {
  494. NSUInteger runningFieldIndex = 0;
  495. NSUInteger runningFieldOffset = 0;
  496. const char *typeStart = equals + 1;
  497. while (*typeStart != FLEXTypeEncodingStructEnd) {
  498. NSUInteger fieldSize = 0;
  499. // If the struct type encoding was successfully handled by
  500. // FLEXGetSizeAndAlignment above, we *should* be ok with the field here.
  501. const char *nextTypeStart = NSGetSizeAndAlignment(typeStart, &fieldSize, NULL);
  502. NSString *typeEncoding = [@(structEncoding)
  503. substringWithRange:NSMakeRange(typeStart - structEncoding, nextTypeStart - typeStart)
  504. ];
  505. // Padding to keep proper alignment. __attribute((packed)) structs
  506. // will break here. The type encoding is no different for packed structs,
  507. // so it's not clear there's anything we can do for those.
  508. const NSUInteger currentSizeSum = runningFieldOffset % fieldAlignment;
  509. if (currentSizeSum != 0 && currentSizeSum + fieldSize > fieldAlignment) {
  510. runningFieldOffset += fieldAlignment - currentSizeSum;
  511. }
  512. typeBlock(
  513. structName,
  514. typeEncoding.UTF8String,
  515. [self readableTypeForEncoding:typeEncoding],
  516. runningFieldIndex,
  517. runningFieldOffset
  518. );
  519. runningFieldOffset += fieldSize;
  520. runningFieldIndex++;
  521. typeStart = nextTypeStart;
  522. }
  523. }
  524. }
  525. }
  526. }
  527. #pragma mark - Metadata Helpers
  528. + (NSDictionary<NSString *, NSString *> *)attributesForProperty:(objc_property_t)property {
  529. NSString *attributes = @(property_getAttributes(property) ?: "");
  530. // Thanks to MAObjcRuntime for inspiration here.
  531. NSArray<NSString *> *attributePairs = [attributes componentsSeparatedByString:@","];
  532. NSMutableDictionary<NSString *, NSString *> *attributesDictionary = [NSMutableDictionary new];
  533. for (NSString *attributePair in attributePairs) {
  534. attributesDictionary[[attributePair substringToIndex:1]] = [attributePair substringFromIndex:1];
  535. }
  536. return attributesDictionary;
  537. }
  538. + (NSString *)appendName:(NSString *)name toType:(NSString *)type {
  539. if (!type.length) {
  540. type = @"(?)";
  541. }
  542. NSString *combined = nil;
  543. if ([type characterAtIndex:type.length - 1] == FLEXTypeEncodingCString) {
  544. combined = [type stringByAppendingString:name];
  545. } else {
  546. combined = [type stringByAppendingFormat:@" %@", name];
  547. }
  548. return combined;
  549. }
  550. + (NSString *)readableTypeForEncoding:(NSString *)encodingString {
  551. if (!encodingString.length) {
  552. return @"?";
  553. }
  554. // See https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html
  555. // class-dump has a much nicer and much more complete implementation for this task, but it is distributed under GPLv2 :/
  556. // See https://github.com/nygard/class-dump/blob/master/Source/CDType.m
  557. // Warning: this method uses multiple middle returns and macros to cut down on boilerplate.
  558. // The use of macros here was inspired by https://www.mikeash.com/pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html
  559. const char *encodingCString = encodingString.UTF8String;
  560. // Some fields have a name, such as {Size=\"width\"d\"height\"d}, we need to extract the name out and recursive
  561. const NSUInteger fieldNameOffset = [FLEXRuntimeUtility fieldNameOffsetForTypeEncoding:encodingCString];
  562. if (fieldNameOffset > 0) {
  563. // According to https://github.com/nygard/class-dump/commit/33fb5ed221810685f57c192e1ce8ab6054949a7c,
  564. // there are some consecutive quoted strings, so use `_` to concatenate the names.
  565. NSString *const fieldNamesString = [encodingString substringWithRange:NSMakeRange(0, fieldNameOffset)];
  566. NSArray<NSString *> *const fieldNames = [fieldNamesString
  567. componentsSeparatedByString:[NSString stringWithFormat:@"%c", FLEXTypeEncodingQuote]
  568. ];
  569. NSMutableString *finalFieldNamesString = [NSMutableString new];
  570. for (NSString *const fieldName in fieldNames) {
  571. if (fieldName.length > 0) {
  572. if (finalFieldNamesString.length > 0) {
  573. [finalFieldNamesString appendString:@"_"];
  574. }
  575. [finalFieldNamesString appendString:fieldName];
  576. }
  577. }
  578. NSString *const recursiveType = [self readableTypeForEncoding:[encodingString substringFromIndex:fieldNameOffset]];
  579. return [NSString stringWithFormat:@"%@ %@", recursiveType, finalFieldNamesString];
  580. }
  581. // Objects
  582. if (encodingCString[0] == FLEXTypeEncodingObjcObject) {
  583. NSString *class = [encodingString substringFromIndex:1];
  584. class = [class stringByReplacingOccurrencesOfString:@"\"" withString:@""];
  585. if (class.length == 0 || (class.length == 1 && [class characterAtIndex:0] == FLEXTypeEncodingUnknown)) {
  586. class = @"id";
  587. } else {
  588. class = [class stringByAppendingString:@" *"];
  589. }
  590. return class;
  591. }
  592. // Qualifier Prefixes
  593. // Do this first since some of the direct translations (i.e. Method) contain a prefix.
  594. #define RECURSIVE_TRANSLATE(prefix, formatString) \
  595. if (encodingCString[0] == prefix) { \
  596. NSString *recursiveType = [self readableTypeForEncoding:[encodingString substringFromIndex:1]]; \
  597. return [NSString stringWithFormat:formatString, recursiveType]; \
  598. }
  599. // If there's a qualifier prefix on the encoding, translate it and then
  600. // recursively call this method with the rest of the encoding string.
  601. RECURSIVE_TRANSLATE('^', @"%@ *");
  602. RECURSIVE_TRANSLATE('r', @"const %@");
  603. RECURSIVE_TRANSLATE('n', @"in %@");
  604. RECURSIVE_TRANSLATE('N', @"inout %@");
  605. RECURSIVE_TRANSLATE('o', @"out %@");
  606. RECURSIVE_TRANSLATE('O', @"bycopy %@");
  607. RECURSIVE_TRANSLATE('R', @"byref %@");
  608. RECURSIVE_TRANSLATE('V', @"oneway %@");
  609. RECURSIVE_TRANSLATE('b', @"bitfield(%@)");
  610. #undef RECURSIVE_TRANSLATE
  611. // C Types
  612. #define TRANSLATE(ctype) \
  613. if (strcmp(encodingCString, @encode(ctype)) == 0) { \
  614. return (NSString *)CFSTR(#ctype); \
  615. }
  616. // Order matters here since some of the cocoa types are typedefed to c types.
  617. // We can't recover the exact mapping, but we choose to prefer the cocoa types.
  618. // This is not an exhaustive list, but it covers the most common types
  619. TRANSLATE(CGRect);
  620. TRANSLATE(CGPoint);
  621. TRANSLATE(CGSize);
  622. TRANSLATE(CGVector);
  623. TRANSLATE(UIEdgeInsets);
  624. if (@available(iOS 11.0, *)) {
  625. TRANSLATE(NSDirectionalEdgeInsets);
  626. }
  627. TRANSLATE(UIOffset);
  628. TRANSLATE(NSRange);
  629. TRANSLATE(CGAffineTransform);
  630. TRANSLATE(CATransform3D);
  631. TRANSLATE(CGColorRef);
  632. TRANSLATE(CGPathRef);
  633. TRANSLATE(CGContextRef);
  634. TRANSLATE(NSInteger);
  635. TRANSLATE(NSUInteger);
  636. TRANSLATE(CGFloat);
  637. TRANSLATE(BOOL);
  638. TRANSLATE(int);
  639. TRANSLATE(short);
  640. TRANSLATE(long);
  641. TRANSLATE(long long);
  642. TRANSLATE(unsigned char);
  643. TRANSLATE(unsigned int);
  644. TRANSLATE(unsigned short);
  645. TRANSLATE(unsigned long);
  646. TRANSLATE(unsigned long long);
  647. TRANSLATE(float);
  648. TRANSLATE(double);
  649. TRANSLATE(long double);
  650. TRANSLATE(char *);
  651. TRANSLATE(Class);
  652. TRANSLATE(objc_property_t);
  653. TRANSLATE(Ivar);
  654. TRANSLATE(Method);
  655. TRANSLATE(Category);
  656. TRANSLATE(NSZone *);
  657. TRANSLATE(SEL);
  658. TRANSLATE(void);
  659. #undef TRANSLATE
  660. // For structs, we only use the name of the structs
  661. if (encodingCString[0] == FLEXTypeEncodingStructBegin) {
  662. // Special case: std::string
  663. if ([encodingString hasPrefix:@"{basic_string<char"]) {
  664. return @"std::string";
  665. }
  666. const char *equals = strchr(encodingCString, '=');
  667. if (equals) {
  668. const char *nameStart = encodingCString + 1;
  669. // For anonymous structs
  670. if (nameStart[0] == FLEXTypeEncodingUnknown) {
  671. return @"anonymous struct";
  672. } else {
  673. NSString *const structName = [encodingString
  674. substringWithRange:NSMakeRange(nameStart - encodingCString, equals - nameStart)
  675. ];
  676. return structName;
  677. }
  678. }
  679. }
  680. // If we couldn't translate, just return the original encoding string
  681. return encodingString;
  682. }
  683. #pragma mark - Internal Helpers
  684. + (NSValue *)valueForPrimitivePointer:(void *)pointer objCType:(const char *)type {
  685. // Remove the field name if there is any (e.g. \"width\"d -> d)
  686. const NSUInteger fieldNameOffset = [FLEXRuntimeUtility fieldNameOffsetForTypeEncoding:type];
  687. if (fieldNameOffset > 0) {
  688. return [self valueForPrimitivePointer:pointer objCType:type + fieldNameOffset];
  689. }
  690. // CASE macro inspired by https://www.mikeash.com/pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html
  691. #define CASE(ctype, selectorpart) \
  692. if (strcmp(type, @encode(ctype)) == 0) { \
  693. return [NSNumber numberWith ## selectorpart: *(ctype *)pointer]; \
  694. }
  695. CASE(BOOL, Bool);
  696. CASE(unsigned char, UnsignedChar);
  697. CASE(short, Short);
  698. CASE(unsigned short, UnsignedShort);
  699. CASE(int, Int);
  700. CASE(unsigned int, UnsignedInt);
  701. CASE(long, Long);
  702. CASE(unsigned long, UnsignedLong);
  703. CASE(long long, LongLong);
  704. CASE(unsigned long long, UnsignedLongLong);
  705. CASE(float, Float);
  706. CASE(double, Double);
  707. CASE(long double, Double);
  708. #undef CASE
  709. NSValue *value = nil;
  710. if (FLEXGetSizeAndAlignment(type, nil, nil)) {
  711. @try {
  712. value = [NSValue valueWithBytes:pointer objCType:type];
  713. } @catch (NSException *exception) {
  714. // Certain type encodings are not supported by valueWithBytes:objCType:.
  715. // Just fail silently if an exception is thrown.
  716. }
  717. }
  718. return value;
  719. }
  720. @end