Move node cloning to YGConfig

Summary:
@public

Encapsulates node cloning within `YGConfig`.
This is necessary for allowing for context-aware cloning functions, which will ultimately allow for removal of weak global JNI references.

Reviewed By: shergin

Differential Revision: D14132608

fbshipit-source-id: 0dec114c8e172b1e34a4b7fd146c43f13c151ade
This commit is contained in:
David Aurelio
2019-02-20 11:52:38 -08:00
committed by Facebook Github Bot
parent 2643b96589
commit 367a93de88
5 changed files with 82 additions and 20 deletions

60
tests/YGConfigTest.cpp Normal file
View File

@@ -0,0 +1,60 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*/
// @Generated by gentest/gentest.rb from gentest/fixtures/YGDisplayTest.html
#include <gtest/gtest.h>
#include <yoga/Yoga.h>
#include <yoga/YGConfig.h>
#include <yoga/YGNode.h>
#include <functional>
#include <memory>
struct ConfigCloningTest : public ::testing::Test {
std::unique_ptr<YGConfig, std::function<void(YGConfig*)>> config;
void SetUp() override;
void TearDown() override;
static YGNode clonedNode;
static YGNodeRef cloneNode(YGNodeRef, YGNodeRef, int) {
return &clonedNode;
}
static YGNodeRef doNotClone(YGNodeRef, YGNodeRef, int) {
return nullptr;
}
};
TEST_F(ConfigCloningTest, uses_values_provided_by_cloning_callback) {
config->setCloneNodeCallback(cloneNode);
YGNode node{}, owner{};
auto clone = config->cloneNode(&node, &owner, 0);
ASSERT_EQ(clone, &clonedNode);
}
TEST_F(
ConfigCloningTest,
falls_back_to_regular_cloning_if_callback_returns_null) {
config->setCloneNodeCallback(doNotClone);
YGNode node{}, owner{};
auto clone = config->cloneNode(&node, &owner, 0);
ASSERT_NE(clone, nullptr);
YGNodeFree(clone);
}
void ConfigCloningTest::SetUp() {
config = {YGConfigNew(), YGConfigFree};
}
void ConfigCloningTest::TearDown() {
config.reset();
}
YGNode ConfigCloningTest::clonedNode = {};

View File

@@ -6,7 +6,7 @@
*/
#include "YGConfig.h"
YGConfig::YGConfig(YGLogger logger) {
YGConfig::YGConfig(YGLogger logger) : cloneNodeCallback_{nullptr} {
logger_.noContext = logger;
loggerUsesContext_ = false;
}
@@ -24,3 +24,14 @@ void YGConfig::log(
logger_.noContext(config, node, logLevel, format, args);
}
}
YGNodeRef YGConfig::cloneNode(YGNodeRef node, YGNodeRef owner, int childIndex) {
YGNodeRef clone = nullptr;
if (cloneNodeCallback_ != nullptr) {
clone = cloneNodeCallback_(node, owner, childIndex);
}
if (clone == nullptr) {
clone = YGNodeClone(node);
}
return clone;
}

View File

@@ -19,6 +19,7 @@ struct YGConfig {
va_list args);
private:
YGCloneNodeFunc cloneNodeCallback_ = nullptr;
union {
LogWithContextFn withContext;
YGLogger noContext;
@@ -31,7 +32,6 @@ public:
bool shouldDiffLayoutWithoutLegacyStretchBehaviour = false;
bool printTree = false;
float pointScaleFactor = 1.0f;
YGCloneNodeFunc cloneNodeCallback = nullptr;
std::array<bool, facebook::yoga::enums::count<YGExperimentalFeature>()>
experimentalFeatures = {};
void* context = nullptr;
@@ -50,4 +50,9 @@ public:
void setLogger(std::nullptr_t) {
setLogger(YGLogger{nullptr});
}
YGNodeRef cloneNode(YGNodeRef node, YGNodeRef owner, int childIndex);
void setCloneNodeCallback(YGCloneNodeFunc cloneNode) {
cloneNodeCallback_ = cloneNode;
}
};

View File

@@ -404,16 +404,9 @@ void YGNode::cloneChildrenIfNeeded() {
return;
}
const YGCloneNodeFunc cloneNodeCallback = config_->cloneNodeCallback;
for (uint32_t i = 0; i < childCount; ++i) {
const YGNodeRef oldChild = children_[i];
YGNodeRef newChild = nullptr;
if (cloneNodeCallback) {
newChild = cloneNodeCallback(oldChild, this, i);
}
if (newChild == nullptr) {
newChild = YGNodeClone(oldChild);
}
YGNodeRef newChild = config_->cloneNode(oldChild, this, i);
replaceChild(newChild, i);
newChild->setOwner(this);
}

View File

@@ -425,8 +425,6 @@ void YGNodeRemoveChild(const YGNodeRef owner, const YGNodeRef excludedChild) {
// Otherwise we have to clone the node list except for the child we're trying
// to delete. We don't want to simply clone all children, because then the
// host will need to free the clone of the child that was just deleted.
const YGCloneNodeFunc cloneNodeCallback =
owner->getConfig()->cloneNodeCallback;
uint32_t nextInsertIndex = 0;
for (uint32_t i = 0; i < childCount; i++) {
const YGNodeRef oldChild = owner->getChild(i);
@@ -437,13 +435,8 @@ void YGNodeRemoveChild(const YGNodeRef owner, const YGNodeRef excludedChild) {
owner->markDirtyAndPropogate();
continue;
}
YGNodeRef newChild = nullptr;
if (cloneNodeCallback) {
newChild = cloneNodeCallback(oldChild, owner, nextInsertIndex);
}
if (newChild == nullptr) {
newChild = YGNodeClone(oldChild);
}
YGNodeRef newChild =
owner->getConfig()->cloneNode(oldChild, owner, nextInsertIndex);
owner->replaceChild(newChild, nextInsertIndex);
newChild->setOwner(owner);
@@ -4293,7 +4286,7 @@ void* YGConfigGetContext(const YGConfigRef config) {
void YGConfigSetCloneNodeFunc(
const YGConfigRef config,
const YGCloneNodeFunc callback) {
config->cloneNodeCallback = callback;
config->setCloneNodeCallback(callback);
}
static void YGTraverseChildrenPreOrder(