Skip to content

Commit 666392e

Browse files
committed
refactor(runtime): rooted scan reads retainCount through ObjectManager::NativeRetainCountImp
main now has the same native -retainCount lookup and the TNSReplaceableRetainCount and TNSUninitializedTimer fixtures (#579), so the rooted scan calls it and the duplicated fixtures are dropped.
1 parent 649e1b2 commit 666392e

3 files changed

Lines changed: 2 additions & 77 deletions

File tree

‎NativeScript/runtime/WrapperLifetime.mm‎

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void ReadConfig() {
6767
std::vector<ObjectWeakCallbackState*> releases;
6868
bool draining = false;
6969
bool drainPosted = false;
70-
// The native -retainCount implementation per class (NativeRetainCountImp):
70+
// ObjectManager::NativeRetainCountImp per class:
7171
// the scan runs in a GC prologue, where JS may not run.
7272
std::unordered_map<Class, IMP> retainCountImps;
7373
};
@@ -138,46 +138,12 @@ bool IsRootable(id obj) {
138138
return false;
139139
}
140140

141-
// The -retainCount the native class of `cls` answers with, never one JS
142-
// implements: JS subclasses are skipped, and a class whose -retainCount JS
143-
// replaced keeps the original under the swizzled-prefixed alias. Read from the
144-
// method lists of each class, so an alias inherited from a replaced ancestor
145-
// never hides a native override below it. Null for NSTimer.alloc()'s
146-
// placeholder, whose -retainCount does not return.
147-
IMP NativeRetainCountImp(Class cls) {
148-
static Class timerPlaceholder = object_getClass([NSTimer alloc]);
149-
if (cls == timerPlaceholder) {
150-
return nullptr;
151-
}
152-
SEL selector = @selector(retainCount);
153-
static SEL original = sel_registerName((Constants::SwizzledPrefix + "retainCount").c_str());
154-
for (Class c = ClassBuilder::FindNativeClass(cls); c != nil; c = class_getSuperclass(c)) {
155-
unsigned int count = 0;
156-
Method* methods = class_copyMethodList(c, &count);
157-
IMP own = nullptr;
158-
IMP kept = nullptr;
159-
for (unsigned int i = 0; i < count; i++) {
160-
SEL name = method_getName(methods[i]);
161-
if (name == original) {
162-
kept = method_getImplementation(methods[i]);
163-
} else if (name == selector) {
164-
own = method_getImplementation(methods[i]);
165-
}
166-
}
167-
free(methods);
168-
if (kept != nullptr || own != nullptr) {
169-
return kept != nullptr ? kept : own;
170-
}
171-
}
172-
return nullptr;
173-
}
174-
175141
// Classes without a native -retainCount read as 0.
176142
NSUInteger NativeRetainCount(RootedState* state, id obj) {
177143
Class cls = object_getClass(obj);
178144
auto found = state->retainCountImps.find(cls);
179145
if (found == state->retainCountImps.end()) {
180-
found = state->retainCountImps.emplace(cls, NativeRetainCountImp(cls)).first;
146+
found = state->retainCountImps.emplace(cls, ObjectManager::NativeRetainCountImp(cls)).first;
181147
}
182148
if (found->second == nullptr) {
183149
return 0;

‎TestFixtures/Marshalling/TNSAllocLog.h‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -116,31 +116,6 @@ id TNSOwnedPrimitivesObjectAt(TNSOwnedPrimitives* holder, NSUInteger index);
116116

117117
@end
118118

119-
// Declares -retainCount and shares its name with the class adopting it, so
120-
// assigning TNSReplaceableRetainCountProtocol.prototype.retainCount replaces
121-
// the method on that class alone, as NSObjectProtocol.prototype does on
122-
// NSObject.
123-
@protocol TNSReplaceableRetainCount
124-
125-
- (NSUInteger)retainCount;
126-
127-
@end
128-
129-
@interface TNSReplaceableRetainCount : TNSAllocLog <TNSReplaceableRetainCount>
130-
131-
// Sends -retainCount to `object`.
132-
+ (NSUInteger)retainCountOf:(id)object;
133-
134-
@end
135-
136-
@interface TNSUninitializedTimer : NSObject
137-
138-
// NSTimer.alloc()'s placeholder, which a JS alloc() call never creates: its
139-
// -retainCount does not return.
140-
+ (id)placeholder;
141-
142-
@end
143-
144119
// Implements NSKeyedUnarchiverDelegate: answers the object it is given, or a
145120
// new TNSAllocLog once `substitutes` is set.
146121
@interface TNSDecodeDelegate : NSObject <NSKeyedUnarchiverDelegate>

‎TestFixtures/Marshalling/TNSAllocLog.m‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -292,22 +292,6 @@ - (void)encodeWithCoder:(NSCoder*)coder {
292292

293293
@end
294294

295-
@implementation TNSReplaceableRetainCount
296-
297-
+ (NSUInteger)retainCountOf:(id)object {
298-
return ((NSUInteger (*)(id, SEL))objc_msgSend)(object, sel_registerName("retainCount"));
299-
}
300-
301-
@end
302-
303-
@implementation TNSUninitializedTimer
304-
305-
+ (id)placeholder {
306-
return [NSTimer alloc];
307-
}
308-
309-
@end
310-
311295
@implementation TNSDecodeDelegate
312296

313297
- (id)unarchiver:(NSKeyedUnarchiver*)unarchiver didDecodeObject:(id)object {

0 commit comments

Comments
 (0)