FLEXIvar.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // FLEXIvar.m
  3. // FLEX
  4. //
  5. // Derived from MirrorKit.
  6. // Created by Tanner on 6/30/15.
  7. // Copyright (c) 2015 Tanner Bennett. All rights reserved.
  8. //
  9. #import "FLEXIvar.h"
  10. #import "FLEXRuntimeUtility.h"
  11. #import "FLEXRuntimeSafety.h"
  12. #import "FLEXTypeEncodingParser.h"
  13. #import "NSString+FLEX.h"
  14. #include "FLEXObjcInternal.h"
  15. #include <dlfcn.h>
  16. @interface FLEXIvar () {
  17. NSString *_flex_description;
  18. }
  19. @end
  20. @implementation FLEXIvar
  21. #pragma mark Initializers
  22. - (id)init {
  23. [NSException
  24. raise:NSInternalInconsistencyException
  25. format:@"Class instance should not be created with -init"
  26. ];
  27. return nil;
  28. }
  29. + (instancetype)ivar:(Ivar)ivar {
  30. return [[self alloc] initWithIvar:ivar];
  31. }
  32. + (instancetype)named:(NSString *)name onClass:(Class)cls {
  33. Ivar ivar = class_getInstanceVariable(cls, name.UTF8String);
  34. return [self ivar:ivar];
  35. }
  36. - (id)initWithIvar:(Ivar)ivar {
  37. NSParameterAssert(ivar);
  38. self = [super init];
  39. if (self) {
  40. _objc_ivar = ivar;
  41. [self examine];
  42. }
  43. return self;
  44. }
  45. #pragma mark Other
  46. - (NSString *)description {
  47. if (!_flex_description) {
  48. NSString *readableType = [FLEXRuntimeUtility readableTypeForEncoding:self.typeEncoding];
  49. _flex_description = [FLEXRuntimeUtility appendName:self.name toType:readableType];
  50. }
  51. return _flex_description;
  52. }
  53. - (NSString *)debugDescription {
  54. return [NSString stringWithFormat:@"<%@ name=%@, encoding=%@, offset=%ld>",
  55. NSStringFromClass(self.class), self.name, self.typeEncoding, (long)self.offset];
  56. }
  57. - (void)examine {
  58. _name = @(ivar_getName(self.objc_ivar) ?: "(nil)");
  59. _offset = ivar_getOffset(self.objc_ivar);
  60. _typeEncoding = @(ivar_getTypeEncoding(self.objc_ivar) ?: "");
  61. NSString *typeForDetails = _typeEncoding;
  62. NSString *sizeForDetails = nil;
  63. if (_typeEncoding.length) {
  64. _type = (FLEXTypeEncoding)[_typeEncoding characterAtIndex:0];
  65. FLEXGetSizeAndAlignment(_typeEncoding.UTF8String, &_size, nil);
  66. sizeForDetails = [@(_size).stringValue stringByAppendingString:@" bytes"];
  67. } else {
  68. _type = FLEXTypeEncodingNull;
  69. typeForDetails = @"no type info";
  70. sizeForDetails = @"unknown size";
  71. }
  72. Dl_info exeInfo;
  73. if (dladdr(_objc_ivar, &exeInfo)) {
  74. _imagePath = exeInfo.dli_fname ? @(exeInfo.dli_fname) : nil;
  75. }
  76. _details = [NSString stringWithFormat:
  77. @"%@, offset %@ — %@",
  78. sizeForDetails, @(_offset), typeForDetails
  79. ];
  80. }
  81. - (id)getValue:(id)target {
  82. id value = nil;
  83. if (!FLEXIvarIsSafe(_objc_ivar) ||
  84. _type == FLEXTypeEncodingNull ||
  85. FLEXIsTaggedPointer(target)) {
  86. return nil;
  87. }
  88. #ifdef __arm64__
  89. // See http://www.sealiesoftware.com/blog/archive/2013/09/24/objc_explain_Non-pointer_isa.html
  90. if (self.type == FLEXTypeEncodingObjcClass && [self.name isEqualToString:@"isa"]) {
  91. value = object_getClass(target);
  92. } else
  93. #endif
  94. if (self.type == FLEXTypeEncodingObjcObject || self.type == FLEXTypeEncodingObjcClass) {
  95. value = object_getIvar(target, self.objc_ivar);
  96. } else {
  97. void *pointer = (__bridge void *)target + self.offset;
  98. value = [FLEXRuntimeUtility
  99. valueForPrimitivePointer:pointer
  100. objCType:self.typeEncoding.UTF8String
  101. ];
  102. }
  103. return value;
  104. }
  105. - (void)setValue:(id)value onObject:(id)target {
  106. const char *typeEncodingCString = self.typeEncoding.UTF8String;
  107. if (self.type == FLEXTypeEncodingObjcObject) {
  108. object_setIvar(target, self.objc_ivar, value);
  109. } else if ([value isKindOfClass:[NSValue class]]) {
  110. // Primitive - unbox the NSValue.
  111. NSValue *valueValue = (NSValue *)value;
  112. // Make sure that the box contained the correct type.
  113. NSAssert(
  114. strcmp(valueValue.objCType, typeEncodingCString) == 0,
  115. @"Type encoding mismatch (value: %s; ivar: %s) in setting ivar named: %@ on object: %@",
  116. valueValue.objCType, typeEncodingCString, self.name, target
  117. );
  118. NSUInteger bufferSize = 0;
  119. if (FLEXGetSizeAndAlignment(typeEncodingCString, &bufferSize, NULL)) {
  120. void *buffer = calloc(bufferSize, 1);
  121. [valueValue getValue:buffer];
  122. void *pointer = (__bridge void *)target + self.offset;
  123. memcpy(pointer, buffer, bufferSize);
  124. free(buffer);
  125. }
  126. }
  127. }
  128. - (id)getPotentiallyUnboxedValue:(id)target {
  129. NSString *type = self.typeEncoding;
  130. if (type.flex_typeIsNonObjcPointer && type.flex_pointeeType != FLEXTypeEncodingVoid) {
  131. return [self getValue:target];
  132. }
  133. return [FLEXRuntimeUtility
  134. potentiallyUnwrapBoxedPointer:[self getValue:target]
  135. type:type.UTF8String
  136. ];
  137. }
  138. @end