weak2.m 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // See instructions in weak.h
  2. #include "test.h"
  3. #include "weak.h"
  4. #include <objc/objc-internal.h>
  5. int state = 0;
  6. static void *noop_fn(void *self, SEL _cmd __unused) {
  7. return self;
  8. }
  9. static void *retain_fn(void *self, SEL _cmd __unused) {
  10. void * (*fn)(void *) = (typeof(fn))_objc_rootRetain;
  11. return fn(self);
  12. }
  13. static void release_fn(void *self, SEL _cmd __unused) {
  14. void (*fn)(void *) = (typeof(fn))_objc_rootRelease;
  15. fn(self);
  16. }
  17. static void *autorelease_fn(void *self, SEL _cmd __unused) {
  18. void * (*fn)(void *) = (typeof(fn))_objc_rootAutorelease;
  19. return fn(self);
  20. }
  21. #if !defined(EMPTY)
  22. @implementation MissingRoot
  23. +(void) initialize { }
  24. +(Class) class { return self; }
  25. +(id) alloc { return _objc_rootAlloc(self); }
  26. +(id) allocWithZone:(void*)zone { return _objc_rootAllocWithZone(self, (malloc_zone_t *)zone); }
  27. -(id) init { return self; }
  28. -(void) dealloc { _objc_rootDealloc(self); }
  29. +(int) method { return 10; }
  30. +(void) load {
  31. class_addMethod(self, sel_registerName("retain"), (IMP)retain_fn, "");
  32. class_addMethod(self, sel_registerName("release"), (IMP)release_fn, "");
  33. class_addMethod(self, sel_registerName("autorelease"), (IMP)autorelease_fn, "");
  34. class_addMethod(object_getClass(self), sel_registerName("retain"), (IMP)noop_fn, "");
  35. class_addMethod(object_getClass(self), sel_registerName("release"), (IMP)noop_fn, "");
  36. class_addMethod(object_getClass(self), sel_registerName("autorelease"), (IMP)noop_fn, "");
  37. state++;
  38. }
  39. @end
  40. @implementation MissingSuper
  41. +(int) method { return 1+[super method]; }
  42. -(id) init { self = [super init]; ivar = 100; return self; }
  43. +(void) load { state++; }
  44. @end
  45. #endif
  46. @implementation NotMissingRoot
  47. +(void) initialize { }
  48. +(Class) class { return self; }
  49. +(id) alloc { return _objc_rootAlloc(self); }
  50. +(id) allocWithZone:(void*)zone { return _objc_rootAllocWithZone(self, (malloc_zone_t *)zone); }
  51. -(id) init { return self; }
  52. -(void) dealloc { _objc_rootDealloc(self); }
  53. +(int) method { return 20; }
  54. +(void) load {
  55. class_addMethod(self, sel_registerName("retain"), (IMP)retain_fn, "");
  56. class_addMethod(self, sel_registerName("release"), (IMP)release_fn, "");
  57. class_addMethod(self, sel_registerName("autorelease"), (IMP)autorelease_fn, "");
  58. class_addMethod(object_getClass(self), sel_registerName("retain"), (IMP)noop_fn, "");
  59. class_addMethod(object_getClass(self), sel_registerName("release"), (IMP)noop_fn, "");
  60. class_addMethod(object_getClass(self), sel_registerName("autorelease"), (IMP)noop_fn, "");
  61. state++;
  62. }
  63. @end
  64. @implementation NotMissingSuper
  65. +(int) method { return 1+[super method]; }
  66. -(id) init { self = [super init]; ivar = 200; return self; }
  67. +(void) load { state++; }
  68. @end