Skip to content

Commit

Permalink
Fabric: folly::dynamic was replaced with RawValue in prop-parsing infra
Browse files Browse the repository at this point in the history
Summary:
Our long-term plan is to completely illuminate `jsi::Value`-to-`folly::dynamic` serialization step in prop parsing process improving performance and memory pressure. At the same time, we don't want to introduce a hard dependency in application code to JSI because it exposes direct access to VM and prevents parsing some data that come *NOT* from JSVM.
RawValue is an extremely light-weight (hopefully fully optimized-out) abstraction that provides limited JSON-like and C++-idiomatic interface.

The current particular implementation is still using `folly::dynamic` inside, but I have fully JSI-powered one which will replace the current one right after we figure out how to deal with folly::dynamic-specific callsites. Or we can implement RawValue in a hybrid manner if a code-size implication of that will be minimal.

Reviewed By: JoshuaGross, mdvacca

Differential Revision: D13962466

fbshipit-source-id: e848522fd242f21e9e771773f2103f1c1d9d7f21
  • Loading branch information
shergin authored and facebook-github-bot committed Feb 7, 2019
1 parent b0c8275 commit 9842e39
Show file tree
Hide file tree
Showing 26 changed files with 651 additions and 310 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,7 @@ local_ref<JMountItem::javaobject> createUpdatePropsMountItem(const jni::global_r
auto newViewProps = *std::dynamic_pointer_cast<const ViewProps>(shadowView.props);

// TODO: move props from map to a typed object.
auto rawProps = shadowView.props->rawProps;
folly::dynamic newProps = folly::dynamic::object();
for (auto element : rawProps) {
newProps[element.first] = element.second;
}
auto newProps = shadowView.props->rawProps;

local_ref<ReadableNativeMap::jhybridobject> readableMap = ReadableNativeMap::newObjectCxxArgs(newProps);

Expand Down
56 changes: 28 additions & 28 deletions ReactCommon/fabric/attributedstring/conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ inline std::string toString(const EllipsizeMode &ellipsisMode) {
}
}

