Fabric: More type asserts in ConcreteComponentDescriptor

Summary:
`ConcreteComponentDescriptor` is a major place where dynamic dispatch calls end up. We see some amount of crashes that can be caused by an invalid pointer to a ComponentDescriptor, those checks can help verify this theory. Anyway, it's a good practice to fail earlier.

Changelog: [Internal] - Stability improvements (Fabric)

Reviewed By: sammy-SC

Differential Revision: D17991515

fbshipit-source-id: 1cac372a12b49430a3d1db66c8fc673e6adc32e9
This commit is contained in:
Valentin Shergin
2019-10-18 09:25:51 -07:00
committed by Facebook Github Bot
parent 3408dc0b3e
commit dbc7e4e527
@@ -77,6 +77,10 @@ class ConcreteComponentDescriptor : public ComponentDescriptor {
UnsharedShadowNode cloneShadowNode(
const ShadowNode &sourceShadowNode,
const ShadowNodeFragment &fragment) const override {
assert(
dynamic_cast<ConcreteShadowNode const *>(&sourceShadowNode) &&
"Provided `sourceShadowNode` has an incompatible type.");
auto shadowNode = std::make_shared<ShadowNodeT>(sourceShadowNode, fragment);
adopt(shadowNode);
@@ -86,6 +90,10 @@ class ConcreteComponentDescriptor : public ComponentDescriptor {
void appendChild(
const SharedShadowNode &parentShadowNode,
const SharedShadowNode &childShadowNode) const override {
assert(
dynamic_cast<ConcreteShadowNode const *>(parentShadowNode.get()) &&
"Provided `parentShadowNode` has an incompatible type.");
auto concreteParentShadowNode =
std::static_pointer_cast<const ShadowNodeT>(parentShadowNode);
auto concreteNonConstParentShadowNode =
@@ -96,6 +104,11 @@ class ConcreteComponentDescriptor : public ComponentDescriptor {
virtual SharedProps cloneProps(
const SharedProps &props,
const RawProps &rawProps) const override {
assert(
!props ||
dynamic_cast<ConcreteProps const *>(props.get()) &&
"Provided `props` has an incompatible type.");
if (rawProps.isEmpty()) {
return props ? props : ShadowNodeT::defaultSharedProps();
}
@@ -132,6 +145,12 @@ class ConcreteComponentDescriptor : public ComponentDescriptor {
return nullptr;
}
assert(previousState && "Provided `previousState` is nullptr.");
assert(data && "Provided `data` is nullptr.");
assert(
dynamic_cast<ConcreteState const *>(previousState.get()) &&
"Provided `previousState` has an incompatible type.");
return std::make_shared<const ConcreteState>(
std::move(*std::static_pointer_cast<ConcreteStateData>(data)),
*std::static_pointer_cast<const ConcreteState>(previousState));