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

@@ -76,7 +76,7 @@ static void __printBenchmarkResult(
}
static YGSize _measure(
YGNodeRef node,
YGNodeConstRef node,
float width,
YGMeasureMode widthMode,
float height,

View File

@@ -27,7 +27,7 @@ using namespace facebook::yoga;
using namespace facebook::yoga::vanillajni;
static inline ScopedLocalRef<jobject> YGNodeJobject(
YGNodeRef node,
YGNodeConstRef node,
void* layoutContext) {
return reinterpret_cast<PtrJNodeMapVanilla*>(layoutContext)->ref(node);
}
@@ -138,8 +138,8 @@ static jlong jni_YGNodeNewWithConfigJNI(
}
static int YGJNILogFunc(
const YGConfigRef config,
const YGNodeRef /*node*/,
const YGConfigConstRef config,
const YGNodeConstRef /*node*/,
YGLogLevel level,
void* /*layoutContext*/,
const char* format,
@@ -639,7 +639,7 @@ static void jni_YGNodeStyleSetBorderJNI(
yogaNodeRef, static_cast<YGEdge>(edge), static_cast<float>(border));
}
static void YGTransferLayoutDirection(YGNodeRef node, jobject javaNode) {
static void YGTransferLayoutDirection(YGNodeConstRef node, jobject javaNode) {
// Don't change this field name without changing the name of the field in
// Database.java
JNIEnv* env = getCurrentEnv();
@@ -655,7 +655,7 @@ static void YGTransferLayoutDirection(YGNodeRef node, jobject javaNode) {
}
static YGSize YGJNIMeasureFunc(
YGNodeRef node,
YGNodeConstRef node,
float width,
YGMeasureMode widthMode,
float height,
@@ -700,7 +700,7 @@ static void jni_YGNodeSetHasMeasureFuncJNI(
}
static float YGJNIBaselineFunc(
YGNodeRef node,
YGNodeConstRef node,
float width,
float height,
void* layoutContext) {

View File

@@ -15,7 +15,7 @@
#include "./Config.h"
static YGSize globalMeasureFunc(
YGNodeRef nodeRef,
YGNodeConstRef nodeRef,
float width,
YGMeasureMode widthMode,
float height,
@@ -29,7 +29,7 @@ static YGSize globalMeasureFunc(
return ygSize;
}
static void globalDirtiedFunc(YGNodeRef nodeRef) {
static void globalDirtiedFunc(YGNodeConstRef nodeRef) {
Node const& node = *reinterpret_cast<Node const*>(YGNodeGetContext(nodeRef));
node.callDirtiedFunc();

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);

View File

@@ -22,15 +22,15 @@ using namespace facebook::yoga;
#ifdef ANDROID
static int YGAndroidLog(
const YGConfigRef config,
const YGNodeRef node,
const YGConfigConstRef config,
const YGNodeConstRef node,
YGLogLevel level,
const char* format,
va_list args);
#else
static int YGDefaultLog(
const YGConfigRef config,
const YGNodeRef node,
const YGConfigConstRef config,
const YGNodeConstRef node,
YGLogLevel level,
const char* format,
va_list args);
@@ -39,8 +39,8 @@ static int YGDefaultLog(
#ifdef ANDROID
#include <android/log.h>
static int YGAndroidLog(
const YGConfigRef /*config*/,
const YGNodeRef /*node*/,
const YGConfigConstRef /*config*/,
const YGNodeConstRef /*node*/,
YGLogLevel level,
const char* format,
va_list args) {
@@ -69,16 +69,12 @@ static int YGAndroidLog(
return result;
}
#else
#define YG_UNUSED(x) (void) (x);
static int YGDefaultLog(
const YGConfigRef config,
const YGNodeRef node,
const YGConfigConstRef /*config*/,
const YGNodeConstRef /*node*/,
YGLogLevel level,
const char* format,
va_list args) {
YG_UNUSED(config);
YG_UNUSED(node);
switch (level) {
case YGLogLevelError:
case YGLogLevelFatal:
@@ -91,8 +87,6 @@ static int YGDefaultLog(
return vprintf(format, args);
}
}
#undef YG_UNUSED
#endif
YOGA_EXPORT bool YGFloatIsUndefined(const float value) {
@@ -202,7 +196,7 @@ YOGA_EXPORT YGNodeRef YGNodeNew(void) {
return YGNodeNewWithConfig(YGConfigGetDefault());
}
YOGA_EXPORT YGNodeRef YGNodeClone(YGNodeRef oldNodeRef) {
YOGA_EXPORT YGNodeRef YGNodeClone(YGNodeConstRef oldNodeRef) {
auto oldNode = resolveRef(oldNodeRef);
const auto node = new yoga::Node(*oldNode);
yoga::assertFatalWithConfig(

View File

@@ -29,28 +29,30 @@ typedef struct YGNode* YGNodeRef;
typedef const struct YGNode* YGNodeConstRef;
typedef YGSize (*YGMeasureFunc)(
YGNodeRef node,
YGNodeConstRef node,
float width,
YGMeasureMode widthMode,
float height,
YGMeasureMode heightMode);
typedef float (*YGBaselineFunc)(YGNodeRef node, float width, float height);
typedef void (*YGDirtiedFunc)(YGNodeRef node);
typedef void (*YGPrintFunc)(YGNodeRef node);
typedef void (*YGNodeCleanupFunc)(YGNodeRef node);
typedef float (*YGBaselineFunc)(YGNodeConstRef node, float width, float height);
typedef void (*YGDirtiedFunc)(YGNodeConstRef node);
typedef void (*YGPrintFunc)(YGNodeConstRef node);
typedef void (*YGNodeCleanupFunc)(YGNodeConstRef node);
typedef int (*YGLogger)(
YGConfigRef config,
YGNodeRef node,
YGConfigConstRef config,
YGNodeConstRef node,
YGLogLevel level,
const char* format,
va_list args);
typedef YGNodeRef (
*YGCloneNodeFunc)(YGNodeRef oldNode, YGNodeRef owner, int childIndex);
typedef YGNodeRef (*YGCloneNodeFunc)(
YGNodeConstRef oldNode,
YGNodeConstRef owner,
int childIndex);
// YGNode
WIN_EXPORT YGNodeRef YGNodeNew(void);
WIN_EXPORT YGNodeRef YGNodeNewWithConfig(YGConfigRef config);
WIN_EXPORT YGNodeRef YGNodeClone(YGNodeRef node);
WIN_EXPORT YGNodeRef YGNodeClone(YGNodeConstRef node);
WIN_EXPORT void YGNodeFree(YGNodeRef node);
WIN_EXPORT void YGNodeFreeRecursiveWithCleanupFunc(
YGNodeRef node,

View File

@@ -107,23 +107,10 @@ void Config::log(
void* logContext,
const char* format,
va_list args) const {
// TODO: Break log callback signatures to make them const correct
if (flags_.loggerUsesContext) {
logger_.withContext(
const_cast<yoga::Config*>(this),
const_cast<yoga::Node*>(node),
logLevel,
logContext,
format,
args);
logger_.withContext(this, node, logLevel, logContext, format, args);
} else {
logger_.noContext(
const_cast<yoga::Config*>(this),
const_cast<yoga::Node*>(node),
logLevel,
format,
args);
logger_.noContext(this, node, logLevel, format, args);
}
}
@@ -142,8 +129,8 @@ void Config::setCloneNodeCallback(std::nullptr_t) {
}
YGNodeRef Config::cloneNode(
YGNodeRef node,
YGNodeRef owner,
YGNodeConstRef node,
YGNodeConstRef owner,
int childIndex,
void* cloneContext) const {
YGNodeRef clone = nullptr;

View File

@@ -25,15 +25,15 @@ bool configUpdateInvalidatesLayout(Config* a, Config* b);
// Internal variants of log functions, currently used only by JNI bindings.
// TODO: Reconcile this with the public API
using LogWithContextFn = int (*)(
YGConfigRef config,
YGNodeRef node,
YGConfigConstRef config,
YGNodeConstRef node,
YGLogLevel level,
void* context,
const char* format,
va_list args);
using CloneWithContextFn = YGNodeRef (*)(
YGNodeRef node,
YGNodeRef owner,
YGNodeConstRef node,
YGNodeConstRef owner,
int childIndex,
void* cloneContext);
@@ -90,8 +90,8 @@ public:
void setCloneNodeCallback(CloneWithContextFn cloneNode);
void setCloneNodeCallback(std::nullptr_t);
YGNodeRef cloneNode(
YGNodeRef node,
YGNodeRef owner,
YGNodeConstRef node,
YGNodeConstRef owner,
int childIndex,
void* cloneContext) const;

View File

@@ -38,10 +38,17 @@ struct NodeFlags {
class YOGA_EXPORT Node : public ::YGNode {
public:
using MeasureWithContextFn =
YGSize (*)(YGNode*, float, YGMeasureMode, float, YGMeasureMode, void*);
using BaselineWithContextFn = float (*)(YGNode*, float, float, void*);
using PrintWithContextFn = void (*)(YGNode*, void*);
// Internal variants of callbacks, currently used only by JNI bindings.
// TODO: Reconcile this with the public API
using MeasureWithContextFn = YGSize (*)(
YGNodeConstRef,
float,
YGMeasureMode,
float,
YGMeasureMode,
void*);
using BaselineWithContextFn = float (*)(YGNodeConstRef, float, float, void*);
using PrintWithContextFn = void (*)(YGNodeConstRef, void*);
private:
void* context_ = nullptr;