Skip to content

Commit

Permalink
Temporary commit at 9/4/2023, 2:51:36 AM
Browse files Browse the repository at this point in the history
Differential Revision: https://www.internalfb.com/diff/D48769809?entry_point=27

fbshipit-source-id: 91b0312a1e15eba4a77d849c5d4bb63b187fae88
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Sep 4, 2023
1 parent 0828d42 commit 79fe987
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 88 deletions.
77 changes: 14 additions & 63 deletions yoga/Yoga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
#include <cmath>
#include <cstring>
#include <memory>
#include <stdexcept>

#include <yoga/Yoga.h>
#include <yoga/Yoga-internal.h>

#include <yoga/algorithm/CollectFlexItemsRowValues.h>
#include <yoga/algorithm/FlexDirection.h>
#include <yoga/algorithm/ResolveValue.h>
#include <yoga/debug/Assert.h>
#include <yoga/debug/Log.h>
#include <yoga/debug/NodeToString.h>
#include <yoga/event/event.h>
Expand Down Expand Up @@ -190,8 +190,9 @@ int32_t gConfigInstanceCount = 0;

YOGA_EXPORT WIN_EXPORT YGNodeRef YGNodeNewWithConfig(const YGConfigRef config) {
auto* node = new yoga::Node{static_cast<yoga::Config*>(config)};
YGAssert(config != nullptr, "Tried to construct YGNode with null config");
YGAssertWithConfig(
yoga::assertFatal(
config != nullptr, "Tried to construct YGNode with null config");
yoga::assertFatalWithConfig(
config, node != nullptr, "Could not allocate memory for node");
Event::publish<Event::NodeAllocation>(node, {config});

Expand All @@ -210,7 +211,7 @@ YOGA_EXPORT YGNodeRef YGNodeNew(void) {
YOGA_EXPORT YGNodeRef YGNodeClone(YGNodeRef oldNodeRef) {
auto oldNode = static_cast<yoga::Node*>(oldNodeRef);
auto node = new yoga::Node(*oldNode);
YGAssertWithConfig(
yoga::assertFatalWithConfig(
oldNode->getConfig(),
node != nullptr,
"Could not allocate memory for node");
Expand Down Expand Up @@ -312,12 +313,12 @@ YOGA_EXPORT void YGNodeInsertChild(
auto owner = static_cast<yoga::Node*>(ownerRef);
auto child = static_cast<yoga::Node*>(childRef);

YGAssertWithNode(
yoga::assertFatalWithNode(
owner,
child->getOwner() == nullptr,
"Child already has a owner, it must be removed first.");

YGAssertWithNode(
yoga::assertFatalWithNode(
owner,
!owner->hasMeasureFunc(),
"Cannot add child: Nodes with measure functions cannot have children.");
Expand Down Expand Up @@ -456,7 +457,7 @@ YOGA_EXPORT YGNodeRef YGNodeGetParent(const YGNodeRef node) {
YOGA_EXPORT void YGNodeMarkDirty(const YGNodeRef nodeRef) {
auto node = static_cast<yoga::Node*>(nodeRef);

YGAssertWithNode(
yoga::assertFatalWithNode(
node,
node->hasMeasureFunc(),
"Only leaf nodes with custom measure functions "
Expand Down Expand Up @@ -943,7 +944,7 @@ YOGA_EXPORT YGValue YGNodeStyleGetMaxHeight(const YGNodeConstRef node) {
YOGA_EXPORT type YGNodeLayoutGet##name( \
const YGNodeRef nodeRef, const YGEdge edge) { \
auto node = static_cast<yoga::Node*>(nodeRef); \
YGAssertWithNode( \
yoga::assertFatalWithNode( \
node, \
edge <= YGEdgeEnd, \
"Cannot get layout properties of multi-edge shorthands"); \
Expand Down Expand Up @@ -1053,7 +1054,7 @@ static float YGBaseline(yoga::Node* node, void* layoutContext) {

Event::publish<Event::NodeBaselineEnd>(node);

YGAssertWithNode(
yoga::assertFatalWithNode(
node,
!yoga::isUndefined(baseline),
"Expect custom baseline function to not return NaN");
Expand Down Expand Up @@ -1669,7 +1670,7 @@ static void YGNodeWithMeasureFuncSetMeasuredDimensions(
LayoutData& layoutMarkerData,
void* const layoutContext,
const LayoutPassReason reason) {
YGAssertWithNode(
yoga::assertFatalWithNode(
node,
node->hasMeasureFunc(),
"Expected node to have custom measure function");
Expand Down Expand Up @@ -2731,14 +2732,14 @@ static void YGNodelayoutImpl(
const uint32_t depth,
const uint32_t generationCount,
const LayoutPassReason reason) {
YGAssertWithNode(
yoga::assertFatalWithNode(
node,
yoga::isUndefined(availableWidth)
? widthMeasureMode == YGMeasureModeUndefined
: true,
"availableWidth is indefinite so widthMeasureMode must be "
"YGMeasureModeUndefined");
YGAssertWithNode(
yoga::assertFatalWithNode(
node,
yoga::isUndefined(availableHeight)
? heightMeasureMode == YGMeasureModeUndefined
Expand Down Expand Up @@ -4089,7 +4090,7 @@ bool YGLayoutNodeInternal(
YOGA_EXPORT void YGConfigSetPointScaleFactor(
const YGConfigRef config,
const float pixelsInPoint) {
YGAssertWithConfig(
yoga::assertFatalWithConfig(
config,
pixelsInPoint >= 0.0f,
"Scale factor should not be less than zero");
Expand Down Expand Up @@ -4287,56 +4288,6 @@ YOGA_EXPORT void YGConfigSetLogger(const YGConfigRef config, YGLogger logger) {
}
}

static void fatalWithMessage(const char* message) {
#if defined(__cpp_exceptions)
throw std::logic_error(message);
#else
std::terminate();
#endif
}

void YGAssert(const bool condition, const char* message) {
if (!condition) {
yoga::log(
static_cast<yoga::Node*>(nullptr),
YGLogLevelFatal,
nullptr,
"%s\n",
message);
fatalWithMessage(message);
}
}

void YGAssertWithNode(
const YGNodeRef node,
const bool condition,
const char* message) {
if (!condition) {
yoga::log(
static_cast<yoga::Node*>(node),
YGLogLevelFatal,
nullptr,
"%s\n",
message);
fatalWithMessage(message);
}
}

void YGAssertWithConfig(
const YGConfigRef config,
const bool condition,
const char* message) {
if (!condition) {
yoga::log(
static_cast<yoga::Config*>(config),
YGLogLevelFatal,
nullptr,
"%s\n",
message);
fatalWithMessage(message);
}
}

YOGA_EXPORT void YGConfigSetExperimentalFeatureEnabled(
const YGConfigRef config,
const YGExperimentalFeature feature,
Expand Down
9 changes: 0 additions & 9 deletions yoga/Yoga.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,6 @@ WIN_EXPORT float YGNodeLayoutGetBorder(YGNodeRef node, YGEdge edge);
WIN_EXPORT float YGNodeLayoutGetPadding(YGNodeRef node, YGEdge edge);

WIN_EXPORT void YGConfigSetLogger(YGConfigRef config, YGLogger logger);
WIN_EXPORT void YGAssert(bool condition, const char* message);
WIN_EXPORT void YGAssertWithNode(
YGNodeRef node,
bool condition,
const char* message);
WIN_EXPORT void YGAssertWithConfig(
YGConfigRef config,
bool condition,
const char* message);
// Set this to number of pixels in 1 point to round calculation results If you
// want to avoid rounding - set PointScaleFactor to 0
WIN_EXPORT void YGConfigSetPointScaleFactor(
Expand Down
14 changes: 4 additions & 10 deletions yoga/algorithm/FlexDirection.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <yoga/Yoga.h>

#include <yoga/debug/Assert.h>

namespace facebook::yoga {

inline bool isRow(const YGFlexDirection flexDirection) {
Expand Down Expand Up @@ -55,11 +57,7 @@ inline YGEdge leadingEdge(const YGFlexDirection flexDirection) {
return YGEdgeRight;
}

YGAssert(false, "Invalid YGFlexDirection");

// Avoid "not all control paths return a value" warning until next diff adds
// assert with [[noreturn]]
return YGEdgeTop;
fatalWithMessage("Invalid YGFlexDirection");
}

inline YGEdge trailingEdge(const YGFlexDirection flexDirection) {
Expand All @@ -74,11 +72,7 @@ inline YGEdge trailingEdge(const YGFlexDirection flexDirection) {
return YGEdgeLeft;
}

YGAssert(false, "Invalid YGFlexDirection");

// Avoid "not all control paths return a value" warning until next diff adds
// assert with [[noreturn]]
return YGEdgeTop;
fatalWithMessage("Invalid YGFlexDirection");
}

} // namespace facebook::yoga
65 changes: 65 additions & 0 deletions yoga/debug/Assert.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <stdexcept>

#include <yoga/debug/Assert.h>
#include <yoga/debug/Log.h>

namespace facebook::yoga {

[[noreturn]] void fatalWithMessage(const char* message) {
#if defined(__cpp_exceptions)
throw std::logic_error(message);
#else
std::terminate();
#endif
}

void assertFatal(const bool condition, const char* message) {
if (!condition) {
yoga::log(
static_cast<yoga::Node*>(nullptr),
YGLogLevelFatal,
nullptr,
"%s\n",
message);
fatalWithMessage(message);
}
}

void assertFatalWithNode(
const YGNodeRef node,
const bool condition,
const char* message) {
if (!condition) {
yoga::log(
static_cast<yoga::Node*>(node),
YGLogLevelFatal,
nullptr,
"%s\n",
message);
fatalWithMessage(message);
}
}

void assertFatalWithConfig(
const YGConfigRef config,
const bool condition,
const char* message) {
if (!condition) {
yoga::log(
static_cast<yoga::Config*>(config),
YGLogLevelFatal,
nullptr,
"%s\n",
message);
fatalWithMessage(message);
}
}

} // namespace facebook::yoga
25 changes: 25 additions & 0 deletions yoga/debug/Assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <yoga/Yoga.h>
#include <yoga/node/Node.h>
#include <yoga/config/Config.h>

namespace facebook::yoga {

[[noreturn]] void fatalWithMessage(const char* message);

void assertFatal(bool condition, const char* message);
void assertFatalWithNode(YGNodeRef node, bool condition, const char* message);
void assertFatalWithConfig(
YGConfigRef config,
bool condition,
const char* message);

} // namespace facebook::yoga
15 changes: 9 additions & 6 deletions yoga/node/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

#include <yoga/algorithm/FlexDirection.h>
#include <yoga/algorithm/ResolveValue.h>
#include <yoga/debug/Assert.h>
#include <yoga/node/Node.h>
#include <yoga/numeric/Comparison.h>

namespace facebook::yoga {

Node::Node(yoga::Config* config) : config_{config} {
YGAssert(config != nullptr, "Attempting to construct Node with null config");
yoga::assertFatal(
config != nullptr, "Attempting to construct Node with null config");

flags_.hasNewLayout = true;
if (config->useWebDefaults()) {
Expand Down Expand Up @@ -234,7 +236,7 @@ void Node::setMeasureFunc(decltype(Node::measure_) measureFunc) {
// places in Litho
setNodeType(YGNodeTypeDefault);
} else {
YGAssertWithNode(
yoga::assertFatalWithNode(
this,
children_.size() == 0,
"Cannot set measure function: Nodes with measure functions cannot have "
Expand Down Expand Up @@ -274,8 +276,9 @@ void Node::insertChild(Node* child, uint32_t index) {
}

void Node::setConfig(yoga::Config* config) {
YGAssert(config != nullptr, "Attempting to set a null config on a Node");
YGAssertWithConfig(
yoga::assertFatal(
config != nullptr, "Attempting to set a null config on a Node");
yoga::assertFatalWithConfig(
config,
config->useWebDefaults() == config_->useWebDefaults(),
"UseWebDefaults may not be changed after constructing a Node");
Expand Down Expand Up @@ -592,11 +595,11 @@ FloatOptional Node::getTrailingPaddingAndBorder(
}

void Node::reset() {
YGAssertWithNode(
yoga::assertFatalWithNode(
this,
children_.size() == 0,
"Cannot reset a node which still has children attached");
YGAssertWithNode(
yoga::assertFatalWithNode(
this, owner_ == nullptr, "Cannot reset a node still attached to a owner");

*this = Node{getConfig()};
Expand Down

0 comments on commit 79fe987

Please sign in to comment.