Files
react-native/ReactCommon/react/renderer/components/text/BaseTextShadowNode.cpp
T
Andrew Coates 050922a17e Remove usages of RTTI in places used by react-native-windows (#31694)
Summary:
Adding runtime type information adds greatly to the binary size, so react-native-windows builds without it.  But some parts of the fabric code currently uses dynamic_cast, which means to use fabric we have to build with RTTI turned on.  This PR removes the usages of dynamic_cast that are hit in release builds, which should allow react-native-windows to turn off RTTI in release builds.

Required for:  https://github.com/microsoft/react-native-windows/issues/7981

One thing to note, the comment in ShadowNodeTraits indicates that core was reserving the first 16 bits.  I'm adding two more.  Is that ok?  Should core be reserving more for future use?

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[Internal] [Fixed] - Remove uses of dynamic_cast in release builds

Pull Request resolved: https://github.com/facebook/react-native/pull/31694

Test Plan:
Verified that I can build react-native-windows with Fabric in release, without RTTI.
Boot / clicked around in RNW RNTester

Reviewed By: sammy-SC

Differential Revision: D29040383

Pulled By: JoshuaGross

fbshipit-source-id: e49286e59c4ba54faf0b4de5e244dfa1f7c3f193
2021-06-11 11:52:30 -07:00

78 lines
2.7 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "BaseTextShadowNode.h"
#include <react/renderer/components/text/RawTextProps.h>
#include <react/renderer/components/text/RawTextShadowNode.h>
#include <react/renderer/components/text/TextProps.h>
#include <react/renderer/components/text/TextShadowNode.h>
#include <react/renderer/mounting/ShadowView.h>
namespace facebook {
namespace react {
inline ShadowView shadowViewFromShadowNode(ShadowNode const &shadowNode) {
auto shadowView = ShadowView{shadowNode};
// Clearing `props` and `state` (which we don't use) allows avoiding retain
// cycles.
shadowView.props = nullptr;
shadowView.state = nullptr;
return shadowView;
}
void BaseTextShadowNode::buildAttributedString(
TextAttributes const &baseTextAttributes,
ShadowNode const &parentNode,
AttributedString &outAttributedString,
Attachments &outAttachments) {
for (auto const &childNode : parentNode.getChildren()) {
// RawShadowNode
auto rawTextShadowNode =
traitCast<RawTextShadowNode const*>(childNode.get());
if (rawTextShadowNode) {
auto fragment = AttributedString::Fragment{};
fragment.string = rawTextShadowNode->getConcreteProps().text;
fragment.textAttributes = baseTextAttributes;
// Storing a retaining pointer to `ParagraphShadowNode` inside
// `attributedString` causes a retain cycle (besides that fact that we
// don't need it at all). Storing a `ShadowView` instance instead of
// `ShadowNode` should properly fix this problem.
fragment.parentShadowView = shadowViewFromShadowNode(parentNode);
outAttributedString.appendFragment(fragment);
continue;
}
// TextShadowNode
auto textShadowNode = traitCast<TextShadowNode const*>(childNode.get());
if (textShadowNode) {
auto localTextAttributes = baseTextAttributes;
localTextAttributes.apply(
textShadowNode->getConcreteProps().textAttributes);
buildAttributedString(
localTextAttributes,
*textShadowNode,
outAttributedString,
outAttachments);
continue;
}
// Any *other* kind of ShadowNode
auto fragment = AttributedString::Fragment{};
fragment.string = AttributedString::Fragment::AttachmentCharacter();
fragment.parentShadowView = shadowViewFromShadowNode(*childNode);
fragment.textAttributes = baseTextAttributes;
outAttributedString.appendFragment(fragment);
outAttachments.push_back(Attachment{
childNode.get(), outAttributedString.getFragments().size() - 1});
}
}
} // namespace react
} // namespace facebook