Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace facebook::react {

static bool
hasValue(const RawProps& rawProps, bool defaultValue, const char* name) {
hasValue(const RawProps& rawProps, bool defaultValue, RawPropName name) {
auto rawValue = rawProps.at(name);

// No change to prop - use default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,37 +374,37 @@ convertRawProp(const PropsParserContext &context, const RawProps &rawProps, cons
}

struct CascadedRectangleCornersNames {
const char *topLeft;
const char *topRight;
const char *bottomLeft;
const char *bottomRight;
const char *topStart;
const char *topEnd;
const char *bottomStart;
const char *bottomEnd;
const char *endEnd;
const char *endStart;
const char *startEnd;
const char *startStart;
const char *all;
RawPropName topLeft;
RawPropName topRight;
RawPropName bottomLeft;
RawPropName bottomRight;
RawPropName topStart;
RawPropName topEnd;
RawPropName bottomStart;
RawPropName bottomEnd;
RawPropName endEnd;
RawPropName endStart;
RawPropName startEnd;
RawPropName startStart;
RawPropName all;
};

struct CascadedRectangleEdgesNames {
const char *left;
const char *right;
const char *top;
const char *bottom;
const char *start;
const char *end;
const char *horizontal;
const char *vertical;
const char *block;
const char *blockEnd;
const char *blockStart;
const char *inline_;
const char *inlineEnd;
const char *inlineStart;
const char *all;
RawPropName left;
RawPropName right;
RawPropName top;
RawPropName bottom;
RawPropName start;
RawPropName end;
RawPropName horizontal;
RawPropName vertical;
RawPropName block;
RawPropName blockEnd;
RawPropName blockStart;
RawPropName inline_;
RawPropName inlineEnd;
RawPropName inlineStart;
RawPropName all;
};

// This can be deleted when non-iterator ViewProp parsing is deleted
Expand Down
18 changes: 11 additions & 7 deletions packages/react-native/ReactCommon/react/renderer/core/RawProps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,26 @@ bool RawProps::isEmpty() const noexcept {
* Returns a const unowning pointer to `RawValue` of a prop with a given name.
* Returns `nullptr` if a prop with the given name does not exist.
*/
const RawValue* RawProps::at(const char* name) const noexcept {
const RawValue* RawProps::at(RawPropName name) const noexcept {
react_native_assert(
parser_ &&
"The object is not parsed. `parse` must be called before `at`.");
return parser_->at(*this, name);
return parser_->at(*this, name.view(), /* copyKey */ false);
}

const RawValue* RawProps::atRuntime(std::string_view name) const noexcept {
react_native_assert(
parser_ &&
"The object is not parsed. `parse` must be called before `atRuntime`.");
return parser_->at(*this, name, /* copyKey */ true);
}

const RawValue* RawProps::at(
const char* name,
const char* prefix,
const char* suffix) const noexcept {
if (prefix == nullptr && suffix == nullptr) {
return at(name);
return atRuntime(name);
}

std::string concatenated;
Expand All @@ -132,10 +139,7 @@ const RawValue* RawProps::at(
if (suffix != nullptr) {
concatenated += suffix;
}
react_native_assert(
parser_ &&
"The object is not parsed. `parse` must be called before `at`.");
return parser_->at(*this, concatenated);
return atRuntime(concatenated);
}

} // namespace facebook::react
32 changes: 29 additions & 3 deletions packages/react-native/ReactCommon/react/renderer/core/RawProps.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

#include <react/cxxstableapi/UmbrellaGuard.h>

#include <cstddef>
#include <limits>
#include <optional>
#include <string>
#include <string_view>

#include <folly/dynamic.h>
#include <jsi/JSIDynamic.h>
Expand All @@ -25,6 +27,26 @@ namespace facebook::react {

class RawPropsParser;

// A classic prop name backed by a string literal or static constexpr array.
// Immediate construction rejects arrays whose addresses cannot escape constant
// evaluation and determines the length at the call site.
class RawPropName final {
public:
template <std::size_t Size>
consteval RawPropName(const char (&name)[Size]) : name_(name, Size)
{
name_ = name_.substr(0, name_.find('\0'));
}

constexpr std::string_view view() const noexcept
{
return name_;
}

private:
std::string_view name_;
};

/*
* `RawProps` represents an untyped map of props comes from JavaScript side.
* `RawProps` stores JSI (or `folly::dynamic`) primitives inside and abstract
Expand Down Expand Up @@ -99,10 +121,14 @@ class RawProps final {
* Returns a const unowning pointer to `RawValue` of a prop with a given name.
* Returns `nullptr` if a prop with the given name does not exist.
*/
const RawValue *at(const char *name) const noexcept;
const RawValue *at(RawPropName name) const noexcept;

// Looks up a name composed at runtime. The name must also be requested while
// preparing the parser, which copies its bytes so they need only live through
// the call.
const RawValue *atRuntime(std::string_view name) const noexcept;

// Deprecated: Use at(name) instead. This overload exists for backwards
// compatibility with callers that pass prefix/suffix separately.
// Deprecated: compose the name and call atRuntime instead.
const RawValue *at(const char *name, const char *prefix, const char *suffix) const noexcept;

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ inline bool keysEqual(std::string_view a, std::string_view b) noexcept {
// which props are accessed during parsing, and in which order.
const RawValue* RawPropsParser::at(
const RawProps& rawProps,
std::string_view key) const noexcept {
std::string_view key,
bool copyKey) const noexcept {
if (!ready_) [[unlikely]] {
// Check against the same key being inserted more than once.
// This happens commonly with nested Props structs, where the higher-level
Expand All @@ -50,6 +51,10 @@ const RawValue* RawPropsParser::at(
}
// This is not thread-safe part; this happens only during initialization of
// a `ComponentDescriptor` where it is actually safe.
if (copyKey) {
ownedKeys_.push_back(std::make_shared<const std::string>(key));
key = *ownedKeys_.back();
}
keys_.push_back(key);
react_native_assert(size < std::numeric_limits<RawPropsValueIndex>::max());
nameToIndex_.insert(key, static_cast<RawPropsValueIndex>(size));
Expand All @@ -65,6 +70,9 @@ const RawValue* RawPropsParser::at(

if (static_cast<size_t>(rawProps.keyIndexCursor_) >= keys_.size())
[[unlikely]] {
if (keys_.empty()) {
Comment thread
chrfalch marked this conversation as resolved.
Resolved
return nullptr;
}
rawProps.keyIndexCursor_ = 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <react/renderer/core/RawPropsKeyMap.h>
#include <react/renderer/core/RawPropsPrimitives.h>
#include <react/renderer/core/RawValue.h>
#include <memory>
#include <string>
#include <string_view>

namespace facebook::react {
Expand Down Expand Up @@ -72,8 +74,10 @@ class RawPropsParser final {
/*
* To be used by `RawProps` only.
*/
const RawValue *at(const RawProps &rawProps, std::string_view key) const noexcept;
const RawValue *at(const RawProps &rawProps, std::string_view key, bool copyKey) const noexcept;

// Keep the backing storage of runtime keys stable across parser copies.
mutable std::vector<std::shared_ptr<const std::string>> ownedKeys_{};
mutable std::vector<std::string_view> keys_{};
mutable RawPropsKeyMap nameToIndex_{};
mutable bool ready_{false};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ template <typename T, typename U = T>
T convertRawProp(
const PropsParserContext &context,
const RawProps &rawProps,
const char *name,
RawPropName name,
const T &sourceValue,
const U &defaultValue)
{
Expand All @@ -194,7 +194,7 @@ T convertRawProp(
} catch (const std::exception &e) {
// In case of errors, log the error and fall back to the default
// TODO: report this using ErrorUtils so it's more visible to the user
LOG(ERROR) << "Error while converting prop '" << name << "': " << e.what();
LOG(ERROR) << "Error while converting prop '" << name.view() << "': " << e.what();
return defaultValue;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,147 @@
#include <react/renderer/core/RawPropsParser.h>
#include <react/renderer/core/ShadowNode.h>
#include <react/renderer/core/propsConversions.h>
#include <string>
#include <string_view>
#include <utility>

using namespace facebook;
using namespace facebook::react;

namespace {

template <typename Name>
concept CanLookUpProp = requires(const RawProps& props, Name&& name) {
props.at(std::forward<Name>(name));
};

template <typename Name>
concept CanConvertProp = requires(
const PropsParserContext& context,
const RawProps& props,
Name&& name) {
convertRawProp(context, props, std::forward<Name>(name), 0, {});
};

static_assert(CanLookUpProp<decltype("value")>);
static_assert(CanConvertProp<decltype("value")>);
static_assert(!CanLookUpProp<std::string>);
static_assert(!CanConvertProp<std::string>);
static_assert(!CanLookUpProp<const char*>);
static_assert(!CanConvertProp<const char*>);
static_assert(!CanLookUpProp<std::string_view>);
static_assert(!CanConvertProp<std::string_view>);

constexpr char unterminatedName[] = {'v', 'a', 'l', 'u', 'e'};
constexpr char paddedName[12] = "value";

struct PropsWithUnterminatedName : Props {
PropsWithUnterminatedName(
const PropsParserContext& context,
const PropsWithUnterminatedName& source,
const RawProps& rawProps)
: value(convertRawProp(
context,
rawProps,
unterminatedName,
source.value,
{})) {}

PropsWithUnterminatedName() = default;
int value{};
};

struct PropsWithComposedNames : Props {
PropsWithComposedNames(
const PropsParserContext&,
const PropsWithComposedNames&,
const RawProps& rawProps) {
std::string name = "test";
name += "PropA";
if (const auto* rawValue = rawProps.atRuntime(name)) {
first = static_cast<int>(*rawValue);
}
name.back() = 'B';
if (const auto* rawValue = rawProps.atRuntime(name)) {
second = static_cast<int>(*rawValue);
}
if (const auto* rawValue = rawProps.at("Prop", "compat", "Value")) {
composed = static_cast<int>(*rawValue);
}
}

PropsWithComposedNames() = default;
int first{};
int second{};
int composed{};
};

// Reads its prop only when props are supplied, so preparing the parser, which
// passes empty props, registers no names
struct PropsWithConditionalRead : Props {
PropsWithConditionalRead(
const PropsParserContext&,
const PropsWithConditionalRead&,
const RawProps& rawProps) {
if (!rawProps.isEmpty()) {
unregistered = rawProps.at("value");
}
}

PropsWithConditionalRead() = default;
const RawValue* unregistered{};
};

} // namespace

TEST(RawPropsTest, looksUpAnUnterminatedName) {
RawPropsParser parser;
parser.prepare<PropsWithUnterminatedName>();
RawProps raw(folly::dynamic::object("value", 42)("valueExtra", 19));
raw.parse(parser);

ContextContainer contextContainer;
PropsParserContext context{-1, contextContainer};
EXPECT_EQ(PropsWithUnterminatedName(context, {}, raw).value, 42);
const auto* value = raw.at(paddedName);
ASSERT_NE(value, nullptr);
EXPECT_EQ(static_cast<int>(*value), 42);
}

TEST(RawPropsTest, copiesComposedNamesWhilePreparing) {
const auto parser = [] {
RawPropsParser original;
original.prepare<PropsWithComposedNames>();
return RawPropsParser(original);
}();
RawProps raw(
folly::dynamic::object("testPropA", 42)("testPropB", 19)(
"compatPropValue", 7));
raw.parse(parser);

ContextContainer contextContainer;
PropsParserContext context{-1, contextContainer};
PropsWithComposedNames props(context, {}, raw);
EXPECT_EQ(props.first, 42);
EXPECT_EQ(props.second, 19);
EXPECT_EQ(props.composed, 7);
const auto* value = raw.at("compatPropValue", nullptr, nullptr);
ASSERT_NE(value, nullptr);
EXPECT_EQ(static_cast<int>(*value), 7);
}

TEST(RawPropsTest, looksUpANameInAParserWithNoNames) {
RawPropsParser parser;
parser.prepare<PropsWithConditionalRead>();
RawProps raw(folly::dynamic::object("value", 42));
raw.parse(parser);

ContextContainer contextContainer;
PropsParserContext context{-1, contextContainer};
EXPECT_EQ(PropsWithConditionalRead(context, {}, raw).unregistered, nullptr);
EXPECT_EQ(raw.at("value"), nullptr);
}

TEST(RawPropsTest, dynamicSourceIsBorrowed) {
const auto rawProps = RawProps(folly::dynamic::object("opacity", 0.5));
const auto* dynamic = rawProps.getDynamic();
Expand Down
Loading
Loading