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

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