diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm index 1a17730e4ad..bd4c1e95e19 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm @@ -99,11 +99,7 @@ static NSSet *returnKeyTypesSet; NSMutableDictionary *defaultAttributes = [_backedTextInputView.defaultTextAttributes mutableCopy]; -#if !TARGET_OS_MACCATALYST - RCTWeakEventEmitterWrapper *eventEmitterWrapper = [RCTWeakEventEmitterWrapper new]; - eventEmitterWrapper.eventEmitter = _eventEmitter; - defaultAttributes[RCTAttributedStringEventEmitterKey] = eventEmitterWrapper; -#endif + defaultAttributes[RCTAttributedStringEventEmitterKey] = RCTWrapEventEmitter(_eventEmitter); _backedTextInputView.defaultTextAttributes = defaultAttributes; } @@ -263,10 +259,8 @@ static NSSet *returnKeyTypesSet; if (newTextInputProps.textAttributes != oldTextInputProps.textAttributes) { NSMutableDictionary *defaultAttributes = RCTNSTextAttributesFromTextAttributes(newTextInputProps.getEffectiveTextAttributes(RCTFontSizeMultiplier())); -#if !TARGET_OS_MACCATALYST defaultAttributes[RCTAttributedStringEventEmitterKey] = _backedTextInputView.defaultTextAttributes[RCTAttributedStringEventEmitterKey]; -#endif _backedTextInputView.defaultTextAttributes = defaultAttributes; } diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.h b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.h index 3427663d53d..908cfc0b612 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.h +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.h @@ -52,8 +52,29 @@ BOOL RCTIsAttributedStringEffectivelySame( NSDictionary *insensitiveAttributes, const facebook::react::TextAttributes &baseTextAttributes); -@interface RCTWeakEventEmitterWrapper : NSObject -@property (nonatomic, assign) facebook::react::SharedEventEmitter eventEmitter; -@end +static inline NSData *RCTWrapEventEmitter(const facebook::react::SharedEventEmitter &eventEmitter) +{ + auto eventEmitterPtr = new std::weak_ptr(eventEmitter); + return [[NSData alloc] initWithBytesNoCopy:eventEmitterPtr + length:sizeof(eventEmitterPtr) + deallocator:^(void *ptrToDelete, NSUInteger) { + delete (std::weak_ptr *)ptrToDelete; + }]; +} + +static inline facebook::react::SharedEventEmitter RCTUnwrapEventEmitter(NSData *data) +{ + if (data.length == 0) { + return nullptr; + } + + auto weakPtr = dynamic_cast *>( + (std::weak_ptr *)data.bytes); + if (weakPtr) { + return weakPtr->lock(); + } + + return nullptr; +} NS_ASSUME_NONNULL_END diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm index f2dde69b83f..0d606352ec8 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm @@ -16,45 +16,6 @@ using namespace facebook::react; -@implementation RCTWeakEventEmitterWrapper { - std::weak_ptr _weakEventEmitter; -} - -- (void)setEventEmitter:(SharedEventEmitter)eventEmitter -{ - _weakEventEmitter = eventEmitter; -} - -- (SharedEventEmitter)eventEmitter -{ - return _weakEventEmitter.lock(); -} - -- (void)dealloc -{ - _weakEventEmitter.reset(); -} - -- (BOOL)isEqual:(id)object -{ - // We consider the underlying EventEmitter as the identity - if (![object isKindOfClass:[self class]]) { - return NO; - } - - auto thisEventEmitter = [self eventEmitter]; - auto otherEventEmitter = [((RCTWeakEventEmitterWrapper *)object) eventEmitter]; - return thisEventEmitter == otherEventEmitter; -} - -- (NSUInteger)hash -{ - // We consider the underlying EventEmitter as the identity - return (NSUInteger)_weakEventEmitter.lock().get(); -} - -@end - inline static UIFontWeight RCTUIFontWeightFromInteger(NSInteger fontWeight) { assert(fontWeight > 50); @@ -405,10 +366,8 @@ static NSMutableAttributedString *RCTNSAttributedStringFragmentWithAttributesFro { auto nsAttributedStringFragment = RCTNSAttributedStringFragmentFromFragment(fragment, placeholderImage); -#if !TARGET_OS_MACCATALYST if (fragment.parentShadowView.componentHandle) { - RCTWeakEventEmitterWrapper *eventEmitterWrapper = [RCTWeakEventEmitterWrapper new]; - eventEmitterWrapper.eventEmitter = fragment.parentShadowView.eventEmitter; + auto eventEmitterWrapper = RCTWrapEventEmitter(fragment.parentShadowView.eventEmitter); NSDictionary *additionalTextAttributes = @{RCTAttributedStringEventEmitterKey : eventEmitterWrapper}; @@ -416,7 +375,6 @@ static NSMutableAttributedString *RCTNSAttributedStringFragmentWithAttributesFro [nsAttributedStringFragment addAttributes:additionalTextAttributes range:NSMakeRange(0, nsAttributedStringFragment.length)]; } -#endif return nsAttributedStringFragment; } diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm index 8b00c5f8e2b..78291bf4a4a 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm @@ -280,11 +280,10 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(EllipsizeMode ellipsi // after (fraction == 1.0) the last character, then the attribute is valid. if (textStorage.length > 0 && (fraction > 0 || characterIndex > 0) && (fraction < 1 || characterIndex < textStorage.length - 1)) { - RCTWeakEventEmitterWrapper *eventEmitterWrapper = - (RCTWeakEventEmitterWrapper *)[textStorage attribute:RCTAttributedStringEventEmitterKey - atIndex:characterIndex - effectiveRange:NULL]; - return eventEmitterWrapper.eventEmitter; + NSData *eventEmitterWrapper = (NSData *)[textStorage attribute:RCTAttributedStringEventEmitterKey + atIndex:characterIndex + effectiveRange:NULL]; + return RCTUnwrapEventEmitter(eventEmitterWrapper); } return nil;