From e1876af923647ff8c10057414cf502e40feafdc6 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Sun, 7 May 2023 01:53:04 -0700 Subject: [PATCH] Refactor MobileConfig Access for "react_fabric: treat_auto_as_undefined" (#37208) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/37208 Reviewed By: javache Differential Revision: D45434603 fbshipit-source-id: 9081ea11c87a05e0d8ff95e55d8aa8bcd52b4c39 --- .../renderer/components/view/conversions.h | 10 ++----- .../react/renderer/core/CMakeLists.txt | 1 + .../renderer/core/PropsParserContext.cpp | 26 +++++++++++++++++++ .../react/renderer/core/PropsParserContext.h | 13 ++++++++++ 4 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 packages/react-native/ReactCommon/react/renderer/core/PropsParserContext.cpp diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h b/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h index 8d73e198fa5360..85191d843731df 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h +++ b/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include @@ -408,19 +407,14 @@ inline void fromRawValue( const PropsParserContext &context, const RawValue &value, YGStyle::ValueRepr &result) { - // For bug compatibility, pass "auto" as YGValueUndefined - static bool treatAutoAsUndefined = - context.contextContainer - .at>("ReactNativeConfig") - ->getBool("react_fabric:treat_auto_as_undefined"); - if (value.hasType()) { result = yogaStyleValueFromFloat((Float)value); return; } else if (value.hasType()) { const auto stringValue = (std::string)value; if (stringValue == "auto") { - result = treatAutoAsUndefined ? YGValueUndefined : YGValueAuto; + result = context.treatAutoAsYGValueUndefined() ? YGValueUndefined + : YGValueAuto; return; } else { if (stringValue.back() == '%') { diff --git a/packages/react-native/ReactCommon/react/renderer/core/CMakeLists.txt b/packages/react-native/ReactCommon/react/renderer/core/CMakeLists.txt index d776881dda16b4..cbbaa6f1f3999e 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/CMakeLists.txt +++ b/packages/react-native/ReactCommon/react/renderer/core/CMakeLists.txt @@ -23,6 +23,7 @@ target_link_libraries(react_render_core folly_runtime glog jsi + react_config react_debug react_render_debug react_render_graphics diff --git a/packages/react-native/ReactCommon/react/renderer/core/PropsParserContext.cpp b/packages/react-native/ReactCommon/react/renderer/core/PropsParserContext.cpp new file mode 100644 index 00000000000000..486323237c5510 --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/core/PropsParserContext.cpp @@ -0,0 +1,26 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include "PropsParserContext.h" + +#include + +namespace facebook::react { + +bool PropsParserContext::treatAutoAsYGValueUndefined() const { + if (treatAutoAsYGValueUndefined_ == std::nullopt) { + auto config = contextContainer.at>( + "ReactNativeConfig"); + treatAutoAsYGValueUndefined_ = config + ? config->getBool("react_fabric:treat_auto_as_undefined") + : false; + } + + return *treatAutoAsYGValueUndefined_; +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/core/PropsParserContext.h b/packages/react-native/ReactCommon/react/renderer/core/PropsParserContext.h index 04ec16f72f19e0..f79bd86b01e8de 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/PropsParserContext.h +++ b/packages/react-native/ReactCommon/react/renderer/core/PropsParserContext.h @@ -7,6 +7,8 @@ #pragma once +#include + #include #include @@ -16,12 +18,23 @@ namespace facebook::react { // It should be used as infrequently as possible - most props can and should // be parsed without any context. struct PropsParserContext { + PropsParserContext( + SurfaceId const surfaceId, + ContextContainer const &contextContainer) + : surfaceId(surfaceId), contextContainer(contextContainer) {} + // Non-copyable PropsParserContext(const PropsParserContext &) = delete; PropsParserContext &operator=(const PropsParserContext &) = delete; SurfaceId const surfaceId; ContextContainer const &contextContainer; + + // Temporary feature flags + bool treatAutoAsYGValueUndefined() const; + + private: + mutable std::optional treatAutoAsYGValueUndefined_; }; } // namespace facebook::react