Support for (de)serializing config values (#1571)

Summary:
Pull Request resolved: https://github.com/facebook/yoga/pull/1571

X-link: https://github.com/facebook/react-native/pull/42750

tsia. This is state we need to capture as it can drastically affect the benchmark times

Reviewed By: NickGerleman

Differential Revision: D53203385

fbshipit-source-id: 47178458d039df90fb15d8a420f9e0f17e4fe6ca
This commit is contained in:
Joe Vilches
2024-02-09 16:44:32 -08:00
committed by Facebook GitHub Bot
parent a37565f70d
commit f90ad378ff
12 changed files with 386 additions and 349 deletions

View File

@@ -9,8 +9,8 @@
#include <fstream> #include <fstream>
#include <benchmark/Benchmark.h> #include <benchmark/Benchmark.h>
#include <benchmark/TreeDeserialization.h>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <yoga/Yoga.h>
namespace facebook::yoga { namespace facebook::yoga {
@@ -21,153 +21,28 @@ constexpr uint32_t kNumRepititions = 1000;
using SteadyClockDurations = using SteadyClockDurations =
std::array<steady_clock::duration, kNumRepititions>; std::array<steady_clock::duration, kNumRepititions>;
std::string invalidArgumentMessage(std::string arg, std::string enumName) { std::shared_ptr<YGConfig> buildConfigFromJson(json& j) {
return arg + " does not represent any " + enumName + " values"; json jsonConfig = j["config"];
} std::shared_ptr<YGConfig> config(YGConfigNew(), YGConfigFree);
for (json::iterator it = jsonConfig.begin(); it != jsonConfig.end(); it++) {
YGFlexDirection flexDirectionFromString(std::string str) { if (it.key() == "use-web-defaults") {
if (str == "row") { YGConfigSetUseWebDefaults(config.get(), it.value());
return YGFlexDirectionRow; } else if (it.key() == "point-scale-factor") {
} else if (str == "row-reverse") { YGConfigSetPointScaleFactor(config.get(), it.value());
return YGFlexDirectionRowReverse; } else if (it.key() == "errata") {
} else if (str == "column") { YGConfigSetErrata(config.get(), errataFromString(it.value()));
return YGFlexDirectionColumn; } else if (it.key() == "experimental-features") {
} else if (str == "column-reverse") { // Experimental features is serialized into an array where the values
return YGFlexDirectionColumnReverse; // present indicate that that feature is enabled
} else { for (json::iterator efIt = it.value().begin(); efIt != it.value().end();
throw std::invalid_argument(invalidArgumentMessage(str, "YGFlexDirection")); efIt++) {
YGConfigSetExperimentalFeatureEnabled(
config.get(), experimentalFeatureFromString(efIt.value()), true);
} }
}
YGJustify justifyContentFromString(std::string str) {
if (str == "flex-start") {
return YGJustifyFlexStart;
} else if (str == "center") {
return YGJustifyCenter;
} else if (str == "flex-end") {
return YGJustifyFlexEnd;
} else if (str == "space-between") {
return YGJustifySpaceBetween;
} else if (str == "space-around") {
return YGJustifySpaceAround;
} else if (str == "space-evenly") {
return YGJustifySpaceEvenly;
} else {
throw std::invalid_argument(invalidArgumentMessage(str, "YGJustify"));
} }
}
YGAlign alignFromString(std::string str) {
if (str == "auto") {
return YGAlignAuto;
} else if (str == "flex-start") {
return YGAlignFlexStart;
} else if (str == "center") {
return YGAlignCenter;
} else if (str == "flex-end") {
return YGAlignFlexEnd;
} else if (str == "stretch") {
return YGAlignStretch;
} else if (str == "baseline") {
return YGAlignBaseline;
} else if (str == "space-between") {
return YGAlignSpaceBetween;
} else if (str == "space-around") {
return YGAlignSpaceAround;
} else if (str == "space-evenly") {
return YGAlignSpaceEvenly;
} else {
throw std::invalid_argument(invalidArgumentMessage(str, "YGAlign"));
}
}
YGWrap wrapFromString(std::string str) {
if (str == "no-wrap") {
return YGWrapNoWrap;
} else if (str == "wrap") {
return YGWrapWrap;
} else if (str == "wrap-reverse") {
return YGWrapWrapReverse;
} else {
throw std::invalid_argument(invalidArgumentMessage(str, "YGAlign"));
}
}
YGOverflow overflowFromString(std::string str) {
if (str == "visible") {
return YGOverflowVisible;
} else if (str == "hidden") {
return YGOverflowHidden;
} else if (str == "scroll") {
return YGOverflowScroll;
} else {
throw std::invalid_argument(invalidArgumentMessage(str, "YGAlign"));
}
}
YGDisplay displayFromString(std::string str) {
if (str == "flex") {
return YGDisplayFlex;
} else if (str == "none") {
return YGDisplayNone;
} else {
throw std::invalid_argument(invalidArgumentMessage(str, "YGAlign"));
}
}
YGPositionType positionTypeFromString(std::string str) {
if (str == "static") {
return YGPositionTypeStatic;
} else if (str == "relative") {
return YGPositionTypeRelative;
} else if (str == "absolute") {
return YGPositionTypeAbsolute;
} else {
throw std::invalid_argument(invalidArgumentMessage(str, "YGAlign"));
}
}
bool isAuto(json& j) {
return j.is_string() && j == "auto";
}
YGUnit unitFromJson(json& j) {
if (isAuto(j)) {
return YGUnitAuto;
} }
std::string unit = j["unit"]; return config;
if (unit == "px") {
return YGUnitPoint;
} else if (unit == "pct") {
return YGUnitPercent;
} else {
throw std::invalid_argument(invalidArgumentMessage(unit, "YGUnit"));
}
}
YGEdge edgeFromString(std::string str) {
if (str == "left") {
return YGEdgeLeft;
} else if (str == "top") {
return YGEdgeTop;
} else if (str == "right") {
return YGEdgeRight;
} else if (str == "bottom") {
return YGEdgeBottom;
} else if (str == "start") {
return YGEdgeStart;
} else if (str == "end") {
return YGEdgeEnd;
} else if (str == "horizontal") {
return YGEdgeHorizontal;
} else if (str == "vertical") {
return YGEdgeVertical;
} else if (str == "all") {
return YGEdgeAll;
} else {
throw std::invalid_argument(invalidArgumentMessage(str, "YGEdge"));
}
} }
std::string edgeStringFromPropertyName( std::string edgeStringFromPropertyName(
@@ -176,151 +51,159 @@ std::string edgeStringFromPropertyName(
return key.substr(propertyName.length() + 1); return key.substr(propertyName.length() + 1);
} }
YGNodeRef buildTreeFromJson(json& j, YGNodeRef parent, size_t index) { void setStylesFromJson(json& j, std::shared_ptr<YGNode> node) {
const YGNodeRef node = YGNodeNew();
if (parent != nullptr) {
YGNodeInsertChild(parent, node, index);
}
json style = j["style"]; json style = j["style"];
for (const auto& [key, value] : style.items()) { for (const auto& [key, value] : style.items()) {
if (key == "flex-direction") { if (key == "flex-direction") {
YGNodeStyleSetFlexDirection(node, flexDirectionFromString(value)); YGNodeStyleSetFlexDirection(node.get(), flexDirectionFromString(value));
} else if (key == "justify-content") { } else if (key == "justify-content") {
YGNodeStyleSetJustifyContent(node, justifyContentFromString(value)); YGNodeStyleSetJustifyContent(node.get(), justifyContentFromString(value));
} else if (key == "align-items") { } else if (key == "align-items") {
YGNodeStyleSetAlignItems(node, alignFromString(value)); YGNodeStyleSetAlignItems(node.get(), alignFromString(value));
} else if (key == "align-content") { } else if (key == "align-content") {
YGNodeStyleSetAlignContent(node, alignFromString(value)); YGNodeStyleSetAlignContent(node.get(), alignFromString(value));
} else if (key == "align-self") { } else if (key == "align-self") {
YGNodeStyleSetAlignSelf(node, alignFromString(value)); YGNodeStyleSetAlignSelf(node.get(), alignFromString(value));
} else if (key == "flex-wrap") { } else if (key == "flex-wrap") {
YGNodeStyleSetFlexWrap(node, wrapFromString(value)); YGNodeStyleSetFlexWrap(node.get(), wrapFromString(value));
} else if (key == "overflow") { } else if (key == "overflow") {
YGNodeStyleSetOverflow(node, overflowFromString(value)); YGNodeStyleSetOverflow(node.get(), overflowFromString(value));
} else if (key == "display") { } else if (key == "display") {
YGNodeStyleSetDisplay(node, displayFromString(value)); YGNodeStyleSetDisplay(node.get(), displayFromString(value));
} else if (key == "position-type") { } else if (key == "position-type") {
YGNodeStyleSetPositionType(node, positionTypeFromString(value)); YGNodeStyleSetPositionType(node.get(), positionTypeFromString(value));
} else if (key == "flex-grow") { } else if (key == "flex-grow") {
YGNodeStyleSetFlexGrow(node, value); YGNodeStyleSetFlexGrow(node.get(), value);
} else if (key == "flex-shrink") { } else if (key == "flex-shrink") {
YGNodeStyleSetFlexShrink(node, value); YGNodeStyleSetFlexShrink(node.get(), value);
} else if (key == "flex") { } else if (key == "flex") {
YGNodeStyleSetFlex(node, value); YGNodeStyleSetFlex(node.get(), value);
} else if (key == "flex-basis") { } else if (key == "flex-basis") {
YGUnit unit = unitFromJson(value); YGUnit unit = unitFromJson(value);
if (unit == YGUnitAuto) { if (unit == YGUnitAuto) {
YGNodeStyleSetFlexBasisAuto(node); YGNodeStyleSetFlexBasisAuto(node.get());
} else if (unit == YGUnitPoint) { } else if (unit == YGUnitPoint) {
YGNodeStyleSetFlexBasis(node, value["value"]); YGNodeStyleSetFlexBasis(node.get(), value["value"]);
} else if (unit == YGUnitPercent) { } else if (unit == YGUnitPercent) {
YGNodeStyleSetFlexBasisPercent(node, value["value"]); YGNodeStyleSetFlexBasisPercent(node.get(), value["value"]);
} }
} else if (key.starts_with("position")) { } else if (key.starts_with("position")) {
YGEdge edge = edgeFromString(edgeStringFromPropertyName(key, "position")); YGEdge edge = edgeFromString(edgeStringFromPropertyName(key, "position"));
YGUnit unit = unitFromJson(value); YGUnit unit = unitFromJson(value);
if (unit == YGUnitPoint) { if (unit == YGUnitPoint) {
YGNodeStyleSetPosition(node, edge, value["value"]); YGNodeStyleSetPosition(node.get(), edge, value["value"]);
} else if (unit == YGUnitPercent) { } else if (unit == YGUnitPercent) {
YGNodeStyleSetPositionPercent(node, edge, value["value"]); YGNodeStyleSetPositionPercent(node.get(), edge, value["value"]);
} }
} else if (key.starts_with("padding")) { } else if (key.starts_with("padding")) {
YGEdge edge = edgeFromString(edgeStringFromPropertyName(key, "padding")); YGEdge edge = edgeFromString(edgeStringFromPropertyName(key, "padding"));
YGUnit unit = unitFromJson(value); YGUnit unit = unitFromJson(value);
if (unit == YGUnitPoint) { if (unit == YGUnitPoint) {
YGNodeStyleSetPadding(node, edge, value["value"]); YGNodeStyleSetPadding(node.get(), edge, value["value"]);
} else if (unit == YGUnitPercent) { } else if (unit == YGUnitPercent) {
YGNodeStyleSetPaddingPercent(node, edge, value["value"]); YGNodeStyleSetPaddingPercent(node.get(), edge, value["value"]);
} }
} else if (key.starts_with("border")) { } else if (key.starts_with("border")) {
YGEdge edge = edgeFromString(edgeStringFromPropertyName(key, "border")); YGEdge edge = edgeFromString(edgeStringFromPropertyName(key, "border"));
YGUnit unit = unitFromJson(value); YGUnit unit = unitFromJson(value);
if (unit == YGUnitPoint) { if (unit == YGUnitPoint) {
YGNodeStyleSetBorder(node, edge, value["value"]); YGNodeStyleSetBorder(node.get(), edge, value["value"]);
} }
} else if (key.starts_with("margin")) { } else if (key.starts_with("margin")) {
YGEdge edge = edgeFromString(edgeStringFromPropertyName(key, "margin")); YGEdge edge = edgeFromString(edgeStringFromPropertyName(key, "margin"));
YGUnit unit = unitFromJson(value); YGUnit unit = unitFromJson(value);
if (unit == YGUnitPoint) { if (unit == YGUnitPoint) {
YGNodeStyleSetMargin(node, edge, value["value"]); YGNodeStyleSetMargin(node.get(), edge, value["value"]);
} else if (unit == YGUnitPercent) { } else if (unit == YGUnitPercent) {
YGNodeStyleSetMarginPercent(node, edge, value["value"]); YGNodeStyleSetMarginPercent(node.get(), edge, value["value"]);
} else if (unit == YGUnitAuto) { } else if (unit == YGUnitAuto) {
YGNodeStyleSetMarginAuto(node, edge); YGNodeStyleSetMarginAuto(node.get(), edge);
} }
} else if (key == "gap") { } else if (key == "gap") {
YGUnit unit = unitFromJson(value); YGUnit unit = unitFromJson(value);
if (unit == YGUnitPoint) { if (unit == YGUnitPoint) {
YGNodeStyleSetGap(node, YGGutterAll, value["value"]); YGNodeStyleSetGap(node.get(), YGGutterAll, value["value"]);
} }
} else if (key == "column-gap") { } else if (key == "column-gap") {
YGUnit unit = unitFromJson(value); YGUnit unit = unitFromJson(value);
if (unit == YGUnitPoint) { if (unit == YGUnitPoint) {
YGNodeStyleSetGap(node, YGGutterColumn, value["value"]); YGNodeStyleSetGap(node.get(), YGGutterColumn, value["value"]);
} }
} else if (key == "row-gap") { } else if (key == "row-gap") {
YGUnit unit = unitFromJson(value); YGUnit unit = unitFromJson(value);
if (unit == YGUnitPoint) { if (unit == YGUnitPoint) {
YGNodeStyleSetGap(node, YGGutterRow, value["value"]); YGNodeStyleSetGap(node.get(), YGGutterRow, value["value"]);
} }
} else if (key == "height") { } else if (key == "height") {
YGUnit unit = unitFromJson(value); YGUnit unit = unitFromJson(value);
if (unit == YGUnitAuto) { if (unit == YGUnitAuto) {
YGNodeStyleSetHeightAuto(node); YGNodeStyleSetHeightAuto(node.get());
} else if (unit == YGUnitPoint) { } else if (unit == YGUnitPoint) {
YGNodeStyleSetHeight(node, value["value"]); YGNodeStyleSetHeight(node.get(), value["value"]);
} else if (unit == YGUnitPercent) { } else if (unit == YGUnitPercent) {
YGNodeStyleSetHeightPercent(node, value["value"]); YGNodeStyleSetHeightPercent(node.get(), value["value"]);
} }
} else if (key == "width") { } else if (key == "width") {
YGUnit unit = unitFromJson(value); YGUnit unit = unitFromJson(value);
if (unit == YGUnitAuto) { if (unit == YGUnitAuto) {
YGNodeStyleSetWidthAuto(node); YGNodeStyleSetWidthAuto(node.get());
} else if (unit == YGUnitPoint) { } else if (unit == YGUnitPoint) {
YGNodeStyleSetWidth(node, value["value"]); YGNodeStyleSetWidth(node.get(), value["value"]);
} else if (unit == YGUnitPercent) { } else if (unit == YGUnitPercent) {
YGNodeStyleSetWidthPercent(node, value["value"]); YGNodeStyleSetWidthPercent(node.get(), value["value"]);
} }
} else if (key == "min-height") { } else if (key == "min-height") {
YGUnit unit = unitFromJson(value); YGUnit unit = unitFromJson(value);
if (unit == YGUnitPoint) { if (unit == YGUnitPoint) {
YGNodeStyleSetMinHeight(node, value["value"]); YGNodeStyleSetMinHeight(node.get(), value["value"]);
} else if (unit == YGUnitPercent) { } else if (unit == YGUnitPercent) {
YGNodeStyleSetMinHeightPercent(node, value["value"]); YGNodeStyleSetMinHeightPercent(node.get(), value["value"]);
} }
} else if (key == "min-width") { } else if (key == "min-width") {
YGUnit unit = unitFromJson(value); YGUnit unit = unitFromJson(value);
if (unit == YGUnitPoint) { if (unit == YGUnitPoint) {
YGNodeStyleSetMinWidth(node, value["value"]); YGNodeStyleSetMinWidth(node.get(), value["value"]);
} else if (unit == YGUnitPercent) { } else if (unit == YGUnitPercent) {
YGNodeStyleSetMinWidthPercent(node, value["value"]); YGNodeStyleSetMinWidthPercent(node.get(), value["value"]);
} }
} else if (key == "max-height") { } else if (key == "max-height") {
YGUnit unit = unitFromJson(value); YGUnit unit = unitFromJson(value);
if (unit == YGUnitPoint) { if (unit == YGUnitPoint) {
YGNodeStyleSetMaxHeight(node, value["value"]); YGNodeStyleSetMaxHeight(node.get(), value["value"]);
} else if (unit == YGUnitPercent) { } else if (unit == YGUnitPercent) {
YGNodeStyleSetMaxHeightPercent(node, value["value"]); YGNodeStyleSetMaxHeightPercent(node.get(), value["value"]);
} }
} else if (key == "max-width") { } else if (key == "max-width") {
YGUnit unit = unitFromJson(value); YGUnit unit = unitFromJson(value);
if (unit == YGUnitPoint) { if (unit == YGUnitPoint) {
YGNodeStyleSetMaxWidth(node, value["value"]); YGNodeStyleSetMaxWidth(node.get(), value["value"]);
} else if (unit == YGUnitPercent) { } else if (unit == YGUnitPercent) {
YGNodeStyleSetMaxWidthPercent(node, value["value"]); YGNodeStyleSetMaxWidthPercent(node.get(), value["value"]);
} }
} }
} }
}
YogaNodeAndConfig
buildTreeFromJson(json& j, YogaNodeAndConfig* parent, size_t index) {
std::shared_ptr<YGConfig> config = buildConfigFromJson(j);
std::shared_ptr<YGNode> node(YGNodeNewWithConfig(config.get()), YGNodeFree);
YogaNodeAndConfig wrapper{node, config, std::vector<YogaNodeAndConfig>{}};
if (parent != nullptr) {
YGNodeInsertChild(parent->node_.get(), node.get(), index);
parent->children_.push_back(wrapper);
}
setStylesFromJson(j, node);
json children = j["children"]; json children = j["children"];
size_t childIndex = 0; size_t childIndex = 0;
for (json child : children) { for (json child : children) {
buildTreeFromJson(child, node, childIndex); buildTreeFromJson(child, &wrapper, childIndex);
childIndex++; childIndex++;
} }
return node; return wrapper;
} }
BenchmarkResult generateBenchmark(const std::filesystem::path& capturePath) { BenchmarkResult generateBenchmark(const std::filesystem::path& capturePath) {
@@ -328,15 +211,14 @@ BenchmarkResult generateBenchmark(const std::filesystem::path& capturePath) {
json capture = json::parse(captureFile); json capture = json::parse(captureFile);
auto treeCreationBegin = steady_clock::now(); auto treeCreationBegin = steady_clock::now();
YGNodeRef root = buildTreeFromJson(capture, nullptr, 0 /*index*/); YogaNodeAndConfig root = buildTreeFromJson(capture, nullptr, 0 /*index*/);
auto treeCreationEnd = steady_clock::now(); auto treeCreationEnd = steady_clock::now();
auto layoutBegin = steady_clock::now(); auto layoutBegin = steady_clock::now();
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeCalculateLayout(
root.node_.get(), YGUndefined, YGUndefined, YGDirectionLTR);
auto layoutEnd = steady_clock::now(); auto layoutEnd = steady_clock::now();
YGNodeFreeRecursive(root);
return BenchmarkResult{ return BenchmarkResult{
treeCreationEnd - treeCreationBegin, layoutEnd - layoutBegin}; treeCreationEnd - treeCreationBegin, layoutEnd - layoutBegin};
} }

View File

@@ -8,10 +8,20 @@
#pragma once #pragma once
#include <chrono> #include <chrono>
#include <memory>
#include <string> #include <string>
#include <vector>
#include <yoga/Yoga.h>
namespace facebook::yoga { namespace facebook::yoga {
struct YogaNodeAndConfig {
std::shared_ptr<YGNode> node_;
std::shared_ptr<YGConfig> config_;
std::vector<YogaNodeAndConfig> children_;
};
struct BenchmarkResult { struct BenchmarkResult {
std::chrono::steady_clock::duration treeCreationDuration; std::chrono::steady_clock::duration treeCreationDuration;
std::chrono::steady_clock::duration layoutDuration; std::chrono::steady_clock::duration layoutDuration;

View File

@@ -0,0 +1,194 @@
/*
* 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 <nlohmann/json.hpp>
#include <yoga/Yoga.h>
namespace facebook::yoga {
using namespace nlohmann;
inline std::string invalidArgumentMessage(
std::string arg,
std::string enumName) {
return arg + " does not represent any " + enumName + " values";
}
inline YGFlexDirection flexDirectionFromString(std::string str) {
if (str == "row") {
return YGFlexDirectionRow;
} else if (str == "row-reverse") {
return YGFlexDirectionRowReverse;
} else if (str == "column") {
return YGFlexDirectionColumn;
} else if (str == "column-reverse") {
return YGFlexDirectionColumnReverse;
} else {
throw std::invalid_argument(invalidArgumentMessage(str, "YGFlexDirection"));
}
}
inline YGJustify justifyContentFromString(std::string str) {
if (str == "flex-start") {
return YGJustifyFlexStart;
} else if (str == "center") {
return YGJustifyCenter;
} else if (str == "flex-end") {
return YGJustifyFlexEnd;
} else if (str == "space-between") {
return YGJustifySpaceBetween;
} else if (str == "space-around") {
return YGJustifySpaceAround;
} else if (str == "space-evenly") {
return YGJustifySpaceEvenly;
} else {
throw std::invalid_argument(invalidArgumentMessage(str, "YGJustify"));
}
}
inline YGAlign alignFromString(std::string str) {
if (str == "auto") {
return YGAlignAuto;
} else if (str == "flex-start") {
return YGAlignFlexStart;
} else if (str == "center") {
return YGAlignCenter;
} else if (str == "flex-end") {
return YGAlignFlexEnd;
} else if (str == "stretch") {
return YGAlignStretch;
} else if (str == "baseline") {
return YGAlignBaseline;
} else if (str == "space-between") {
return YGAlignSpaceBetween;
} else if (str == "space-around") {
return YGAlignSpaceAround;
} else if (str == "space-evenly") {
return YGAlignSpaceEvenly;
} else {
throw std::invalid_argument(invalidArgumentMessage(str, "YGAlign"));
}
}
inline YGWrap wrapFromString(std::string str) {
if (str == "no-wrap") {
return YGWrapNoWrap;
} else if (str == "wrap") {
return YGWrapWrap;
} else if (str == "wrap-reverse") {
return YGWrapWrapReverse;
} else {
throw std::invalid_argument(invalidArgumentMessage(str, "YGAlign"));
}
}
inline YGOverflow overflowFromString(std::string str) {
if (str == "visible") {
return YGOverflowVisible;
} else if (str == "hidden") {
return YGOverflowHidden;
} else if (str == "scroll") {
return YGOverflowScroll;
} else {
throw std::invalid_argument(invalidArgumentMessage(str, "YGAlign"));
}
}
inline YGDisplay displayFromString(std::string str) {
if (str == "flex") {
return YGDisplayFlex;
} else if (str == "none") {
return YGDisplayNone;
} else {
throw std::invalid_argument(invalidArgumentMessage(str, "YGAlign"));
}
}
inline YGPositionType positionTypeFromString(std::string str) {
if (str == "static") {
return YGPositionTypeStatic;
} else if (str == "relative") {
return YGPositionTypeRelative;
} else if (str == "absolute") {
return YGPositionTypeAbsolute;
} else {
throw std::invalid_argument(invalidArgumentMessage(str, "YGAlign"));
}
}
inline bool isAuto(json& j) {
return j.is_string() && j == "auto";
}
inline YGUnit unitFromJson(json& j) {
if (isAuto(j)) {
return YGUnitAuto;
}
std::string unit = j["unit"];
if (unit == "px") {
return YGUnitPoint;
} else if (unit == "pct") {
return YGUnitPercent;
} else {
throw std::invalid_argument(invalidArgumentMessage(unit, "YGUnit"));
}
}
inline YGEdge edgeFromString(std::string str) {
if (str == "left") {
return YGEdgeLeft;
} else if (str == "top") {
return YGEdgeTop;
} else if (str == "right") {
return YGEdgeRight;
} else if (str == "bottom") {
return YGEdgeBottom;
} else if (str == "start") {
return YGEdgeStart;
} else if (str == "end") {
return YGEdgeEnd;
} else if (str == "horizontal") {
return YGEdgeHorizontal;
} else if (str == "vertical") {
return YGEdgeVertical;
} else if (str == "all") {
return YGEdgeAll;
} else {
throw std::invalid_argument(invalidArgumentMessage(str, "YGEdge"));
}
}
inline YGErrata errataFromString(std::string str) {
if (str == "none") {
return YGErrataNone;
} else if (str == "all") {
return YGErrataAll;
} else if (str == "classic") {
return YGErrataClassic;
} else {
throw std::invalid_argument(invalidArgumentMessage(str, "YGErrata"));
}
}
inline YGExperimentalFeature experimentalFeatureFromString(std::string str) {
if (str == "web-flex-basis") {
return YGExperimentalFeatureWebFlexBasis;
} else {
throw std::invalid_argument(
invalidArgumentMessage(str, "YGExperimentalFeature"));
}
}
inline std::string edgeStringFromPropertyName(
json::iterator it,
std::string propertyName) {
return it.key().substr(propertyName.length() + 1);
}
} // namespace facebook::yoga

View File

@@ -14,7 +14,10 @@ namespace facebook::yoga {
void captureTree(YGNodeRef node, const std::filesystem::path& path) { void captureTree(YGNodeRef node, const std::filesystem::path& path) {
std::string str; std::string str;
nodeToString(str, node, YGPrintOptionsStyle | YGPrintOptionsChildren); nodeToString(
str,
node,
PrintOptions::Style | PrintOptions::Children | PrintOptions::Config);
std::ofstream file(path); std::ofstream file(path);
file << str; file << str;
} }

View File

@@ -18,7 +18,7 @@ static void appendFloatIfNotDefault(
float num, float num,
float defaultNum) { float defaultNum) {
if (num != defaultNum && !YGFloatIsUndefined(num)) { if (num != defaultNum && !YGFloatIsUndefined(num)) {
j["style"][key] = num; j[key] = num;
} }
} }
@@ -29,13 +29,13 @@ static void appendYGValueIfNotDefault(
const YGValue& defaultValue) { const YGValue& defaultValue) {
if (value != defaultValue) { if (value != defaultValue) {
if (value.unit == YGUnitAuto) { if (value.unit == YGUnitAuto) {
j["style"][key] = "auto"; j[key] = "auto";
} else if (value.unit == YGUnitUndefined) { } else if (value.unit == YGUnitUndefined) {
j["style"][key] = "undefined"; j[key] = "undefined";
} else { } else {
std::string unit = value.unit == YGUnitPoint ? "px" : "pct"; std::string unit = value.unit == YGUnitPoint ? "px" : "pct";
j["style"][key]["value"] = value.value; j[key]["value"] = value.value;
j["style"][key]["unit"] = unit; j[key]["unit"] = unit;
} }
} }
} }
@@ -46,7 +46,17 @@ static void appendEnumValueIfNotDefault(
std::string_view value, std::string_view value,
std::string_view defaultValue) { std::string_view defaultValue) {
if (value != defaultValue) { if (value != defaultValue) {
j["style"][key] = value; j[key] = value;
}
}
static void appendBoolIfNotDefault(
json& j,
std::string_view key,
bool value,
bool defaultValue) {
if (value != defaultValue) {
j[key] = value;
} }
} }
@@ -57,47 +67,47 @@ static void appendEdges(
YGNodeRef node, YGNodeRef node,
YGNodeRef defaultNode) { YGNodeRef defaultNode) {
appendYGValueIfNotDefault( appendYGValueIfNotDefault(
j, j["style"],
key + "-left", key + "-left",
(*Field)(node, YGEdgeLeft), (*Field)(node, YGEdgeLeft),
(*Field)(defaultNode, YGEdgeLeft)); (*Field)(defaultNode, YGEdgeLeft));
appendYGValueIfNotDefault( appendYGValueIfNotDefault(
j, j["style"],
key + "-right", key + "-right",
(*Field)(node, YGEdgeRight), (*Field)(node, YGEdgeRight),
(*Field)(defaultNode, YGEdgeRight)); (*Field)(defaultNode, YGEdgeRight));
appendYGValueIfNotDefault( appendYGValueIfNotDefault(
j, j["style"],
key + "-top", key + "-top",
(*Field)(node, YGEdgeTop), (*Field)(node, YGEdgeTop),
(*Field)(defaultNode, YGEdgeTop)); (*Field)(defaultNode, YGEdgeTop));
appendYGValueIfNotDefault( appendYGValueIfNotDefault(
j, j["style"],
key + "-bottom", key + "-bottom",
(*Field)(node, YGEdgeBottom), (*Field)(node, YGEdgeBottom),
(*Field)(defaultNode, YGEdgeBottom)); (*Field)(defaultNode, YGEdgeBottom));
appendYGValueIfNotDefault( appendYGValueIfNotDefault(
j, j["style"],
key + "-all", key + "-all",
(*Field)(node, YGEdgeAll), (*Field)(node, YGEdgeAll),
(*Field)(defaultNode, YGEdgeAll)); (*Field)(defaultNode, YGEdgeAll));
appendYGValueIfNotDefault( appendYGValueIfNotDefault(
j, j["style"],
key + "-start", key + "-start",
(*Field)(node, YGEdgeStart), (*Field)(node, YGEdgeStart),
(*Field)(defaultNode, YGEdgeStart)); (*Field)(defaultNode, YGEdgeStart));
appendYGValueIfNotDefault( appendYGValueIfNotDefault(
j, j["style"],
key + "-end", key + "-end",
(*Field)(node, YGEdgeEnd), (*Field)(node, YGEdgeEnd),
(*Field)(defaultNode, YGEdgeEnd)); (*Field)(defaultNode, YGEdgeEnd));
appendYGValueIfNotDefault( appendYGValueIfNotDefault(
j, j["style"],
key + "-vertical", key + "-vertical",
(*Field)(node, YGEdgeVertical), (*Field)(node, YGEdgeVertical),
(*Field)(defaultNode, YGEdgeVertical)); (*Field)(defaultNode, YGEdgeVertical));
appendYGValueIfNotDefault( appendYGValueIfNotDefault(
j, j["style"],
key + "-horizontal", key + "-horizontal",
(*Field)(node, YGEdgeHorizontal), (*Field)(node, YGEdgeHorizontal),
(*Field)(defaultNode, YGEdgeHorizontal)); (*Field)(defaultNode, YGEdgeHorizontal));
@@ -109,76 +119,79 @@ YGValue borderFloatToYGValue(YGNodeRef node, YGEdge edge) {
return YGValue{val, unit}; return YGValue{val, unit};
} }
static void nodeToStringImpl(json& j, YGNodeRef node, YGPrintOptions options) { static void nodeToStringImpl(json& j, YGNodeRef node, PrintOptions options) {
if ((options & YGPrintOptionsLayout) == YGPrintOptionsLayout) { if ((options & PrintOptions::Layout) == PrintOptions::Layout) {
j["layout"]["width"] = YGNodeStyleGetWidth(node).value; j["layout"]["width"] = YGNodeStyleGetWidth(node).value;
j["layout"]["height"] = YGNodeStyleGetHeight(node).value; j["layout"]["height"] = YGNodeStyleGetHeight(node).value;
j["layout"]["top"] = YGNodeStyleGetPosition(node, YGEdgeTop).value; j["layout"]["top"] = YGNodeStyleGetPosition(node, YGEdgeTop).value;
j["layout"]["left"] = YGNodeStyleGetPosition(node, YGEdgeLeft).value; j["layout"]["left"] = YGNodeStyleGetPosition(node, YGEdgeLeft).value;
} }
if ((options & YGPrintOptionsStyle) == YGPrintOptionsStyle) { if ((options & PrintOptions::Style) == PrintOptions::Style) {
const YGNodeRef defaultNode = YGNodeNew(); const YGNodeRef defaultNode = YGNodeNew();
appendEnumValueIfNotDefault( appendEnumValueIfNotDefault(
j, j["style"],
"flex-direction", "flex-direction",
YGFlexDirectionToString(YGNodeStyleGetFlexDirection(node)), YGFlexDirectionToString(YGNodeStyleGetFlexDirection(node)),
YGFlexDirectionToString(YGNodeStyleGetFlexDirection(defaultNode))); YGFlexDirectionToString(YGNodeStyleGetFlexDirection(defaultNode)));
appendEnumValueIfNotDefault( appendEnumValueIfNotDefault(
j, j["style"],
"justify-content", "justify-content",
YGJustifyToString(YGNodeStyleGetJustifyContent(node)), YGJustifyToString(YGNodeStyleGetJustifyContent(node)),
YGJustifyToString(YGNodeStyleGetJustifyContent(defaultNode))); YGJustifyToString(YGNodeStyleGetJustifyContent(defaultNode)));
appendEnumValueIfNotDefault( appendEnumValueIfNotDefault(
j, j["style"],
"align-items", "align-items",
YGAlignToString(YGNodeStyleGetAlignItems(node)), YGAlignToString(YGNodeStyleGetAlignItems(node)),
YGAlignToString(YGNodeStyleGetAlignItems(defaultNode))); YGAlignToString(YGNodeStyleGetAlignItems(defaultNode)));
appendEnumValueIfNotDefault( appendEnumValueIfNotDefault(
j, j["style"],
"align-content", "align-content",
YGAlignToString(YGNodeStyleGetAlignContent(node)), YGAlignToString(YGNodeStyleGetAlignContent(node)),
YGAlignToString(YGNodeStyleGetAlignContent(defaultNode))); YGAlignToString(YGNodeStyleGetAlignContent(defaultNode)));
appendEnumValueIfNotDefault( appendEnumValueIfNotDefault(
j, j["style"],
"align-self", "align-self",
YGAlignToString(YGNodeStyleGetAlignSelf(node)), YGAlignToString(YGNodeStyleGetAlignSelf(node)),
YGAlignToString(YGNodeStyleGetAlignSelf(defaultNode))); YGAlignToString(YGNodeStyleGetAlignSelf(defaultNode)));
appendEnumValueIfNotDefault( appendEnumValueIfNotDefault(
j, j["style"],
"flex-wrap", "flex-wrap",
YGWrapToString(YGNodeStyleGetFlexWrap(node)), YGWrapToString(YGNodeStyleGetFlexWrap(node)),
YGWrapToString(YGNodeStyleGetFlexWrap(defaultNode))); YGWrapToString(YGNodeStyleGetFlexWrap(defaultNode)));
appendEnumValueIfNotDefault( appendEnumValueIfNotDefault(
j, j["style"],
"overflow", "overflow",
YGOverflowToString(YGNodeStyleGetOverflow(node)), YGOverflowToString(YGNodeStyleGetOverflow(node)),
YGOverflowToString(YGNodeStyleGetOverflow(defaultNode))); YGOverflowToString(YGNodeStyleGetOverflow(defaultNode)));
appendEnumValueIfNotDefault( appendEnumValueIfNotDefault(
j, j["style"],
"display", "display",
YGDisplayToString(YGNodeStyleGetDisplay(node)), YGDisplayToString(YGNodeStyleGetDisplay(node)),
YGDisplayToString(YGNodeStyleGetDisplay(defaultNode))); YGDisplayToString(YGNodeStyleGetDisplay(defaultNode)));
appendEnumValueIfNotDefault( appendEnumValueIfNotDefault(
j, j["style"],
"position-type", "position-type",
YGPositionTypeToString(YGNodeStyleGetPositionType(node)), YGPositionTypeToString(YGNodeStyleGetPositionType(node)),
YGPositionTypeToString(YGNodeStyleGetPositionType(defaultNode))); YGPositionTypeToString(YGNodeStyleGetPositionType(defaultNode)));
appendFloatIfNotDefault( appendFloatIfNotDefault(
j, j["style"],
"flex-grow", "flex-grow",
YGNodeStyleGetFlexGrow(node), YGNodeStyleGetFlexGrow(node),
YGNodeStyleGetFlexGrow(defaultNode)); YGNodeStyleGetFlexGrow(defaultNode));
appendFloatIfNotDefault( appendFloatIfNotDefault(
j, j["style"],
"flex-shrink", "flex-shrink",
YGNodeStyleGetFlexShrink(node), YGNodeStyleGetFlexShrink(node),
YGNodeStyleGetFlexShrink(defaultNode)); YGNodeStyleGetFlexShrink(defaultNode));
appendFloatIfNotDefault( appendFloatIfNotDefault(
j, "flex", YGNodeStyleGetFlex(node), YGNodeStyleGetFlex(defaultNode)); j["style"],
"flex",
YGNodeStyleGetFlex(node),
YGNodeStyleGetFlex(defaultNode));
appendYGValueIfNotDefault( appendYGValueIfNotDefault(
j, j["style"],
"flex-basis", "flex-basis",
YGNodeStyleGetFlexBasis(node), YGNodeStyleGetFlexBasis(node),
YGNodeStyleGetFlexBasis(defaultNode)); YGNodeStyleGetFlexBasis(defaultNode));
@@ -189,48 +202,48 @@ static void nodeToStringImpl(json& j, YGNodeRef node, YGPrintOptions options) {
appendEdges<&YGNodeStyleGetPosition>(j, "position", node, defaultNode); appendEdges<&YGNodeStyleGetPosition>(j, "position", node, defaultNode);
appendFloatIfNotDefault( appendFloatIfNotDefault(
j, j["style"],
"gap", "gap",
YGNodeStyleGetGap(node, YGGutterAll), YGNodeStyleGetGap(node, YGGutterAll),
YGNodeStyleGetGap(defaultNode, YGGutterAll)); YGNodeStyleGetGap(defaultNode, YGGutterAll));
appendFloatIfNotDefault( appendFloatIfNotDefault(
j, j["style"],
"column-gap", "column-gap",
YGNodeStyleGetGap(node, YGGutterColumn), YGNodeStyleGetGap(node, YGGutterColumn),
YGNodeStyleGetGap(defaultNode, YGGutterColumn)); YGNodeStyleGetGap(defaultNode, YGGutterColumn));
appendFloatIfNotDefault( appendFloatIfNotDefault(
j, j["style"],
"row-gap", "row-gap",
YGNodeStyleGetGap(node, YGGutterRow), YGNodeStyleGetGap(node, YGGutterRow),
YGNodeStyleGetGap(defaultNode, YGGutterRow)); YGNodeStyleGetGap(defaultNode, YGGutterRow));
appendYGValueIfNotDefault( appendYGValueIfNotDefault(
j, j["style"],
"width", "width",
YGNodeStyleGetWidth(node), YGNodeStyleGetWidth(node),
YGNodeStyleGetWidth(defaultNode)); YGNodeStyleGetWidth(defaultNode));
appendYGValueIfNotDefault( appendYGValueIfNotDefault(
j, j["style"],
"height", "height",
YGNodeStyleGetHeight(node), YGNodeStyleGetHeight(node),
YGNodeStyleGetHeight(defaultNode)); YGNodeStyleGetHeight(defaultNode));
appendYGValueIfNotDefault( appendYGValueIfNotDefault(
j, j["style"],
"max-width", "max-width",
YGNodeStyleGetMaxWidth(node), YGNodeStyleGetMaxWidth(node),
YGNodeStyleGetMaxWidth(defaultNode)); YGNodeStyleGetMaxWidth(defaultNode));
appendYGValueIfNotDefault( appendYGValueIfNotDefault(
j, j["style"],
"max-height", "max-height",
YGNodeStyleGetMaxHeight(node), YGNodeStyleGetMaxHeight(node),
YGNodeStyleGetMaxHeight(defaultNode)); YGNodeStyleGetMaxHeight(defaultNode));
appendYGValueIfNotDefault( appendYGValueIfNotDefault(
j, j["style"],
"min-width", "min-width",
YGNodeStyleGetMinWidth(node), YGNodeStyleGetMinWidth(node),
YGNodeStyleGetMinWidth(defaultNode)); YGNodeStyleGetMinWidth(defaultNode));
appendYGValueIfNotDefault( appendYGValueIfNotDefault(
j, j["style"],
"min-height", "min-height",
YGNodeStyleGetMinHeight(node), YGNodeStyleGetMinHeight(node),
YGNodeStyleGetMinHeight(defaultNode)); YGNodeStyleGetMinHeight(defaultNode));
@@ -240,8 +253,41 @@ static void nodeToStringImpl(json& j, YGNodeRef node, YGPrintOptions options) {
} }
} }
if ((options & PrintOptions::Config) == PrintOptions::Config) {
YGConfigConstRef config = YGNodeGetConfig(node);
YGConfigConstRef defaultConfig = YGConfigGetDefault();
appendBoolIfNotDefault(
j["config"],
"use-web-defaults",
YGConfigGetUseWebDefaults(config),
YGConfigGetUseWebDefaults(defaultConfig));
appendFloatIfNotDefault(
j["config"],
"point-scale-factor",
YGConfigGetPointScaleFactor(config),
YGConfigGetPointScaleFactor(defaultConfig));
YGErrata errata = YGConfigGetErrata(config);
if (errata == YGErrataNone || errata == YGErrataAll ||
errata == YGErrataClassic) {
appendEnumValueIfNotDefault(
j["config"],
"errata",
YGErrataToString(errata),
YGErrataToString(YGConfigGetErrata(defaultConfig)));
}
if (YGConfigIsExperimentalFeatureEnabled(
config, YGExperimentalFeatureWebFlexBasis) !=
YGConfigIsExperimentalFeatureEnabled(
defaultConfig, YGExperimentalFeatureWebFlexBasis)) {
j["config"]["experimental-features"].push_back(
YGExperimentalFeatureToString(YGExperimentalFeatureWebFlexBasis));
}
}
const size_t childCount = YGNodeGetChildCount(node); const size_t childCount = YGNodeGetChildCount(node);
if ((options & YGPrintOptionsChildren) == YGPrintOptionsChildren && if ((options & PrintOptions::Children) == PrintOptions::Children &&
childCount > 0) { childCount > 0) {
for (size_t i = 0; i < childCount; i++) { for (size_t i = 0; i < childCount; i++) {
j["children"].push_back({}); j["children"].push_back({});
@@ -250,7 +296,7 @@ static void nodeToStringImpl(json& j, YGNodeRef node, YGPrintOptions options) {
} }
} }
void nodeToString(std::string& str, YGNodeRef node, YGPrintOptions options) { void nodeToString(std::string& str, YGNodeRef node, PrintOptions options) {
json j; json j;
nodeToStringImpl(j, node, options); nodeToStringImpl(j, node, options);
str = j.dump(2); str = j.dump(2);

View File

@@ -13,6 +13,14 @@
namespace facebook::yoga { namespace facebook::yoga {
void nodeToString(std::string& str, YGNodeRef node, YGPrintOptions options); enum class PrintOptions : uint8_t {
Layout = 1 << 0,
Children = 1 << 1,
Style = 1 << 2,
Config = 1 << 3,
};
YG_DEFINE_ENUM_FLAG_OPERATORS(PrintOptions);
void nodeToString(std::string& str, YGNodeRef node, PrintOptions options);
} // namespace facebook::yoga } // namespace facebook::yoga

View File

@@ -53,11 +53,6 @@ ENUMS = {
# Mimic web flex-basis behavior (experiment may be broken) # Mimic web flex-basis behavior (experiment may be broken)
"WebFlexBasis", "WebFlexBasis",
], ],
"PrintOptions": [
("Layout", 1 << 0),
("Style", 1 << 1),
("Children", 1 << 2),
],
"Gutter": ["Column", "Row", "All"], "Gutter": ["Column", "Row", "All"],
# Known incorrect behavior which can be enabled for compatibility # Known incorrect behavior which can be enabled for compatibility
"Errata": [ "Errata": [
@@ -82,7 +77,7 @@ ENUMS = {
DO_NOT_STRIP = ["LogLevel"] DO_NOT_STRIP = ["LogLevel"]
BITSET_ENUMS = ["PrintOptions", "Errata"] BITSET_ENUMS = ["Errata"]
def get_license(ext): def get_license(ext):

View File

@@ -1,35 +0,0 @@
/*
* 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.
*/
// @generated by enums.py
package com.facebook.yoga;
public enum YogaPrintOptions {
LAYOUT(1),
STYLE(2),
CHILDREN(4);
private final int mIntValue;
YogaPrintOptions(int intValue) {
mIntValue = intValue;
}
public int intValue() {
return mIntValue;
}
public static YogaPrintOptions fromInt(int value) {
switch (value) {
case 1: return LAYOUT;
case 2: return STYLE;
case 4: return CHILDREN;
default: throw new IllegalArgumentException("Unknown enum value: " + value);
}
}
}

View File

@@ -114,12 +114,6 @@ export enum PositionType {
Absolute = 2, Absolute = 2,
} }
export enum PrintOptions {
Layout = 1,
Style = 2,
Children = 4,
}
export enum Unit { export enum Unit {
Undefined = 0, Undefined = 0,
Point = 1, Point = 1,
@@ -196,9 +190,6 @@ const constants = {
POSITION_TYPE_STATIC: PositionType.Static, POSITION_TYPE_STATIC: PositionType.Static,
POSITION_TYPE_RELATIVE: PositionType.Relative, POSITION_TYPE_RELATIVE: PositionType.Relative,
POSITION_TYPE_ABSOLUTE: PositionType.Absolute, POSITION_TYPE_ABSOLUTE: PositionType.Absolute,
PRINT_OPTIONS_LAYOUT: PrintOptions.Layout,
PRINT_OPTIONS_STYLE: PrintOptions.Style,
PRINT_OPTIONS_CHILDREN: PrintOptions.Children,
UNIT_UNDEFINED: Unit.Undefined, UNIT_UNDEFINED: Unit.Undefined,
UNIT_POINT: Unit.Point, UNIT_POINT: Unit.Point,
UNIT_PERCENT: Unit.Percent, UNIT_PERCENT: Unit.Percent,

View File

@@ -223,18 +223,6 @@ const char* YGPositionTypeToString(const YGPositionType value) {
return "unknown"; return "unknown";
} }
const char* YGPrintOptionsToString(const YGPrintOptions value) {
switch (value) {
case YGPrintOptionsLayout:
return "layout";
case YGPrintOptionsStyle:
return "style";
case YGPrintOptionsChildren:
return "children";
}
return "unknown";
}
const char* YGUnitToString(const YGUnit value) { const char* YGUnitToString(const YGUnit value) {
switch (value) { switch (value) {
case YGUnitUndefined: case YGUnitUndefined:

View File

@@ -120,13 +120,6 @@ YG_ENUM_DECL(
YGPositionTypeRelative, YGPositionTypeRelative,
YGPositionTypeAbsolute) YGPositionTypeAbsolute)
YG_ENUM_DECL(
YGPrintOptions,
YGPrintOptionsLayout = 1,
YGPrintOptionsStyle = 2,
YGPrintOptionsChildren = 4)
YG_DEFINE_ENUM_FLAG_OPERATORS(YGPrintOptions)
YG_ENUM_DECL( YG_ENUM_DECL(
YGUnit, YGUnit,
YGUnitUndefined, YGUnitUndefined,

View File

@@ -1,38 +0,0 @@
/*
* 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.
*/
// @generated by enums.py
// clang-format off
#pragma once
#include <cstdint>
#include <yoga/YGEnums.h>
#include <yoga/enums/YogaEnums.h>
namespace facebook::yoga {
enum class PrintOptions : uint32_t {
Layout = YGPrintOptionsLayout,
Style = YGPrintOptionsStyle,
Children = YGPrintOptionsChildren,
};
YG_DEFINE_ENUM_FLAG_OPERATORS(PrintOptions)
constexpr PrintOptions scopedEnum(YGPrintOptions unscoped) {
return static_cast<PrintOptions>(unscoped);
}
constexpr YGPrintOptions unscopedEnum(PrintOptions scoped) {
return static_cast<YGPrintOptions>(scoped);
}
inline const char* toString(PrintOptions e) {
return YGPrintOptionsToString(unscopedEnum(e));
}
} // namespace facebook::yoga