Skip to content
Closed
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
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/Image/RCTImageLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static NSInteger RCTImageBytesForImage(UIImage *image)
return image.images ? image.images.count * singleImageBytes : singleImageBytes;
}

static auto currentRequestCount = std::atomic<uint64_t>(0);
static constinit auto currentRequestCount = std::atomic<uint64_t>(0);

static NSError *addResponseHeadersToError(NSError *originalError, NSHTTPURLResponse *response)
{
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/Network/RCTNetworkTask.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ @implementation RCTNetworkTask {
RCTNetworkTask *_selfReference;
}

static auto currentRequestId = std::atomic<NSUInteger>(0);
static constinit auto currentRequestId = std::atomic<NSUInteger>(0);

- (instancetype)initWithRequest:(NSURLRequest *)request
handler:(id<RCTURLRequestHandler>)handler
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/React/Base/RCTUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ UIDeviceOrientation RCTDeviceOrientation(void)

CGSize RCTScreenSize(void)
{
static std::atomic<CGSize> cachedSize;
static constinit std::atomic<CGSize> cachedSize;
__block CGSize size = cachedSize.load(std::memory_order_relaxed);

if (CGSizeEqualToSize(size, CGSizeZero)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/React/CoreModules/RCTDevSettings.mm
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ - (void)_reloadWithDefaults:(NSDictionary *)defaultValues
#if RCT_DEV_SETTINGS_ENABLE_PACKAGER_CONNECTION
static RCTHandlerToken reloadToken;
static RCTHandlerToken devMenuToken;
static std::atomic<int> numInitializedModules{0};
static constinit std::atomic<int> numInitializedModules{0};
#endif

@interface RCTDevSettings () <RCTBridgeModule, RCTInvalidating, NativeDevSettingsSpec, RCTDevSettingsInspectable> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ lastResort(const char* tag, const char* msg, const char* arg = nullptr) {
namespace facebook::gloginit {

void initialize(const char* tag) {
static std::once_flag flag{};
static constinit std::once_flag flag{};
static auto failed = false;

std::call_once(flag, [tag] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@

#include <folly/Conv.h>
#include <react/renderer/animationbackend/AnimatedPropsSerializer.h>
#include <array>
#include <numbers>
#include <optional>
#include <span>
#include <string_view>
#include <unordered_map>
#include <utility>

namespace facebook::react {

Expand Down Expand Up @@ -87,74 +90,110 @@ constexpr int CMD_TRANSFORM_PERSPECTIVE = 112;
constexpr int CMD_UNIT_PX = 202;
constexpr int CMD_UNIT_PERCENT = 203;

using CommandEntry = std::pair<std::string_view, int>;

// Group by name length so lookup only scans a small set of candidates.
template <size_t MaxLength>
constexpr auto indexCommandLengths(std::span<const CommandEntry> entries) {
std::array<std::span<const CommandEntry>, MaxLength + 1> index{};
size_t start = 0;
for (size_t length = 0; length < index.size(); ++length) {
auto end = start;
while (end < entries.size() && entries[end].first.size() == length) {
++end;
}
index[length] = entries.subspan(start, end - start);
start = end;
}
return index;
}

std::optional<int> findCommand(
std::string_view name,
std::span<const std::span<const CommandEntry>> index) {
if (name.size() >= index.size()) {
return std::nullopt;
}
for (const auto& [key, command] : index[name.size()]) {
if (key == name) {
return command;
}
}
return std::nullopt;
}

std::optional<int> propNameToCommand(const std::string& name) {
static const std::unordered_map<std::string_view, int> kMap = {
static constexpr std::array<CommandEntry, 37> kMap{{
{"color", CMD_COLOR},
{"zIndex", CMD_Z_INDEX},
{"opacity", CMD_OPACITY},
{"elevation", CMD_ELEVATION},
{"zIndex", CMD_Z_INDEX},
{"shadowOpacity", CMD_SHADOW_OPACITY},
{"shadowRadius", CMD_SHADOW_RADIUS},
{"backgroundColor", CMD_BACKGROUND_COLOR},
{"color", CMD_COLOR},
{"tintColor", CMD_TINT_COLOR},
{"placeholderTextColor", CMD_PLACEHOLDER_TEXT_COLOR},
{"transform", CMD_START_OF_TRANSFORM},
{"borderColor", CMD_BORDER_COLOR},
{"shadowColor", CMD_SHADOW_COLOR},
{"borderRadius", CMD_BORDER_RADIUS},
{"outlineColor", CMD_OUTLINE_COLOR},
{"outlineWidth", CMD_OUTLINE_WIDTH},
{"shadowRadius", CMD_SHADOW_RADIUS},
{"outlineOffset", CMD_OUTLINE_OFFSET},
{"shadowOpacity", CMD_SHADOW_OPACITY},
{"borderEndColor", CMD_BORDER_END_COLOR},
{"borderTopColor", CMD_BORDER_TOP_COLOR},
{"backgroundColor", CMD_BACKGROUND_COLOR},
{"borderLeftColor", CMD_BORDER_LEFT_COLOR},
{"borderBlockColor", CMD_BORDER_BLOCK_COLOR},
{"borderRightColor", CMD_BORDER_RIGHT_COLOR},
{"borderStartColor", CMD_BORDER_START_COLOR},
{"borderBottomColor", CMD_BORDER_BOTTOM_COLOR},
{"borderEndEndRadius", CMD_BORDER_END_END_RADIUS},
{"borderTopEndRadius", CMD_BORDER_TOP_END_RADIUS},
{"borderBlockEndColor", CMD_BORDER_BLOCK_END_COLOR},
{"borderTopLeftRadius", CMD_BORDER_TOP_LEFT_RADIUS},
{"borderEndStartRadius", CMD_BORDER_END_START_RADIUS},
{"borderStartEndRadius", CMD_BORDER_START_END_RADIUS},
{"borderTopRightRadius", CMD_BORDER_TOP_RIGHT_RADIUS},
{"borderTopStartRadius", CMD_BORDER_TOP_START_RADIUS},
{"borderTopEndRadius", CMD_BORDER_TOP_END_RADIUS},
{"placeholderTextColor", CMD_PLACEHOLDER_TEXT_COLOR},
{"borderBlockStartColor", CMD_BORDER_BLOCK_START_COLOR},
{"borderBottomEndRadius", CMD_BORDER_BOTTOM_END_RADIUS},
{"borderBottomLeftRadius", CMD_BORDER_BOTTOM_LEFT_RADIUS},
{"borderStartStartRadius", CMD_BORDER_START_START_RADIUS},
{"borderBottomRightRadius", CMD_BORDER_BOTTOM_RIGHT_RADIUS},
{"borderBottomStartRadius", CMD_BORDER_BOTTOM_START_RADIUS},
{"borderBottomEndRadius", CMD_BORDER_BOTTOM_END_RADIUS},
{"borderStartStartRadius", CMD_BORDER_START_START_RADIUS},
{"borderStartEndRadius", CMD_BORDER_START_END_RADIUS},
{"borderEndStartRadius", CMD_BORDER_END_START_RADIUS},
{"borderEndEndRadius", CMD_BORDER_END_END_RADIUS},
{"borderColor", CMD_BORDER_COLOR},
{"borderTopColor", CMD_BORDER_TOP_COLOR},
{"borderBottomColor", CMD_BORDER_BOTTOM_COLOR},
{"borderLeftColor", CMD_BORDER_LEFT_COLOR},
{"borderRightColor", CMD_BORDER_RIGHT_COLOR},
{"borderStartColor", CMD_BORDER_START_COLOR},
{"borderEndColor", CMD_BORDER_END_COLOR},
{"borderBlockColor", CMD_BORDER_BLOCK_COLOR},
{"borderBlockStartColor", CMD_BORDER_BLOCK_START_COLOR},
{"borderBlockEndColor", CMD_BORDER_BLOCK_END_COLOR},
{"outlineColor", CMD_OUTLINE_COLOR},
{"outlineOffset", CMD_OUTLINE_OFFSET},
{"outlineWidth", CMD_OUTLINE_WIDTH},
{"transform", CMD_START_OF_TRANSFORM},
};
auto it = kMap.find(name);
if (it == kMap.end()) {
return std::nullopt;
}
return it->second;
}};
static constexpr auto kIndex =
indexCommandLengths<kMap.back().first.size()>(kMap);
static_assert(
kIndex.back().data() ==
std::span<const CommandEntry>{kMap}.last(kIndex.back().size()).data(),
"Command entries must be sorted by name length");
return findCommand(name, kIndex);
}

std::optional<int> transformNameToCommand(const std::string& name) {
static const std::unordered_map<std::string_view, int> kMap = {
{"translateX", CMD_TRANSFORM_TRANSLATE_X},
{"translateY", CMD_TRANSFORM_TRANSLATE_Y},
static constexpr std::array<CommandEntry, 13> kMap{{
{"scale", CMD_TRANSFORM_SCALE},
{"skewX", CMD_TRANSFORM_SKEW_X},
{"skewY", CMD_TRANSFORM_SKEW_Y},
{"matrix", CMD_TRANSFORM_MATRIX},
{"rotate", CMD_TRANSFORM_ROTATE},
{"scaleX", CMD_TRANSFORM_SCALE_X},
{"scaleY", CMD_TRANSFORM_SCALE_Y},
{"rotate", CMD_TRANSFORM_ROTATE},
{"rotateX", CMD_TRANSFORM_ROTATE_X},
{"rotateY", CMD_TRANSFORM_ROTATE_Y},
{"rotateZ", CMD_TRANSFORM_ROTATE_Z},
{"skewX", CMD_TRANSFORM_SKEW_X},
{"skewY", CMD_TRANSFORM_SKEW_Y},
{"matrix", CMD_TRANSFORM_MATRIX},
{"translateX", CMD_TRANSFORM_TRANSLATE_X},
{"translateY", CMD_TRANSFORM_TRANSLATE_Y},
{"perspective", CMD_TRANSFORM_PERSPECTIVE},
};
auto it = kMap.find(name);
if (it == kMap.end()) {
return std::nullopt;
}
return it->second;
}};
static constexpr auto kIndex =
indexCommandLengths<kMap.back().first.size()>(kMap);
static_assert(
kIndex.back().data() ==
std::span<const CommandEntry>{kMap}.last(kIndex.back().size()).data(),
"Command entries must be sorted by name length");
return findCommand(name, kIndex);
}

bool packLength(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
#include <glog/logging.h>

#include <algorithm>
#include <array>
#include <cfenv>
#include <cmath>
#include <string_view>
#include <vector>

namespace facebook::react {
Expand Down Expand Up @@ -81,7 +83,7 @@ namespace {
// List of layout-only props extracted from ViewProps.kt used to filter out
// component props from Props 1.5 to validate the Props 2.0 output
inline bool isLayoutOnlyProp(const std::string& propName) {
static const std::vector<std::string> layoutOnlyProps = {
static constexpr std::array<std::string_view, 46> layoutOnlyProps = {
// Flexbox Alignment
"alignItems",
"alignSelf",
Expand Down Expand Up @@ -146,8 +148,10 @@ inline bool isLayoutOnlyProp(const std::string& propName) {
// Other
"collapsable",
};
return std::find(layoutOnlyProps.begin(), layoutOnlyProps.end(), propName) !=
layoutOnlyProps.end();
return std::find(
layoutOnlyProps.begin(),
layoutOnlyProps.end(),
std::string_view{propName}) != layoutOnlyProps.end();
}
#endif

Expand Down
Loading
Loading