From 046d86e296acd606ecb8a959b77dab4e5eb5b409 Mon Sep 17 00:00:00 2001 From: Reinier Hartog Date: Mon, 31 Dec 2018 13:16:37 +0100 Subject: [PATCH 01/18] Expose dirtied func in Javascript bindings --- javascript/sources/Node.cc | 25 +++++++++++++++++++++++++ javascript/sources/Node.hh | 11 ++++++++++- javascript/sources/nbind.cc | 3 +++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/javascript/sources/Node.cc b/javascript/sources/Node.cc index 108bc4b5..2268c805 100644 --- a/javascript/sources/Node.cc +++ b/javascript/sources/Node.cc @@ -23,6 +23,13 @@ static YGSize globalMeasureFunc(YGNodeRef nodeRef, float width, YGMeasureMode wi return ygSize; } +static void globalDirtiedFunc(YGNodeRef nodeRef) +{ + Node const & node = *reinterpret_cast(YGNodeGetContext(nodeRef)); + + node.callDirtiedFunc(); +} + /* static */ Node * Node::createDefault(void) { return new Node(nullptr); @@ -429,6 +436,24 @@ Size Node::callMeasureFunc(double width, int widthMode, double height, int heigh return m_measureFunc->call(width, widthMode, height, heightMode); } +void Node::setDirtiedFunc(nbind::cbFunction & dirtiedFunc) +{ + m_dirtiedFunc.reset(new nbind::cbFunction(dirtiedFunc)); + + YGNodeSetDirtiedFunc(m_node, &globalDirtiedFunc); +} + +void Node::unsetDirtiedFunc(void) { + m_dirtiedFunc.reset(nullptr); + + YGNodeSetDirtiedFunc(m_node, nullptr); +} + +void Node::callDirtiedFunc(void) const +{ + m_dirtiedFunc->call(); +} + void Node::markDirty(void) { YGNodeMarkDirty(m_node); diff --git a/javascript/sources/Node.hh b/javascript/sources/Node.hh index 593745b2..a3114505 100644 --- a/javascript/sources/Node.hh +++ b/javascript/sources/Node.hh @@ -161,6 +161,15 @@ class Node { Size callMeasureFunc(double width, int widthMode, double height, int heightMode) const; + public: // Dirtied func mutators + + void setDirtiedFunc(nbind::cbFunction & dirtiedFunc); + void unsetDirtiedFunc(void); + + public: // Dirtied func inspectors + + void callDirtiedFunc(void) const; + public: // Dirtiness accessors void markDirty(void); @@ -194,5 +203,5 @@ class Node { YGNodeRef m_node; std::unique_ptr m_measureFunc; - + std::unique_ptr m_dirtiedFunc; }; diff --git a/javascript/sources/nbind.cc b/javascript/sources/nbind.cc index c778fe8a..7f1aac0f 100644 --- a/javascript/sources/nbind.cc +++ b/javascript/sources/nbind.cc @@ -155,6 +155,9 @@ NBIND_CLASS(Node) method(setMeasureFunc); method(unsetMeasureFunc); + method(setDirtiedFunc); + method(unsetDirtiedFunc); + method(markDirty); method(isDirty); -- 2.50.1.windows.1 From b66642cb863c12ab1c2c1227b32302adee7df374 Mon Sep 17 00:00:00 2001 From: Sidharth Guglani Date: Fri, 4 Jan 2019 06:02:41 -0800 Subject: [PATCH 02/18] fixed a csharp test case Summary: Using default logger in test case Reviewed By: davidaurelio Differential Revision: D13565071 fbshipit-source-id: 93cee223a7f366e3e965fde2d5cdc7d4b27e310f --- csharp/tests/Facebook.Yoga/YogaNodeTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/csharp/tests/Facebook.Yoga/YogaNodeTest.cs b/csharp/tests/Facebook.Yoga/YogaNodeTest.cs index eb9f1e8d..69332735 100644 --- a/csharp/tests/Facebook.Yoga/YogaNodeTest.cs +++ b/csharp/tests/Facebook.Yoga/YogaNodeTest.cs @@ -259,7 +259,7 @@ namespace Facebook.Yoga [Test] public void TestPrintWithLogger() { - YogaNode node = new YogaNode(new YogaConfig{Logger = (c, n, l, m) => {}}); + YogaNode node = new YogaNode(); node.Width = 110; node.Height = 105; node.CalculateLayout(); -- 2.50.1.windows.1 From 899664dddd39eeee2f784c2671bc9bdb22005b54 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Tue, 8 Jan 2019 09:27:46 -0800 Subject: [PATCH 03/18] Remove stray file Summary: @public Remove accidental copy of `Yoga-internal.h` Reviewed By: SidharthGuglani Differential Revision: D13596615 fbshipit-source-id: c137b45caa9c55f75f9bd199be535f1b08977249 --- fbcode/xplat/yoga/yoga/Yoga-internal.h | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 fbcode/xplat/yoga/yoga/Yoga-internal.h diff --git a/fbcode/xplat/yoga/yoga/Yoga-internal.h b/fbcode/xplat/yoga/yoga/Yoga-internal.h deleted file mode 100644 index e69de29b..00000000 -- 2.50.1.windows.1 From 5ee32fbefcccb580d93d7cfe60b552e01f9f5bc4 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Tue, 8 Jan 2019 12:47:53 -0800 Subject: [PATCH 04/18] Add `.clang-format` Summary: @public Add `.clang-format` for automatic formatting of our C++ code. Reviewed By: SidharthGuglani Differential Revision: D13596651 fbshipit-source-id: c3f866ad552cd7a1d5824f43b093f45842a2142e --- .clang-format | 56 ++++++++++++++++++++++++++++++++++++++++++++ .clang-format-ignore | 1 + 2 files changed, 57 insertions(+) create mode 100644 .clang-format create mode 100644 .clang-format-ignore diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..52a48cf2 --- /dev/null +++ b/.clang-format @@ -0,0 +1,56 @@ +Language: Cpp +AccessModifierOffset: -2 +AlignAfterOpenBracket: AlwaysBreak +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Left +AlignOperands: false +AlignTrailingComments: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: Yes +BinPackArguments: false +BinPackParameters: false +BreakBeforeBinaryOperators: false +BreakBeforeBraces: Attach +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +ColumnLimit: 80 +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +FixNamespaceComments: true +IndentCaseLabels: true +IndentPPDirectives: None +IndentWidth: 2 +IndentWrappedFunctionNames: false +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +PenaltyReturnTypeOnItsOwnLine: 2000 +PointerAlignment: Left +ReflowComments: true +SortIncludes: false +SortUsingDeclarations: true +SpaceAfterCStyleCast: true +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +Standard: Cpp11 +UseTab: Never diff --git a/.clang-format-ignore b/.clang-format-ignore new file mode 100644 index 00000000..51430a06 --- /dev/null +++ b/.clang-format-ignore @@ -0,0 +1 @@ +^lib/.* -- 2.50.1.windows.1 From ab9d06abf595448983d5a120003312abc2140ad6 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Tue, 8 Jan 2019 12:47:53 -0800 Subject: [PATCH 05/18] Apply clang-format rules Summary: @public Formats Yoga's source according to our clang-format configuration Reviewed By: SidharthGuglani Differential Revision: D13596650 fbshipit-source-id: c3722d4eafd63b7596a8b1e85c0197e9d2d6cb7d --- benchmark/YGBenchmark.c | 33 ++++---- java/jni/YGJNI.cpp | 28 ++++--- yoga/CompactValue.h | 4 +- yoga/YGEnums.cpp | 64 +++++++-------- yoga/YGEnums.h | 173 ++++++++++++++++++++-------------------- yoga/YGFloatOptional.h | 4 +- yoga/YGLayout.h | 2 +- yoga/YGMacros.h | 8 +- yoga/YGNode.cpp | 3 +- yoga/YGNode.h | 4 +- yoga/YGNodePrint.cpp | 12 ++- yoga/YGStyle.h | 4 +- yoga/YGValue.h | 2 +- yoga/Yoga-internal.h | 8 +- yoga/Yoga.cpp | 8 +- yoga/Yoga.h | 7 +- 16 files changed, 188 insertions(+), 176 deletions(-) diff --git a/benchmark/YGBenchmark.c b/benchmark/YGBenchmark.c index 7256e8f3..2b534378 100644 --- a/benchmark/YGBenchmark.c +++ b/benchmark/YGBenchmark.c @@ -15,7 +15,7 @@ #define NUM_REPETITIONS 1000 #define YGBENCHMARKS(BLOCK) \ - int main(int argc, char const *argv[]) { \ + int main(int argc, char const* argv[]) { \ clock_t __start; \ clock_t __endTimes[NUM_REPETITIONS]; \ { BLOCK } \ @@ -25,14 +25,13 @@ #define YGBENCHMARK(NAME, BLOCK) \ __start = clock(); \ for (uint32_t __i = 0; __i < NUM_REPETITIONS; __i++) { \ - { BLOCK } \ - __endTimes[__i] = clock(); \ + {BLOCK} __endTimes[__i] = clock(); \ } \ __printBenchmarkResult(NAME, __start, __endTimes); -static int __compareDoubles(const void *a, const void *b) { - double arg1 = *(const double *) a; - double arg2 = *(const double *) b; +static int __compareDoubles(const void* a, const void* b) { + double arg1 = *(const double*) a; + double arg2 = *(const double*) b; if (arg1 < arg2) { return -1; @@ -45,7 +44,10 @@ static int __compareDoubles(const void *a, const void *b) { return 0; } -static void __printBenchmarkResult(char *name, clock_t start, clock_t *endTimes) { +static void __printBenchmarkResult( + char* name, + clock_t start, + clock_t* endTimes) { double timesInMs[NUM_REPETITIONS]; double mean = 0; clock_t lastEnd = start; @@ -69,12 +71,12 @@ static void __printBenchmarkResult(char *name, clock_t start, clock_t *endTimes) printf("%s: median: %lf ms, stddev: %lf ms\n", name, median, stddev); } - -static YGSize _measure(YGNodeRef node, - float width, - YGMeasureMode widthMode, - float height, - YGMeasureMode heightMode) { +static YGSize _measure( + YGNodeRef node, + float width, + YGMeasureMode widthMode, + float height, + YGMeasureMode heightMode) { return (YGSize){ .width = widthMode == YGMeasureModeUndefined ? 10 : width, .height = heightMode == YGMeasureModeUndefined ? 10 : width, @@ -82,7 +84,6 @@ static YGSize _measure(YGNodeRef node, } YGBENCHMARKS({ - YGBENCHMARK("Stack with flex", { const YGNodeRef root = YGNodeNew(); YGNodeStyleSetWidth(root, 100); @@ -160,7 +161,8 @@ YGBENCHMARKS({ for (uint32_t iiii = 0; iiii < 10; iiii++) { const YGNodeRef grandGrandGrandChild = YGNodeNew(); - YGNodeStyleSetFlexDirection(grandGrandGrandChild, YGFlexDirectionRow); + YGNodeStyleSetFlexDirection( + grandGrandGrandChild, YGFlexDirectionRow); YGNodeStyleSetFlexGrow(grandGrandGrandChild, 1); YGNodeStyleSetWidth(grandGrandGrandChild, 10); YGNodeStyleSetHeight(grandGrandGrandChild, 10); @@ -173,5 +175,4 @@ YGBENCHMARKS({ YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); YGNodeFreeRecursive(root); }); - }); diff --git a/java/jni/YGJNI.cpp b/java/jni/YGJNI.cpp index fb4ec3c6..ea0d9d07 100644 --- a/java/jni/YGJNI.cpp +++ b/java/jni/YGJNI.cpp @@ -100,7 +100,7 @@ static void YGTransferLayoutOutputsRecursive(YGNodeRef root) { const int PADDING = 2; const int BORDER = 4; - int hasEdgeSetFlag = (int)obj->getFieldValue(edgeSetFlagField); + int hasEdgeSetFlag = (int) obj->getFieldValue(edgeSetFlagField); obj->setFieldValue(widthField, YGNodeLayoutGetWidth(root)); obj->setFieldValue(heightField, YGNodeLayoutGetHeight(root)); @@ -180,8 +180,10 @@ static inline YGConfigRef _jlong2YGConfigRef(jlong addr) { return reinterpret_cast(static_cast(addr)); } -static YGNodeRef -YGJNIOnNodeClonedFunc(YGNodeRef oldNode, YGNodeRef owner, int childIndex) { +static YGNodeRef YGJNIOnNodeClonedFunc( + YGNodeRef oldNode, + YGNodeRef owner, + int childIndex) { auto config = oldNode->getConfig(); if (!config) { return nullptr; @@ -404,7 +406,7 @@ void jni_YGNodeMarkDirtyAndPropogateToDescendants(jlong nativePointer) { } jboolean jni_YGNodeIsDirty(jlong nativePointer) { - return (jboolean)_jlong2YGNodeRef(nativePointer)->isDirty(); + return (jboolean) _jlong2YGNodeRef(nativePointer)->isDirty(); } void jni_YGNodeSetHasMeasureFunc(jlong nativePointer, jboolean hasMeasureFunc) { @@ -432,14 +434,14 @@ struct JYogaValue : public JavaClass { } }; -#define YG_NODE_JNI_STYLE_PROP(javatype, type, name) \ - javatype jni_YGNodeStyleGet##name(jlong nativePointer) { \ - return (javatype)YGNodeStyleGet##name(_jlong2YGNodeRef(nativePointer)); \ - } \ - \ - void jni_YGNodeStyleSet##name(jlong nativePointer, javatype value) { \ - YGNodeStyleSet##name( \ - _jlong2YGNodeRef(nativePointer), static_cast(value)); \ +#define YG_NODE_JNI_STYLE_PROP(javatype, type, name) \ + javatype jni_YGNodeStyleGet##name(jlong nativePointer) { \ + return (javatype) YGNodeStyleGet##name(_jlong2YGNodeRef(nativePointer)); \ + } \ + \ + void jni_YGNodeStyleSet##name(jlong nativePointer, javatype value) { \ + YGNodeStyleSet##name( \ + _jlong2YGNodeRef(nativePointer), static_cast(value)); \ } #define YG_NODE_JNI_STYLE_UNIT_PROP(name) \ @@ -467,7 +469,7 @@ struct JYogaValue : public JavaClass { #define YG_NODE_JNI_STYLE_EDGE_PROP(javatype, type, name) \ javatype jni_YGNodeStyleGet##name(jlong nativePointer, jint edge) { \ - return (javatype)YGNodeStyleGet##name( \ + return (javatype) YGNodeStyleGet##name( \ _jlong2YGNodeRef(nativePointer), static_cast(edge)); \ } \ \ diff --git a/yoga/CompactValue.h b/yoga/CompactValue.h index 7120f1c1..22b9624e 100644 --- a/yoga/CompactValue.h +++ b/yoga/CompactValue.h @@ -42,7 +42,7 @@ namespace detail { class CompactValue { friend constexpr bool operator==(CompactValue, CompactValue) noexcept; - public: +public: static constexpr auto LOWER_BOUND = 1.08420217e-19f; static constexpr auto UPPER_BOUND_POINT = 36893485948395847680.0f; static constexpr auto UPPER_BOUND_PERCENT = 18446742974197923840.0f; @@ -137,7 +137,7 @@ class CompactValue { return payload_.repr == AUTO_BITS; } - private: +private: union Payload { float value; uint32_t repr; diff --git a/yoga/YGEnums.cpp b/yoga/YGEnums.cpp index e2518176..ff4b1307 100644 --- a/yoga/YGEnums.cpp +++ b/yoga/YGEnums.cpp @@ -6,8 +6,8 @@ */ #include "YGEnums.h" -const char *YGAlignToString(const YGAlign value){ - switch(value){ +const char* YGAlignToString(const YGAlign value) { + switch (value) { case YGAlignAuto: return "auto"; case YGAlignFlexStart: @@ -28,8 +28,8 @@ const char *YGAlignToString(const YGAlign value){ return "unknown"; } -const char *YGDimensionToString(const YGDimension value){ - switch(value){ +const char* YGDimensionToString(const YGDimension value) { + switch (value) { case YGDimensionWidth: return "width"; case YGDimensionHeight: @@ -38,8 +38,8 @@ const char *YGDimensionToString(const YGDimension value){ return "unknown"; } -const char *YGDirectionToString(const YGDirection value){ - switch(value){ +const char* YGDirectionToString(const YGDirection value) { + switch (value) { case YGDirectionInherit: return "inherit"; case YGDirectionLTR: @@ -50,8 +50,8 @@ const char *YGDirectionToString(const YGDirection value){ return "unknown"; } -const char *YGDisplayToString(const YGDisplay value){ - switch(value){ +const char* YGDisplayToString(const YGDisplay value) { + switch (value) { case YGDisplayFlex: return "flex"; case YGDisplayNone: @@ -60,8 +60,8 @@ const char *YGDisplayToString(const YGDisplay value){ return "unknown"; } -const char *YGEdgeToString(const YGEdge value){ - switch(value){ +const char* YGEdgeToString(const YGEdge value) { + switch (value) { case YGEdgeLeft: return "left"; case YGEdgeTop: @@ -84,16 +84,16 @@ const char *YGEdgeToString(const YGEdge value){ return "unknown"; } -const char *YGExperimentalFeatureToString(const YGExperimentalFeature value){ - switch(value){ +const char* YGExperimentalFeatureToString(const YGExperimentalFeature value) { + switch (value) { case YGExperimentalFeatureWebFlexBasis: return "web-flex-basis"; } return "unknown"; } -const char *YGFlexDirectionToString(const YGFlexDirection value){ - switch(value){ +const char* YGFlexDirectionToString(const YGFlexDirection value) { + switch (value) { case YGFlexDirectionColumn: return "column"; case YGFlexDirectionColumnReverse: @@ -106,8 +106,8 @@ const char *YGFlexDirectionToString(const YGFlexDirection value){ return "unknown"; } -const char *YGJustifyToString(const YGJustify value){ - switch(value){ +const char* YGJustifyToString(const YGJustify value) { + switch (value) { case YGJustifyFlexStart: return "flex-start"; case YGJustifyCenter: @@ -124,8 +124,8 @@ const char *YGJustifyToString(const YGJustify value){ return "unknown"; } -const char *YGLogLevelToString(const YGLogLevel value){ - switch(value){ +const char* YGLogLevelToString(const YGLogLevel value) { + switch (value) { case YGLogLevelError: return "error"; case YGLogLevelWarn: @@ -142,8 +142,8 @@ const char *YGLogLevelToString(const YGLogLevel value){ return "unknown"; } -const char *YGMeasureModeToString(const YGMeasureMode value){ - switch(value){ +const char* YGMeasureModeToString(const YGMeasureMode value) { + switch (value) { case YGMeasureModeUndefined: return "undefined"; case YGMeasureModeExactly: @@ -154,8 +154,8 @@ const char *YGMeasureModeToString(const YGMeasureMode value){ return "unknown"; } -const char *YGNodeTypeToString(const YGNodeType value){ - switch(value){ +const char* YGNodeTypeToString(const YGNodeType value) { + switch (value) { case YGNodeTypeDefault: return "default"; case YGNodeTypeText: @@ -164,8 +164,8 @@ const char *YGNodeTypeToString(const YGNodeType value){ return "unknown"; } -const char *YGOverflowToString(const YGOverflow value){ - switch(value){ +const char* YGOverflowToString(const YGOverflow value) { + switch (value) { case YGOverflowVisible: return "visible"; case YGOverflowHidden: @@ -176,8 +176,8 @@ const char *YGOverflowToString(const YGOverflow value){ return "unknown"; } -const char *YGPositionTypeToString(const YGPositionType value){ - switch(value){ +const char* YGPositionTypeToString(const YGPositionType value) { + switch (value) { case YGPositionTypeRelative: return "relative"; case YGPositionTypeAbsolute: @@ -186,8 +186,8 @@ const char *YGPositionTypeToString(const YGPositionType value){ return "unknown"; } -const char *YGPrintOptionsToString(const YGPrintOptions value){ - switch(value){ +const char* YGPrintOptionsToString(const YGPrintOptions value) { + switch (value) { case YGPrintOptionsLayout: return "layout"; case YGPrintOptionsStyle: @@ -198,8 +198,8 @@ const char *YGPrintOptionsToString(const YGPrintOptions value){ return "unknown"; } -const char *YGUnitToString(const YGUnit value){ - switch(value){ +const char* YGUnitToString(const YGUnit value) { + switch (value) { case YGUnitUndefined: return "undefined"; case YGUnitPoint: @@ -212,8 +212,8 @@ const char *YGUnitToString(const YGUnit value){ return "unknown"; } -const char *YGWrapToString(const YGWrap value){ - switch(value){ +const char* YGWrapToString(const YGWrap value) { + switch (value) { case YGWrapNoWrap: return "no-wrap"; case YGWrapWrap: diff --git a/yoga/YGEnums.h b/yoga/YGEnums.h index da223051..222f0ed0 100644 --- a/yoga/YGEnums.h +++ b/yoga/YGEnums.h @@ -11,68 +11,69 @@ YG_EXTERN_C_BEGIN #define YGAlignCount 8 -typedef YG_ENUM_BEGIN(YGAlign) { - YGAlignAuto, - YGAlignFlexStart, - YGAlignCenter, - YGAlignFlexEnd, - YGAlignStretch, - YGAlignBaseline, - YGAlignSpaceBetween, - YGAlignSpaceAround, +typedef YG_ENUM_BEGIN(YGAlign){ + YGAlignAuto, + YGAlignFlexStart, + YGAlignCenter, + YGAlignFlexEnd, + YGAlignStretch, + YGAlignBaseline, + YGAlignSpaceBetween, + YGAlignSpaceAround, } YG_ENUM_END(YGAlign); -WIN_EXPORT const char *YGAlignToString(const YGAlign value); +WIN_EXPORT const char* YGAlignToString(const YGAlign value); #define YGDimensionCount 2 -typedef YG_ENUM_BEGIN(YGDimension) { - YGDimensionWidth, - YGDimensionHeight, +typedef YG_ENUM_BEGIN(YGDimension){ + YGDimensionWidth, + YGDimensionHeight, } YG_ENUM_END(YGDimension); -WIN_EXPORT const char *YGDimensionToString(const YGDimension value); +WIN_EXPORT const char* YGDimensionToString(const YGDimension value); #define YGDirectionCount 3 -typedef YG_ENUM_BEGIN(YGDirection) { - YGDirectionInherit, - YGDirectionLTR, - YGDirectionRTL, +typedef YG_ENUM_BEGIN(YGDirection){ + YGDirectionInherit, + YGDirectionLTR, + YGDirectionRTL, } YG_ENUM_END(YGDirection); -WIN_EXPORT const char *YGDirectionToString(const YGDirection value); +WIN_EXPORT const char* YGDirectionToString(const YGDirection value); #define YGDisplayCount 2 -typedef YG_ENUM_BEGIN(YGDisplay) { - YGDisplayFlex, - YGDisplayNone, +typedef YG_ENUM_BEGIN(YGDisplay){ + YGDisplayFlex, + YGDisplayNone, } YG_ENUM_END(YGDisplay); -WIN_EXPORT const char *YGDisplayToString(const YGDisplay value); +WIN_EXPORT const char* YGDisplayToString(const YGDisplay value); #define YGEdgeCount 9 -typedef YG_ENUM_BEGIN(YGEdge) { - YGEdgeLeft, - YGEdgeTop, - YGEdgeRight, - YGEdgeBottom, - YGEdgeStart, - YGEdgeEnd, - YGEdgeHorizontal, - YGEdgeVertical, - YGEdgeAll, +typedef YG_ENUM_BEGIN(YGEdge){ + YGEdgeLeft, + YGEdgeTop, + YGEdgeRight, + YGEdgeBottom, + YGEdgeStart, + YGEdgeEnd, + YGEdgeHorizontal, + YGEdgeVertical, + YGEdgeAll, } YG_ENUM_END(YGEdge); -WIN_EXPORT const char *YGEdgeToString(const YGEdge value); +WIN_EXPORT const char* YGEdgeToString(const YGEdge value); #define YGExperimentalFeatureCount 1 -typedef YG_ENUM_BEGIN(YGExperimentalFeature) { - YGExperimentalFeatureWebFlexBasis, +typedef YG_ENUM_BEGIN(YGExperimentalFeature){ + YGExperimentalFeatureWebFlexBasis, } YG_ENUM_END(YGExperimentalFeature); -WIN_EXPORT const char *YGExperimentalFeatureToString(const YGExperimentalFeature value); +WIN_EXPORT const char* YGExperimentalFeatureToString( + const YGExperimentalFeature value); #define YGFlexDirectionCount 4 -typedef YG_ENUM_BEGIN(YGFlexDirection) { - YGFlexDirectionColumn, - YGFlexDirectionColumnReverse, - YGFlexDirectionRow, - YGFlexDirectionRowReverse, +typedef YG_ENUM_BEGIN(YGFlexDirection){ + YGFlexDirectionColumn, + YGFlexDirectionColumnReverse, + YGFlexDirectionRow, + YGFlexDirectionRowReverse, } YG_ENUM_END(YGFlexDirection); -WIN_EXPORT const char *YGFlexDirectionToString(const YGFlexDirection value); +WIN_EXPORT const char* YGFlexDirectionToString(const YGFlexDirection value); #define YGJustifyCount 6 typedef YG_ENUM_BEGIN(YGJustify){ @@ -83,72 +84,72 @@ typedef YG_ENUM_BEGIN(YGJustify){ YGJustifySpaceAround, YGJustifySpaceEvenly, } YG_ENUM_END(YGJustify); -WIN_EXPORT const char *YGJustifyToString(const YGJustify value); +WIN_EXPORT const char* YGJustifyToString(const YGJustify value); #define YGLogLevelCount 6 -typedef YG_ENUM_BEGIN(YGLogLevel) { - YGLogLevelError, - YGLogLevelWarn, - YGLogLevelInfo, - YGLogLevelDebug, - YGLogLevelVerbose, - YGLogLevelFatal, +typedef YG_ENUM_BEGIN(YGLogLevel){ + YGLogLevelError, + YGLogLevelWarn, + YGLogLevelInfo, + YGLogLevelDebug, + YGLogLevelVerbose, + YGLogLevelFatal, } YG_ENUM_END(YGLogLevel); -WIN_EXPORT const char *YGLogLevelToString(const YGLogLevel value); +WIN_EXPORT const char* YGLogLevelToString(const YGLogLevel value); #define YGMeasureModeCount 3 -typedef YG_ENUM_BEGIN(YGMeasureMode) { - YGMeasureModeUndefined, - YGMeasureModeExactly, - YGMeasureModeAtMost, +typedef YG_ENUM_BEGIN(YGMeasureMode){ + YGMeasureModeUndefined, + YGMeasureModeExactly, + YGMeasureModeAtMost, } YG_ENUM_END(YGMeasureMode); -WIN_EXPORT const char *YGMeasureModeToString(const YGMeasureMode value); +WIN_EXPORT const char* YGMeasureModeToString(const YGMeasureMode value); #define YGNodeTypeCount 2 -typedef YG_ENUM_BEGIN(YGNodeType) { - YGNodeTypeDefault, - YGNodeTypeText, +typedef YG_ENUM_BEGIN(YGNodeType){ + YGNodeTypeDefault, + YGNodeTypeText, } YG_ENUM_END(YGNodeType); -WIN_EXPORT const char *YGNodeTypeToString(const YGNodeType value); +WIN_EXPORT const char* YGNodeTypeToString(const YGNodeType value); #define YGOverflowCount 3 -typedef YG_ENUM_BEGIN(YGOverflow) { - YGOverflowVisible, - YGOverflowHidden, - YGOverflowScroll, +typedef YG_ENUM_BEGIN(YGOverflow){ + YGOverflowVisible, + YGOverflowHidden, + YGOverflowScroll, } YG_ENUM_END(YGOverflow); -WIN_EXPORT const char *YGOverflowToString(const YGOverflow value); +WIN_EXPORT const char* YGOverflowToString(const YGOverflow value); #define YGPositionTypeCount 2 -typedef YG_ENUM_BEGIN(YGPositionType) { - YGPositionTypeRelative, - YGPositionTypeAbsolute, +typedef YG_ENUM_BEGIN(YGPositionType){ + YGPositionTypeRelative, + YGPositionTypeAbsolute, } YG_ENUM_END(YGPositionType); -WIN_EXPORT const char *YGPositionTypeToString(const YGPositionType value); +WIN_EXPORT const char* YGPositionTypeToString(const YGPositionType value); #define YGPrintOptionsCount 3 -typedef YG_ENUM_BEGIN(YGPrintOptions) { - YGPrintOptionsLayout = 1, - YGPrintOptionsStyle = 2, - YGPrintOptionsChildren = 4, +typedef YG_ENUM_BEGIN(YGPrintOptions){ + YGPrintOptionsLayout = 1, + YGPrintOptionsStyle = 2, + YGPrintOptionsChildren = 4, } YG_ENUM_END(YGPrintOptions); -WIN_EXPORT const char *YGPrintOptionsToString(const YGPrintOptions value); +WIN_EXPORT const char* YGPrintOptionsToString(const YGPrintOptions value); #define YGUnitCount 4 -typedef YG_ENUM_BEGIN(YGUnit) { - YGUnitUndefined, - YGUnitPoint, - YGUnitPercent, - YGUnitAuto, +typedef YG_ENUM_BEGIN(YGUnit){ + YGUnitUndefined, + YGUnitPoint, + YGUnitPercent, + YGUnitAuto, } YG_ENUM_END(YGUnit); -WIN_EXPORT const char *YGUnitToString(const YGUnit value); +WIN_EXPORT const char* YGUnitToString(const YGUnit value); #define YGWrapCount 3 -typedef YG_ENUM_BEGIN(YGWrap) { - YGWrapNoWrap, - YGWrapWrap, - YGWrapWrapReverse, +typedef YG_ENUM_BEGIN(YGWrap){ + YGWrapNoWrap, + YGWrapWrap, + YGWrapWrapReverse, } YG_ENUM_END(YGWrap); -WIN_EXPORT const char *YGWrapToString(const YGWrap value); +WIN_EXPORT const char* YGWrapToString(const YGWrap value); YG_EXTERN_C_END diff --git a/yoga/YGFloatOptional.h b/yoga/YGFloatOptional.h index 44cf344e..02d4c856 100644 --- a/yoga/YGFloatOptional.h +++ b/yoga/YGFloatOptional.h @@ -11,10 +11,10 @@ #include "Yoga-internal.h" struct YGFloatOptional { - private: +private: float value_ = std::numeric_limits::quiet_NaN(); - public: +public: explicit constexpr YGFloatOptional(float value) : value_(value) {} constexpr YGFloatOptional() = default; diff --git a/yoga/YGLayout.h b/yoga/YGLayout.h index 94a92adf..4a68d86a 100644 --- a/yoga/YGLayout.h +++ b/yoga/YGLayout.h @@ -28,7 +28,7 @@ struct YGLayout { // Instead of recomputing the entire layout every single time, we // cache some information to break early when nothing changed uint32_t generationCount = 0; - YGDirection lastOwnerDirection = (YGDirection)-1; + YGDirection lastOwnerDirection = (YGDirection) -1; uint32_t nextCachedMeasurementsIndex = 0; std::array diff --git a/yoga/YGMacros.h b/yoga/YGMacros.h index 7da8a870..0e131902 100644 --- a/yoga/YGMacros.h +++ b/yoga/YGMacros.h @@ -21,7 +21,7 @@ #endif #ifdef WINARMDLL -#define WIN_STRUCT(type) type * +#define WIN_STRUCT(type) type* #define WIN_STRUCT_REF(value) &value #else #define WIN_STRUCT(type) type @@ -29,9 +29,9 @@ #endif #ifdef NS_ENUM -// Cannot use NSInteger as NSInteger has a different size than int (which is the default type of a -// enum). -// Therefor when linking the Yoga C library into obj-c the header is a missmatch for the Yoga ABI. +// Cannot use NSInteger as NSInteger has a different size than int (which is the +// default type of a enum). Therefor when linking the Yoga C library into obj-c +// the header is a missmatch for the Yoga ABI. #define YG_ENUM_BEGIN(name) NS_ENUM(int, name) #define YG_ENUM_END(name) #else diff --git a/yoga/YGNode.cpp b/yoga/YGNode.cpp index 8e7e210b..022e6067 100644 --- a/yoga/YGNode.cpp +++ b/yoga/YGNode.cpp @@ -113,7 +113,8 @@ void YGNode::setMeasureFunc(YGMeasureFunc measureFunc) { YGAssertWithNode( this, children_.size() == 0, - "Cannot set measure function: Nodes with measure functions cannot have children."); + "Cannot set measure function: Nodes with measure functions cannot have " + "children."); measure_ = measureFunc; // TODO: t18095186 Move nodeType to opt-in function and mark appropriate // places in Litho diff --git a/yoga/YGNode.h b/yoga/YGNode.h index 2c00ec75..db1e7d42 100644 --- a/yoga/YGNode.h +++ b/yoga/YGNode.h @@ -12,7 +12,7 @@ #include "Yoga-internal.h" struct YGNode { - private: +private: void* context_ = nullptr; YGPrintFunc print_ = nullptr; bool hasNewLayout_ : 1; @@ -35,7 +35,7 @@ struct YGNode { const YGFlexDirection axis, const float axisSize) const; - public: +public: YGNode() : hasNewLayout_(true), isReferenceBaseline_(false), diff --git a/yoga/YGNodePrint.cpp b/yoga/YGNodePrint.cpp index 4be2ff5a..1c0c2708 100644 --- a/yoga/YGNodePrint.cpp +++ b/yoga/YGNodePrint.cpp @@ -63,15 +63,19 @@ static void appendNumberIfNotUndefined( } } -static void -appendNumberIfNotAuto(string& base, const string& key, const YGValue number) { +static void appendNumberIfNotAuto( + string& base, + const string& key, + const YGValue number) { if (number.unit != YGUnitAuto) { appendNumberIfNotUndefined(base, key, number); } } -static void -appendNumberIfNotZero(string& base, const string& str, const YGValue number) { +static void appendNumberIfNotZero( + string& base, + const string& str, + const YGValue number) { if (number.unit == YGUnitAuto) { base.append(str + ": auto; "); } else if (!YGFloatsEqual(number.value, 0)) { diff --git a/yoga/YGStyle.h b/yoga/YGStyle.h index abcc7052..05d594b1 100644 --- a/yoga/YGStyle.h +++ b/yoga/YGStyle.h @@ -18,10 +18,10 @@ constexpr YGValue kYGValueUndefined = {0, YGUnitUndefined}; constexpr YGValue kYGValueAuto = {0, YGUnitAuto}; struct YGStyle { - private: +private: using CompactValue = facebook::yoga::detail::CompactValue; - public: +public: using Dimensions = facebook::yoga::detail::Values<2>; using Edges = facebook::yoga::detail::Values; diff --git a/yoga/YGValue.h b/yoga/YGValue.h index c3df5fc9..2395e07c 100644 --- a/yoga/YGValue.h +++ b/yoga/YGValue.h @@ -15,7 +15,7 @@ YG_EXTERN_C_BEGIN // Not defined in MSVC++ #ifndef NAN static const uint32_t __nan = 0x7fc00000; -#define NAN (*(const float*)__nan) +#define NAN (*(const float*) __nan) #endif #define YGUndefined NAN diff --git a/yoga/Yoga-internal.h b/yoga/Yoga-internal.h index a182a465..50d96dc2 100644 --- a/yoga/Yoga-internal.h +++ b/yoga/Yoga-internal.h @@ -55,8 +55,8 @@ struct YGCachedMeasurement { YGCachedMeasurement() : availableWidth(0), availableHeight(0), - widthMeasureMode((YGMeasureMode)-1), - heightMeasureMode((YGMeasureMode)-1), + widthMeasureMode((YGMeasureMode) -1), + heightMeasureMode((YGMeasureMode) -1), computedWidth(-1), computedHeight(-1) {} @@ -95,10 +95,10 @@ namespace detail { template class Values { - private: +private: std::array values_; - public: +public: Values() = default; explicit Values(const YGValue& defaultValue) noexcept { values_.fill(defaultValue); diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index 9b8aa8a4..fc9bb0da 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -74,7 +74,7 @@ static int YGAndroidLog( return result; } #else -#define YG_UNUSED(x) (void)(x); +#define YG_UNUSED(x) (void) (x); static int YGDefaultLog( const YGConfigRef config, @@ -921,7 +921,7 @@ float YGNodeStyleGetBorder(const YGNodeRef node, const YGEdge edge) { return YGUndefined; } - auto border = (YGValue)node->getStyle().border[edge]; + auto border = (YGValue) node->getStyle().border[edge]; return border.value; } @@ -3745,8 +3745,8 @@ bool YGLayoutNodeInternal( if (needToVisitNode) { // Invalidate the cached results. layout->nextCachedMeasurementsIndex = 0; - layout->cachedLayout.widthMeasureMode = (YGMeasureMode)-1; - layout->cachedLayout.heightMeasureMode = (YGMeasureMode)-1; + layout->cachedLayout.widthMeasureMode = (YGMeasureMode) -1; + layout->cachedLayout.heightMeasureMode = (YGMeasureMode) -1; layout->cachedLayout.computedWidth = -1; layout->cachedLayout.computedHeight = -1; } diff --git a/yoga/Yoga.h b/yoga/Yoga.h index 787af6df..0c45cbe1 100644 --- a/yoga/Yoga.h +++ b/yoga/Yoga.h @@ -358,8 +358,11 @@ WIN_EXPORT float YGNodeLayoutGetPadding( const YGEdge edge); WIN_EXPORT void YGConfigSetLogger(const YGConfigRef config, YGLogger logger); -WIN_EXPORT void -YGLog(const YGNodeRef node, YGLogLevel level, const char* message, ...); +WIN_EXPORT void YGLog( + const YGNodeRef node, + YGLogLevel level, + const char* message, + ...); WIN_EXPORT void YGLogWithConfig( const YGConfigRef config, YGLogLevel level, -- 2.50.1.windows.1 From 4b97375524185dfb5124b9a8c30799bd8acda5e6 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Tue, 8 Jan 2019 12:47:54 -0800 Subject: [PATCH 06/18] Remove repetition in `YGEnums.h` Summary: @public Wraps enum definition in a single macro. Down the line, this will allow us to add more functionality: - Replace manual counts with `yoga::enums::count()` - `yoga::enums::max()` - A bitfield implementation with known memory layout using `max<>()` Reviewed By: SidharthGuglani Differential Revision: D13597259 fbshipit-source-id: 543a11a53090bedf1474d00328565ed57d059e23 --- yoga/YGEnums.h | 114 ++++++++++++++++--------------------------------- 1 file changed, 36 insertions(+), 78 deletions(-) diff --git a/yoga/YGEnums.h b/yoga/YGEnums.h index 222f0ed0..00be5044 100644 --- a/yoga/YGEnums.h +++ b/yoga/YGEnums.h @@ -8,10 +8,15 @@ #include "YGMacros.h" +#define YG_ENUM_DECL(NAME, ...) \ + typedef YG_ENUM_BEGIN(NAME){__VA_ARGS__} YG_ENUM_END(NAME); \ + WIN_EXPORT const char* NAME##ToString(NAME); + YG_EXTERN_C_BEGIN #define YGAlignCount 8 -typedef YG_ENUM_BEGIN(YGAlign){ +YG_ENUM_DECL( + YGAlign, YGAlignAuto, YGAlignFlexStart, YGAlignCenter, @@ -19,34 +24,20 @@ typedef YG_ENUM_BEGIN(YGAlign){ YGAlignStretch, YGAlignBaseline, YGAlignSpaceBetween, - YGAlignSpaceAround, -} YG_ENUM_END(YGAlign); -WIN_EXPORT const char* YGAlignToString(const YGAlign value); + YGAlignSpaceAround); #define YGDimensionCount 2 -typedef YG_ENUM_BEGIN(YGDimension){ - YGDimensionWidth, - YGDimensionHeight, -} YG_ENUM_END(YGDimension); -WIN_EXPORT const char* YGDimensionToString(const YGDimension value); +YG_ENUM_DECL(YGDimension, YGDimensionWidth, YGDimensionHeight) #define YGDirectionCount 3 -typedef YG_ENUM_BEGIN(YGDirection){ - YGDirectionInherit, - YGDirectionLTR, - YGDirectionRTL, -} YG_ENUM_END(YGDirection); -WIN_EXPORT const char* YGDirectionToString(const YGDirection value); +YG_ENUM_DECL(YGDirection, YGDirectionInherit, YGDirectionLTR, YGDirectionRTL) #define YGDisplayCount 2 -typedef YG_ENUM_BEGIN(YGDisplay){ - YGDisplayFlex, - YGDisplayNone, -} YG_ENUM_END(YGDisplay); -WIN_EXPORT const char* YGDisplayToString(const YGDisplay value); +YG_ENUM_DECL(YGDisplay, YGDisplayFlex, YGDisplayNone) #define YGEdgeCount 9 -typedef YG_ENUM_BEGIN(YGEdge){ +YG_ENUM_DECL( + YGEdge, YGEdgeLeft, YGEdgeTop, YGEdgeRight, @@ -55,101 +46,68 @@ typedef YG_ENUM_BEGIN(YGEdge){ YGEdgeEnd, YGEdgeHorizontal, YGEdgeVertical, - YGEdgeAll, -} YG_ENUM_END(YGEdge); -WIN_EXPORT const char* YGEdgeToString(const YGEdge value); + YGEdgeAll) #define YGExperimentalFeatureCount 1 -typedef YG_ENUM_BEGIN(YGExperimentalFeature){ - YGExperimentalFeatureWebFlexBasis, -} YG_ENUM_END(YGExperimentalFeature); -WIN_EXPORT const char* YGExperimentalFeatureToString( - const YGExperimentalFeature value); +YG_ENUM_DECL(YGExperimentalFeature, YGExperimentalFeatureWebFlexBasis) #define YGFlexDirectionCount 4 -typedef YG_ENUM_BEGIN(YGFlexDirection){ +YG_ENUM_DECL( + YGFlexDirection, YGFlexDirectionColumn, YGFlexDirectionColumnReverse, YGFlexDirectionRow, - YGFlexDirectionRowReverse, -} YG_ENUM_END(YGFlexDirection); -WIN_EXPORT const char* YGFlexDirectionToString(const YGFlexDirection value); + YGFlexDirectionRowReverse) #define YGJustifyCount 6 -typedef YG_ENUM_BEGIN(YGJustify){ +YG_ENUM_DECL( + YGJustify, YGJustifyFlexStart, YGJustifyCenter, YGJustifyFlexEnd, YGJustifySpaceBetween, YGJustifySpaceAround, - YGJustifySpaceEvenly, -} YG_ENUM_END(YGJustify); -WIN_EXPORT const char* YGJustifyToString(const YGJustify value); + YGJustifySpaceEvenly) #define YGLogLevelCount 6 -typedef YG_ENUM_BEGIN(YGLogLevel){ +YG_ENUM_DECL( + YGLogLevel, YGLogLevelError, YGLogLevelWarn, YGLogLevelInfo, YGLogLevelDebug, YGLogLevelVerbose, - YGLogLevelFatal, -} YG_ENUM_END(YGLogLevel); -WIN_EXPORT const char* YGLogLevelToString(const YGLogLevel value); + YGLogLevelFatal) #define YGMeasureModeCount 3 -typedef YG_ENUM_BEGIN(YGMeasureMode){ +YG_ENUM_DECL( + YGMeasureMode, YGMeasureModeUndefined, YGMeasureModeExactly, - YGMeasureModeAtMost, -} YG_ENUM_END(YGMeasureMode); -WIN_EXPORT const char* YGMeasureModeToString(const YGMeasureMode value); + YGMeasureModeAtMost) #define YGNodeTypeCount 2 -typedef YG_ENUM_BEGIN(YGNodeType){ - YGNodeTypeDefault, - YGNodeTypeText, -} YG_ENUM_END(YGNodeType); -WIN_EXPORT const char* YGNodeTypeToString(const YGNodeType value); +YG_ENUM_DECL(YGNodeType, YGNodeTypeDefault, YGNodeTypeText) #define YGOverflowCount 3 -typedef YG_ENUM_BEGIN(YGOverflow){ - YGOverflowVisible, - YGOverflowHidden, - YGOverflowScroll, -} YG_ENUM_END(YGOverflow); -WIN_EXPORT const char* YGOverflowToString(const YGOverflow value); +YG_ENUM_DECL(YGOverflow, YGOverflowVisible, YGOverflowHidden, YGOverflowScroll) #define YGPositionTypeCount 2 -typedef YG_ENUM_BEGIN(YGPositionType){ - YGPositionTypeRelative, - YGPositionTypeAbsolute, -} YG_ENUM_END(YGPositionType); -WIN_EXPORT const char* YGPositionTypeToString(const YGPositionType value); +YG_ENUM_DECL(YGPositionType, YGPositionTypeRelative, YGPositionTypeAbsolute) #define YGPrintOptionsCount 3 -typedef YG_ENUM_BEGIN(YGPrintOptions){ +YG_ENUM_DECL( + YGPrintOptions, YGPrintOptionsLayout = 1, YGPrintOptionsStyle = 2, - YGPrintOptionsChildren = 4, -} YG_ENUM_END(YGPrintOptions); -WIN_EXPORT const char* YGPrintOptionsToString(const YGPrintOptions value); + YGPrintOptionsChildren = 4) #define YGUnitCount 4 -typedef YG_ENUM_BEGIN(YGUnit){ - YGUnitUndefined, - YGUnitPoint, - YGUnitPercent, - YGUnitAuto, -} YG_ENUM_END(YGUnit); -WIN_EXPORT const char* YGUnitToString(const YGUnit value); +YG_ENUM_DECL(YGUnit, YGUnitUndefined, YGUnitPoint, YGUnitPercent, YGUnitAuto) #define YGWrapCount 3 -typedef YG_ENUM_BEGIN(YGWrap){ - YGWrapNoWrap, - YGWrapWrap, - YGWrapWrapReverse, -} YG_ENUM_END(YGWrap); -WIN_EXPORT const char* YGWrapToString(const YGWrap value); +YG_ENUM_DECL(YGWrap, YGWrapNoWrap, YGWrapWrap, YGWrapWrapReverse) YG_EXTERN_C_END + +#undef YG_ENUM_DECL -- 2.50.1.windows.1 From efac22a508bb5be9ac80b04f854d46f681a3c639 Mon Sep 17 00:00:00 2001 From: Marko Novakovic Date: Tue, 8 Jan 2019 16:16:59 -0800 Subject: [PATCH 07/18] Using ENUM_BITFIELDS_NOT_SUPPORTED for enum bitfields Reviewed By: davidaurelio Differential Revision: D13556160 fbshipit-source-id: e6d373a4e1dd702509b5d63606c08fae9cdb5498 --- yoga/YGStyle.h | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/yoga/YGStyle.h b/yoga/YGStyle.h index 05d594b1..9193c69d 100644 --- a/yoga/YGStyle.h +++ b/yoga/YGStyle.h @@ -13,6 +13,12 @@ #include "Yoga-internal.h" #include "Yoga.h" +#if !defined(ENUM_BITFIELDS_NOT_SUPPORTED) +#define BITFIELD_ENUM_SIZED(num) : num +#else +#define BITFIELD_ENUM_SIZED(num) +#endif + constexpr YGValue kYGValueUndefined = {0, YGUnitUndefined}; constexpr YGValue kYGValueAuto = {0, YGUnitAuto}; @@ -25,16 +31,18 @@ public: using Dimensions = facebook::yoga::detail::Values<2>; using Edges = facebook::yoga::detail::Values; - YGDirection direction : 2; - YGFlexDirection flexDirection : 2; - YGJustify justifyContent : 3; - YGAlign alignContent : 3; - YGAlign alignItems : 3; - YGAlign alignSelf : 3; - YGPositionType positionType : 1; - YGWrap flexWrap : 2; - YGOverflow overflow : 2; - YGDisplay display : 1; + /* Some platforms don't support enum bitfields, + so please use BITFIELD_ENUM_SIZED(BITS_COUNT) */ + YGDirection direction BITFIELD_ENUM_SIZED(2); + YGFlexDirection flexDirection BITFIELD_ENUM_SIZED(2); + YGJustify justifyContent BITFIELD_ENUM_SIZED(3); + YGAlign alignContent BITFIELD_ENUM_SIZED(3); + YGAlign alignItems BITFIELD_ENUM_SIZED(3); + YGAlign alignSelf BITFIELD_ENUM_SIZED(3); + YGPositionType positionType BITFIELD_ENUM_SIZED(1); + YGWrap flexWrap BITFIELD_ENUM_SIZED(2); + YGOverflow overflow BITFIELD_ENUM_SIZED(2); + YGDisplay display BITFIELD_ENUM_SIZED(1); YGFloatOptional flex = {}; YGFloatOptional flexGrow = {}; YGFloatOptional flexShrink = {}; -- 2.50.1.windows.1 From ef2c693b05bb4d43506be83438d8c1bca92bde76 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Thu, 10 Jan 2019 07:35:55 -0800 Subject: [PATCH 08/18] Remove enum count macros Summary: @public Removes all `YG...Count` macros for enums and replaces them with `facebook::yoga::enums::count()`. This removes the need to manually maintain enum counts. Reviewed By: shergin Differential Revision: D13597449 fbshipit-source-id: edcee225ada4058e94f3a727246763e3cc45873d --- yoga/YGConfig.h | 3 +- yoga/YGEnums.h | 99 ++++++++++++++++++++++++++++++-------------- yoga/YGNode.cpp | 3 +- yoga/YGNodePrint.cpp | 7 ++-- yoga/YGStyle.h | 4 +- yoga/Yoga-internal.h | 3 +- yoga/Yoga.cpp | 14 +++---- 7 files changed, 86 insertions(+), 47 deletions(-) diff --git a/yoga/YGConfig.h b/yoga/YGConfig.h index c9b871fe..00415ce9 100644 --- a/yoga/YGConfig.h +++ b/yoga/YGConfig.h @@ -10,7 +10,8 @@ #include "Yoga.h" struct YGConfig { - std::array experimentalFeatures = {}; + std::array()> + experimentalFeatures = {}; bool useWebDefaults = false; bool useLegacyStretchBehaviour = false; bool shouldDiffLayoutWithoutLegacyStretchBehaviour = false; diff --git a/yoga/YGEnums.h b/yoga/YGEnums.h index 00be5044..ea4e1882 100644 --- a/yoga/YGEnums.h +++ b/yoga/YGEnums.h @@ -8,14 +8,52 @@ #include "YGMacros.h" +#ifdef __cplusplus +namespace facebook { +namespace yoga { +namespace enums { + +template +constexpr int count() = delete; + +namespace detail { +template +constexpr int n() { + return sizeof...(xs); +} +} // namespace detail + +} // namespace enums +} // namespace yoga +} // namespace facebook +#endif + #define YG_ENUM_DECL(NAME, ...) \ typedef YG_ENUM_BEGIN(NAME){__VA_ARGS__} YG_ENUM_END(NAME); \ WIN_EXPORT const char* NAME##ToString(NAME); +#ifdef __cplusplus +#define YG_ENUM_SEQ_DECL(NAME, ...) \ + YG_ENUM_DECL(NAME, __VA_ARGS__) \ + YG_EXTERN_C_END \ + namespace facebook { \ + namespace yoga { \ + namespace enums { \ + template <> \ + constexpr int count() { \ + return detail::n<__VA_ARGS__>(); \ + } \ + } \ + } \ + } \ + YG_EXTERN_C_BEGIN +#else +#define YG_ENUM_SEQ_DECL YG_ENUM_DECL +#endif + YG_EXTERN_C_BEGIN -#define YGAlignCount 8 -YG_ENUM_DECL( +YG_ENUM_SEQ_DECL( YGAlign, YGAlignAuto, YGAlignFlexStart, @@ -26,17 +64,17 @@ YG_ENUM_DECL( YGAlignSpaceBetween, YGAlignSpaceAround); -#define YGDimensionCount 2 -YG_ENUM_DECL(YGDimension, YGDimensionWidth, YGDimensionHeight) +YG_ENUM_SEQ_DECL(YGDimension, YGDimensionWidth, YGDimensionHeight) -#define YGDirectionCount 3 -YG_ENUM_DECL(YGDirection, YGDirectionInherit, YGDirectionLTR, YGDirectionRTL) +YG_ENUM_SEQ_DECL( + YGDirection, + YGDirectionInherit, + YGDirectionLTR, + YGDirectionRTL) -#define YGDisplayCount 2 -YG_ENUM_DECL(YGDisplay, YGDisplayFlex, YGDisplayNone) +YG_ENUM_SEQ_DECL(YGDisplay, YGDisplayFlex, YGDisplayNone) -#define YGEdgeCount 9 -YG_ENUM_DECL( +YG_ENUM_SEQ_DECL( YGEdge, YGEdgeLeft, YGEdgeTop, @@ -48,19 +86,16 @@ YG_ENUM_DECL( YGEdgeVertical, YGEdgeAll) -#define YGExperimentalFeatureCount 1 -YG_ENUM_DECL(YGExperimentalFeature, YGExperimentalFeatureWebFlexBasis) +YG_ENUM_SEQ_DECL(YGExperimentalFeature, YGExperimentalFeatureWebFlexBasis) -#define YGFlexDirectionCount 4 -YG_ENUM_DECL( +YG_ENUM_SEQ_DECL( YGFlexDirection, YGFlexDirectionColumn, YGFlexDirectionColumnReverse, YGFlexDirectionRow, YGFlexDirectionRowReverse) -#define YGJustifyCount 6 -YG_ENUM_DECL( +YG_ENUM_SEQ_DECL( YGJustify, YGJustifyFlexStart, YGJustifyCenter, @@ -69,8 +104,7 @@ YG_ENUM_DECL( YGJustifySpaceAround, YGJustifySpaceEvenly) -#define YGLogLevelCount 6 -YG_ENUM_DECL( +YG_ENUM_SEQ_DECL( YGLogLevel, YGLogLevelError, YGLogLevelWarn, @@ -79,35 +113,38 @@ YG_ENUM_DECL( YGLogLevelVerbose, YGLogLevelFatal) -#define YGMeasureModeCount 3 -YG_ENUM_DECL( +YG_ENUM_SEQ_DECL( YGMeasureMode, YGMeasureModeUndefined, YGMeasureModeExactly, YGMeasureModeAtMost) -#define YGNodeTypeCount 2 -YG_ENUM_DECL(YGNodeType, YGNodeTypeDefault, YGNodeTypeText) +YG_ENUM_SEQ_DECL(YGNodeType, YGNodeTypeDefault, YGNodeTypeText) -#define YGOverflowCount 3 -YG_ENUM_DECL(YGOverflow, YGOverflowVisible, YGOverflowHidden, YGOverflowScroll) +YG_ENUM_SEQ_DECL( + YGOverflow, + YGOverflowVisible, + YGOverflowHidden, + YGOverflowScroll) -#define YGPositionTypeCount 2 -YG_ENUM_DECL(YGPositionType, YGPositionTypeRelative, YGPositionTypeAbsolute) +YG_ENUM_SEQ_DECL(YGPositionType, YGPositionTypeRelative, YGPositionTypeAbsolute) -#define YGPrintOptionsCount 3 YG_ENUM_DECL( YGPrintOptions, YGPrintOptionsLayout = 1, YGPrintOptionsStyle = 2, YGPrintOptionsChildren = 4) -#define YGUnitCount 4 -YG_ENUM_DECL(YGUnit, YGUnitUndefined, YGUnitPoint, YGUnitPercent, YGUnitAuto) +YG_ENUM_SEQ_DECL( + YGUnit, + YGUnitUndefined, + YGUnitPoint, + YGUnitPercent, + YGUnitAuto) -#define YGWrapCount 3 -YG_ENUM_DECL(YGWrap, YGWrapNoWrap, YGWrapWrap, YGWrapWrapReverse) +YG_ENUM_SEQ_DECL(YGWrap, YGWrapNoWrap, YGWrapWrap, YGWrapWrapReverse) YG_EXTERN_C_END #undef YG_ENUM_DECL +#undef YG_ENUM_SEQ_DECL diff --git a/yoga/YGNode.cpp b/yoga/YGNode.cpp index 022e6067..255db920 100644 --- a/yoga/YGNode.cpp +++ b/yoga/YGNode.cpp @@ -313,7 +313,8 @@ YGValue YGNode::resolveFlexBasisPtr() const { } void YGNode::resolveDimension() { - for (uint32_t dim = YGDimensionWidth; dim < YGDimensionCount; dim++) { + using namespace yoga; + for (int dim = YGDimensionWidth; dim < enums::count(); dim++) { if (!getStyle().maxDimensions[dim].isUndefined() && YGValueEqual( getStyle().maxDimensions[dim], style_.minDimensions[dim])) { diff --git a/yoga/YGNodePrint.cpp b/yoga/YGNodePrint.cpp index 1c0c2708..4e02deee 100644 --- a/yoga/YGNodePrint.cpp +++ b/yoga/YGNodePrint.cpp @@ -20,8 +20,7 @@ static void indent(string& base, uint32_t level) { } } -static bool areFourValuesEqual( - const facebook::yoga::detail::Values& four) { +static bool areFourValuesEqual(const YGStyle::Edges& four) { return YGValueEqual(four[0], four[1]) && YGValueEqual(four[0], four[2]) && YGValueEqual(four[0], four[3]); } @@ -86,7 +85,7 @@ static void appendNumberIfNotZero( static void appendEdges( string& base, const string& key, - const facebook::yoga::detail::Values& edges) { + const YGStyle::Edges& edges) { if (areFourValuesEqual(edges)) { appendNumberIfNotZero(base, key, edges[YGEdgeLeft]); } else { @@ -100,7 +99,7 @@ static void appendEdges( static void appendEdgeIfNotUndefined( string& base, const string& str, - const facebook::yoga::detail::Values& edges, + const YGStyle::Edges& edges, const YGEdge edge) { appendNumberIfNotUndefined( base, diff --git a/yoga/YGStyle.h b/yoga/YGStyle.h index 9193c69d..e540635d 100644 --- a/yoga/YGStyle.h +++ b/yoga/YGStyle.h @@ -9,6 +9,7 @@ #include #include #include "CompactValue.h" +#include "YGEnums.h" #include "YGFloatOptional.h" #include "Yoga-internal.h" #include "Yoga.h" @@ -29,7 +30,8 @@ private: public: using Dimensions = facebook::yoga::detail::Values<2>; - using Edges = facebook::yoga::detail::Values; + using Edges = + facebook::yoga::detail::Values()>; /* Some platforms don't support enum bitfields, so please use BITFIELD_ENUM_SIZED(BITS_COUNT) */ diff --git a/yoga/Yoga-internal.h b/yoga/Yoga-internal.h index 50d96dc2..1932cf1a 100644 --- a/yoga/Yoga-internal.h +++ b/yoga/Yoga-internal.h @@ -149,6 +149,7 @@ static const float kWebDefaultFlexShrink = 1.0f; extern bool YGFloatsEqual(const float a, const float b); extern bool YGValueEqual(const YGValue a, const YGValue b); extern facebook::yoga::detail::CompactValue YGComputedEdgeValue( - const facebook::yoga::detail::Values& edges, + const facebook::yoga::detail::Values< + facebook::yoga::enums::count()>& edges, YGEdge edge, facebook::yoga::detail::CompactValue defaultValue); diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index fc9bb0da..0d11c7a3 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -105,7 +105,7 @@ bool YGFloatIsUndefined(const float value) { } detail::CompactValue YGComputedEdgeValue( - const facebook::yoga::detail::Values& edges, + const YGStyle::Edges& edges, YGEdge edge, detail::CompactValue defaultValue) { if (!edges[edge].isUndefined()) { @@ -3546,14 +3546,12 @@ static const char* YGSpacer(const unsigned long level) { static const char* YGMeasureModeName( const YGMeasureMode mode, const bool performLayout) { - const char* kMeasureModeNames[YGMeasureModeCount] = { - "UNDEFINED", "EXACTLY", "AT_MOST"}; - const char* kLayoutModeNames[YGMeasureModeCount] = {"LAY_UNDEFINED", - "LAY_EXACTLY", - "LAY_AT_" - "MOST"}; + constexpr auto N = enums::count(); + const char* kMeasureModeNames[N] = {"UNDEFINED", "EXACTLY", "AT_MOST"}; + const char* kLayoutModeNames[N] = { + "LAY_UNDEFINED", "LAY_EXACTLY", "LAY_AT_MOST"}; - if (mode >= YGMeasureModeCount) { + if (mode >= N) { return ""; } -- 2.50.1.windows.1 From 34673088749ce252afd392f0ed1eb60246aed945 Mon Sep 17 00:00:00 2001 From: Taras Tsugrii Date: Thu, 10 Jan 2019 19:57:40 -0800 Subject: [PATCH 09/18] Reformat xplat build files according to new formatting rules. Summary: drop-conflicts Reviewed By: zertosh Differential Revision: D13610808 fbshipit-source-id: 52c2a90bba3d9b0ac383f6e4a76b4fb3e60bf382 --- android/sample/BUCK | 2 +- csharp/BUCK | 2 +- lib/appcompat/BUCK | 2 +- lib/soloader/BUCK | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/android/sample/BUCK b/android/sample/BUCK index 7a408bb9..0989fcd6 100644 --- a/android/sample/BUCK +++ b/android/sample/BUCK @@ -4,8 +4,8 @@ # This source code is licensed under the license found in the # LICENSE-examples file in the root directory of this source tree. -load("//tools/build_defs/oss:yoga_defs.bzl", "ANDROID_RES_TARGET", "ANDROID_SAMPLE_JAVA_TARGET", "ANDROID_SAMPLE_RES_TARGET", "yoga_android_binary", "yoga_android_resource") load("//tools/build_defs:fb_native_wrapper.bzl", "fb_native") +load("//tools/build_defs/oss:yoga_defs.bzl", "ANDROID_RES_TARGET", "ANDROID_SAMPLE_JAVA_TARGET", "ANDROID_SAMPLE_RES_TARGET", "yoga_android_binary", "yoga_android_resource") yoga_android_binary( name = "sample", diff --git a/csharp/BUCK b/csharp/BUCK index 0df9bea4..81b570e5 100644 --- a/csharp/BUCK +++ b/csharp/BUCK @@ -3,6 +3,7 @@ # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. +load("//tools/build_defs:fb_native_wrapper.bzl", "fb_native") load( "//tools/build_defs/oss:yoga_defs.bzl", "BASE_COMPILER_FLAGS", @@ -10,7 +11,6 @@ load( "yoga_cxx_library", "yoga_dep", ) -load("//tools/build_defs:fb_native_wrapper.bzl", "fb_native") COMPILER_FLAGS = BASE_COMPILER_FLAGS + ["-std=c++11"] diff --git a/lib/appcompat/BUCK b/lib/appcompat/BUCK index 670c5f20..b3262494 100644 --- a/lib/appcompat/BUCK +++ b/lib/appcompat/BUCK @@ -3,8 +3,8 @@ # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. -load("//tools/build_defs/oss:yoga_defs.bzl", "YOGA_ROOTS") load("//tools/build_defs:fb_native_wrapper.bzl", "fb_native") +load("//tools/build_defs/oss:yoga_defs.bzl", "YOGA_ROOTS") fb_native.android_prebuilt_aar( name = "appcompat", diff --git a/lib/soloader/BUCK b/lib/soloader/BUCK index 5f3cc331..04e2f5ff 100644 --- a/lib/soloader/BUCK +++ b/lib/soloader/BUCK @@ -3,8 +3,8 @@ # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. -load("//tools/build_defs/oss:yoga_defs.bzl", "YOGA_ROOTS") load("//tools/build_defs:fb_native_wrapper.bzl", "fb_native") +load("//tools/build_defs/oss:yoga_defs.bzl", "YOGA_ROOTS") fb_native.android_prebuilt_aar( name = "soloader", -- 2.50.1.windows.1 From 866bf101b2fe029bb4665a8b431d08e47ba027ce Mon Sep 17 00:00:00 2001 From: Amir Shalem Date: Thu, 10 Jan 2019 23:48:36 -0800 Subject: [PATCH 10/18] Revert D13597449: [Yoga][cleanup] Remove enum count macros Differential Revision: D13597449 Original commit changeset: edcee225ada4 fbshipit-source-id: 1afc24833c8657a8a198bc4529d98c8f605b7fbd --- yoga/YGConfig.h | 3 +- yoga/YGEnums.h | 99 ++++++++++++++------------------------------ yoga/YGNode.cpp | 3 +- yoga/YGNodePrint.cpp | 7 ++-- yoga/YGStyle.h | 4 +- yoga/Yoga-internal.h | 3 +- yoga/Yoga.cpp | 14 ++++--- 7 files changed, 47 insertions(+), 86 deletions(-) diff --git a/yoga/YGConfig.h b/yoga/YGConfig.h index 00415ce9..c9b871fe 100644 --- a/yoga/YGConfig.h +++ b/yoga/YGConfig.h @@ -10,8 +10,7 @@ #include "Yoga.h" struct YGConfig { - std::array()> - experimentalFeatures = {}; + std::array experimentalFeatures = {}; bool useWebDefaults = false; bool useLegacyStretchBehaviour = false; bool shouldDiffLayoutWithoutLegacyStretchBehaviour = false; diff --git a/yoga/YGEnums.h b/yoga/YGEnums.h index ea4e1882..00be5044 100644 --- a/yoga/YGEnums.h +++ b/yoga/YGEnums.h @@ -8,52 +8,14 @@ #include "YGMacros.h" -#ifdef __cplusplus -namespace facebook { -namespace yoga { -namespace enums { - -template -constexpr int count() = delete; - -namespace detail { -template -constexpr int n() { - return sizeof...(xs); -} -} // namespace detail - -} // namespace enums -} // namespace yoga -} // namespace facebook -#endif - #define YG_ENUM_DECL(NAME, ...) \ typedef YG_ENUM_BEGIN(NAME){__VA_ARGS__} YG_ENUM_END(NAME); \ WIN_EXPORT const char* NAME##ToString(NAME); -#ifdef __cplusplus -#define YG_ENUM_SEQ_DECL(NAME, ...) \ - YG_ENUM_DECL(NAME, __VA_ARGS__) \ - YG_EXTERN_C_END \ - namespace facebook { \ - namespace yoga { \ - namespace enums { \ - template <> \ - constexpr int count() { \ - return detail::n<__VA_ARGS__>(); \ - } \ - } \ - } \ - } \ - YG_EXTERN_C_BEGIN -#else -#define YG_ENUM_SEQ_DECL YG_ENUM_DECL -#endif - YG_EXTERN_C_BEGIN -YG_ENUM_SEQ_DECL( +#define YGAlignCount 8 +YG_ENUM_DECL( YGAlign, YGAlignAuto, YGAlignFlexStart, @@ -64,17 +26,17 @@ YG_ENUM_SEQ_DECL( YGAlignSpaceBetween, YGAlignSpaceAround); -YG_ENUM_SEQ_DECL(YGDimension, YGDimensionWidth, YGDimensionHeight) +#define YGDimensionCount 2 +YG_ENUM_DECL(YGDimension, YGDimensionWidth, YGDimensionHeight) -YG_ENUM_SEQ_DECL( - YGDirection, - YGDirectionInherit, - YGDirectionLTR, - YGDirectionRTL) +#define YGDirectionCount 3 +YG_ENUM_DECL(YGDirection, YGDirectionInherit, YGDirectionLTR, YGDirectionRTL) -YG_ENUM_SEQ_DECL(YGDisplay, YGDisplayFlex, YGDisplayNone) +#define YGDisplayCount 2 +YG_ENUM_DECL(YGDisplay, YGDisplayFlex, YGDisplayNone) -YG_ENUM_SEQ_DECL( +#define YGEdgeCount 9 +YG_ENUM_DECL( YGEdge, YGEdgeLeft, YGEdgeTop, @@ -86,16 +48,19 @@ YG_ENUM_SEQ_DECL( YGEdgeVertical, YGEdgeAll) -YG_ENUM_SEQ_DECL(YGExperimentalFeature, YGExperimentalFeatureWebFlexBasis) +#define YGExperimentalFeatureCount 1 +YG_ENUM_DECL(YGExperimentalFeature, YGExperimentalFeatureWebFlexBasis) -YG_ENUM_SEQ_DECL( +#define YGFlexDirectionCount 4 +YG_ENUM_DECL( YGFlexDirection, YGFlexDirectionColumn, YGFlexDirectionColumnReverse, YGFlexDirectionRow, YGFlexDirectionRowReverse) -YG_ENUM_SEQ_DECL( +#define YGJustifyCount 6 +YG_ENUM_DECL( YGJustify, YGJustifyFlexStart, YGJustifyCenter, @@ -104,7 +69,8 @@ YG_ENUM_SEQ_DECL( YGJustifySpaceAround, YGJustifySpaceEvenly) -YG_ENUM_SEQ_DECL( +#define YGLogLevelCount 6 +YG_ENUM_DECL( YGLogLevel, YGLogLevelError, YGLogLevelWarn, @@ -113,38 +79,35 @@ YG_ENUM_SEQ_DECL( YGLogLevelVerbose, YGLogLevelFatal) -YG_ENUM_SEQ_DECL( +#define YGMeasureModeCount 3 +YG_ENUM_DECL( YGMeasureMode, YGMeasureModeUndefined, YGMeasureModeExactly, YGMeasureModeAtMost) -YG_ENUM_SEQ_DECL(YGNodeType, YGNodeTypeDefault, YGNodeTypeText) +#define YGNodeTypeCount 2 +YG_ENUM_DECL(YGNodeType, YGNodeTypeDefault, YGNodeTypeText) -YG_ENUM_SEQ_DECL( - YGOverflow, - YGOverflowVisible, - YGOverflowHidden, - YGOverflowScroll) +#define YGOverflowCount 3 +YG_ENUM_DECL(YGOverflow, YGOverflowVisible, YGOverflowHidden, YGOverflowScroll) -YG_ENUM_SEQ_DECL(YGPositionType, YGPositionTypeRelative, YGPositionTypeAbsolute) +#define YGPositionTypeCount 2 +YG_ENUM_DECL(YGPositionType, YGPositionTypeRelative, YGPositionTypeAbsolute) +#define YGPrintOptionsCount 3 YG_ENUM_DECL( YGPrintOptions, YGPrintOptionsLayout = 1, YGPrintOptionsStyle = 2, YGPrintOptionsChildren = 4) -YG_ENUM_SEQ_DECL( - YGUnit, - YGUnitUndefined, - YGUnitPoint, - YGUnitPercent, - YGUnitAuto) +#define YGUnitCount 4 +YG_ENUM_DECL(YGUnit, YGUnitUndefined, YGUnitPoint, YGUnitPercent, YGUnitAuto) -YG_ENUM_SEQ_DECL(YGWrap, YGWrapNoWrap, YGWrapWrap, YGWrapWrapReverse) +#define YGWrapCount 3 +YG_ENUM_DECL(YGWrap, YGWrapNoWrap, YGWrapWrap, YGWrapWrapReverse) YG_EXTERN_C_END #undef YG_ENUM_DECL -#undef YG_ENUM_SEQ_DECL diff --git a/yoga/YGNode.cpp b/yoga/YGNode.cpp index 255db920..022e6067 100644 --- a/yoga/YGNode.cpp +++ b/yoga/YGNode.cpp @@ -313,8 +313,7 @@ YGValue YGNode::resolveFlexBasisPtr() const { } void YGNode::resolveDimension() { - using namespace yoga; - for (int dim = YGDimensionWidth; dim < enums::count(); dim++) { + for (uint32_t dim = YGDimensionWidth; dim < YGDimensionCount; dim++) { if (!getStyle().maxDimensions[dim].isUndefined() && YGValueEqual( getStyle().maxDimensions[dim], style_.minDimensions[dim])) { diff --git a/yoga/YGNodePrint.cpp b/yoga/YGNodePrint.cpp index 4e02deee..1c0c2708 100644 --- a/yoga/YGNodePrint.cpp +++ b/yoga/YGNodePrint.cpp @@ -20,7 +20,8 @@ static void indent(string& base, uint32_t level) { } } -static bool areFourValuesEqual(const YGStyle::Edges& four) { +static bool areFourValuesEqual( + const facebook::yoga::detail::Values& four) { return YGValueEqual(four[0], four[1]) && YGValueEqual(four[0], four[2]) && YGValueEqual(four[0], four[3]); } @@ -85,7 +86,7 @@ static void appendNumberIfNotZero( static void appendEdges( string& base, const string& key, - const YGStyle::Edges& edges) { + const facebook::yoga::detail::Values& edges) { if (areFourValuesEqual(edges)) { appendNumberIfNotZero(base, key, edges[YGEdgeLeft]); } else { @@ -99,7 +100,7 @@ static void appendEdges( static void appendEdgeIfNotUndefined( string& base, const string& str, - const YGStyle::Edges& edges, + const facebook::yoga::detail::Values& edges, const YGEdge edge) { appendNumberIfNotUndefined( base, diff --git a/yoga/YGStyle.h b/yoga/YGStyle.h index e540635d..9193c69d 100644 --- a/yoga/YGStyle.h +++ b/yoga/YGStyle.h @@ -9,7 +9,6 @@ #include #include #include "CompactValue.h" -#include "YGEnums.h" #include "YGFloatOptional.h" #include "Yoga-internal.h" #include "Yoga.h" @@ -30,8 +29,7 @@ private: public: using Dimensions = facebook::yoga::detail::Values<2>; - using Edges = - facebook::yoga::detail::Values()>; + using Edges = facebook::yoga::detail::Values; /* Some platforms don't support enum bitfields, so please use BITFIELD_ENUM_SIZED(BITS_COUNT) */ diff --git a/yoga/Yoga-internal.h b/yoga/Yoga-internal.h index 1932cf1a..50d96dc2 100644 --- a/yoga/Yoga-internal.h +++ b/yoga/Yoga-internal.h @@ -149,7 +149,6 @@ static const float kWebDefaultFlexShrink = 1.0f; extern bool YGFloatsEqual(const float a, const float b); extern bool YGValueEqual(const YGValue a, const YGValue b); extern facebook::yoga::detail::CompactValue YGComputedEdgeValue( - const facebook::yoga::detail::Values< - facebook::yoga::enums::count()>& edges, + const facebook::yoga::detail::Values& edges, YGEdge edge, facebook::yoga::detail::CompactValue defaultValue); diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index 0d11c7a3..fc9bb0da 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -105,7 +105,7 @@ bool YGFloatIsUndefined(const float value) { } detail::CompactValue YGComputedEdgeValue( - const YGStyle::Edges& edges, + const facebook::yoga::detail::Values& edges, YGEdge edge, detail::CompactValue defaultValue) { if (!edges[edge].isUndefined()) { @@ -3546,12 +3546,14 @@ static const char* YGSpacer(const unsigned long level) { static const char* YGMeasureModeName( const YGMeasureMode mode, const bool performLayout) { - constexpr auto N = enums::count(); - const char* kMeasureModeNames[N] = {"UNDEFINED", "EXACTLY", "AT_MOST"}; - const char* kLayoutModeNames[N] = { - "LAY_UNDEFINED", "LAY_EXACTLY", "LAY_AT_MOST"}; + const char* kMeasureModeNames[YGMeasureModeCount] = { + "UNDEFINED", "EXACTLY", "AT_MOST"}; + const char* kLayoutModeNames[YGMeasureModeCount] = {"LAY_UNDEFINED", + "LAY_EXACTLY", + "LAY_AT_" + "MOST"}; - if (mode >= N) { + if (mode >= YGMeasureModeCount) { return ""; } -- 2.50.1.windows.1 From 4840495d3f6ae80cee54fd5d061fb252187f3363 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Fri, 11 Jan 2019 03:09:11 -0800 Subject: [PATCH 11/18] Remove enum count macros Summary: @public Removes all `YG...Count` macros for enums and replaces them with `facebook::yoga::enums::count()`. This removes the need to manually maintain enum counts. Same as D13597449, working around a defect in clang < 3.9 Reviewed By: amir-shalem Differential Revision: D13634622 fbshipit-source-id: 344dc70e167b0caf746fe396cedd200f54e52219 --- yoga/YGConfig.h | 3 +- yoga/YGEnums.h | 99 ++++++++++++++++++++++++++++++-------------- yoga/YGNode.cpp | 3 +- yoga/YGNodePrint.cpp | 7 ++-- yoga/YGStyle.h | 4 +- yoga/Yoga-internal.h | 3 +- yoga/Yoga.cpp | 14 +++---- 7 files changed, 86 insertions(+), 47 deletions(-) diff --git a/yoga/YGConfig.h b/yoga/YGConfig.h index c9b871fe..00415ce9 100644 --- a/yoga/YGConfig.h +++ b/yoga/YGConfig.h @@ -10,7 +10,8 @@ #include "Yoga.h" struct YGConfig { - std::array experimentalFeatures = {}; + std::array()> + experimentalFeatures = {}; bool useWebDefaults = false; bool useLegacyStretchBehaviour = false; bool shouldDiffLayoutWithoutLegacyStretchBehaviour = false; diff --git a/yoga/YGEnums.h b/yoga/YGEnums.h index 00be5044..f06b0e04 100644 --- a/yoga/YGEnums.h +++ b/yoga/YGEnums.h @@ -8,14 +8,52 @@ #include "YGMacros.h" +#ifdef __cplusplus +namespace facebook { +namespace yoga { +namespace enums { + +template +constexpr int count(); // can't use `= delete` due to a defect in clang < 3.9 + +namespace detail { +template +constexpr int n() { + return sizeof...(xs); +} +} // namespace detail + +} // namespace enums +} // namespace yoga +} // namespace facebook +#endif + #define YG_ENUM_DECL(NAME, ...) \ typedef YG_ENUM_BEGIN(NAME){__VA_ARGS__} YG_ENUM_END(NAME); \ WIN_EXPORT const char* NAME##ToString(NAME); +#ifdef __cplusplus +#define YG_ENUM_SEQ_DECL(NAME, ...) \ + YG_ENUM_DECL(NAME, __VA_ARGS__) \ + YG_EXTERN_C_END \ + namespace facebook { \ + namespace yoga { \ + namespace enums { \ + template <> \ + constexpr int count() { \ + return detail::n<__VA_ARGS__>(); \ + } \ + } \ + } \ + } \ + YG_EXTERN_C_BEGIN +#else +#define YG_ENUM_SEQ_DECL YG_ENUM_DECL +#endif + YG_EXTERN_C_BEGIN -#define YGAlignCount 8 -YG_ENUM_DECL( +YG_ENUM_SEQ_DECL( YGAlign, YGAlignAuto, YGAlignFlexStart, @@ -26,17 +64,17 @@ YG_ENUM_DECL( YGAlignSpaceBetween, YGAlignSpaceAround); -#define YGDimensionCount 2 -YG_ENUM_DECL(YGDimension, YGDimensionWidth, YGDimensionHeight) +YG_ENUM_SEQ_DECL(YGDimension, YGDimensionWidth, YGDimensionHeight) -#define YGDirectionCount 3 -YG_ENUM_DECL(YGDirection, YGDirectionInherit, YGDirectionLTR, YGDirectionRTL) +YG_ENUM_SEQ_DECL( + YGDirection, + YGDirectionInherit, + YGDirectionLTR, + YGDirectionRTL) -#define YGDisplayCount 2 -YG_ENUM_DECL(YGDisplay, YGDisplayFlex, YGDisplayNone) +YG_ENUM_SEQ_DECL(YGDisplay, YGDisplayFlex, YGDisplayNone) -#define YGEdgeCount 9 -YG_ENUM_DECL( +YG_ENUM_SEQ_DECL( YGEdge, YGEdgeLeft, YGEdgeTop, @@ -48,19 +86,16 @@ YG_ENUM_DECL( YGEdgeVertical, YGEdgeAll) -#define YGExperimentalFeatureCount 1 -YG_ENUM_DECL(YGExperimentalFeature, YGExperimentalFeatureWebFlexBasis) +YG_ENUM_SEQ_DECL(YGExperimentalFeature, YGExperimentalFeatureWebFlexBasis) -#define YGFlexDirectionCount 4 -YG_ENUM_DECL( +YG_ENUM_SEQ_DECL( YGFlexDirection, YGFlexDirectionColumn, YGFlexDirectionColumnReverse, YGFlexDirectionRow, YGFlexDirectionRowReverse) -#define YGJustifyCount 6 -YG_ENUM_DECL( +YG_ENUM_SEQ_DECL( YGJustify, YGJustifyFlexStart, YGJustifyCenter, @@ -69,8 +104,7 @@ YG_ENUM_DECL( YGJustifySpaceAround, YGJustifySpaceEvenly) -#define YGLogLevelCount 6 -YG_ENUM_DECL( +YG_ENUM_SEQ_DECL( YGLogLevel, YGLogLevelError, YGLogLevelWarn, @@ -79,35 +113,38 @@ YG_ENUM_DECL( YGLogLevelVerbose, YGLogLevelFatal) -#define YGMeasureModeCount 3 -YG_ENUM_DECL( +YG_ENUM_SEQ_DECL( YGMeasureMode, YGMeasureModeUndefined, YGMeasureModeExactly, YGMeasureModeAtMost) -#define YGNodeTypeCount 2 -YG_ENUM_DECL(YGNodeType, YGNodeTypeDefault, YGNodeTypeText) +YG_ENUM_SEQ_DECL(YGNodeType, YGNodeTypeDefault, YGNodeTypeText) -#define YGOverflowCount 3 -YG_ENUM_DECL(YGOverflow, YGOverflowVisible, YGOverflowHidden, YGOverflowScroll) +YG_ENUM_SEQ_DECL( + YGOverflow, + YGOverflowVisible, + YGOverflowHidden, + YGOverflowScroll) -#define YGPositionTypeCount 2 -YG_ENUM_DECL(YGPositionType, YGPositionTypeRelative, YGPositionTypeAbsolute) +YG_ENUM_SEQ_DECL(YGPositionType, YGPositionTypeRelative, YGPositionTypeAbsolute) -#define YGPrintOptionsCount 3 YG_ENUM_DECL( YGPrintOptions, YGPrintOptionsLayout = 1, YGPrintOptionsStyle = 2, YGPrintOptionsChildren = 4) -#define YGUnitCount 4 -YG_ENUM_DECL(YGUnit, YGUnitUndefined, YGUnitPoint, YGUnitPercent, YGUnitAuto) +YG_ENUM_SEQ_DECL( + YGUnit, + YGUnitUndefined, + YGUnitPoint, + YGUnitPercent, + YGUnitAuto) -#define YGWrapCount 3 -YG_ENUM_DECL(YGWrap, YGWrapNoWrap, YGWrapWrap, YGWrapWrapReverse) +YG_ENUM_SEQ_DECL(YGWrap, YGWrapNoWrap, YGWrapWrap, YGWrapWrapReverse) YG_EXTERN_C_END #undef YG_ENUM_DECL +#undef YG_ENUM_SEQ_DECL diff --git a/yoga/YGNode.cpp b/yoga/YGNode.cpp index 022e6067..255db920 100644 --- a/yoga/YGNode.cpp +++ b/yoga/YGNode.cpp @@ -313,7 +313,8 @@ YGValue YGNode::resolveFlexBasisPtr() const { } void YGNode::resolveDimension() { - for (uint32_t dim = YGDimensionWidth; dim < YGDimensionCount; dim++) { + using namespace yoga; + for (int dim = YGDimensionWidth; dim < enums::count(); dim++) { if (!getStyle().maxDimensions[dim].isUndefined() && YGValueEqual( getStyle().maxDimensions[dim], style_.minDimensions[dim])) { diff --git a/yoga/YGNodePrint.cpp b/yoga/YGNodePrint.cpp index 1c0c2708..4e02deee 100644 --- a/yoga/YGNodePrint.cpp +++ b/yoga/YGNodePrint.cpp @@ -20,8 +20,7 @@ static void indent(string& base, uint32_t level) { } } -static bool areFourValuesEqual( - const facebook::yoga::detail::Values& four) { +static bool areFourValuesEqual(const YGStyle::Edges& four) { return YGValueEqual(four[0], four[1]) && YGValueEqual(four[0], four[2]) && YGValueEqual(four[0], four[3]); } @@ -86,7 +85,7 @@ static void appendNumberIfNotZero( static void appendEdges( string& base, const string& key, - const facebook::yoga::detail::Values& edges) { + const YGStyle::Edges& edges) { if (areFourValuesEqual(edges)) { appendNumberIfNotZero(base, key, edges[YGEdgeLeft]); } else { @@ -100,7 +99,7 @@ static void appendEdges( static void appendEdgeIfNotUndefined( string& base, const string& str, - const facebook::yoga::detail::Values& edges, + const YGStyle::Edges& edges, const YGEdge edge) { appendNumberIfNotUndefined( base, diff --git a/yoga/YGStyle.h b/yoga/YGStyle.h index 9193c69d..e540635d 100644 --- a/yoga/YGStyle.h +++ b/yoga/YGStyle.h @@ -9,6 +9,7 @@ #include #include #include "CompactValue.h" +#include "YGEnums.h" #include "YGFloatOptional.h" #include "Yoga-internal.h" #include "Yoga.h" @@ -29,7 +30,8 @@ private: public: using Dimensions = facebook::yoga::detail::Values<2>; - using Edges = facebook::yoga::detail::Values; + using Edges = + facebook::yoga::detail::Values()>; /* Some platforms don't support enum bitfields, so please use BITFIELD_ENUM_SIZED(BITS_COUNT) */ diff --git a/yoga/Yoga-internal.h b/yoga/Yoga-internal.h index 50d96dc2..1932cf1a 100644 --- a/yoga/Yoga-internal.h +++ b/yoga/Yoga-internal.h @@ -149,6 +149,7 @@ static const float kWebDefaultFlexShrink = 1.0f; extern bool YGFloatsEqual(const float a, const float b); extern bool YGValueEqual(const YGValue a, const YGValue b); extern facebook::yoga::detail::CompactValue YGComputedEdgeValue( - const facebook::yoga::detail::Values& edges, + const facebook::yoga::detail::Values< + facebook::yoga::enums::count()>& edges, YGEdge edge, facebook::yoga::detail::CompactValue defaultValue); diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index fc9bb0da..0d11c7a3 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -105,7 +105,7 @@ bool YGFloatIsUndefined(const float value) { } detail::CompactValue YGComputedEdgeValue( - const facebook::yoga::detail::Values& edges, + const YGStyle::Edges& edges, YGEdge edge, detail::CompactValue defaultValue) { if (!edges[edge].isUndefined()) { @@ -3546,14 +3546,12 @@ static const char* YGSpacer(const unsigned long level) { static const char* YGMeasureModeName( const YGMeasureMode mode, const bool performLayout) { - const char* kMeasureModeNames[YGMeasureModeCount] = { - "UNDEFINED", "EXACTLY", "AT_MOST"}; - const char* kLayoutModeNames[YGMeasureModeCount] = {"LAY_UNDEFINED", - "LAY_EXACTLY", - "LAY_AT_" - "MOST"}; + constexpr auto N = enums::count(); + const char* kMeasureModeNames[N] = {"UNDEFINED", "EXACTLY", "AT_MOST"}; + const char* kLayoutModeNames[N] = { + "LAY_UNDEFINED", "LAY_EXACTLY", "LAY_AT_MOST"}; - if (mode >= YGMeasureModeCount) { + if (mode >= N) { return ""; } -- 2.50.1.windows.1 From 7fa0816d8144c8dfe79c7d5795ef028950bf3b58 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Wed, 16 Jan 2019 08:39:57 -0800 Subject: [PATCH 12/18] Fix URLs to CSS spec in comments Summary: @public These URLs probably have been changed when CSSLayout was renamed to Yoga by search & replacing Reviewed By: SidharthGuglani Differential Revision: D13690829 fbshipit-source-id: 44dbd8fe256fc286006d164390609a7c3de6c4e6 --- yoga/Yoga.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index 0d11c7a3..0a049e6b 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -2336,7 +2336,7 @@ static void YGDistributeFreeSpaceFirstPass( // // This two pass approach for resolving min/max constraints deviates from // the spec. The -// spec (https://www.w3.org/TR/YG-flexbox-1/#resolve-flexible-lengths) +// spec (https://www.w3.org/TR/CSS-flexbox-1/#resolve-flexible-lengths) // describes a process // that needs to be repeated a variable number of times. The algorithm // implemented here @@ -2609,7 +2609,7 @@ static void YGJustifyMainAxis( // // This is the main routine that implements a subset of the flexbox layout // algorithm -// described in the W3C YG documentation: https://www.w3.org/TR/YG3-flexbox/. +// described in the W3C YG documentation: https://www.w3.org/TR/CSS3-flexbox/. // // Limitations of this algorithm, compared to the full standard: // * Display property is always assumed to be 'flex' except for Text nodes, @@ -2682,7 +2682,7 @@ static void YGJustifyMainAxis( // support default // minimum main sizes (see above for details). Each of our measure modes maps // to a layout mode -// from the spec (https://www.w3.org/TR/YG3-sizing/#terms): +// from the spec (https://www.w3.org/TR/CSS3-sizing/#terms): // - YGMeasureModeUndefined: max content // - YGMeasureModeExactly: fill available // - YGMeasureModeAtMost: fit content -- 2.50.1.windows.1 From 71321f763a6142c4531684ada6a87954b12bd362 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Wed, 16 Jan 2019 08:39:58 -0800 Subject: [PATCH 13/18] Reflow comments Summary: @public Repeated application and alternation of Clang Format rules has lead to unfortunate block comment formatting. Here, we reflow comments Reviewed By: SidharthGuglani Differential Revision: D13677242 fbshipit-source-id: 3f1f5e38693eb15e9705f24fd363fc1618c78974 --- yoga/CompactValue.h | 4 +- yoga/Utils.h | 26 ++--- yoga/YGLayout.h | 4 +- yoga/YGMarker.h | 4 +- yoga/YGNode.cpp | 4 +- yoga/YGNode.h | 7 +- yoga/Yoga.cpp | 231 ++++++++++++++++++-------------------------- yoga/Yoga.h | 42 ++++---- 8 files changed, 140 insertions(+), 182 deletions(-) diff --git a/yoga/CompactValue.h b/yoga/CompactValue.h index 22b9624e..9c9a92e0 100644 --- a/yoga/CompactValue.h +++ b/yoga/CompactValue.h @@ -149,8 +149,8 @@ private: static constexpr uint32_t BIAS = 0x20000000; static constexpr uint32_t PERCENT_BIT = 0x40000000; - // these are signaling NaNs with specific bit pattern as payload - // they will be silenced whenever going through an FPU operation on ARM + x86 + // these are signaling NaNs with specific bit pattern as payload they will be + // silenced whenever going through an FPU operation on ARM + x86 static constexpr uint32_t AUTO_BITS = 0x7faaaaaa; static constexpr uint32_t ZERO_BITS_POINT = 0x7f8f0f0f; static constexpr uint32_t ZERO_BITS_PERCENT = 0x7f80f0f0; diff --git a/yoga/Utils.h b/yoga/Utils.h index 7e5a8636..a27b62a1 100644 --- a/yoga/Utils.h +++ b/yoga/Utils.h @@ -9,31 +9,31 @@ #include "Yoga-internal.h" #include "CompactValue.h" -// This struct is an helper model to hold the data for step 4 of flexbox -// algo, which is collecting the flex items in a line. +// This struct is an helper model to hold the data for step 4 of flexbox algo, +// which is collecting the flex items in a line. // // - itemsOnLine: Number of items which can fit in a line considering the -// available Inner dimension, the flex items computed flexbasis and their -// margin. It may be different than the difference between start and end -// indicates because we skip over absolute-positioned items. +// available Inner dimension, the flex items computed flexbasis and their +// margin. It may be different than the difference between start and end +// indicates because we skip over absolute-positioned items. // // - sizeConsumedOnCurrentLine: It is accumulation of the dimensions and margin -// of all the children on the current line. This will be used in order to either -// set the dimensions of the node if none already exist or to compute the -// remaining space left for the flexible children. +// of all the children on the current line. This will be used in order to +// either set the dimensions of the node if none already exist or to compute +// the remaining space left for the flexible children. // // - totalFlexGrowFactors: total flex grow factors of flex items which are to be -// layed in the current line +// layed in the current line // // - totalFlexShrinkFactors: total flex shrink factors of flex items which are -// to be layed in the current line +// to be layed in the current line // // - endOfLineIndex: Its the end index of the last flex item which was examined -// and it may or may not be part of the current line(as it may be absolutely -// positioned or inculding it may have caused to overshoot availableInnerDim) +// and it may or may not be part of the current line(as it may be absolutely +// positioned or inculding it may have caused to overshoot availableInnerDim) // // - relativeChildren: Maintain a vector of the child nodes that can shrink -// and/or grow. +// and/or grow. struct YGCollectFlexItemsRowValues { uint32_t itemsOnLine; diff --git a/yoga/YGLayout.h b/yoga/YGLayout.h index 4a68d86a..4daa28ed 100644 --- a/yoga/YGLayout.h +++ b/yoga/YGLayout.h @@ -25,8 +25,8 @@ struct YGLayout { uint32_t computedFlexBasisGeneration = 0; YGFloatOptional computedFlexBasis = {}; - // Instead of recomputing the entire layout every single time, we - // cache some information to break early when nothing changed + // Instead of recomputing the entire layout every single time, we cache some + // information to break early when nothing changed uint32_t generationCount = 0; YGDirection lastOwnerDirection = (YGDirection) -1; diff --git a/yoga/YGMarker.h b/yoga/YGMarker.h index 3eb665fe..d4f4de64 100644 --- a/yoga/YGMarker.h +++ b/yoga/YGMarker.h @@ -23,8 +23,8 @@ typedef struct { // accepts marker type, a node ref, and marker data (depends on marker type) // can return a handle or id that Yoga will pass to endMarker void* (*startMarker)(YGMarkerType, YGNodeRef, YGMarkerData); - // accepts marker type, a node ref, marker data, and marker id as returned - // by startMarker + // accepts marker type, a node ref, marker data, and marker id as returned by + // startMarker void (*endMarker)(YGMarkerType, YGNodeRef, YGMarkerData, void* id); } YGMarkerCallbacks; diff --git a/yoga/YGNode.cpp b/yoga/YGNode.cpp index 255db920..0b7fc3c7 100644 --- a/yoga/YGNode.cpp +++ b/yoga/YGNode.cpp @@ -206,8 +206,8 @@ void YGNode::setLayoutDimension(float dimension, int index) { layout_.dimensions[index] = dimension; } -// If both left and right are defined, then use left. Otherwise return -// +left or -right depending on which is defined. +// If both left and right are defined, then use left. Otherwise return +left or +// -right depending on which is defined. YGFloatOptional YGNode::relativePosition( const YGFlexDirection axis, const float axisSize) const { diff --git a/yoga/YGNode.h b/yoga/YGNode.h index db1e7d42..ffb01832 100644 --- a/yoga/YGNode.h +++ b/yoga/YGNode.h @@ -102,10 +102,9 @@ public: } // returns the YGNodeRef that owns this YGNode. An owner is used to identify - // the YogaTree that a YGNode belongs to. - // This method will return the parent of the YGNode when a YGNode only belongs - // to one YogaTree or nullptr when the YGNode is shared between two or more - // YogaTrees. + // the YogaTree that a YGNode belongs to. This method will return the parent + // of the YGNode when a YGNode only belongs to one YogaTree or nullptr when + // the YGNode is shared between two or more YogaTrees. YGNodeRef getOwner() const { return owner_; } diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index 0a049e6b..38287e76 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -1322,8 +1322,8 @@ static void YGNodeComputeFlexBasisForChild( child->getResolvedDimension(YGDimensionHeight), ownerHeight), paddingAndBorder)); } else { - // Compute the flex basis and hypothetical main size (i.e. the clamped - // flex basis). + // Compute the flex basis and hypothetical main size (i.e. the clamped flex + // basis). childWidth = YGUndefined; childHeight = YGUndefined; childWidthMeasureMode = YGMeasureModeUndefined; @@ -1351,8 +1351,8 @@ static void YGNodeComputeFlexBasisForChild( childHeightMeasureMode = YGMeasureModeExactly; } - // The W3C spec doesn't say anything about the 'overflow' property, - // but all major browsers appear to implement the following logic. + // The W3C spec doesn't say anything about the 'overflow' property, but all + // major browsers appear to implement the following logic. if ((!isMainAxisRow && node->getStyle().overflow == YGOverflowScroll) || node->getStyle().overflow != YGOverflowScroll) { if (YGFloatIsUndefined(childWidth) && !YGFloatIsUndefined(width)) { @@ -1383,9 +1383,8 @@ static void YGNodeComputeFlexBasisForChild( } } - // If child has no defined size in the cross axis and is set to stretch, - // set the cross - // axis to be measured exactly with the available inner width + // If child has no defined size in the cross axis and is set to stretch, set + // the cross axis to be measured exactly with the available inner width const bool hasExactWidth = !YGFloatIsUndefined(width) && widthMode == YGMeasureModeExactly; @@ -1484,9 +1483,8 @@ static void YGNodeAbsoluteLayoutChild( .unwrap() + marginRow; } else { - // If the child doesn't have a specified width, compute the width based - // on the left/right - // offsets if they're defined. + // If the child doesn't have a specified width, compute the width based on + // the left/right offsets if they're defined. if (child->isLeadingPositionDefined(YGFlexDirectionRow) && child->isTrailingPosDefined(YGFlexDirectionRow)) { childWidth = node->getLayout().measuredDimensions[YGDimensionWidth] - @@ -1506,9 +1504,8 @@ static void YGNodeAbsoluteLayoutChild( .unwrap() + marginColumn; } else { - // If the child doesn't have a specified height, compute the height - // based on the top/bottom - // offsets if they're defined. + // If the child doesn't have a specified height, compute the height based on + // the top/bottom offsets if they're defined. if (child->isLeadingPositionDefined(YGFlexDirectionColumn) && child->isTrailingPosDefined(YGFlexDirectionColumn)) { childHeight = node->getLayout().measuredDimensions[YGDimensionHeight] - @@ -1961,8 +1958,8 @@ static float YGNodeComputeFlexBasisForChildren( // This function assumes that all the children of node have their // computedFlexBasis properly computed(To do this use -// YGNodeComputeFlexBasisForChildren function). -// This function calculates YGCollectFlexItemsRowMeasurement +// YGNodeComputeFlexBasisForChildren function). This function calculates +// YGCollectFlexItemsRowMeasurement static YGCollectFlexItemsRowValues YGCalculateCollectFlexItemsRowValues( const YGNodeRef& node, const YGDirection ownerDirection, @@ -1998,10 +1995,9 @@ static YGCollectFlexItemsRowValues YGCalculateCollectFlexItemsRowValues( mainAxisownerSize) .unwrap(); - // If this is a multi-line flow and this item pushes us over the - // available size, we've - // hit the end of the current line. Break out of the loop and lay out - // the current line. + // If this is a multi-line flow and this item pushes us over the available + // size, we've hit the end of the current line. Break out of the loop and + // lay out the current line. if (sizeConsumedOnCurrentLineIncludingMinConstraint + flexBasisWithMinAndMaxConstraints + childMarginMainAxis > availableInnerMainDim && @@ -2276,12 +2272,10 @@ static void YGDistributeFreeSpaceFirstPass( if (!YGFloatIsUndefined(baseMainSize) && !YGFloatIsUndefined(boundMainSize) && baseMainSize != boundMainSize) { - // By excluding this item's size and flex factor from remaining, - // this item's - // min/max constraints should also trigger in the second pass - // resulting in the - // item's size calculation being identical in the first and second - // passes. + // By excluding this item's size and flex factor from remaining, this + // item's min/max constraints should also trigger in the second pass + // resulting in the item's size calculation being identical in the + // first and second passes. deltaFreeSpace += boundMainSize - childFlexBasis; collectedFlexItemsValues.totalFlexShrinkScaledFactors -= flexShrinkScaledFactor; @@ -2307,12 +2301,10 @@ static void YGDistributeFreeSpaceFirstPass( if (!YGFloatIsUndefined(baseMainSize) && !YGFloatIsUndefined(boundMainSize) && baseMainSize != boundMainSize) { - // By excluding this item's size and flex factor from remaining, - // this item's - // min/max constraints should also trigger in the second pass - // resulting in the - // item's size calculation being identical in the first and second - // passes. + // By excluding this item's size and flex factor from remaining, this + // item's min/max constraints should also trigger in the second pass + // resulting in the item's size calculation being identical in the + // first and second passes. deltaFreeSpace += boundMainSize - childFlexBasis; collectedFlexItemsValues.totalFlexGrowFactors -= flexGrowFactor; } @@ -2324,25 +2316,22 @@ static void YGDistributeFreeSpaceFirstPass( // Do two passes over the flex items to figure out how to distribute the // remaining space. -// The first pass finds the items whose min/max constraints trigger, -// freezes them at those -// sizes, and excludes those sizes from the remaining space. The second -// pass sets the size -// of each flexible item. It distributes the remaining space amongst the -// items whose min/max -// constraints didn't trigger in pass 1. For the other items, it sets -// their sizes by forcing -// their min/max constraints to trigger again. // -// This two pass approach for resolving min/max constraints deviates from -// the spec. The -// spec (https://www.w3.org/TR/CSS-flexbox-1/#resolve-flexible-lengths) -// describes a process -// that needs to be repeated a variable number of times. The algorithm -// implemented here -// won't handle all cases but it was simpler to implement and it mitigates -// performance -// concerns because we know exactly how many passes it'll do. +// The first pass finds the items whose min/max constraints trigger, freezes +// them at those sizes, and excludes those sizes from the remaining space. +// +// The second pass sets the size of each flexible item. It distributes the +// remaining space amongst the items whose min/max constraints didn't trigger in +// the first pass. For the other items, it sets their sizes by forcing their +// min/max constraints to trigger again. +// +// This two pass approach for resolving min/max constraints deviates from the +// spec. The spec +// (https://www.w3.org/TR/CSS-flexbox-1/#resolve-flexible-lengths) describes a +// process that needs to be repeated a variable number of times. The algorithm +// implemented here won't handle all cases but it was simpler to implement and +// it mitigates performance concerns because we know exactly how many passes +// it'll do. // // At the end of this function the child nodes would have the proper size // assigned to them. @@ -2451,9 +2440,9 @@ static void YGJustifyMainAxis( } } - // In order to position the elements in the main axis, we have two - // controls. The space between the beginning and the first element - // and the space between each two elements. + // In order to position the elements in the main axis, we have two controls. + // The space between the beginning and the first element and the space between + // each two elements. float leadingMainDim = 0; float betweenMainDim = 0; const YGJustify justifyContent = node->getStyle().justifyContent; @@ -2512,8 +2501,8 @@ static void YGJustifyMainAxis( child->isLeadingPositionDefined(mainAxis)) { if (performLayout) { // In case the child is position absolute and has left/top being - // defined, we override the position to whatever the user said - // (and margin/border). + // defined, we override the position to whatever the user said (and + // margin/border). child->setLayoutPosition( child->getLeadingPosition(mainAxis, availableInnerMainDim) .unwrap() + @@ -2523,8 +2512,8 @@ static void YGJustifyMainAxis( } } else { // Now that we placed the element, we need to update the variables. - // We need to do that only for relative elements. Absolute elements - // do not take part in that phase. + // We need to do that only for relative elements. Absolute elements do not + // take part in that phase. if (childStyle.positionType == YGPositionTypeRelative) { if (child->marginLeadingValue(mainAxis).unit == YGUnitAuto) { collectedFlexItemsValues.mainDim += @@ -2547,9 +2536,8 @@ static void YGJustifyMainAxis( bool canSkipFlex = !performLayout && measureModeCrossDim == YGMeasureModeExactly; if (canSkipFlex) { - // If we skipped the flex step, then we can't rely on the - // measuredDims because - // they weren't computed. This means we can't call + // If we skipped the flex step, then we can't rely on the measuredDims + // because they weren't computed. This means we can't call // YGNodeDimWithMargin. collectedFlexItemsValues.mainDim += betweenMainDim + child->getMarginForAxis(mainAxis, availableInnerWidth).unwrap() + @@ -2608,81 +2596,61 @@ static void YGJustifyMainAxis( // // This is the main routine that implements a subset of the flexbox layout -// algorithm -// described in the W3C YG documentation: https://www.w3.org/TR/CSS3-flexbox/. +// algorithm described in the W3C CSS documentation: +// https://www.w3.org/TR/CSS3-flexbox/. // // Limitations of this algorithm, compared to the full standard: // * Display property is always assumed to be 'flex' except for Text nodes, -// which -// are assumed to be 'inline-flex'. +// which are assumed to be 'inline-flex'. // * The 'zIndex' property (or any form of z ordering) is not supported. Nodes -// are -// stacked in document order. +// are stacked in document order. // * The 'order' property is not supported. The order of flex items is always -// defined -// by document order. +// defined by document order. // * The 'visibility' property is always assumed to be 'visible'. Values of -// 'collapse' -// and 'hidden' are not supported. +// 'collapse' and 'hidden' are not supported. // * There is no support for forced breaks. // * It does not support vertical inline directions (top-to-bottom or -// bottom-to-top text). +// bottom-to-top text). // // Deviations from standard: // * Section 4.5 of the spec indicates that all flex items have a default -// minimum -// main size. For text blocks, for example, this is the width of the widest -// word. -// Calculating the minimum width is expensive, so we forego it and assume a -// default -// minimum main size of 0. +// minimum main size. For text blocks, for example, this is the width of the +// widest word. Calculating the minimum width is expensive, so we forego it +// and assume a default minimum main size of 0. // * Min/Max sizes in the main axis are not honored when resolving flexible -// lengths. +// lengths. // * The spec indicates that the default value for 'flexDirection' is 'row', -// but -// the algorithm below assumes a default of 'column'. +// but the algorithm below assumes a default of 'column'. // // Input parameters: // - node: current node to be sized and layed out // - availableWidth & availableHeight: available size to be used for sizing -// the node -// or YGUndefined if the size is not available; interpretation depends on -// layout -// flags +// the node or YGUndefined if the size is not available; interpretation +// depends on layout flags // - ownerDirection: the inline (text) direction within the owner -// (left-to-right or -// right-to-left) +// (left-to-right or right-to-left) // - widthMeasureMode: indicates the sizing rules for the width (see below -// for explanation) +// for explanation) // - heightMeasureMode: indicates the sizing rules for the height (see below -// for explanation) +// for explanation) // - performLayout: specifies whether the caller is interested in just the -// dimensions -// of the node or it requires the entire node and its subtree to be layed -// out -// (with final positions) +// dimensions of the node or it requires the entire node and its subtree to +// be layed out (with final positions) // // Details: // This routine is called recursively to lay out subtrees of flexbox -// elements. It uses the -// information in node.style, which is treated as a read-only input. It is -// responsible for -// setting the layout.direction and layout.measuredDimensions fields for the -// input node as well -// as the layout.position and layout.lineIndex fields for its child nodes. -// The +// elements. It uses the information in node.style, which is treated as a +// read-only input. It is responsible for setting the layout.direction and +// layout.measuredDimensions fields for the input node as well as the +// layout.position and layout.lineIndex fields for its child nodes. The // layout.measuredDimensions field includes any border or padding for the -// node but does -// not include margins. +// node but does not include margins. // // The spec describes four different layout modes: "fill available", "max -// content", "min -// content", -// and "fit content". Of these, we don't use "min content" because we don't -// support default -// minimum main sizes (see above for details). Each of our measure modes maps -// to a layout mode -// from the spec (https://www.w3.org/TR/CSS3-sizing/#terms): +// content", "min content", and "fit content". Of these, we don't use "min +// content" because we don't support default minimum main sizes (see above +// for details). Each of our measure modes maps to a layout mode from the +// spec (https://www.w3.org/TR/CSS3-sizing/#terms): // - YGMeasureModeUndefined: max content // - YGMeasureModeExactly: fill available // - YGMeasureModeAtMost: fit content @@ -2924,9 +2892,9 @@ static void YGNodelayoutImpl( !performLayout && measureModeCrossDim == YGMeasureModeExactly; // STEP 5: RESOLVING FLEXIBLE LENGTHS ON MAIN AXIS - // Calculate the remaining available space that needs to be allocated. - // If the main dimension size isn't known, it is computed based on - // the line length, so there's no more space left to distribute. + // Calculate the remaining available space that needs to be allocated. If + // the main dimension size isn't known, it is computed based on the line + // length, so there's no more space left to distribute. bool sizeBasedOnContent = false; // If we don't measure with exact main dimension we want to ensure we don't @@ -2998,11 +2966,9 @@ static void YGNodelayoutImpl( // STEP 6: MAIN-AXIS JUSTIFICATION & CROSS-AXIS SIZE DETERMINATION // At this point, all the children have their dimensions set in the main - // axis. - // Their dimensions are also set in the cross axis with the exception of - // items - // that are aligned "stretch". We need to compute these stretch values and - // set the final positions. + // axis. Their dimensions are also set in the cross axis with the exception + // of items that are aligned "stretch". We need to compute these stretch + // values and set the final positions. YGJustifyMainAxis( node, @@ -3058,8 +3024,8 @@ static void YGNodelayoutImpl( } if (child->getStyle().positionType == YGPositionTypeAbsolute) { // If the child is absolutely positioned and has a - // top/left/bottom/right set, override - // all the previously computed positions to set it correctly. + // top/left/bottom/right set, override all the previously computed + // positions to set it correctly. const bool isChildLeadingPosDefined = child->isLeadingPositionDefined(crossAxis); if (isChildLeadingPosDefined) { @@ -3090,9 +3056,8 @@ static void YGNodelayoutImpl( const YGAlign alignItem = YGNodeAlignItem(node, child); // If the child uses align stretch, we need to lay it out one more - // time, this time - // forcing the cross-axis size to be the computed cross size for the - // current line. + // time, this time forcing the cross-axis size to be the computed + // cross size for the current line. if (alignItem == YGAlignStretch && child->marginLeadingValue(crossAxis).unit != YGUnitAuto && child->marginTrailingValue(crossAxis).unit != YGUnitAuto) { @@ -3713,8 +3678,8 @@ bool YGNodeCanUseCachedMeasurement( } // -// This is a wrapper around the YGNodelayoutImpl function. It determines -// whether the layout request is redundant and can be skipped. +// This is a wrapper around the YGNodelayoutImpl function. It determines whether +// the layout request is redundant and can be skipped. // // Parameters: // Input parameters are the same as YGNodelayoutImpl (see above) @@ -3753,16 +3718,12 @@ bool YGLayoutNodeInternal( // Determine whether the results are already cached. We maintain a separate // cache for layouts and measurements. A layout operation modifies the - // positions - // and dimensions for nodes in the subtree. The algorithm assumes that each - // node - // gets layed out a maximum of one time per tree layout, but multiple - // measurements - // may be required to resolve all of the flex dimensions. - // We handle nodes with measure functions specially here because they are the - // most - // expensive to measure, so it's worth avoiding redundant measurements if at - // all possible. + // positions and dimensions for nodes in the subtree. The algorithm assumes + // that each node gets layed out a maximum of one time per tree layout, but + // multiple measurements may be required to resolve all of the flex + // dimensions. We handle nodes with measure functions specially here because + // they are the most expensive to measure, so it's worth avoiding redundant + // measurements if at all possible. if (node->getMeasure() != nullptr) { const float marginAxisRow = node->getMarginForAxis(YGFlexDirectionRow, ownerWidth).unwrap(); @@ -4058,10 +4019,8 @@ void YGNodeCalculateLayout( const float ownerHeight, const YGDirection ownerDirection) { // Increment the generation count. This will force the recursive routine to - // visit - // all dirty nodes at least once. Subsequent visits will be skipped if the - // input - // parameters don't change. + // visit all dirty nodes at least once. Subsequent visits will be skipped if + // the input parameters don't change. gCurrentGenerationCount++; node->resolveDimension(); float width = YGUndefined; diff --git a/yoga/Yoga.h b/yoga/Yoga.h index 0c45cbe1..afb4c9be 100644 --- a/yoga/Yoga.h +++ b/yoga/Yoga.h @@ -104,16 +104,16 @@ WIN_EXPORT void YGNodeCalculateLayout( // Mark a node as dirty. Only valid for nodes with a custom measure function // set. -// YG knows when to mark all other nodes as dirty but because nodes with -// measure functions -// depends on information not known to YG they must perform this dirty -// marking manually. +// +// Yoga knows when to mark all other nodes as dirty but because nodes with +// measure functions depend on information not known to Yoga they must perform +// this dirty marking manually. WIN_EXPORT void YGNodeMarkDirty(const YGNodeRef node); -// This function marks the current node and all its descendants as dirty. This -// function is added to test yoga benchmarks. This function is not expected to -// be used in production as calling `YGCalculateLayout` will cause the -// recalculation of each and every node. +// Marks the current node and all its descendants as dirty. +// +// Intended to be used for Uoga benchmarks. Don't use in production, as calling +// `YGCalculateLayout` will cause the recalculation of each and every node. WIN_EXPORT void YGNodeMarkDirtyAndPropogateToDescendants(const YGNodeRef node); WIN_EXPORT void YGNodePrint(const YGNodeRef node, const YGPrintOptions options); @@ -317,20 +317,20 @@ WIN_EXPORT void YGNodeStyleSetMaxHeightPercent( const float maxHeight); WIN_EXPORT YGValue YGNodeStyleGetMaxHeight(const YGNodeRef node); -// Yoga specific properties, not compatible with flexbox specification -// Aspect ratio control the size of the undefined dimension of a node. -// Aspect ratio is encoded as a floating point value width/height. e.g. A value -// of 2 leads to a node with a width twice the size of its height while a value -// of 0.5 gives the opposite effect. +// Yoga specific properties, not compatible with flexbox specification Aspect +// ratio control the size of the undefined dimension of a node. Aspect ratio is +// encoded as a floating point value width/height. e.g. A value of 2 leads to a +// node with a width twice the size of its height while a value of 0.5 gives the +// opposite effect. // // - On a node with a set width/height aspect ratio control the size of the -// unset dimension +// unset dimension // - On a node with a set flex basis aspect ratio controls the size of the node -// in the cross axis if unset +// in the cross axis if unset // - On a node with a measure function aspect ratio works as though the measure -// function measures the flex basis +// function measures the flex basis // - On a node with flex grow/shrink aspect ratio controls the size of the node -// in the cross axis if unset +// in the cross axis if unset // - Aspect ratio takes min/max dimensions into account WIN_EXPORT void YGNodeStyleSetAspectRatio( const YGNodeRef node, @@ -377,8 +377,8 @@ WIN_EXPORT void YGAssertWithConfig( const YGConfigRef config, const 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 +// 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( const YGConfigRef config, const float pixelsInPoint); @@ -409,8 +409,8 @@ WIN_EXPORT bool YGConfigIsExperimentalFeatureEnabled( const YGConfigRef config, const YGExperimentalFeature feature); -// Using the web defaults is the prefered configuration for new projects. -// Usage of non web defaults should be considered as legacy. +// Using the web defaults is the prefered configuration for new projects. Usage +// of non web defaults should be considered as legacy. WIN_EXPORT void YGConfigSetUseWebDefaults( const YGConfigRef config, const bool enabled); -- 2.50.1.windows.1 From 604a9a98a972ae11590e0de4e3e9faac385302fd Mon Sep 17 00:00:00 2001 From: Eduardo Roman Date: Wed, 16 Jan 2019 11:53:04 -0800 Subject: [PATCH 14/18] guard against INF values in CompactValue Summary: After this diff D13403925 that got rid of `-ffast-math` we started to have a very odd behavior on Yoga when using release builds. After digging a while we found that certain set of conditions on O2 and O3 optimization levels was causing Origami to set some `INFINITE` values on Yoga. We found the root of the problem and fix it on Origami side. But I'm wondering if guarding agains `INFINITE` on Yoga side would be good too. Since now Yoga it's not using `-ffast-math` anymore, and I think infinite is not a a valid value anywhere on Yoga side, it seems to support the reason to guard against it. I'm happy to abandon this diff if you guys think this is not a good solution. Reviewed By: davidaurelio Differential Revision: D13679319 fbshipit-source-id: 061448fea9d1a8496362ff07dc46b546e7f1ffa3 --- tests/CompactValueTest.cpp | 12 ++++++++++++ yoga/CompactValue.h | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/CompactValueTest.cpp b/tests/CompactValueTest.cpp index 7d2afc13..d0a2c669 100644 --- a/tests/CompactValueTest.cpp +++ b/tests/CompactValueTest.cpp @@ -29,6 +29,18 @@ TEST(YogaTest, compact_value_can_represent_undefined) { ASSERT_FALSE(c.isAuto()); } +TEST(YogaTest, compact_value_manages_infinity_as_undefined) { + auto c = CompactValue{ + YGValue{std::numeric_limits::infinity(), YGUnitUndefined}}; + YGValue v = c; + ASSERT_EQ(v, YGValueUndefined); + ASSERT_NE(v, YGValueAuto); + ASSERT_NE(v, (YGValue{-1.25, YGUnitPoint})); + ASSERT_NE(v, (YGValue{25, YGUnitPercent})); + ASSERT_TRUE(c.isUndefined()); + ASSERT_FALSE(c.isAuto()); +} + TEST(YogaTest, compact_value_can_represent_auto) { auto c = CompactValue{YGValue{0, YGUnitAuto}}; YGValue v = c; diff --git a/yoga/CompactValue.h b/yoga/CompactValue.h index 9c9a92e0..3cc36a48 100644 --- a/yoga/CompactValue.h +++ b/yoga/CompactValue.h @@ -70,7 +70,8 @@ public: template static CompactValue ofMaybe(float value) noexcept { - return std::isnan(value) ? ofUndefined() : of(value); + return std::isnan(value) || std::isinf(value) ? ofUndefined() + : of(value); } static constexpr CompactValue ofZero() noexcept { -- 2.50.1.windows.1 From 8b8932811e3306c5ed35e566135893ffc72e2617 Mon Sep 17 00:00:00 2001 From: Amir Shalem Date: Thu, 17 Jan 2019 07:33:24 -0800 Subject: [PATCH 15/18] Make Yoga's libfb code to be gcc7 compatible Summary: Make the libfb jni code to be compatible with gcc7 compiler Reviewed By: davidaurelio Differential Revision: D13711390 fbshipit-source-id: 9a378a3ee4feb7e928dd97dae7d79a41f0658992 --- lib/fb/src/main/cpp/jni/Exceptions.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/fb/src/main/cpp/jni/Exceptions.cpp b/lib/fb/src/main/cpp/jni/Exceptions.cpp index 8688dfbf..73166c04 100644 --- a/lib/fb/src/main/cpp/jni/Exceptions.cpp +++ b/lib/fb/src/main/cpp/jni/Exceptions.cpp @@ -219,11 +219,11 @@ void translatePendingCppExceptionToJavaException() noexcept { setJavaExceptionAndAbortOnFailure(previous); } catch (std::exception& e) { FBLOGE("unexpected exception in translatePendingCppExceptionToJavaException: %s", e.what()); - // rethrow the exception and let the noexcept handling abort. - throw; + // std::terminate will print the message of the pending exception e + std::terminate(); } catch (...) { FBLOGE("unexpected exception in translatePendingCppExceptionToJavaException"); - throw; + std::terminate(); } } -- 2.50.1.windows.1 From 5a9f29debbe9cfe4a40576143647861fa56664f2 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Thu, 24 Jan 2019 06:28:01 -0800 Subject: [PATCH 16/18] Delete functionality for shared childen Summary: @public Removes `YGNodeInsertSharedChild` / `addSharedChildAt`. This functionality is unused, and can cause memory leaks. Reviewed By: SidharthGuglani Differential Revision: D13711105 fbshipit-source-id: 86206c05393b3f1a497e6b046006f94ead88c6ce --- java/com/facebook/yoga/YogaNode.java | 20 +++--------- java/jni/YGJNI.cpp | 9 ------ .../tests/com/facebook/yoga/YogaNodeTest.java | 32 +++---------------- yoga/Yoga.cpp | 14 -------- yoga/Yoga.h | 10 ------ 5 files changed, 8 insertions(+), 77 deletions(-) diff --git a/java/com/facebook/yoga/YogaNode.java b/java/com/facebook/yoga/YogaNode.java index 7dd26947..a8168cf1 100644 --- a/java/com/facebook/yoga/YogaNode.java +++ b/java/com/facebook/yoga/YogaNode.java @@ -1,9 +1,8 @@ -/* - * Copyright (c) Facebook, Inc. - * - * This source code is licensed under the MIT license found in the LICENSE - * file in the root directory of this source tree. +/** + * 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. */ package com.facebook.yoga; @@ -174,17 +173,6 @@ public class YogaNode implements Cloneable { jni_YGNodeInsertChild(mNativePointer, child.mNativePointer, i); } - private static native void jni_YGNodeInsertSharedChild(long nativePointer, long childPointer, int index); - - public void addSharedChildAt(YogaNode child, int i) { - if (mChildren == null) { - mChildren = new ArrayList<>(4); - } - mChildren.add(i, child); - child.mOwner = null; - jni_YGNodeInsertSharedChild(mNativePointer, child.mNativePointer, i); - } - private static native void jni_YGNodeSetIsReferenceBaseline(long nativePointer, boolean isReferenceBaseline); public void setIsReferenceBaseline(boolean isReferenceBaseline) { diff --git a/java/jni/YGJNI.cpp b/java/jni/YGJNI.cpp index ea0d9d07..915876b5 100644 --- a/java/jni/YGJNI.cpp +++ b/java/jni/YGJNI.cpp @@ -359,14 +359,6 @@ void jni_YGNodeInsertChild( _jlong2YGNodeRef(nativePointer), _jlong2YGNodeRef(childPointer), index); } -void jni_YGNodeInsertSharedChild( - jlong nativePointer, - jlong childPointer, - jint index) { - YGNodeInsertSharedChild( - _jlong2YGNodeRef(nativePointer), _jlong2YGNodeRef(childPointer), index); -} - void jni_YGNodeRemoveChild(jlong nativePointer, jlong childPointer) { YGNodeRemoveChild( _jlong2YGNodeRef(nativePointer), _jlong2YGNodeRef(childPointer)); @@ -672,7 +664,6 @@ jint JNI_OnLoad(JavaVM* vm, void*) { YGMakeCriticalNativeMethod(jni_YGNodeReset), YGMakeCriticalNativeMethod(jni_YGNodeClearChildren), YGMakeCriticalNativeMethod(jni_YGNodeInsertChild), - YGMakeCriticalNativeMethod(jni_YGNodeInsertSharedChild), YGMakeCriticalNativeMethod(jni_YGNodeRemoveChild), YGMakeCriticalNativeMethod(jni_YGNodeSetIsReferenceBaseline), YGMakeCriticalNativeMethod(jni_YGNodeIsReferenceBaseline), diff --git a/java/tests/com/facebook/yoga/YogaNodeTest.java b/java/tests/com/facebook/yoga/YogaNodeTest.java index a792fe52..4b8a2ce5 100644 --- a/java/tests/com/facebook/yoga/YogaNodeTest.java +++ b/java/tests/com/facebook/yoga/YogaNodeTest.java @@ -1,9 +1,8 @@ -/* - * 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. +/** + * 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. */ package com.facebook.yoga; @@ -280,29 +279,6 @@ public class YogaNodeTest { assertEquals(1, child.getChildCount()); } - @Test - public void testAddSharedChildCloneWithNewChildren() throws Exception { - YogaConfig config = new YogaConfig(); - YogaNode root = createNode(config); - YogaNode child = createNode(config); - YogaNode grandChild = createNode(config); - root.addChildAt(child, 0); - child.addChildAt(grandChild, 0); - child.setFlexDirection(YogaFlexDirection.ROW); - - YogaNode clonedChild = child.cloneWithNewChildren(); - - assertNotSame(clonedChild, child); - assertEquals(YogaFlexDirection.ROW, clonedChild.getFlexDirection()); - assertEquals(child.getFlexDirection(), clonedChild.getFlexDirection()); - assertEquals(0, clonedChild.getChildCount()); - assertEquals(1, child.getChildCount()); - - clonedChild.addSharedChildAt(grandChild, 0); - assertEquals(1, clonedChild.getChildCount()); - assertNull(grandChild.getOwner()); - } - @Test public void testCloneNodeListener() throws Exception { final AtomicBoolean onNodeClonedExecuted = new AtomicBoolean(false); diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index 38287e76..357e137b 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -402,20 +402,6 @@ void YGNodeInsertChild( node->markDirtyAndPropogate(); } -void YGNodeInsertSharedChild( - const YGNodeRef node, - const YGNodeRef child, - const uint32_t index) { - YGAssertWithNode( - node, - node->getMeasure() == nullptr, - "Cannot add child: Nodes with measure functions cannot have children."); - - node->insertChild(child, index); - child->setOwner(nullptr); - node->markDirtyAndPropogate(); -} - void YGNodeRemoveChild(const YGNodeRef owner, const YGNodeRef excludedChild) { // This algorithm is a forked variant from cloneChildrenIfNeeded in YGNode // that excludes a child. diff --git a/yoga/Yoga.h b/yoga/Yoga.h index afb4c9be..b15f8bb2 100644 --- a/yoga/Yoga.h +++ b/yoga/Yoga.h @@ -69,16 +69,6 @@ WIN_EXPORT void YGNodeInsertChild( const YGNodeRef child, const uint32_t index); -// This function inserts the child YGNodeRef as a children of the node received -// by parameter and set the Owner of the child object to null. This function is -// expected to be called when using Yoga in persistent mode in order to share a -// YGNodeRef object as a child of two different Yoga trees. The child YGNodeRef -// is expected to be referenced from its original owner and from a clone of its -// original owner. -WIN_EXPORT void YGNodeInsertSharedChild( - const YGNodeRef node, - const YGNodeRef child, - const uint32_t index); WIN_EXPORT void YGNodeRemoveChild(const YGNodeRef node, const YGNodeRef child); WIN_EXPORT void YGNodeRemoveAllChildren(const YGNodeRef node); WIN_EXPORT YGNodeRef YGNodeGetChild(const YGNodeRef node, const uint32_t index); -- 2.50.1.windows.1 From 7a930957a9ddb69fb10abbf9a03aa9a0e7e7ec07 Mon Sep 17 00:00:00 2001 From: Reinier Hartog Date: Tue, 29 Jan 2019 10:28:35 +0100 Subject: [PATCH 17/18] Port YGDirtiedTest to JavaScript --- .../tests/Facebook.Yoga/YGDirtiedTest.js | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 javascript/tests/Facebook.Yoga/YGDirtiedTest.js diff --git a/javascript/tests/Facebook.Yoga/YGDirtiedTest.js b/javascript/tests/Facebook.Yoga/YGDirtiedTest.js new file mode 100644 index 00000000..460a04c4 --- /dev/null +++ b/javascript/tests/Facebook.Yoga/YGDirtiedTest.js @@ -0,0 +1,126 @@ +/** + * 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. + */ + +var Yoga = Yoga || require("../../sources/entry-" + process.env.TEST_ENTRY); + +it("dirtied", function() { + var root = Yoga.Node.create(); + root.setAlignItems(Yoga.ALIGN_FLEX_START); + root.setWidth(100); + root.setHeight(100); + + root.calculateLayout(undefined, undefined, Yoga.DIRECTION_LTR); + + let dirtied = 0; + root.setDirtiedFunc(function() { dirtied++; }); + // only nodes with a measure function can be marked dirty + root.setMeasureFunc(function() {}); + + console.assert(0 === dirtied, "0 === dirtied"); + + // dirtied func MUST be called in case of explicit dirtying. + root.markDirty(); + console.assert(1 === dirtied, "1 === dirtied"); + + // dirtied func MUST be called ONCE. + root.markDirty(); + console.assert(1 === dirtied, "1 === dirtied"); + + if (typeof root !== "undefined") + root.freeRecursive(); + + typeof gc !== "undefined" && gc(); + console.assert(0 === Yoga.getInstanceCount(), "0 === Yoga.getInstanceCount() (" + Yoga.getInstanceCount() + ")"); +}); + +it("dirtied_propagation", function() { + var root = Yoga.Node.create(); + root.setAlignItems(Yoga.ALIGN_FLEX_START); + root.setWidth(100); + root.setHeight(100); + + var root_child0 = Yoga.Node.create(); + root_child0.setAlignItems(Yoga.ALIGN_FLEX_START); + root_child0.setWidth(50); + root_child0.setHeight(20); + root_child0.setMeasureFunc(function() {}); + root.insertChild(root_child0, 0); + + var root_child1 = Yoga.Node.create(); + root_child1.setAlignItems(Yoga.ALIGN_FLEX_START); + root_child1.setWidth(50); + root_child1.setHeight(20); + root.insertChild(root_child1, 0); + + root.calculateLayout(undefined, undefined, Yoga.DIRECTION_LTR); + + let dirtied = 0; + root.setDirtiedFunc(function() { dirtied++; }); + + console.assert(0 === dirtied, "0 === dirtied"); + + // dirtied func MUST be called for the first time. + root_child0.markDirty(); + console.assert(1 === dirtied, "1 === dirtied"); + + // dirtied func must NOT be called for the second time. + root_child0.markDirty(); + console.assert(1 === dirtied, "1 === dirtied"); + + if (typeof root !== "undefined") + root.freeRecursive(); + + typeof gc !== "undefined" && gc(); + console.assert(0 === Yoga.getInstanceCount(), "0 === Yoga.getInstanceCount() (" + Yoga.getInstanceCount() + ")"); +}); + +it("dirtied_hierarchy", function() { + var root = Yoga.Node.create(); + root.setAlignItems(Yoga.ALIGN_FLEX_START); + root.setWidth(100); + root.setHeight(100); + + var root_child0 = Yoga.Node.create(); + root_child0.setAlignItems(Yoga.ALIGN_FLEX_START); + root_child0.setWidth(50); + root_child0.setHeight(20); + root_child0.setMeasureFunc(function() {}); + root.insertChild(root_child0, 0); + + var root_child1 = Yoga.Node.create(); + root_child1.setAlignItems(Yoga.ALIGN_FLEX_START); + root_child1.setWidth(50); + root_child1.setHeight(20); + root_child1.setMeasureFunc(function() {}); + root.insertChild(root_child1, 0); + + root.calculateLayout(undefined, undefined, Yoga.DIRECTION_LTR); + + let dirtied = 0; + root_child0.setDirtiedFunc(function() { + dirtied++; + }); + + console.assert(0 === dirtied, "0 === dirtied"); + + // dirtied func must NOT be called for descendants. + // NOTE: nodes without a measure function cannot be marked dirty manually, + // but nodes with a measure function can not have children. + // Update the width to dirty the node instead. + root.setWidth(110); + console.assert(0 === dirtied, "0 === dirtied"); + + // dirtied func MUST be called in case of explicit dirtying. + root_child0.markDirty(); + console.assert(1 === dirtied, "1 === dirtied"); + + if (typeof root !== "undefined") + root.freeRecursive(); + + typeof gc !== "undefined" && gc(); + console.assert(0 === Yoga.getInstanceCount(), "0 === Yoga.getInstanceCount() (" + Yoga.getInstanceCount() + ")"); +}); -- 2.50.1.windows.1 From 79cdd9a2d71aac843085d2a0a6c2fe9c5ab191ad Mon Sep 17 00:00:00 2001 From: Reinier Hartog Date: Tue, 29 Jan 2019 13:06:07 +0100 Subject: [PATCH 18/18] Add test for 'reset' of a node --- javascript/sources/Node.cc | 2 + .../tests/Facebook.Yoga/YGDirtiedTest.js | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/javascript/sources/Node.cc b/javascript/sources/Node.cc index 2268c805..748a6cd0 100644 --- a/javascript/sources/Node.cc +++ b/javascript/sources/Node.cc @@ -53,6 +53,7 @@ static void globalDirtiedFunc(YGNodeRef nodeRef) Node::Node(Config * config) : m_node(config != nullptr ? YGNodeNewWithConfig(config->m_config) : YGNodeNew()) , m_measureFunc(nullptr) +, m_dirtiedFunc(nullptr) { YGNodeSetContext(m_node, reinterpret_cast(this)); } @@ -65,6 +66,7 @@ Node::~Node(void) void Node::reset(void) { m_measureFunc.reset(nullptr); + m_dirtiedFunc.reset(nullptr); YGNodeReset(m_node); } diff --git a/javascript/tests/Facebook.Yoga/YGDirtiedTest.js b/javascript/tests/Facebook.Yoga/YGDirtiedTest.js index 460a04c4..20320c43 100644 --- a/javascript/tests/Facebook.Yoga/YGDirtiedTest.js +++ b/javascript/tests/Facebook.Yoga/YGDirtiedTest.js @@ -124,3 +124,45 @@ it("dirtied_hierarchy", function() { typeof gc !== "undefined" && gc(); console.assert(0 === Yoga.getInstanceCount(), "0 === Yoga.getInstanceCount() (" + Yoga.getInstanceCount() + ")"); }); + +it("dirtied_reset", function() { + var root = Yoga.Node.create(); + root.setAlignItems(Yoga.ALIGN_FLEX_START); + root.setWidth(100); + root.setHeight(100); + root.setMeasureFunc(function() {}); + + root.calculateLayout(undefined, undefined, Yoga.DIRECTION_LTR); + + let dirtied = 0; + root.setDirtiedFunc(function() { + dirtied++; + }); + + console.assert(0 === dirtied, "0 === dirtied"); + + // dirtied func MUST be called in case of explicit dirtying. + root.markDirty(); + console.assert(1 === dirtied, "1 === dirtied"); + + // recalculate so the root is no longer dirty + root.calculateLayout(undefined, undefined, Yoga.DIRECTION_LTR); + + root.reset(); + root.setAlignItems(Yoga.ALIGN_FLEX_START); + root.setWidth(100); + root.setHeight(100); + root.setMeasureFunc(function() {}); + + root.markDirty(); + + // dirtied func must NOT be called after reset. + root.markDirty(); + console.assert(1 === dirtied, "1 === dirtied"); + + if (typeof root !== "undefined") + root.freeRecursive(); + + typeof gc !== "undefined" && gc(); + console.assert(0 === Yoga.getInstanceCount(), "0 === Yoga.getInstanceCount() (" + Yoga.getInstanceCount() + ")"); +}); -- 2.50.1.windows.1