Files
react-native/ReactCommon/fabric/core/primitives/RawProps.h
T
Valentin SherginandFacebook Github Bot 9842e39019 Fabric: folly::dynamic was replaced with RawValue in prop-parsing infra
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
2019-02-06 16:34:46 -08:00

98 lines
2.6 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/dynamic.h>
#include <jsi/JSIDynamic.h>
#include <jsi/jsi.h>
#include <react/core/RawValue.h>
namespace facebook {
namespace react {
/*
* `RawProps` represents an untyped map of props comes from JavaScript side.
* `RawProps` stores JSI (or `folly::dynamic`) primitives inside and abstract
* them as `RawValue` objects.
* `RawProps` is NOT a thread-safe type nor long-living type.
* The caller must not store values of this type.
* The class is practically a wrapper around a `jsi::Value and `jsi::Runtime`
* pair (or folly::dynamic) preventing direct access to it and inefficient
* misuse. Not copyable, not moveable.
*/
class RawProps {
public:
/*
* Creates an object with given `runtime` and `value`.
*/
RawProps(jsi::Runtime &runtime, const jsi::Value &value) noexcept
: RawProps(
value.isNull() ? folly::dynamic::object()
: jsi::dynamicFromValue(runtime, value)) {}
/*
* Creates an object with given `folly::dynamic` object.
* Deprecated.
* We need this temporary, only because we have a callsite that does not have
* a `jsi::Runtime` behind the data.
*/
RawProps(const folly::dynamic &dynamic) noexcept
:
#ifdef ANDROID
dynamic_(dynamic),
#endif
map_((std::unordered_map<std::string, RawValue>)RawValue(dynamic)) {
}
/*
* Not moveable.
*/
RawProps(RawProps &&other) noexcept = delete;
RawProps &operator=(RawProps &&other) noexcept = delete;
/*
* Not copyable.
*/
RawProps(const RawProps &other) noexcept = delete;
RawProps &operator=(const RawProps &other) noexcept = delete;
#ifdef ANDROID
/*
* Deprecated. Do not use.
* The support for explicit conversion to `folly::dynamic` is deprecated and
* will be removed as soon Android implementation does not need it.
*/
explicit operator folly::dynamic() const noexcept {
return dynamic_;
}
#endif
/*
* Returns a const unowning pointer to `RawValue` of a prop with a given name.
* Returns `nullptr`, it a prop with the given name does not exist.
*/
const RawValue *at(const std::string &name) const noexcept {
auto iterator = map_.find(name);
if (iterator == map_.end()) {
return nullptr;
}
return &iterator->second;
}
private:
#ifdef ANDROID
const folly::dynamic dynamic_;
#endif
const std::unordered_map<std::string, RawValue> map_;
};
} // namespace react
} // namespace facebook