From 170fa2227e754c6dbe352eb060350d33a8bfb825 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Sun, 10 Sep 2023 03:28:17 -0700 Subject: [PATCH] Breaking: Fix callback const-correctness Differential Revision: D49130714 fbshipit-source-id: 810dbad5ec52e415c5279b80759ba449e8c8aa08 --- .../Text/BaseText/RCTBaseTextShadowView.mm | 2 +- .../Libraries/Text/Text/RCTTextShadowView.mm | 10 +++++--- .../TextInput/RCTBaseTextInputShadowView.mm | 4 ++-- .../Libraries/Wrapper/RCTWrapperShadowView.m | 2 +- .../react-native/React/Views/RCTShadowView.m | 4 ++-- .../first-party/yogajni/jni/YGJNIVanilla.cpp | 12 +++++----- .../view/YogaLayoutableShadowNode.cpp | 14 +++++------ .../view/YogaLayoutableShadowNode.h | 9 +++---- .../ReactCommon/yoga/yoga/Yoga.cpp | 24 +++++++------------ .../react-native/ReactCommon/yoga/yoga/Yoga.h | 22 +++++++++-------- .../ReactCommon/yoga/yoga/config/Config.cpp | 21 ++++------------ .../ReactCommon/yoga/yoga/config/Config.h | 12 +++++----- .../ReactCommon/yoga/yoga/node/Node.h | 15 ++++++++---- 13 files changed, 73 insertions(+), 78 deletions(-) diff --git a/packages/react-native/Libraries/Text/BaseText/RCTBaseTextShadowView.mm b/packages/react-native/Libraries/Text/BaseText/RCTBaseTextShadowView.mm index c8fd3b15a804e5..eab17f67dd7df4 100644 --- a/packages/react-native/Libraries/Text/BaseText/RCTBaseTextShadowView.mm +++ b/packages/react-native/Libraries/Text/BaseText/RCTBaseTextShadowView.mm @@ -15,7 +15,7 @@ NSString *const RCTBaseTextShadowViewEmbeddedShadowViewAttributeName = @"RCTBaseTextShadowViewEmbeddedShadowViewAttributeName"; -static void RCTInlineViewYogaNodeDirtied(YGNodeRef node) +static void RCTInlineViewYogaNodeDirtied(YGNodeConstRef node) { // An inline view (a view nested inside of a text node) does not have a parent // in the Yoga tree. Consequently, we have to manually propagate the inline diff --git a/packages/react-native/Libraries/Text/Text/RCTTextShadowView.mm b/packages/react-native/Libraries/Text/Text/RCTTextShadowView.mm index d84aca5d34479f..5afa863ad0600c 100644 --- a/packages/react-native/Libraries/Text/Text/RCTTextShadowView.mm +++ b/packages/react-native/Libraries/Text/Text/RCTTextShadowView.mm @@ -368,8 +368,12 @@ - (CGFloat)lastBaselineForSize:(CGSize)size return size.height + maximumDescender; } -static YGSize -RCTTextShadowViewMeasure(YGNodeRef node, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode) +static YGSize RCTTextShadowViewMeasure( + YGNodeConstRef node, + float width, + YGMeasureMode widthMode, + float height, + YGMeasureMode heightMode) { CGSize maximumSize = (CGSize){ widthMode == YGMeasureModeUndefined ? CGFLOAT_MAX : RCTCoreGraphicsFloatFromYogaFloat(width), @@ -402,7 +406,7 @@ - (CGFloat)lastBaselineForSize:(CGSize)size RCTYogaFloatFromCoreGraphicsFloat(size.height + epsilon)}; } -static float RCTTextShadowViewBaseline(YGNodeRef node, const float width, const float height) +static float RCTTextShadowViewBaseline(YGNodeConstRef node, const float width, const float height) { RCTTextShadowView *shadowTextView = (__bridge RCTTextShadowView *)YGNodeGetContext(node); diff --git a/packages/react-native/Libraries/Text/TextInput/RCTBaseTextInputShadowView.mm b/packages/react-native/Libraries/Text/TextInput/RCTBaseTextInputShadowView.mm index 04d2446f86d9b3..1f06b79070aa54 100644 --- a/packages/react-native/Libraries/Text/TextInput/RCTBaseTextInputShadowView.mm +++ b/packages/react-native/Libraries/Text/TextInput/RCTBaseTextInputShadowView.mm @@ -259,7 +259,7 @@ - (CGFloat)lastBaselineForSize:(CGSize)size } static YGSize RCTBaseTextInputShadowViewMeasure( - YGNodeRef node, + YGNodeConstRef node, float width, YGMeasureMode widthMode, float height, @@ -302,7 +302,7 @@ static YGSize RCTBaseTextInputShadowViewMeasure( RCTYogaFloatFromCoreGraphicsFloat(measuredSize.width), RCTYogaFloatFromCoreGraphicsFloat(measuredSize.height)}; } -static float RCTTextInputShadowViewBaseline(YGNodeRef node, const float width, const float height) +static float RCTTextInputShadowViewBaseline(YGNodeConstRef node, const float width, const float height) { RCTBaseTextInputShadowView *shadowTextView = (__bridge RCTBaseTextInputShadowView *)YGNodeGetContext(node); diff --git a/packages/react-native/Libraries/Wrapper/RCTWrapperShadowView.m b/packages/react-native/Libraries/Wrapper/RCTWrapperShadowView.m index 99b8acfd1a1759..1425767be5cf8c 100644 --- a/packages/react-native/Libraries/Wrapper/RCTWrapperShadowView.m +++ b/packages/react-native/Libraries/Wrapper/RCTWrapperShadowView.m @@ -30,7 +30,7 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge } static YGSize RCTWrapperShadowViewMeasure( - YGNodeRef node, + YGNodeConstRef node, float width, YGMeasureMode widthMode, float height, diff --git a/packages/react-native/React/Views/RCTShadowView.m b/packages/react-native/React/Views/RCTShadowView.m index 7f8a47a22c9828..e0084ea7f1d656 100644 --- a/packages/react-native/React/Views/RCTShadowView.m +++ b/packages/react-native/React/Views/RCTShadowView.m @@ -60,7 +60,7 @@ + (YGConfigRef)yogaConfig // YogaNode API -static void RCTPrint(YGNodeRef node) +static void RCTPrint(YGNodeConstRef node) { RCTShadowView *shadowView = (__bridge RCTShadowView *)YGNodeGetContext(node); printf("%s(%lld), ", shadowView.viewName.UTF8String, (long long)shadowView.reactTag.integerValue); @@ -568,7 +568,7 @@ - (void)setSize:(CGSize)size // IntrinsicContentSize static inline YGSize -RCTShadowViewMeasure(YGNodeRef node, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode) +RCTShadowViewMeasure(YGNodeConstRef node, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode) { RCTShadowView *shadowView = (__bridge RCTShadowView *)YGNodeGetContext(node); diff --git a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp index 6a1406c170e5ac..af06c4a3965eb9 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp @@ -27,7 +27,7 @@ using namespace facebook::yoga; using namespace facebook::yoga::vanillajni; static inline ScopedLocalRef YGNodeJobject( - YGNodeRef node, + YGNodeConstRef node, void* layoutContext) { return reinterpret_cast(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(edge), static_cast(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) { diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp index 4fa4e629234de4..d026972495d9df 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp @@ -26,8 +26,8 @@ namespace facebook::react { static int FabricDefaultYogaLog( - const YGConfigRef /*unused*/, - const YGNodeRef /*unused*/, + const YGConfigConstRef /*unused*/, + const YGNodeConstRef /*unused*/, YGLogLevel level, const char* format, va_list args) { @@ -787,9 +787,9 @@ Rect YogaLayoutableShadowNode::getContentBounds() const { #pragma mark - Yoga Connectors -YGNode* YogaLayoutableShadowNode::yogaNodeCloneCallbackConnector( - YGNode* /*oldYogaNode*/, - YGNode* parentYogaNode, +YGNodeRef YogaLayoutableShadowNode::yogaNodeCloneCallbackConnector( + YGNodeConstRef /*oldYogaNode*/, + YGNodeConstRef parentYogaNode, int childIndex) { SystraceSection s("YogaLayoutableShadowNode::yogaNodeCloneCallbackConnector"); @@ -798,7 +798,7 @@ YGNode* YogaLayoutableShadowNode::yogaNodeCloneCallbackConnector( } YGSize YogaLayoutableShadowNode::yogaNodeMeasureCallbackConnector( - YGNode* yogaNode, + YGNodeConstRef yogaNode, float width, YGMeasureMode widthMode, float height, @@ -845,7 +845,7 @@ YGSize YogaLayoutableShadowNode::yogaNodeMeasureCallbackConnector( } YogaLayoutableShadowNode& YogaLayoutableShadowNode::shadowNodeFromContext( - YGNodeRef yogaNode) { + YGNodeConstRef yogaNode) { return traitCast( *static_cast(YGNodeGetContext(yogaNode))); } diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h index 288b6c2bfdc7bf..03a5997e643b40 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.h @@ -159,16 +159,17 @@ class YogaLayoutableShadowNode : public LayoutableShadowNode { yoga::Config& config, YGConfigRef previousConfig = nullptr); static YGNodeRef yogaNodeCloneCallbackConnector( - YGNodeRef oldYogaNode, - YGNodeRef parentYogaNode, + YGNodeConstRef oldYogaNode, + YGNodeConstRef parentYogaNode, int childIndex); static YGSize yogaNodeMeasureCallbackConnector( - YGNodeRef yogaNode, + YGNodeConstRef yogaNode, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode); - static YogaLayoutableShadowNode& shadowNodeFromContext(YGNodeRef yogaNode); + static YogaLayoutableShadowNode& shadowNodeFromContext( + YGNodeConstRef yogaNode); #pragma mark - RTL Legacy Autoflip diff --git a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp b/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp index 1b81de2be51e6b..0e89c06d8a83ca 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/Yoga.cpp @@ -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 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( diff --git a/packages/react-native/ReactCommon/yoga/yoga/Yoga.h b/packages/react-native/ReactCommon/yoga/yoga/Yoga.h index 54569adc5d61d4..fa5b9834ec6169 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/Yoga.h +++ b/packages/react-native/ReactCommon/yoga/yoga/Yoga.h @@ -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, diff --git a/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp b/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp index 3032a5b7d14150..deb7b5fe4d8768 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp @@ -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(this), - const_cast(node), - logLevel, - logContext, - format, - args); + logger_.withContext(this, node, logLevel, logContext, format, args); } else { - logger_.noContext( - const_cast(this), - const_cast(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; diff --git a/packages/react-native/ReactCommon/yoga/yoga/config/Config.h b/packages/react-native/ReactCommon/yoga/yoga/config/Config.h index 0a422c9c295a01..75bde993b61a96 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/config/Config.h +++ b/packages/react-native/ReactCommon/yoga/yoga/config/Config.h @@ -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 @@ class YOGA_EXPORT Config : public ::YGConfig { void setCloneNodeCallback(CloneWithContextFn cloneNode); void setCloneNodeCallback(std::nullptr_t); YGNodeRef cloneNode( - YGNodeRef node, - YGNodeRef owner, + YGNodeConstRef node, + YGNodeConstRef owner, int childIndex, void* cloneContext) const; diff --git a/packages/react-native/ReactCommon/yoga/yoga/node/Node.h b/packages/react-native/ReactCommon/yoga/yoga/node/Node.h index 8a21d4306072e1..4e8ef24edea3f8 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/Node.h +++ b/packages/react-native/ReactCommon/yoga/yoga/node/Node.h @@ -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;