Breaking: Fix callback const-correctness (#1369)

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

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

This fixes const-correctness of callbacks (e.g. not letting a logger function modify nodes during layout). This helps us to continue to fix const-correctness issues inside of Yoga.

This change is breaking to the public API, since it requires a change in signature passed to Yoga.

Changelog: [Internal]

Reviewed By: rshest

Differential Revision: D49130714

fbshipit-source-id: 4305f8882d89f296e45b78497a51716a0dbb3b2d
This commit is contained in:
Nick Gerleman
2023-09-11 19:51:40 -07:00
committed by Facebook GitHub Bot
parent b12a6a340c
commit 26f2b28eca
22 changed files with 111 additions and 111 deletions

View File

@@ -249,7 +249,7 @@ TEST_F(EventTest, layout_events_has_max_measure_cache) {
TEST_F(EventTest, measure_functions_get_wrapped) {
auto root = YGNodeNew();
YGNodeSetMeasureFunc(
root, [](YGNodeRef, float, YGMeasureMode, float, YGMeasureMode) {
root, [](YGNodeConstRef, float, YGMeasureMode, float, YGMeasureMode) {
return YGSize{};
});
@@ -267,7 +267,8 @@ TEST_F(EventTest, baseline_functions_get_wrapped) {
auto child = YGNodeNew();
YGNodeInsertChild(root, child, 0);
YGNodeSetBaselineFunc(child, [](YGNodeRef, float, float) { return 0.0f; });
YGNodeSetBaselineFunc(
child, [](YGNodeConstRef, float, float) { return 0.0f; });
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
YGNodeStyleSetAlignItems(root, YGAlignBaseline);

View File

@@ -9,14 +9,14 @@
#include <yoga/Yoga.h>
static float _baselineFunc(
YGNodeRef /*node*/,
YGNodeConstRef /*node*/,
const float /*width*/,
const float height) {
return height / 2;
}
static YGSize _measure1(
YGNodeRef /*node*/,
YGNodeConstRef /*node*/,
float /*width*/,
YGMeasureMode /*widthMode*/,
float /*height*/,
@@ -25,7 +25,7 @@ static YGSize _measure1(
}
static YGSize _measure2(
YGNodeRef /*node*/,
YGNodeConstRef /*node*/,
float /*width*/,
YGMeasureMode /*widthMode*/,
float /*height*/,

View File

@@ -9,7 +9,7 @@
#include <yoga/Yoga.h>
static YGSize _measure(
YGNodeRef /*node*/,
YGNodeConstRef /*node*/,
float width,
YGMeasureMode widthMode,
float height,

View File

@@ -9,7 +9,7 @@
#include <yoga/Yoga.h>
static float _baseline(
YGNodeRef node,
YGNodeConstRef node,
const float /*width*/,
const float /*height*/) {
float* baseline = (float*) YGNodeGetContext(node);

View File

@@ -11,7 +11,7 @@
using namespace facebook;
static void _dirtied(YGNodeRef node) {
static void _dirtied(YGNodeConstRef node) {
int* dirtiedCount = (int*) YGNodeGetContext(node);
(*dirtiedCount)++;
}

View File

@@ -147,7 +147,7 @@ TEST(YogaTest, dirty_propagation_changing_benign_config) {
YGConfigRef newConfig = YGConfigNew();
YGConfigSetLogger(
newConfig,
[](const YGConfigRef, const YGNodeRef, YGLogLevel, const char*, va_list) {
[](YGConfigConstRef, YGNodeConstRef, YGLogLevel, const char*, va_list) {
return 0;
});
YGNodeSetConfig(root_child0, newConfig);

View File

@@ -14,8 +14,8 @@
namespace {
char writeBuffer[4096];
int _unmanagedLogger(
const YGConfigRef /*config*/,
const YGNodeRef /*node*/,
const YGConfigConstRef /*config*/,
const YGNodeConstRef /*node*/,
YGLogLevel /*level*/,
const char* format,
va_list args) {

View File

@@ -9,7 +9,7 @@
#include <yoga/Yoga.h>
static YGSize _measureMax(
YGNodeRef node,
YGNodeConstRef node,
float width,
YGMeasureMode widthMode,
float height,
@@ -24,7 +24,7 @@ static YGSize _measureMax(
}
static YGSize _measureMin(
YGNodeRef node,
YGNodeConstRef node,
float width,
YGMeasureMode widthMode,
float height,
@@ -44,7 +44,7 @@ static YGSize _measureMin(
}
static YGSize _measure_84_49(
YGNodeRef node,
YGNodeConstRef node,
float /*width*/,
YGMeasureMode /*widthMode*/,
float /*height*/,

View File

@@ -21,7 +21,7 @@ struct _MeasureConstraintList {
};
static YGSize _measure(
YGNodeRef node,
YGNodeConstRef node,
float width,
YGMeasureMode widthMode,
float height,

View File

@@ -9,7 +9,7 @@
#include <yoga/Yoga.h>
static YGSize _measure(
YGNodeRef node,
YGNodeConstRef node,
float /*width*/,
YGMeasureMode /*widthMode*/,
float /*height*/,
@@ -23,7 +23,7 @@ static YGSize _measure(
}
static YGSize _simulate_wrapping_text(
YGNodeRef /*node*/,
YGNodeConstRef /*node*/,
float width,
YGMeasureMode widthMode,
float /*height*/,
@@ -36,7 +36,7 @@ static YGSize _simulate_wrapping_text(
}
static YGSize _measure_assert_negative(
YGNodeRef /*node*/,
YGNodeConstRef /*node*/,
float width,
YGMeasureMode /*widthMode*/,
float height,
@@ -640,7 +640,7 @@ TEST(YogaTest, cant_call_negative_measure_horizontal) {
}
static YGSize _measure_90_10(
YGNodeRef /*node*/,
YGNodeConstRef /*node*/,
float /*width*/,
YGMeasureMode /*widthMode*/,
float /*height*/,
@@ -650,7 +650,7 @@ static YGSize _measure_90_10(
}
static YGSize _measure_100_100(
YGNodeRef /*node*/,
YGNodeConstRef /*node*/,
float /*width*/,
YGMeasureMode /*widthMode*/,
float /*height*/,

View File

@@ -22,9 +22,10 @@ TEST(Node, hasMeasureFunc_initial) {
TEST(Node, hasMeasureFunc_with_measure_fn) {
auto n = Node{};
n.setMeasureFunc([](YGNodeRef, float, YGMeasureMode, float, YGMeasureMode) {
return YGSize{};
});
n.setMeasureFunc(
[](YGNodeConstRef, float, YGMeasureMode, float, YGMeasureMode) {
return YGSize{};
});
ASSERT_TRUE(n.hasMeasureFunc());
}
@@ -32,7 +33,7 @@ TEST(Node, measure_with_measure_fn) {
auto n = Node{};
n.setMeasureFunc(
[](YGNodeRef, float w, YGMeasureMode wm, float h, YGMeasureMode hm) {
[](YGNodeConstRef, float w, YGMeasureMode wm, float h, YGMeasureMode hm) {
return YGSize{w * static_cast<float>(wm), h / static_cast<float>(hm)};
});
@@ -43,10 +44,12 @@ TEST(Node, measure_with_measure_fn) {
TEST(Node, measure_with_context_measure_fn) {
auto n = Node{};
n.setMeasureFunc(
[](YGNodeRef, float, YGMeasureMode, float, YGMeasureMode, void* ctx) {
return *(YGSize*) ctx;
});
n.setMeasureFunc([](YGNodeConstRef,
float,
YGMeasureMode,
float,
YGMeasureMode,
void* ctx) { return *(YGSize*) ctx; });
auto result = YGSize{123.4f, -56.7f};
ASSERT_EQ(
@@ -57,11 +60,11 @@ TEST(Node, measure_with_context_measure_fn) {
TEST(Node, switching_measure_fn_types) {
auto n = Node{};
n.setMeasureFunc(
[](YGNodeRef, float, YGMeasureMode, float, YGMeasureMode, void*) {
[](YGNodeConstRef, float, YGMeasureMode, float, YGMeasureMode, void*) {
return YGSize{};
});
n.setMeasureFunc(
[](YGNodeRef, float w, YGMeasureMode wm, float h, YGMeasureMode hm) {
[](YGNodeConstRef, float w, YGMeasureMode wm, float h, YGMeasureMode hm) {
return YGSize{w * static_cast<float>(wm), h / static_cast<float>(hm)};
});
@@ -72,9 +75,10 @@ TEST(Node, switching_measure_fn_types) {
TEST(Node, hasMeasureFunc_after_unset) {
auto n = Node{};
n.setMeasureFunc([](YGNodeRef, float, YGMeasureMode, float, YGMeasureMode) {
return YGSize{};
});
n.setMeasureFunc(
[](YGNodeConstRef, float, YGMeasureMode, float, YGMeasureMode) {
return YGSize{};
});
n.setMeasureFunc(nullptr);
ASSERT_FALSE(n.hasMeasureFunc());
@@ -83,7 +87,7 @@ TEST(Node, hasMeasureFunc_after_unset) {
TEST(Node, hasMeasureFunc_after_unset_context) {
auto n = Node{};
n.setMeasureFunc(
[](YGNodeRef, float, YGMeasureMode, float, YGMeasureMode, void*) {
[](YGNodeConstRef, float, YGMeasureMode, float, YGMeasureMode, void*) {
return YGSize{};
});
@@ -98,20 +102,20 @@ TEST(Node, hasBaselineFunc_initial) {
TEST(Node, hasBaselineFunc_with_baseline_fn) {
auto n = Node{};
n.setBaselineFunc([](YGNodeRef, float, float) { return 0.0f; });
n.setBaselineFunc([](YGNodeConstRef, float, float) { return 0.0f; });
ASSERT_TRUE(n.hasBaselineFunc());
}
TEST(Node, baseline_with_baseline_fn) {
auto n = Node{};
n.setBaselineFunc([](YGNodeRef, float w, float h) { return w + h; });
n.setBaselineFunc([](YGNodeConstRef, float w, float h) { return w + h; });
ASSERT_EQ(n.baseline(1.25f, 2.5f, nullptr), 3.75f);
}
TEST(Node, baseline_with_context_baseline_fn) {
auto n = Node{};
n.setBaselineFunc([](YGNodeRef, float w, float h, void* ctx) {
n.setBaselineFunc([](YGNodeConstRef, float w, float h, void* ctx) {
return w + h + *(float*) ctx;
});
@@ -121,7 +125,7 @@ TEST(Node, baseline_with_context_baseline_fn) {
TEST(Node, hasBaselineFunc_after_unset) {
auto n = Node{};
n.setBaselineFunc([](YGNodeRef, float, float) { return 0.0f; });
n.setBaselineFunc([](YGNodeConstRef, float, float) { return 0.0f; });
n.setBaselineFunc(nullptr);
ASSERT_FALSE(n.hasBaselineFunc());
@@ -129,7 +133,7 @@ TEST(Node, hasBaselineFunc_after_unset) {
TEST(Node, hasBaselineFunc_after_unset_context) {
auto n = Node{};
n.setBaselineFunc([](YGNodeRef, float, float, void*) { return 0.0f; });
n.setBaselineFunc([](YGNodeConstRef, float, float, void*) { return 0.0f; });
n.setMeasureFunc(nullptr);
ASSERT_FALSE(n.hasMeasureFunc());
@@ -137,7 +141,7 @@ TEST(Node, hasBaselineFunc_after_unset_context) {
TEST(Node, switching_baseline_fn_types) {
auto n = Node{};
n.setBaselineFunc([](YGNodeRef, float, float, void*) { return 0.0f; });
n.setBaselineFunc([](YGNodeRef, float, float) { return 1.0f; });
n.setBaselineFunc([](YGNodeConstRef, float, float, void*) { return 0.0f; });
n.setBaselineFunc([](YGNodeConstRef, float, float) { return 1.0f; });
ASSERT_EQ(n.baseline(1, 2, nullptr), 1.0f);
}

View File

@@ -43,7 +43,7 @@ TEST(YogaTest, rounding_value) {
}
static YGSize measureText(
YGNodeRef /*node*/,
YGNodeConstRef /*node*/,
float /*width*/,
YGMeasureMode /*widthMode*/,
float /*height*/,

View File

@@ -9,7 +9,7 @@
#include <yoga/Yoga.h>
static YGSize _measureFloor(
YGNodeRef /*node*/,
YGNodeConstRef /*node*/,
float width,
YGMeasureMode /*widthMode*/,
float height,
@@ -21,7 +21,7 @@ static YGSize _measureFloor(
}
static YGSize _measureCeil(
YGNodeRef /*node*/,
YGNodeConstRef /*node*/,
float width,
YGMeasureMode /*widthMode*/,
float height,
@@ -33,7 +33,7 @@ static YGSize _measureCeil(
}
static YGSize _measureFractial(
YGNodeRef /*node*/,
YGNodeConstRef /*node*/,
float width,
YGMeasureMode /*widthMode*/,
float height,

View File

@@ -23,8 +23,12 @@ struct ConfigCloningTest : public ::testing::Test {
void TearDown() override;
static yoga::Node clonedNode;
static YGNodeRef cloneNode(YGNodeRef, YGNodeRef, int) { return &clonedNode; }
static YGNodeRef doNotClone(YGNodeRef, YGNodeRef, int) { return nullptr; }
static YGNodeRef cloneNode(YGNodeConstRef, YGNodeConstRef, int) {
return &clonedNode;
}
static YGNodeRef doNotClone(YGNodeConstRef, YGNodeConstRef, int) {
return nullptr;
}
};
TEST_F(ConfigCloningTest, uses_values_provided_by_cloning_callback) {
@@ -49,9 +53,10 @@ TEST_F(
}
TEST_F(ConfigCloningTest, can_clone_with_context) {
config->setCloneNodeCallback([](YGNodeRef, YGNodeRef, int, void* context) {
return (YGNodeRef) context;
});
config->setCloneNodeCallback(
[](YGNodeConstRef, YGNodeConstRef, int, void* context) {
return (YGNodeRef) context;
});
yoga::Node node{}, owner{}, clone{};
ASSERT_EQ(config->cloneNode(&node, &owner, 0, &clone), &clone);