inline void fromDynamic(const folly::dynamic &value, EllipsizeMode &result) {
auto string = value.getString();
inline void fromRawValue(const RawValue &value, EllipsizeMode &result) {
auto string = (std::string)value;
if (string == "clip") {
result = EllipsizeMode::Clip;
return;
Expand All @@ -57,8 +57,8 @@ inline void fromDynamic(const folly::dynamic &value, EllipsizeMode &result) {
abort();
}

inline void fromDynamic(const folly::dynamic &value, FontWeight &result) {
auto string = value.asString();
inline void fromRawValue(const RawValue &value, FontWeight &result) {
auto string = (std::string)value;
if (string == "normal") {
result = FontWeight::Regular;
return;
Expand Down Expand Up @@ -114,8 +114,8 @@ inline std::string toString(const FontWeight &fontWeight) {
return folly::to<std::string>((int)fontWeight);
}

inline void fromDynamic(const folly::dynamic &value, FontStyle &result) {
auto string = value.asString();
inline void fromRawValue(const RawValue &value, FontStyle &result) {
auto string = (std::string)value;
if (string == "normal") {
result = FontStyle::Normal;
return;
Expand All @@ -142,28 +142,28 @@ inline std::string toString(const FontStyle &fontStyle) {
}
}

inline void fromDynamic(const folly::dynamic &value, FontVariant &result) {
assert(value.isArray());
inline void fromRawValue(const RawValue &value, FontVariant &result) {
assert(value.hasType<std::vector<std::string>>());
result = FontVariant::Default;
for (auto &&item : value) {
auto string = item.asString();
if (string == "small-caps") {
auto items = std::vector<std::string>{value};
for (const auto &item : items) {
if (item == "small-caps") {
result = (FontVariant)((int)result | (int)FontVariant::SmallCaps);
continue;
}
if (string == "oldstyle-nums") {
if (item == "oldstyle-nums") {
result = (FontVariant)((int)result | (int)FontVariant::OldstyleNums);
continue;
}
if (string == "lining-nums") {
if (item == "lining-nums") {
result = (FontVariant)((int)result | (int)FontVariant::LiningNums);
continue;
}
if (string == "tabular-nums") {
if (item == "tabular-nums") {
result = (FontVariant)((int)result | (int)FontVariant::TabularNums);
continue;
}
if (string == "proportional-nums") {
if (item == "proportional-nums") {
result = (FontVariant)((int)result | (int)FontVariant::ProportionalNums);
continue;
}
Expand Down Expand Up @@ -196,8 +196,8 @@ inline std::string toString(const FontVariant &fontVariant) {
return result;
}

inline void fromDynamic(const folly::dynamic &value, TextAlignment &result) {
auto string = value.asString();
inline void fromRawValue(const RawValue &value, TextAlignment &result) {
auto string = (std::string)value;
if (string == "natural") {
result = TextAlignment::Natural;
return;
Expand Down Expand Up @@ -236,8 +236,8 @@ inline std::string toString(const TextAlignment &textAlignment) {
}
}

inline void fromDynamic(const folly::dynamic &value, WritingDirection &result) {
auto string = value.asString();
inline void fromRawValue(const RawValue &value, WritingDirection &result) {
auto string = (std::string)value;
if (string == "natural") {
result = WritingDirection::Natural;
return;
Expand All @@ -264,10 +264,10 @@ inline std::string toString(const WritingDirection &writingDirection) {
}
}

inline void fromDynamic(
const folly::dynamic &value,
inline void fromRawValue(
const RawValue &value,
TextDecorationLineType &result) {
auto string = value.asString();
auto string = (std::string)value;
if (string == "none") {
result = TextDecorationLineType::None;
return;
Expand Down Expand Up @@ -301,10 +301,10 @@ inline std::string toString(
}
}

inline void fromDynamic(
const folly::dynamic &value,
inline void fromRawValue(
const RawValue &value,
TextDecorationLineStyle &result) {
auto string = value.asString();
auto string = (std::string)value;
if (string == "single") {
result = TextDecorationLineStyle::Single;
return;
Expand Down Expand Up @@ -332,10 +332,10 @@ inline std::string toString(
}
}

inline void fromDynamic(
const folly::dynamic &value,
inline void fromRawValue(
const RawValue &value,
TextDecorationLinePattern &result) {
auto string = value.asString();
auto string = (std::string)value;
if (string == "solid") {
result = TextDecorationLinePattern::Solid;
return;
Expand Down
6 changes: 3 additions & 3 deletions ReactCommon/fabric/components/activityindicator/conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
namespace facebook {
namespace react {

inline void fromDynamic(
const folly::dynamic &value,
inline void fromRawValue(
const RawValue &value,
ActivityIndicatorViewSize &result) {
auto string = value.asString();
auto string = (std::string)value;
if (string == "large") {
result = ActivityIndicatorViewSize::Large;
return;
Expand Down
40 changes: 21 additions & 19 deletions ReactCommon/fabric/components/image/conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,43 @@
namespace facebook {
namespace react {

inline void fromDynamic(const folly::dynamic &value, ImageSource &result) {
if (value.isString()) {
result = {.type = ImageSource::Type::Remote, .uri = value.asString()};
inline void fromRawValue(const RawValue &value, ImageSource &result) {
if (value.hasType<std::string>()) {
result = {.type = ImageSource::Type::Remote, .uri = (std::string)value};
return;
}

if (value.isObject()) {
if (value.hasType<std::unordered_map<std::string, RawValue>>()) {
auto items = (std::unordered_map<std::string, RawValue>)value;
result = {};

result.type = ImageSource::Type::Remote;

if (value.count("__packager_asset")) {
if (items.find("__packager_asset") != items.end()) {
result.type = ImageSource::Type::Local;
}

if (value.count("width") && value.count("height")) {
fromDynamic(value, result.size);
if (items.find("width") != items.end() &&
items.find("height") != items.end()) {
result.size = {(Float)items.at("width"), (Float)items.at("height")};
}

if (value.count("scale")) {
result.scale = (Float)value["scale"].asDouble();
if (items.find("scale") != items.end()) {
result.scale = (Float)items.at("scale");
} else {
result.scale = value.count("deprecated") ? 0.0 : 1.0;
result.scale = items.find("deprecated") != items.end() ? 0.0 : 1.0;
}

if (value.count("url")) {
result.uri = value["url"].asString();
if (items.find("url") != items.end()) {
result.uri = (std::string)items.at("url");
}

if (value.count("uri")) {
result.uri = value["uri"].asString();
if (items.find("uri") != items.end()) {
result.uri = (std::string)items.at("uri");
}

if (value.count("bundle")) {
result.bundle = value["bundle"].asString();
if (items.find("bundle") != items.end()) {
result.bundle = (std::string)items.at("bundle");
result.type = ImageSource::Type::Local;
}

Expand All @@ -62,9 +64,9 @@ inline std::string toString(const ImageSource &value) {
return "{uri: " + value.uri + "}";
}

inline void fromDynamic(const folly::dynamic &value, ImageResizeMode &result) {
assert(value.isString());
auto stringValue = value.asString();
inline void fromRawValue(const RawValue &value, ImageResizeMode &result) {
assert(value.hasType<std::string>());
auto stringValue = (std::string)value;
if (stringValue == "cover") {
result = ImageResizeMode::Cover;
return;
Expand Down
18 changes: 9 additions & 9 deletions ReactCommon/fabric/components/scrollview/conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
namespace facebook {
namespace react {

inline void fromDynamic(
const folly::dynamic &value,
inline void fromRawValue(
const RawValue &value,
ScrollViewSnapToAlignment &result) {
auto string = value.asString();
auto string = (std::string)value;
if (string == "start") {
result = ScrollViewSnapToAlignment::Start;
return;
Expand All @@ -32,10 +32,10 @@ inline void fromDynamic(
abort();
}

inline void fromDynamic(
const folly::dynamic &value,
inline void fromRawValue(
const RawValue &value,
ScrollViewIndicatorStyle &result) {
auto string = value.asString();
auto string = (std::string)value;
if (string == "default") {
result = ScrollViewIndicatorStyle::Default;
return;
Expand All @@ -51,10 +51,10 @@ inline void fromDynamic(
abort();
}

inline void fromDynamic(
const folly::dynamic &value,
inline void fromRawValue(
const RawValue &value,
ScrollViewKeyboardDismissMode &result) {
auto string = value.asString();
auto string = (std::string)value;
if (string == "none") {
result = ScrollViewKeyboardDismissMode::None;
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,18 @@ inline void fromString(const std::string &string, AccessibilityTraits &result) {
abort();
}

inline void fromDynamic(
const folly::dynamic &value,
AccessibilityTraits &result) {
if (value.isString()) {
fromString(value.asString(), result);
inline void fromRawValue(const RawValue &value, AccessibilityTraits &result) {
if (value.hasType<std::string>()) {
fromString((std::string)value, result);
return;
}

if (value.isArray()) {
if (value.hasType<std::vector<std::string>>()) {
result = {};
for (auto &item : value) {
auto string = item.asString();
auto items = (std::vector<std::string>)value;
for (auto &item : items) {
AccessibilityTraits itemAccessibilityTraits;
fromString(value.asString(), itemAccessibilityTraits);
fromString(item, itemAccessibilityTraits);
result = result | itemAccessibilityTraits;
}
}
Expand Down
Loading

0 comments on commit 9842e39

Please sign in to comment.