copyFixupHandler.mm 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // TEST_CONFIG MEM=mrc
  2. #define TEST_CALLS_OPERATOR_NEW
  3. #include "test.h"
  4. #include "testroot.i"
  5. #include "swift-class-def.m"
  6. #include <objc/objc-internal.h>
  7. #include <vector>
  8. static Class expectedOldClass;
  9. static std::vector<Class> observedNewClasses1;
  10. static void handler1(Class _Nonnull oldClass, Class _Nonnull newClass) {
  11. testprintf("%s(%p, %p)", __func__, oldClass, newClass);
  12. testassert(oldClass == expectedOldClass);
  13. observedNewClasses1.push_back(newClass);
  14. }
  15. static std::vector<Class> observedNewClasses2;
  16. static void handler2(Class _Nonnull oldClass, Class _Nonnull newClass) {
  17. testprintf("%s(%p, %p)", __func__, oldClass, newClass);
  18. testassert(oldClass == expectedOldClass);
  19. observedNewClasses2.push_back(newClass);
  20. }
  21. static std::vector<Class> observedNewClasses3;
  22. static void handler3(Class _Nonnull oldClass, Class _Nonnull newClass) {
  23. testprintf("%s(%p, %p)", __func__, oldClass, newClass);
  24. testassert(oldClass == expectedOldClass);
  25. observedNewClasses3.push_back(newClass);
  26. }
  27. EXTERN_C Class _objc_realizeClassFromSwift(Class, void *);
  28. EXTERN_C Class init(Class cls, void *arg) {
  29. (void)arg;
  30. _objc_realizeClassFromSwift(cls, cls);
  31. return cls;
  32. }
  33. @interface SwiftRoot: TestRoot @end
  34. SWIFT_CLASS(SwiftRoot, TestRoot, init);
  35. int main()
  36. {
  37. expectedOldClass = [SwiftRoot class];
  38. Class A = objc_allocateClassPair([RawSwiftRoot class], "A", 0);
  39. objc_registerClassPair(A);
  40. testassert(observedNewClasses1.size() == 0);
  41. testassert(observedNewClasses2.size() == 0);
  42. testassert(observedNewClasses3.size() == 0);
  43. _objc_setClassCopyFixupHandler(handler1);
  44. expectedOldClass = A;
  45. Class B = objc_allocateClassPair(A, "B", 0);
  46. objc_registerClassPair(B);
  47. testassert(observedNewClasses1.size() == 2);
  48. testassert(observedNewClasses2.size() == 0);
  49. testassert(observedNewClasses3.size() == 0);
  50. testassert(observedNewClasses1[0] == B);
  51. _objc_setClassCopyFixupHandler(handler2);
  52. expectedOldClass = B;
  53. Class C = objc_allocateClassPair(B, "C", 0);
  54. objc_registerClassPair(C);
  55. testassert(observedNewClasses1.size() == 4);
  56. testassert(observedNewClasses2.size() == 2);
  57. testassert(observedNewClasses3.size() == 0);
  58. testassert(observedNewClasses1[2] == C);
  59. testassert(observedNewClasses2[0] == C);
  60. _objc_setClassCopyFixupHandler(handler3);
  61. expectedOldClass = C;
  62. Class D = objc_allocateClassPair(C, "D", 0);
  63. objc_registerClassPair(D);
  64. testassert(observedNewClasses1.size() == 6);
  65. testassert(observedNewClasses2.size() == 4);
  66. testassert(observedNewClasses3.size() == 2);
  67. testassert(observedNewClasses1[4] == D);
  68. testassert(observedNewClasses2[2] == D);
  69. testassert(observedNewClasses3[0] == D);
  70. succeed(__FILE__);
  71. }