mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
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
96 lines
2.2 KiB
C++
96 lines
2.2 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.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <folly/Optional.h>
|
|
#include <folly/dynamic.h>
|
|
#include <react/core/RawProps.h>
|
|
#include <react/graphics/Color.h>
|
|
#include <react/graphics/Geometry.h>
|
|
#include <react/graphics/conversions.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
template <typename T>
|
|
void fromRawValue(const RawValue &rawValue, T &result) {
|
|
result = (T)rawValue;
|
|
}
|
|
|
|
template <typename T>
|
|
void fromRawValue(const RawValue &rawValue, std::vector<T> &result) {
|
|
if (rawValue.hasType<std::vector<RawValue>>()) {
|
|
auto items = (std::vector<RawValue>)rawValue;
|
|
auto length = items.size();
|
|
result.clear();
|
|
result.reserve(length);
|
|
for (int i = 0; i < length; i++) {
|
|
T itemResult;
|
|
fromRawValue(items.at(i), itemResult);
|
|
result.push_back(itemResult);
|
|
}
|
|
return;
|
|
}
|
|
|
|
// The case where `value` is not an array.
|
|
result.clear();
|
|
result.reserve(1);
|
|
T itemResult;
|
|
fromRawValue(rawValue, itemResult);
|
|
result.push_back(itemResult);
|
|
}
|
|
|
|
template <typename T>
|
|
T convertRawProp(
|
|
const RawProps &rawProps,
|
|
const std::string &name,
|
|
const T &sourceValue,
|
|
const T &defaultValue = T()) {
|
|
const auto rawValue = rawProps.at(name);
|
|
|
|
if (!rawValue) {
|
|
return sourceValue;
|
|
}
|
|
|
|
// Special case: `null` always means `the prop was removed, use default
|
|
// value`.
|
|
if (!rawValue->hasValue()) {
|
|
return defaultValue;
|
|
}
|
|
|
|
T result;
|
|
fromRawValue(*rawValue, result);
|
|
return result;
|
|
}
|
|
|
|
template <typename T>
|
|
static folly::Optional<T> convertRawProp(
|
|
const RawProps &rawProps,
|
|
const std::string &name,
|
|
const folly::Optional<T> &sourceValue,
|
|
const folly::Optional<T> &defaultValue = {}) {
|
|
const auto rawValue = rawProps.at(name);
|
|
|
|
if (!rawValue) {
|
|
return sourceValue;
|
|
}
|
|
|
|
// Special case: `null` always means `the prop was removed, use default
|
|
// value`.
|
|
if (!rawValue->hasValue()) {
|
|
return defaultValue;
|
|
}
|
|
|
|
T result;
|
|
fromRawValue(*rawValue, result);
|
|
return folly::Optional<T>{result};
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|