From 4f9a3bc8f6af6a96e196cc1e520146915e65c10f Mon Sep 17 00:00:00 2001 From: Kevin Gozali Date: Wed, 5 Dec 2018 15:00:35 -0800 Subject: [PATCH] xplat: pass through config object to fabric UIManager layer Summary: For configuration purpose, pass down config object from the hosting app and use it in UITemplateProcessor. Reviewed By: sahrens Differential Revision: D13290322 fbshipit-source-id: 8bb6d7f5a3f977b873e548e15603259876b46dc8 --- ReactCommon/fabric/uimanager/BUCK | 2 ++ ReactCommon/fabric/uimanager/Scheduler.cpp | 9 +++-- ReactCommon/fabric/uimanager/Scheduler.h | 2 ++ .../fabric/uimanager/UITemplateProcessor.cpp | 17 ++++++---- .../fabric/uimanager/UITemplateProcessor.h | 7 ++-- .../tests/UITemplateProcessorTest.cpp | 33 +++++++++++++++++-- 6 files changed, 55 insertions(+), 15 deletions(-) diff --git a/ReactCommon/fabric/uimanager/BUCK b/ReactCommon/fabric/uimanager/BUCK index 286c17ac7..dc23ab77f 100644 --- a/ReactCommon/fabric/uimanager/BUCK +++ b/ReactCommon/fabric/uimanager/BUCK @@ -58,6 +58,7 @@ rn_xplat_cxx_library( "xplat//jsi:JSIDynamic", "xplat//jsi:jsi", "xplat//third-party/glog:glog", + react_native_xplat_target("config:config"), react_native_xplat_target("fabric/components/root:root"), react_native_xplat_target("fabric/components/view:view"), react_native_xplat_target("fabric/mounting:mounting"), @@ -83,6 +84,7 @@ fb_xplat_cxx_test( "xplat//folly:molly", "xplat//third-party/gmock:gtest", ":uimanager", + react_native_xplat_target("config:config"), react_native_xplat_target("fabric/components/activityindicator:activityindicator"), react_native_xplat_target("fabric/components/image:image"), react_native_xplat_target("fabric/components/root:root"), diff --git a/ReactCommon/fabric/uimanager/Scheduler.cpp b/ReactCommon/fabric/uimanager/Scheduler.cpp index f3f8f76dc..80040655d 100644 --- a/ReactCommon/fabric/uimanager/Scheduler.cpp +++ b/ReactCommon/fabric/uimanager/Scheduler.cpp @@ -28,10 +28,12 @@ Scheduler::Scheduler(const SharedContextContainer &contextContainer) runtimeExecutor_ = contextContainer->getInstance("runtime-executor"); + reactNativeConfig_ = + contextContainer->getInstance>(); + auto uiManager = std::make_unique(); auto &uiManagerRef = *uiManager; - uiManagerBinding_ = - std::make_shared(std::move(uiManager)); + uiManagerBinding_ = std::make_shared(std::move(uiManager)); auto eventPipe = [uiManagerBinding = uiManagerBinding_.get()]( jsi::Runtime &runtime, @@ -93,7 +95,8 @@ void Scheduler::renderTemplateToSurface( surfaceId, folly::dynamic::object(), *componentDescriptorRegistry_, - nMR); + nMR, + reactNativeConfig_); shadowTreeRegistry_.visit(surfaceId, [=](const ShadowTree &shadowTree) { shadowTree.complete( diff --git a/ReactCommon/fabric/uimanager/Scheduler.h b/ReactCommon/fabric/uimanager/Scheduler.h index ac7981333..ff45fa9ae 100644 --- a/ReactCommon/fabric/uimanager/Scheduler.h +++ b/ReactCommon/fabric/uimanager/Scheduler.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -94,6 +95,7 @@ class Scheduler final : public UIManagerDelegate, public ShadowTreeDelegate { SharedContextContainer contextContainer_; RuntimeExecutor runtimeExecutor_; std::shared_ptr uiManagerBinding_; + std::shared_ptr reactNativeConfig_; }; } // namespace react diff --git a/ReactCommon/fabric/uimanager/UITemplateProcessor.cpp b/ReactCommon/fabric/uimanager/UITemplateProcessor.cpp index 2e6aad3c8..ce1a4f5cb 100644 --- a/ReactCommon/fabric/uimanager/UITemplateProcessor.cpp +++ b/ReactCommon/fabric/uimanager/UITemplateProcessor.cpp @@ -38,7 +38,8 @@ SharedShadowNode UITemplateProcessor::runCommand( std::vector &nodes, std::vector ®isters, const ComponentDescriptorRegistry &componentDescriptorRegistry, - const NativeModuleRegistry &nativeModuleRegistry) { + const NativeModuleRegistry &nativeModuleRegistry, + const std::shared_ptr reactNativeConfig) { const std::string &opcode = command[0].asString(); const int tagOffset = 420000; // TODO: change to integer codes and a switch statement @@ -61,9 +62,8 @@ SharedShadowNode UITemplateProcessor::runCommand( return nodes[command[1].asInt()]; } else if (opcode == "loadNativeBool") { int registerNumber = command[1].asInt(); - const folly::dynamic &value = nativeModuleRegistry.call( - command[2].asString(), command[3].asString(), command[4]); - registers[registerNumber] = value.asBool(); + std::string param = command[4][0].asString(); + registers[registerNumber] = reactNativeConfig->getBool(param); } else if (opcode == "conditional") { int registerNumber = command[1].asInt(); auto conditionDynamic = registers[registerNumber]; @@ -89,7 +89,8 @@ SharedShadowNode UITemplateProcessor::runCommand( nodes, registers, componentDescriptorRegistry, - nativeModuleRegistry); + nativeModuleRegistry, + reactNativeConfig); } } else { throw std::runtime_error("Unsupported opcode: " + command[0].asString()); @@ -102,7 +103,8 @@ SharedShadowNode UITemplateProcessor::buildShadowTree( Tag rootTag, const folly::dynamic ¶ms, const ComponentDescriptorRegistry &componentDescriptorRegistry, - const NativeModuleRegistry &nativeModuleRegistry) { + const NativeModuleRegistry &nativeModuleRegistry, + const std::shared_ptr reactNativeConfig) { LOG(INFO) << "(strt) UITemplateProcessor inject hardcoded 'server rendered' view tree"; @@ -129,7 +131,8 @@ SharedShadowNode UITemplateProcessor::buildShadowTree( nodes, registers, componentDescriptorRegistry, - nativeModuleRegistry); + nativeModuleRegistry, + reactNativeConfig); if (ret != nullptr) { return ret; } diff --git a/ReactCommon/fabric/uimanager/UITemplateProcessor.h b/ReactCommon/fabric/uimanager/UITemplateProcessor.h index a478eb6f0..d3ad9c3fc 100644 --- a/ReactCommon/fabric/uimanager/UITemplateProcessor.h +++ b/ReactCommon/fabric/uimanager/UITemplateProcessor.h @@ -11,6 +11,7 @@ #include +#include #include #include #include @@ -48,7 +49,8 @@ class UITemplateProcessor { int rootTag, const folly::dynamic ¶ms, const ComponentDescriptorRegistry &componentDescriptorRegistry, - const NativeModuleRegistry &nativeModuleRegistry); + const NativeModuleRegistry &nativeModuleRegistry, + const std::shared_ptr reactNativeConfig); private: static SharedShadowNode runCommand( @@ -57,7 +59,8 @@ class UITemplateProcessor { std::vector &nodes, std::vector ®isters, const ComponentDescriptorRegistry &componentDescriptorRegistry, - const NativeModuleRegistry &nativeModuleRegistry); + const NativeModuleRegistry &nativeModuleRegistry, + const std::shared_ptr reactNativeConfig); }; } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/uimanager/tests/UITemplateProcessorTest.cpp b/ReactCommon/fabric/uimanager/tests/UITemplateProcessorTest.cpp index d1a52576d..377e5daa3 100644 --- a/ReactCommon/fabric/uimanager/tests/UITemplateProcessorTest.cpp +++ b/ReactCommon/fabric/uimanager/tests/UITemplateProcessorTest.cpp @@ -20,6 +20,7 @@ using namespace facebook::react; #include #include #include +#include #include namespace facebook { @@ -63,6 +64,29 @@ NativeModuleRegistry buildNativeModuleRegistry() { return nMR; } +class MockReactNativeConfig : public ReactNativeConfig { + public: + MockReactNativeConfig() {} + bool getBool(const std::string ¶m) const override { + return mockSimpleTestValue_; + } + + std::string getString(const std::string ¶m) const override { + return ""; + } + + int64_t getInt64(const std::string ¶m) const override { + return 0; + } + + double getDouble(const std::string ¶m) const override { + return 0.0; + } +}; + +std::shared_ptr mockReactNativeConfig_ = + std::make_shared(); + } // namespace react } // namespace facebook @@ -85,7 +109,8 @@ TEST(UITemplateProcessorTest, testSimpleBytecode) { surfaceId, folly::dynamic::object(), *componentDescriptorRegistry, - nativeModuleRegistry); + nativeModuleRegistry, + mockReactNativeConfig_); LOG(INFO) << std::endl << root1->getDebugDescription(); auto props1 = std::dynamic_pointer_cast(root1->getProps()); ASSERT_NEAR(props1->opacity, 0.5, 0.001); @@ -120,7 +145,8 @@ TEST(UITemplateProcessorTest, testConditionalBytecode) { surfaceId, folly::dynamic::object(), *componentDescriptorRegistry, - nativeModuleRegistry); + nativeModuleRegistry, + mockReactNativeConfig_); LOG(INFO) << std::endl << root1->getDebugDescription(); auto props1 = std::dynamic_pointer_cast(root1->getProps()); ASSERT_STREQ(props1->testId.c_str(), "root"); @@ -137,7 +163,8 @@ TEST(UITemplateProcessorTest, testConditionalBytecode) { surfaceId, folly::dynamic::object(), *componentDescriptorRegistry, - nativeModuleRegistry); + nativeModuleRegistry, + mockReactNativeConfig_); auto child_props2 = std::dynamic_pointer_cast( root2->getChildren().at(0)->getProps()); ASSERT_STREQ(child_props2->testId.c_str(), "cond_false");