react-native/ReactCommon/fabric/uimanager/UITemplateProcessor.h

67 lines
1.8 KiB
C
Raw Normal View History

Update and expand bytecode spec 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
2018-11-05 15:32:46 -08:00
/**
* 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.
*/
#pragma once
#include <memory>
#include <folly/dynamic.h>
#include <react/config/ReactNativeConfig.h>
#include <react/core/ShadowNode.h>
#include <react/uimanager/ComponentDescriptorRegistry.h>
#include <react/uimanager/UIManagerDelegate.h>
Update and expand bytecode spec 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
2018-11-05 15:32:46 -08:00
namespace facebook {
namespace react {
// Temporary NativeModuleRegistry definition
using NativeModuleCallFn =
std::function<folly::dynamic(const std::string &, const folly::dynamic &)>;
class NativeModuleRegistry {
public:
void registerModule(
const std::string &moduleName,
NativeModuleCallFn callFn) {
modules_.emplace(moduleName, callFn);
}
folly::dynamic call(
const std::string &moduleName,
const std::string &methodName,
const folly::dynamic &args) const {
return modules_.at(moduleName)(methodName, args);
}
private:
std::unordered_map<std::string, NativeModuleCallFn> modules_;
};
class UITemplateProcessor {
Update and expand bytecode spec 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
2018-11-05 15:32:46 -08:00
public:
static SharedShadowNode buildShadowTree(
const std::string &jsonStr,
int rootTag,
const folly::dynamic &params,
const ComponentDescriptorRegistry &componentDescriptorRegistry,
const NativeModuleRegistry &nativeModuleRegistry,
const std::shared_ptr<const ReactNativeConfig> reactNativeConfig);
Update and expand bytecode spec 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
2018-11-05 15:32:46 -08:00
private:
static SharedShadowNode runCommand(
const folly::dynamic &command,
Tag rootTag,
std::vector<SharedShadowNode> &nodes,
std::vector<folly::dynamic> &registers,
const ComponentDescriptorRegistry &componentDescriptorRegistry,
const NativeModuleRegistry &nativeModuleRegistry,
const std::shared_ptr<const ReactNativeConfig> reactNativeConfig);
Update and expand bytecode spec 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
2018-11-05 15:32:46 -08:00
};
} // namespace react
} // namespace facebook