FLEXIvar.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. @interface FLEXIvar () {
  13. NSString *_flex_description;
  14. }
  15. @end
  16. @implementation FLEXIvar
  17. #pragma mark Initializers
  18. - (id)init {
  19. [NSException
  20. raise:NSInternalInconsistencyException
  21. format:@"Class instance should not be created with -init"
  22. ];
  23. return nil;
  24. }
  25. + (instancetype)ivar:(Ivar)ivar {
  26. return [[self alloc] initWithIvar:ivar];
  27. }
  28. + (instancetype)named:(NSString *)name onClass:(Class)cls {
  29. Ivar ivar = class_getInstanceVariable(cls, name.UTF8String);
  30. return [self ivar:ivar];
  31. }
  32. - (id)initWithIvar:(Ivar)ivar {
  33. NSParameterAssert(ivar);
  34. self = [super init];
  35. if (self) {
  36. _objc_ivar = ivar;
  37. [self examine];
  38. }
  39. return self;
  40. }
  41. #pragma mark Other
  42. - (NSString *)description {
  43. if (!_flex_description) {
  44. NSString *readableType = [FLEXRuntimeUtility readableTypeForEncoding:self.typeEncoding];
  45. _flex_description = [FLEXRuntimeUtility appendName:self.name toType:readableType];
  46. }
  47. return _flex_description;
  48. }
  49. - (NSString *)debugDescription {
  50. return [NSString stringWithFormat:@"<%@ name=%@, encoding=%@, offset=%ld>",
  51. NSStringFromClass(self.class), self.name, self.typeEncoding, (long)self.offset];
  52. }
  53. - (void)examine {
  54. _name = @(ivar_getName(self.objc_ivar));
  55. _typeEncoding = @(ivar_getTypeEncoding(self.objc_ivar));
  56. _type = (FLEXTypeEncoding)[_typeEncoding characterAtIndex:0];
  57. _offset = ivar_getOffset(self.objc_ivar);
  58. NSUInteger size = 0;
  59. @try {
  60. NSGetSizeAndAlignment(_typeEncoding.UTF8String, &size, nil);
  61. } @catch (NSException *exception) { }
  62. _details = [NSString stringWithFormat:
  63. @"%@ bytes, offset %@ — %@",
  64. (size ? @(size) : @"?"), @(_offset), _typeEncoding
  65. ];
  66. }
  67. - (id)getValue:(id)target {
  68. id value = nil;
  69. if (!FLEXIvarIsSafe(_objc_ivar)) {
  70. return nil;
  71. }
  72. #ifdef __arm64__
  73. // See http://www.sealiesoftware.com/blog/archive/2013/09/24/objc_explain_Non-pointer_isa.html
  74. if (self.type == FLEXTypeEncodingObjcClass && [self.name isEqualToString:@"isa"]) {
  75. value = object_getClass(target);
  76. } else
  77. #endif
  78. if (self.type == FLEXTypeEncodingObjcObject || self.type == FLEXTypeEncodingObjcClass) {
  79. value = object_getIvar(target, self.objc_ivar);
  80. } else {
  81. void *pointer = (__bridge void *)target + self.offset;
  82. value = [FLEXRuntimeUtility
  83. valueForPrimitivePointer:pointer
  84. objCType:self.typeEncoding.UTF8String
  85. ];
  86. }
  87. return value;
  88. }
  89. - (void)setValue:(id)value onObject:(id)target {
  90. const char *typeEncodingCString = self.typeEncoding.UTF8String;
  91. if (self.type == FLEXTypeEncodingObjcObject) {
  92. object_setIvar(target, self.objc_ivar, value);
  93. } else if ([value isKindOfClass:[NSValue class]]) {
  94. // Primitive - unbox the NSValue.
  95. NSValue *valueValue = (NSValue *)value;
  96. // Make sure that the box contained the correct type.
  97. NSAssert(
  98. strcmp(valueValue.objCType, typeEncodingCString) == 0,
  99. @"Type encoding mismatch (value: %s; ivar: %s) in setting ivar named: %@ on object: %@",
  100. valueValue.objCType, typeEncodingCString, self.name, target
  101. );
  102. NSUInteger bufferSize = 0;
  103. @try {
  104. // NSGetSizeAndAlignment barfs on type encoding for bitfields.
  105. NSGetSizeAndAlignment(typeEncodingCString, &bufferSize, NULL);
  106. } @catch (NSException *exception) { }
  107. if (bufferSize > 0) {
  108. void *buffer = calloc(bufferSize, 1);
  109. [valueValue getValue:buffer];
  110. void *pointer = (__bridge void *)target + self.offset;
  111. memcpy(pointer, buffer, bufferSize);
  112. free(buffer);
  113. }
  114. }
  115. }
  116. - (id)getPotentiallyUnboxedValue:(id)target {
  117. return [FLEXRuntimeUtility
  118. potentiallyUnwrapBoxedPointer:[self getValue:target]
  119. type:self.typeEncoding.UTF8String
  120. ];
  121. }
  122. @end