FLEXIvar.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. @try {
  59. NSGetSizeAndAlignment(_typeEncoding.UTF8String, &_size, nil);
  60. } @catch (NSException *exception) { }
  61. _details = [NSString stringWithFormat:
  62. @"%@ bytes, offset %@ — %@",
  63. (_size ? @(_size) : @"?"), @(_offset), _typeEncoding
  64. ];
  65. }
  66. - (id)getValue:(id)target {
  67. id value = nil;
  68. if (!FLEXIvarIsSafe(_objc_ivar)) {
  69. return nil;
  70. }
  71. #ifdef __arm64__
  72. // See http://www.sealiesoftware.com/blog/archive/2013/09/24/objc_explain_Non-pointer_isa.html
  73. if (self.type == FLEXTypeEncodingObjcClass && [self.name isEqualToString:@"isa"]) {
  74. value = object_getClass(target);
  75. } else
  76. #endif
  77. if (self.type == FLEXTypeEncodingObjcObject || self.type == FLEXTypeEncodingObjcClass) {
  78. value = object_getIvar(target, self.objc_ivar);
  79. } else {
  80. void *pointer = (__bridge void *)target + self.offset;
  81. value = [FLEXRuntimeUtility
  82. valueForPrimitivePointer:pointer
  83. objCType:self.typeEncoding.UTF8String
  84. ];
  85. }
  86. return value;
  87. }
  88. - (void)setValue:(id)value onObject:(id)target {
  89. const char *typeEncodingCString = self.typeEncoding.UTF8String;
  90. if (self.type == FLEXTypeEncodingObjcObject) {
  91. object_setIvar(target, self.objc_ivar, value);
  92. } else if ([value isKindOfClass:[NSValue class]]) {
  93. // Primitive - unbox the NSValue.
  94. NSValue *valueValue = (NSValue *)value;
  95. // Make sure that the box contained the correct type.
  96. NSAssert(
  97. strcmp(valueValue.objCType, typeEncodingCString) == 0,
  98. @"Type encoding mismatch (value: %s; ivar: %s) in setting ivar named: %@ on object: %@",
  99. valueValue.objCType, typeEncodingCString, self.name, target
  100. );
  101. NSUInteger bufferSize = 0;
  102. @try {
  103. // NSGetSizeAndAlignment barfs on type encoding for bitfields.
  104. NSGetSizeAndAlignment(typeEncodingCString, &bufferSize, NULL);
  105. } @catch (NSException *exception) { }
  106. if (bufferSize > 0) {
  107. void *buffer = calloc(bufferSize, 1);
  108. [valueValue getValue:buffer];
  109. void *pointer = (__bridge void *)target + self.offset;
  110. memcpy(pointer, buffer, bufferSize);
  111. free(buffer);
  112. }
  113. }
  114. }
  115. - (id)getPotentiallyUnboxedValue:(id)target {
  116. return [FLEXRuntimeUtility
  117. potentiallyUnwrapBoxedPointer:[self getValue:target]
  118. type:self.typeEncoding.UTF8String
  119. ];
  120. }
  121. @end