Breaking: size_t indices (#1366)

Summary:
Pull Request resolved: https://github.com/facebook/yoga/pull/1366

X-link: https://github.com/facebook/react-native/pull/39371

Yoga's public API exposes indices most often as `uint32_t`, with exception of clone callbacks which are `int32_t`. Yoga internally represents these indices as `size_t` when dealing with the child vector, and this is the true index.

This changes the API to consistently be `size_t`. This should not be breaking for most users, but will cause breaks where:

1. Users set a clone node callback (I think this should be rare. RN uses it, but only because it relies on a separate private API).
2. Callers of `YGNodeGetChildCount()` are assigning to an int with less width than `size_t` and have strong warnings enabled.
3. Using a newer Yoga binary with older source, since we are not preserving ABI compatibility (Yoga in general does not aim to be ABI stable between major versions, only ABI safe for a given set of sources).

Changelog: [Internal]

Reviewed By: sammy-SC

Differential Revision: D49130914

fbshipit-source-id: 6a004c160c4c50f68047b108508fd437156f5fac
This commit is contained in:
Nick Gerleman
2023-09-11 19:51:40 -07:00
committed by Facebook GitHub Bot
parent 26f2b28eca
commit 776065d7c7
14 changed files with 60 additions and 66 deletions

71
tests/YGConfigTest.cpp Normal file
View File

@@ -0,0 +1,71 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <gtest/gtest.h>
#include <yoga/Yoga.h>
#include <yoga/config/Config.h>
#include <yoga/node/Node.h>
#include <functional>
#include <memory>
using namespace facebook;
struct ConfigCloningTest : public ::testing::Test {
std::unique_ptr<yoga::Config, std::function<void(yoga::Config*)>> config;
void SetUp() override;
void TearDown() override;
static yoga::Node clonedNode;
static YGNodeRef cloneNode(YGNodeConstRef, YGNodeConstRef, size_t) {
return &clonedNode;
}
static YGNodeRef doNotClone(YGNodeConstRef, YGNodeConstRef, size_t) {
return nullptr;
}
};
TEST_F(ConfigCloningTest, uses_values_provided_by_cloning_callback) {
config->setCloneNodeCallback(cloneNode);
yoga::Node node{}, owner{};
auto clone = config->cloneNode(&node, &owner, 0, nullptr);
ASSERT_EQ(clone, &clonedNode);
}
TEST_F(
ConfigCloningTest,
falls_back_to_regular_cloning_if_callback_returns_null) {
config->setCloneNodeCallback(doNotClone);
yoga::Node node{}, owner{};
auto clone = config->cloneNode(&node, &owner, 0, nullptr);
ASSERT_NE(clone, nullptr);
YGNodeFree(clone);
}
TEST_F(ConfigCloningTest, can_clone_with_context) {
config->setCloneNodeCallback(
[](YGNodeConstRef, YGNodeConstRef, size_t, void* context) {
return (YGNodeRef) context;
});
yoga::Node node{}, owner{}, clone{};
ASSERT_EQ(config->cloneNode(&node, &owner, 0, &clone), &clone);
}
void ConfigCloningTest::SetUp() {
config = {static_cast<yoga::Config*>(YGConfigNew()), YGConfigFree};
}
void ConfigCloningTest::TearDown() {
config.reset();
}
yoga::Node ConfigCloningTest::clonedNode = {};