Skip to content

Commit

Permalink
Rollout useNativeState (#40864)
Browse files Browse the repository at this point in the history
Summary:

Changelog: [Internal] Use JSI's NativeState abstraction to reference C++ objects across JS calls

Reviewed By: rubennorte, sammy-SC

Differential Revision: D50176083
  • Loading branch information
javache authored and facebook-github-bot committed Oct 13, 2023
1 parent edb7332 commit d57a837
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 84 deletions.
4 changes: 0 additions & 4 deletions packages/react-native/React/Fabric/RCTSurfacePresenter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,6 @@ - (RCTScheduler *)_createScheduler
CoreFeatures::enablePropIteratorSetter = true;
}

if (reactNativeConfig && reactNativeConfig->getBool("react_fabric:use_native_state")) {
CoreFeatures::useNativeState = true;
}

if (reactNativeConfig && reactNativeConfig->getBool("react_fabric:cancel_image_downloads_on_recycle")) {
CoreFeatures::cancelImageDownloadsOnRecycle = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,6 @@ public class ReactFeatureFlags {
*/
public static boolean reduceDeleteCreateMutation = false;

/**
* Use JSI NativeState API to store references to native objects rather than the more expensive
* HostObject pattern
*/
public static boolean useNativeState = false;

/** Report mount operations from the host platform to notify mount hooks. */
public static boolean enableMountHooks = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,6 @@ void Binding::installFabricUIManager(

CoreFeatures::enablePropIteratorSetter =
getFeatureFlagValue("enableCppPropsIteratorSetter");
CoreFeatures::useNativeState = getFeatureFlagValue("useNativeState");
CoreFeatures::doNotSwapLeftAndRightOnAndroidInLTR =
getFeatureFlagValue("doNotSwapLeftAndRightOnAndroidInLTR");
CoreFeatures::enableCleanParagraphYogaNode =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,12 @@

namespace facebook::react {

struct TaskWrapper : public jsi::HostObject {
TaskWrapper(const std::shared_ptr<Task>& task) : task(task) {}

std::shared_ptr<Task> task;
};

inline static jsi::Value valueFromTask(
jsi::Runtime& runtime,
std::shared_ptr<Task> task) {
if (CoreFeatures::useNativeState) {
jsi::Object obj(runtime);
obj.setNativeState(runtime, std::move(task));
return obj;
} else {
return jsi::Object::createFromHostObject(
runtime, std::make_shared<TaskWrapper>(task));
}
jsi::Object obj(runtime);
obj.setNativeState(runtime, std::move(task));
return obj;
}

inline static std::shared_ptr<Task> taskFromValue(
Expand All @@ -38,12 +27,7 @@ inline static std::shared_ptr<Task> taskFromValue(
if (value.isNull()) {
return nullptr;
}

if (CoreFeatures::useNativeState) {
return value.getObject(runtime).getNativeState<Task>(runtime);
} else {
return value.getObject(runtime).getHostObject<TaskWrapper>(runtime)->task;
}
return value.getObject(runtime).getNativeState<Task>(runtime);
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ namespace facebook::react {
// "key function" for the ShadowNodeWrapper class -- this allow for RTTI to work
// properly across dynamic library boundaries (i.e. dynamic_cast that is used by
// isHostObject method)
ShadowNodeWrapper::~ShadowNodeWrapper() = default;
ShadowNodeListWrapper::~ShadowNodeListWrapper() = default;

static std::unique_ptr<LeakChecker> constructLeakCheckerIfNeeded(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <react/renderer/core/ShadowNode.h>
#include <react/renderer/core/TraitCast.h>
#include <react/renderer/graphics/Rect.h>
#include <react/utils/CoreFeatures.h>

namespace facebook::react {

Expand All @@ -31,19 +30,7 @@ struct EventHandlerWrapper : public EventHandler {
jsi::Function callback;
};

struct ShadowNodeWrapper : public jsi::HostObject {
ShadowNodeWrapper(ShadowNode::Shared shadowNode)
: shadowNode(std::move(shadowNode)) {}

// The below method needs to be implemented out-of-line in order for the class
// to have at least one "key function" (see
// https://itanium-cxx-abi.github.io/cxx-abi/abi.html#vague-vtable)
~ShadowNodeWrapper() override;

ShadowNode::Shared shadowNode;
};

struct ShadowNodeListWrapper : public jsi::HostObject, public jsi::NativeState {
struct ShadowNodeListWrapper : public jsi::NativeState {
ShadowNodeListWrapper(ShadowNode::UnsharedListOfShared shadowNodeList)
: shadowNodeList(std::move(shadowNodeList)) {}

Expand All @@ -62,28 +49,17 @@ inline static ShadowNode::Shared shadowNodeFromValue(
return nullptr;
}

if (CoreFeatures::useNativeState) {
return value.getObject(runtime).getNativeState<ShadowNode>(runtime);
} else {
return value.getObject(runtime)
.getHostObject<ShadowNodeWrapper>(runtime)
->shadowNode;
}
return value.getObject(runtime).getNativeState<ShadowNode>(runtime);
}

inline static jsi::Value valueFromShadowNode(
jsi::Runtime& runtime,
ShadowNode::Shared shadowNode) {
if (CoreFeatures::useNativeState) {
jsi::Object obj(runtime);
// Need to const_cast since JSI only allows non-const pointees
obj.setNativeState(
runtime, std::const_pointer_cast<ShadowNode>(std::move(shadowNode)));
return obj;
} else {
return jsi::Object::createFromHostObject(
runtime, std::make_shared<ShadowNodeWrapper>(std::move(shadowNode)));
}
jsi::Object obj(runtime);
// Need to const_cast since JSI only allows non-const pointees
obj.setNativeState(
runtime, std::const_pointer_cast<ShadowNode>(std::move(shadowNode)));
return obj;
}

// TODO: once we no longer need to mutate the return value (appendChildToSet)
Expand Down Expand Up @@ -112,13 +88,8 @@ inline static ShadowNode::UnsharedListOfShared shadowNodeListFromValue(
;
}
} else {
if (CoreFeatures::useNativeState) {
return object.getNativeState<ShadowNodeListWrapper>(runtime)
->shadowNodeList;
} else {
return object.getHostObject<ShadowNodeListWrapper>(runtime)
->shadowNodeList;
}
return object.getNativeState<ShadowNodeListWrapper>(runtime)
->shadowNodeList;
}
}

Expand All @@ -127,15 +98,11 @@ inline static jsi::Value valueFromShadowNodeList(
ShadowNode::UnsharedListOfShared shadowNodeList) {
auto wrapper =
std::make_shared<ShadowNodeListWrapper>(std::move(shadowNodeList));
if (CoreFeatures::useNativeState) {
// Use the wrapper for NativeState too, otherwise we can't implement
// the marker interface. Could be simplified to a simple struct wrapper.
jsi::Object obj(runtime);
obj.setNativeState(runtime, std::move(wrapper));
return obj;
} else {
return jsi::Object::createFromHostObject(runtime, std::move(wrapper));
}
// Use the wrapper for NativeState too, otherwise we can't implement
// the marker interface. Could be simplified to a simple struct wrapper.
jsi::Object obj(runtime);
obj.setNativeState(runtime, std::move(wrapper));
return obj;
}

inline static ShadowNode::UnsharedListOfShared shadowNodeListFromWeakList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace facebook::react {

bool CoreFeatures::enablePropIteratorSetter = false;
bool CoreFeatures::blockPaintForUseLayoutEffect = false;
bool CoreFeatures::useNativeState = false;
bool CoreFeatures::cacheLastTextMeasurement = false;
bool CoreFeatures::cancelImageDownloadsOnRecycle = false;
bool CoreFeatures::enableGranularScrollViewStateUpdatesIOS = false;
Expand Down
4 changes: 0 additions & 4 deletions packages/react-native/ReactCommon/react/utils/CoreFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ class CoreFeatures {
// when a transaction is mounted.
static bool blockPaintForUseLayoutEffect;

// Whether to use Hermes' NativeState instead of HostObject
// in simple data passing scenarios with JS
static bool useNativeState;

// Yoga might measure multiple times the same Text with the same constraints
// This flag enables a caching mechanism to avoid subsequents measurements
// of the same Text with the same constrainst.
Expand Down

0 comments on commit d57a837

Please sign in to comment.