Skip to content

Commit

Permalink
[ASTextNode2] Simplify allocWithZone: + initialize implementation #tr…
Browse files Browse the repository at this point in the history
…ivial (#1059)

* Simplify ASTextNode2 alloc + initialize implementation

* Kick the CI by marking two methods as NO_ESCAPE for Xcode 10
  • Loading branch information
Adlai-Holler committed Aug 2, 2018
1 parent 9d8406e commit 78be342
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
41 changes: 17 additions & 24 deletions Source/ASTextNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1346,36 +1346,29 @@ + (void)_registerAttributedText:(NSAttributedString *)str
}
#endif

+ (id)allocWithZone:(struct _NSZone *)zone
// All direct descendants of ASTextNode get their superclass replaced by ASTextNode2.
+ (void)initialize
{
// If they're not experimenting, just forward.
if (!ASActivateExperimentalFeature(ASExperimentalTextNode)) {
return [super allocWithZone:zone];
}

// We are plain ASTextNode. Just swap in an ASTextNode2 instead.
if (self == [ASTextNode class]) {
return (ASTextNode *)[ASTextNode2 allocWithZone:zone];
}

// We are descended from ASTextNode. We need to change the superclass for the
// ASTextNode subclass to ASTextNode2.
// Walk up the class hierarchy until we find ASTextNode.
// Note: This may be called on multiple threads simultaneously.
Class s;
for (Class c = self; c != Nil && c != [ASTextNode class]; c = s) {
s = class_getSuperclass(c);
if (s == [ASTextNode class]) {
// Texture requires that node subclasses call [super initialize]
[super initialize];

if (class_getSuperclass(self) == [ASTextNode class]
&& ASActivateExperimentalFeature(ASExperimentalTextNode)) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// Direct descendent. Update superclass of c and end.
class_setSuperclass(c, [ASTextNode2 class]);
class_setSuperclass(self, [ASTextNode2 class]);
#pragma clang diagnostic pop
break;
}
}
}

return [super allocWithZone:zone];
// For direct allocations of ASTextNode itself, we override allocWithZone:
+ (id)allocWithZone:(struct _NSZone *)zone
{
if (ASActivateExperimentalFeature(ASExperimentalTextNode)) {
return (ASTextNode *)[ASTextNode2 allocWithZone:zone];
} else {
return [super allocWithZone:zone];
}
}

@end
Expand Down
4 changes: 2 additions & 2 deletions Tests/ASDisplayLayerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ + (BOOL)respondsToSelector:(SEL)selector
}

// DANGER: Don't use the delegate as the parameters in real code; this is not thread-safe and just for accounting in unit tests!
+ (UIImage *)displayWithParameters:(_ASDisplayLayerTestDelegate *)delegate isCancelled:(asdisplaynode_iscancelled_block_t)sentinelBlock
+ (UIImage *)displayWithParameters:(_ASDisplayLayerTestDelegate *)delegate isCancelled:(NS_NOESCAPE asdisplaynode_iscancelled_block_t)sentinelBlock
{
UIImage *contents = bogusImage();
if (delegate->_displayLayerBlock != NULL) {
Expand All @@ -228,7 +228,7 @@ + (UIImage *)displayWithParameters:(_ASDisplayLayerTestDelegate *)delegate isCan
}

// DANGER: Don't use the delegate as the parameters in real code; this is not thread-safe and just for accounting in unit tests!
+ (void)drawRect:(CGRect)bounds withParameters:(_ASDisplayLayerTestDelegate *)delegate isCancelled:(asdisplaynode_iscancelled_block_t)sentinelBlock isRasterizing:(BOOL)isRasterizing
+ (void)drawRect:(CGRect)bounds withParameters:(_ASDisplayLayerTestDelegate *)delegate isCancelled:(NS_NOESCAPE asdisplaynode_iscancelled_block_t)sentinelBlock isRasterizing:(BOOL)isRasterizing
{
__atomic_add_fetch(&delegate->_drawRectCount, 1, __ATOMIC_SEQ_CST);
}
Expand Down

0 comments on commit 78be342

Please sign in to comment.