mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: * Adds parent tag as param for createNode in place of explicit appendChild commands. * Adds version info to bytecode * Adds native conditional support: Conditionals are represented in product code with the new `NativeConditional` React component. It takes params necessary to construct a native function call, and takes a render prop as a child that passes the value of the native call as an arg. In prod, the component would actually call the native module and render with that value, but in jest we render for *both* true and false and set them as children of a new jest-only primitive/host component which we special-case and generate a special command with `OP_CODE.conditional`, generate the appropriate bytecode commands for each branch, and embed them as args in the conditional OP_CODE command. When evaluating the bytecode, only one set of commands is executed, based on the native module value (which is evaluated with another new opcode which computes the value and stuffs it in a "register"). Obviously generating this bytecode is kind of a cludge compared to prepack, but when I asked @[501709947:Dominic] about it, he said they had no bytecode spec right now, so I'm running ahead with this prototype. The main thing I'm focused on is the C++/RN bytecode interpretter - this jest stuff is just a way to generate bytecode for it to consume which could be replaced or augmented with many other approaches, such as prepack, server rendering, etc. Also piggybacked a bunch of other cleanup. Reviewed By: shergin Differential Revision: D10277121 fbshipit-source-id: 15d3217a59ef481b574c742d17d8a7dc893cba90
145 lines
5.1 KiB
C++
145 lines
5.1 KiB
C++
/**
|
|
* 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.
|
|
*/
|
|
|
|
#include <exception>
|
|
|
|
#include <fabric/uimanager/ComponentDescriptorFactory.h>
|
|
#include <fabric/uimanager/ReactBytecodeInterpreter.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
using namespace facebook::react;
|
|
|
|
#include <fabric/components/activityindicator/ActivityIndicatorViewComponentDescriptor.h>
|
|
#include <fabric/components/image/ImageComponentDescriptor.h>
|
|
#include <fabric/components/scrollview/ScrollViewComponentDescriptor.h>
|
|
#include <fabric/components/text/ParagraphComponentDescriptor.h>
|
|
#include <fabric/components/text/RawTextComponentDescriptor.h>
|
|
#include <fabric/components/text/TextComponentDescriptor.h>
|
|
#include <fabric/components/view/ViewComponentDescriptor.h>
|
|
#include <fabric/uimanager/ComponentDescriptorRegistry.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
SharedComponentDescriptorRegistry ComponentDescriptorFactory::buildRegistry(
|
|
const SharedEventDispatcher &eventDispatcher,
|
|
const SharedContextContainer &contextContainer) {
|
|
auto registry = std::make_shared<ComponentDescriptorRegistry>();
|
|
registry->registerComponentDescriptor(
|
|
std::make_shared<ViewComponentDescriptor>(eventDispatcher));
|
|
registry->registerComponentDescriptor(
|
|
std::make_shared<ImageComponentDescriptor>(
|
|
eventDispatcher, contextContainer));
|
|
registry->registerComponentDescriptor(
|
|
std::make_shared<ScrollViewComponentDescriptor>(eventDispatcher));
|
|
registry->registerComponentDescriptor(
|
|
std::make_shared<ParagraphComponentDescriptor>(
|
|
eventDispatcher, contextContainer));
|
|
registry->registerComponentDescriptor(
|
|
std::make_shared<TextComponentDescriptor>(eventDispatcher));
|
|
registry->registerComponentDescriptor(
|
|
std::make_shared<RawTextComponentDescriptor>(eventDispatcher));
|
|
registry->registerComponentDescriptor(
|
|
std::make_shared<ActivityIndicatorViewComponentDescriptor>(
|
|
eventDispatcher));
|
|
return registry;
|
|
}
|
|
|
|
bool mockSimpleTestValue_;
|
|
|
|
NativeModuleRegistry buildNativeModuleRegistry();
|
|
|
|
NativeModuleRegistry buildNativeModuleRegistry() {
|
|
NativeModuleRegistry nMR;
|
|
nMR.registerModule(
|
|
"MobileConfig",
|
|
[&](const std::string &methodName, const folly::dynamic &args) {
|
|
return mockSimpleTestValue_;
|
|
});
|
|
return nMR;
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|
|
|
|
TEST(ReactBytecodeInterpreterTest, testSimpleBytecode) {
|
|
auto surfaceId = 11;
|
|
auto componentDescriptorRegistry =
|
|
ComponentDescriptorFactory::buildRegistry(nullptr, nullptr);
|
|
auto nativeModuleRegistry = buildNativeModuleRegistry();
|
|
|
|
auto bytecode = R"delim({"version":0.1,"commands":[
|
|
["createNode",2,"RCTView",-1,{"opacity": 0.5, "testId": "root"}],
|
|
["createNode",4,"RCTView",2,{"testId": "child"}],
|
|
["childSetNode",2]
|
|
]})delim";
|
|
|
|
mockSimpleTestValue_ = true;
|
|
|
|
auto root1 = ReactBytecodeInterpreter::buildShadowTree(
|
|
bytecode,
|
|
surfaceId,
|
|
folly::dynamic::object(),
|
|
*componentDescriptorRegistry,
|
|
nativeModuleRegistry);
|
|
LOG(INFO) << std::endl << root1->getDebugDescription();
|
|
auto props1 = std::dynamic_pointer_cast<const ViewProps>(root1->getProps());
|
|
ASSERT_NEAR(props1->opacity, 0.5, 0.001);
|
|
ASSERT_STREQ(props1->testId.c_str(), "root");
|
|
auto children1 = root1->getChildren();
|
|
ASSERT_EQ(children1.size(), 1);
|
|
auto child_props1 =
|
|
std::dynamic_pointer_cast<const ViewProps>(children1.at(0)->getProps());
|
|
ASSERT_STREQ(child_props1->testId.c_str(), "child");
|
|
}
|
|
|
|
TEST(ReactBytecodeInterpreterTest, testConditionalBytecode) {
|
|
auto surfaceId = 11;
|
|
auto componentDescriptorRegistry =
|
|
ComponentDescriptorFactory::buildRegistry(nullptr, nullptr);
|
|
auto nativeModuleRegistry = buildNativeModuleRegistry();
|
|
|
|
auto bytecode = R"delim({"version":0.1,"commands":[
|
|
["createNode",2,"RCTView",-1,{"testId": "root"}],
|
|
["loadNativeBool",1,"MobileConfig","getBool",["qe:simple_test"]],
|
|
["conditional",1,
|
|
[["createNode",4,"RCTView",2,{"testId": "cond_true"}]],
|
|
[["createNode",4,"RCTView",2,{"testId": "cond_false"}]]
|
|
],
|
|
["childSetNode",2]
|
|
]})delim";
|
|
|
|
mockSimpleTestValue_ = true;
|
|
|
|
auto root1 = ReactBytecodeInterpreter::buildShadowTree(
|
|
bytecode,
|
|
surfaceId,
|
|
folly::dynamic::object(),
|
|
*componentDescriptorRegistry,
|
|
nativeModuleRegistry);
|
|
LOG(INFO) << std::endl << root1->getDebugDescription();
|
|
auto props1 = std::dynamic_pointer_cast<const ViewProps>(root1->getProps());
|
|
ASSERT_STREQ(props1->testId.c_str(), "root");
|
|
auto children1 = root1->getChildren();
|
|
ASSERT_EQ(children1.size(), 1);
|
|
auto child_props1 =
|
|
std::dynamic_pointer_cast<const ViewProps>(children1.at(0)->getProps());
|
|
ASSERT_STREQ(child_props1->testId.c_str(), "cond_true");
|
|
|
|
mockSimpleTestValue_ = false;
|
|
|
|
auto root2 = ReactBytecodeInterpreter::buildShadowTree(
|
|
bytecode,
|
|
surfaceId,
|
|
folly::dynamic::object(),
|
|
*componentDescriptorRegistry,
|
|
nativeModuleRegistry);
|
|
auto child_props2 = std::dynamic_pointer_cast<const ViewProps>(
|
|
root2->getChildren().at(0)->getProps());
|
|
ASSERT_STREQ(child_props2->testId.c_str(), "cond_false");
|
|
}
|