FLEXBlockDescription.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // FLEXBlockDescription.m
  3. // FLEX
  4. //
  5. // Created by Oliver Letterer on 2012-09-01
  6. // Forked from CTObjectiveCRuntimeAdditions (MIT License)
  7. // https://github.com/ebf/CTObjectiveCRuntimeAdditions
  8. //
  9. // Copyright (c) 2012 EBF-EDV Beratung Föllmer GmbH
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  11. // software and associated documentation files (the "Software"), to deal in the Software
  12. // without restriction, including without limitation the rights to use, copy, modify, merge,
  13. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  14. // to whom the Software is furnished to do so, subject to the following conditions:
  15. // The above copyright notice and this permission notice shall be included in all copies or
  16. // substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  19. // BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  21. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. #import "FLEXBlockDescription.h"
  24. #import "FLEXRuntimeUtility.h"
  25. struct block_object {
  26. void *isa;
  27. int flags;
  28. int reserved;
  29. void (*invoke)(void *, ...);
  30. struct block_descriptor {
  31. unsigned long int reserved; // NULL
  32. unsigned long int size; // sizeof(struct Block_literal_1)
  33. // optional helper functions
  34. void (*copy_helper)(void *dst, void *src); // IFF (1<<25)
  35. void (*dispose_helper)(void *src); // IFF (1<<25)
  36. // required ABI.2010.3.16
  37. const char *signature; // IFF (1<<30)
  38. } *descriptor;
  39. // imported variables
  40. };
  41. @implementation FLEXBlockDescription
  42. + (instancetype)describing:(id)block {
  43. return [[self alloc] initWithObjcBlock:block];
  44. }
  45. - (id)initWithObjcBlock:(id)block {
  46. self = [super init];
  47. if (self) {
  48. _block = block;
  49. struct block_object *blockRef = (__bridge struct block_object *)block;
  50. _flags = blockRef->flags;
  51. _size = blockRef->descriptor->size;
  52. if (_flags & FLEXBlockOptionHasSignature) {
  53. void *signatureLocation = blockRef->descriptor;
  54. signatureLocation += sizeof(unsigned long int);
  55. signatureLocation += sizeof(unsigned long int);
  56. if (_flags & FLEXBlockOptionHasCopyDispose) {
  57. signatureLocation += sizeof(void(*)(void *dst, void *src));
  58. signatureLocation += sizeof(void (*)(void *src));
  59. }
  60. const char *signature = (*(const char **)signatureLocation);
  61. _signatureString = @(signature);
  62. @try {
  63. _signature = [NSMethodSignature signatureWithObjCTypes:signature];
  64. } @catch (NSException *exception) { }
  65. }
  66. NSMutableString *summary = [NSMutableString stringWithFormat:
  67. @"Type signature: %@\nSize: %@\nIs global: %@\nHas constructor: %@\nIs stret: %@",
  68. self.signatureString ?: @"nil", @(self.size),
  69. @((BOOL)(_flags & FLEXBlockOptionIsGlobal)),
  70. @((BOOL)(_flags & FLEXBlockOptionHasCtor)),
  71. @((BOOL)(_flags & FLEXBlockOptionHasStret))
  72. ];
  73. if (!self.signature) {
  74. [summary appendFormat:@"\nNumber of arguments: %@", @(self.signature.numberOfArguments)];
  75. }
  76. _summary = summary.copy;
  77. _sourceDeclaration = [self buildLikelyDeclaration];
  78. }
  79. return self;
  80. }
  81. - (BOOL)isCompatibleForBlockSwizzlingWithMethodSignature:(NSMethodSignature *)methodSignature {
  82. if (!self.signature) {
  83. return NO;
  84. }
  85. if (self.signature.numberOfArguments != methodSignature.numberOfArguments + 1) {
  86. return NO;
  87. }
  88. if (strcmp(self.signature.methodReturnType, methodSignature.methodReturnType) != 0) {
  89. return NO;
  90. }
  91. for (int i = 0; i < methodSignature.numberOfArguments; i++) {
  92. if (i == 1) {
  93. // SEL in method, IMP in block
  94. if (strcmp([methodSignature getArgumentTypeAtIndex:i], ":") != 0) {
  95. return NO;
  96. }
  97. if (strcmp([self.signature getArgumentTypeAtIndex:i + 1], "^?") != 0) {
  98. return NO;
  99. }
  100. } else {
  101. if (strcmp([self.signature getArgumentTypeAtIndex:i], [self.signature getArgumentTypeAtIndex:i + 1]) != 0) {
  102. return NO;
  103. }
  104. }
  105. }
  106. return YES;
  107. }
  108. - (NSString *)buildLikelyDeclaration {
  109. NSMethodSignature *signature = self.signature;
  110. NSUInteger numberOfArguments = signature.numberOfArguments;
  111. const char *returnType = signature.methodReturnType;
  112. // Return type
  113. NSMutableString *decl = [NSMutableString stringWithString:@"^"];
  114. if (returnType[0] != FLEXTypeEncodingVoid) {
  115. [decl appendString:[FLEXRuntimeUtility readableTypeForEncoding:@(returnType)]];
  116. [decl appendString:@" "];
  117. }
  118. // Arguments
  119. if (numberOfArguments) {
  120. [decl appendString:@"("];
  121. for (NSUInteger i = 1; i < numberOfArguments; i++) {
  122. const char *argType = [self.signature getArgumentTypeAtIndex:i] ?: "?";
  123. NSString *readableArgType = [FLEXRuntimeUtility readableTypeForEncoding:@(argType)];
  124. [decl appendFormat:@"%@ arg%@, ", readableArgType, @(i)];
  125. }
  126. [decl deleteCharactersInRange:NSMakeRange(decl.length-2, 2)];
  127. [decl appendString:@")"];
  128. }
  129. return decl.copy;
  130. }
  131. @end