From fefb6c0031c03f6525b922a3e067db741105c70c Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Fri, 22 Mar 2024 05:12:08 -0700 Subject: [PATCH] Make UIConstantsProviderManager a static method Differential Revision: D55241233 --- .../facebook/react/runtime/ReactInstance.java | 82 ++++++++--------- ...r.java => UIConstantsProviderBinding.java} | 32 +------ .../src/main/jni/react/uimanager/OnLoad.cpp | 4 +- .../uimanager/UIConstantsProviderBinding.cpp | 64 +++++++++++++ ...Manager.h => UIConstantsProviderBinding.h} | 54 +++-------- .../uimanager/UIConstantsProviderManager.cpp | 92 ------------------- ...egacyUIManagerConstantsProviderBinding.cpp | 8 +- .../LegacyUIManagerConstantsProviderBinding.h | 6 +- .../RCTLegacyUIManagerConstantsProvider.mm | 3 +- 9 files changed, 129 insertions(+), 216 deletions(-) rename packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/{UIConstantsProviderManager.java => UIConstantsProviderBinding.java} (61%) create mode 100644 packages/react-native/ReactAndroid/src/main/jni/react/uimanager/UIConstantsProviderBinding.cpp rename packages/react-native/ReactAndroid/src/main/jni/react/uimanager/{UIConstantsProviderManager.h => UIConstantsProviderBinding.h} (56%) delete mode 100644 packages/react-native/ReactAndroid/src/main/jni/react/uimanager/UIConstantsProviderManager.cpp diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactInstance.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactInstance.java index 9e195cc49ae044..70ec7e1f2f8e02 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactInstance.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactInstance.java @@ -55,7 +55,7 @@ import com.facebook.react.uimanager.ComponentNameResolverBinding; import com.facebook.react.uimanager.DisplayMetricsHolder; import com.facebook.react.uimanager.IllegalViewOperationException; -import com.facebook.react.uimanager.UIConstantsProviderManager; +import com.facebook.react.uimanager.UIConstantsProviderBinding; import com.facebook.react.uimanager.UIManagerModule; import com.facebook.react.uimanager.UIManagerModuleConstantsHelper; import com.facebook.react.uimanager.ViewManager; @@ -97,8 +97,6 @@ final class ReactInstance { private JavaScriptContextHolder mJavaScriptContextHolder; - @DoNotStrip @Nullable private UIConstantsProviderManager mUIConstantsProviderManager; - static { loadLibraryIfNeeded(); } @@ -231,46 +229,43 @@ final class ReactInstance { if (ReactFeatureFlags.useNativeViewConfigsInBridgelessMode) { Map customDirectEvents = new HashMap<>(); - mUIConstantsProviderManager = - new UIConstantsProviderManager( - // Use unbuffered RuntimeExecutor to install binding - unbufferedRuntimeExecutor, - // Here we are construncting the return value for UIManager.getConstants call. - // The old architectre relied on the constatnts struct to contain: - // 1. Eagerly loaded view configs for all native components. - // 2. genericBubblingEventTypes. - // 3. genericDirectEventTypes. - // We want to match this beahavior. - () -> { - return (NativeMap) - Arguments.makeNativeMap( - UIManagerModuleConstantsHelper.getDefaultExportableEventTypes()); - }, - (String viewManagerName) -> { - ViewManager viewManager = mViewManagerResolver.getViewManager(viewManagerName); - if (viewManager == null) { - return null; - } - return (NativeMap) - UIManagerModule.getConstantsForViewManager(viewManager, customDirectEvents); - }, - () -> { - List viewManagers = - new ArrayList( - mViewManagerResolver.getEagerViewManagerMap().values()); - - Map constants = - UIManagerModule.createConstants(viewManagers, null, customDirectEvents); - - Collection lazyViewManagers = - mViewManagerResolver.getLazyViewManagerNames(); - if (lazyViewManagers.size() > 0) { - constants.put("ViewManagerNames", new ArrayList<>(lazyViewManagers)); - constants.put("LazyViewManagersEnabled", true); - } - - return Arguments.makeNativeMap(constants); - }); + UIConstantsProviderBinding.install( + // Use unbuffered RuntimeExecutor to install binding + unbufferedRuntimeExecutor, + // Here we are construncting the return value for UIManager.getConstants call. + // The old architectre relied on the constatnts struct to contain: + // 1. Eagerly loaded view configs for all native components. + // 2. genericBubblingEventTypes. + // 3. genericDirectEventTypes. + // We want to match this beahavior. + () -> { + return (NativeMap) + Arguments.makeNativeMap( + UIManagerModuleConstantsHelper.getDefaultExportableEventTypes()); + }, + (String viewManagerName) -> { + ViewManager viewManager = mViewManagerResolver.getViewManager(viewManagerName); + if (viewManager == null) { + return null; + } + return (NativeMap) + UIManagerModule.getConstantsForViewManager(viewManager, customDirectEvents); + }, + () -> { + List viewManagers = + new ArrayList(mViewManagerResolver.getEagerViewManagerMap().values()); + + Map constants = + UIManagerModule.createConstants(viewManagers, null, customDirectEvents); + + Collection lazyViewManagers = mViewManagerResolver.getLazyViewManagerNames(); + if (lazyViewManagers.size() > 0) { + constants.put("ViewManagerNames", new ArrayList<>(lazyViewManagers)); + constants.put("LazyViewManagersEnabled", true); + } + + return Arguments.makeNativeMap(constants); + }); } EventBeatManager eventBeatManager = new EventBeatManager(); @@ -430,7 +425,6 @@ public Collection getNativeModules() { mFabricUIManager.invalidate(); mJavaTimerManager.onInstanceDestroy(); mHybridData.resetNative(); - mUIConstantsProviderManager = null; mJavaScriptContextHolder.clear(); } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIConstantsProviderManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIConstantsProviderBinding.java similarity index 61% rename from packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIConstantsProviderManager.java rename to packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIConstantsProviderBinding.java index 5fdf6e535e2012..312b414e339be9 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIConstantsProviderManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIConstantsProviderBinding.java @@ -8,8 +8,6 @@ package com.facebook.react.uimanager; import com.facebook.infer.annotation.Nullsafe; -import com.facebook.jni.HybridData; -import com.facebook.proguard.annotations.DoNotStrip; import com.facebook.proguard.annotations.DoNotStripAny; import com.facebook.react.bridge.NativeMap; import com.facebook.react.bridge.RuntimeExecutor; @@ -18,42 +16,18 @@ @Nullsafe(Nullsafe.Mode.LOCAL) @DoNotStripAny -public class UIConstantsProviderManager { +public class UIConstantsProviderBinding { static { - staticInit(); - } - - @DoNotStrip - @SuppressWarnings("unused") - private final HybridData mHybridData; - - public UIConstantsProviderManager( - RuntimeExecutor runtimeExecutor, - DefaultEventTypesProvider defaultEventTypesProvider, - ConstantsForViewManagerProvider viewManagerConstantsProvider, - ConstantsProvider constantsProvider) { - mHybridData = - initHybrid( - runtimeExecutor, - defaultEventTypesProvider, - viewManagerConstantsProvider, - constantsProvider); - installJSIBindings(); + SoLoader.loadLibrary("uimanagerjni"); } - private native HybridData initHybrid( + public static native void install( RuntimeExecutor runtimeExecutor, DefaultEventTypesProvider defaultEventTypesProvider, ConstantsForViewManagerProvider viewManagerConstantsProvider, ConstantsProvider constantsProvider); - private native void installJSIBindings(); - - private static void staticInit() { - SoLoader.loadLibrary("uimanagerjni"); - } - @DoNotStripAny public static interface DefaultEventTypesProvider { /* Returns UIManager's constants. */ diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/uimanager/OnLoad.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/uimanager/OnLoad.cpp index 620e8cff630311..d3aa6726dcbfc4 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/uimanager/OnLoad.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/uimanager/OnLoad.cpp @@ -8,11 +8,11 @@ #include #include "ComponentNameResolverBinding.h" -#include "UIConstantsProviderManager.h" +#include "UIConstantsProviderBinding.h" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) { return facebook::jni::initialize(vm, [] { facebook::react::ComponentNameResolverBinding::registerNatives(); - facebook::react::UIConstantsProviderManager::registerNatives(); + facebook::react::UIConstantsProviderBinding::registerNatives(); }); } diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/uimanager/UIConstantsProviderBinding.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/uimanager/UIConstantsProviderBinding.cpp new file mode 100644 index 00000000000000..4d14ee722658f7 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/jni/react/uimanager/UIConstantsProviderBinding.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include "UIConstantsProviderBinding.h" + +#include + +namespace facebook::react { + +using namespace facebook::jni; + +void UIConstantsProviderBinding::registerNatives() { + javaClassStatic()->registerNatives({ + makeNativeMethod("install", UIConstantsProviderBinding::install), + }); +} + +void UIConstantsProviderBinding::install( + jni::alias_ref /* unused */, + jni::alias_ref runtimeExecutor, + jni::alias_ref + defaultExportableEventTypesProvider, + jni::alias_ref + constantsForViewManagerProvider, + jni::alias_ref constantsProvider) { + auto executor = runtimeExecutor->cthis()->get(); + executor([defaultExportableEventTypesProvider = + make_global(defaultExportableEventTypesProvider), + constantsForViewManagerProvider = + make_global(constantsForViewManagerProvider), + constantsProvider = + make_global(constantsProvider)](jsi::Runtime& runtime) { + LegacyUIManagerConstantsProviderBinding::install( + runtime, + "getDefaultEventTypes", + [provider = std::move(defaultExportableEventTypesProvider)]( + jsi::Runtime& runtime) { + return jsi::valueFromDynamic( + runtime, provider->getDefaultEventTypes()); + }); + + LegacyUIManagerConstantsProviderBinding::install( + runtime, + "getConstantsForViewManager", + [provider = std::move(constantsForViewManagerProvider)]( + jsi::Runtime& runtime, const std::string& viewManagerName) { + return jsi::valueFromDynamic( + runtime, provider->getConstantsForViewManager(viewManagerName)); + }); + + LegacyUIManagerConstantsProviderBinding::install( + runtime, + "getConstants", + [provider = std::move(constantsProvider)](jsi::Runtime& runtime) { + return jsi::valueFromDynamic(runtime, provider->getConstants()); + }); + }); +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/uimanager/UIConstantsProviderManager.h b/packages/react-native/ReactAndroid/src/main/jni/react/uimanager/UIConstantsProviderBinding.h similarity index 56% rename from packages/react-native/ReactAndroid/src/main/jni/react/uimanager/UIConstantsProviderManager.h rename to packages/react-native/ReactAndroid/src/main/jni/react/uimanager/UIConstantsProviderBinding.h index eadc1fa987f8d0..a3f80b032461cd 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/uimanager/UIConstantsProviderManager.h +++ b/packages/react-native/ReactAndroid/src/main/jni/react/uimanager/UIConstantsProviderBinding.h @@ -22,13 +22,12 @@ class DefaultEventTypesProvider static constexpr const char* kJavaDescriptor = "Lcom/facebook/react/uimanager/UIConstantsProviderManager$DefaultEventTypesProvider;"; - jsi::Value getDefaultEventTypes(jsi::Runtime& runtime) { + folly::dynamic getDefaultEventTypes() const { static auto method = javaClassStatic() ->getMethod()>( "getDefaultEventTypes"); - auto result = method(self()); - return jsi::valueFromDynamic(runtime, result->cthis()->consume()); + return method(self())->cthis()->consume(); } }; @@ -38,18 +37,17 @@ class ConstantsForViewManagerProvider static constexpr const char* kJavaDescriptor = "Lcom/facebook/react/uimanager/UIConstantsProviderManager$ConstantsForViewManagerProvider;"; - jsi::Value getConstantsForViewManager( - jsi::Runtime& runtime, - std::string viewManagerName) { + folly::dynamic getConstantsForViewManager( + const std::string& viewManagerName) const { static auto method = javaClassStatic() - ->getMethod(std::string)>( - "getConstantsForViewManager"); + ->getMethod( + const std::string&)>("getConstantsForViewManager"); auto result = method(self(), viewManagerName); if (result == nullptr) { - return jsi::Value::null(); + return nullptr; } - return jsi::valueFromDynamic(runtime, result->cthis()->consume()); + return result->cthis()->consume(); } }; @@ -58,24 +56,23 @@ class ConstantsProvider : public jni::JavaClass { static constexpr const char* kJavaDescriptor = "Lcom/facebook/react/uimanager/UIConstantsProviderManager$ConstantsProvider;"; - jsi::Value getConstants(jsi::Runtime& runtime) { + folly::dynamic getConstants() const { static auto method = javaClassStatic() ->getMethod()>( "getConstants"); - auto result = method(self()); - return jsi::valueFromDynamic(runtime, result->cthis()->consume()); + return method(self())->cthis()->consume(); } }; -class UIConstantsProviderManager - : public facebook::jni::HybridClass { +class UIConstantsProviderBinding + : public facebook::jni::JavaClass { public: static auto constexpr kJavaDescriptor = - "Lcom/facebook/react/uimanager/UIConstantsProviderManager;"; + "Lcom/facebook/react/uimanager/UIConstantsProviderBinding;"; - static facebook::jni::local_ref initHybrid( - facebook::jni::alias_ref /* unused */, + static void install( + facebook::jni::alias_ref /* unused */, facebook::jni::alias_ref runtimeExecutor, facebook::jni::alias_ref defaultExportableEventTypesProvider, @@ -85,27 +82,6 @@ class UIConstantsProviderManager constantsProvider); static void registerNatives(); - - private: - friend HybridBase; - RuntimeExecutor runtimeExecutor_; - - facebook::jni::global_ref - defaultExportableEventTypesProvider_; - facebook::jni::global_ref - constantsForViewManagerProvider_; - facebook::jni::global_ref constantsProvider_; - - void installJSIBindings(); - - explicit UIConstantsProviderManager( - RuntimeExecutor runtimeExecutor, - facebook::jni::alias_ref - defaultExportableEventTypesProvider, - facebook::jni::alias_ref - constantsForViewManagerProvider, - facebook::jni::alias_ref - constantsProvider); }; } // namespace facebook::react diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/uimanager/UIConstantsProviderManager.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/uimanager/UIConstantsProviderManager.cpp deleted file mode 100644 index 65b783669debb8..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/jni/react/uimanager/UIConstantsProviderManager.cpp +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include - -#include -#include -#include -#include - -#include -#include "UIConstantsProviderManager.h" - -namespace facebook::react { - -using namespace facebook::jni; - -UIConstantsProviderManager::UIConstantsProviderManager( - RuntimeExecutor runtimeExecutor, - jni::alias_ref - defaultExportableEventTypesProvider, - jni::alias_ref - constantsForViewManagerProvider, - jni::alias_ref constantsProvider) - : runtimeExecutor_(runtimeExecutor), - defaultExportableEventTypesProvider_( - jni::make_global(defaultExportableEventTypesProvider)), - constantsForViewManagerProvider_( - jni::make_global(constantsForViewManagerProvider)), - constantsProvider_(jni::make_global(constantsProvider)) {} - -jni::local_ref -UIConstantsProviderManager::initHybrid( - jni::alias_ref /* unused */, - jni::alias_ref runtimeExecutor, - jni::alias_ref - defaultExportableEventTypesProvider, - jni::alias_ref - constantsForViewManagerProvider, - jni::alias_ref constantsProvider) { - return makeCxxInstance( - runtimeExecutor->cthis()->get(), - defaultExportableEventTypesProvider, - constantsForViewManagerProvider, - constantsProvider); -} - -void UIConstantsProviderManager::registerNatives() { - registerHybrid({ - makeNativeMethod("initHybrid", UIConstantsProviderManager::initHybrid), - makeNativeMethod( - "installJSIBindings", UIConstantsProviderManager::installJSIBindings), - }); -} - -void UIConstantsProviderManager::installJSIBindings() { - runtimeExecutor_([thizz = this](jsi::Runtime& runtime) { - auto jsiDefaultEventTypesProvider = [thizz, &runtime]() -> jsi::Value { - return thizz->defaultExportableEventTypesProvider_->getDefaultEventTypes( - runtime); - }; - - auto jsiConstantsForViewManagerProvider = - [thizz, &runtime](std::string viewManagerName) -> jsi::Value { - return thizz->constantsForViewManagerProvider_ - ->getConstantsForViewManager(runtime, viewManagerName); - }; - - auto jsiConstantsProvider = [thizz, &runtime]() -> jsi::Value { - return thizz->constantsProvider_->getConstants(runtime); - }; - - LegacyUIManagerConstantsProviderBinding::install( - runtime, - "getDefaultEventTypes", - std::move(jsiDefaultEventTypesProvider)); - - LegacyUIManagerConstantsProviderBinding::install( - runtime, - "getConstantsForViewManager", - std::move(jsiConstantsForViewManagerProvider)); - - LegacyUIManagerConstantsProviderBinding::install( - runtime, "getConstants", std::move(jsiConstantsProvider)); - }); -} - -} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/runtime/nativeviewconfig/LegacyUIManagerConstantsProviderBinding.cpp b/packages/react-native/ReactCommon/react/runtime/nativeviewconfig/LegacyUIManagerConstantsProviderBinding.cpp index ae10b3f32ff8e9..249ac80357850c 100644 --- a/packages/react-native/ReactCommon/react/runtime/nativeviewconfig/LegacyUIManagerConstantsProviderBinding.cpp +++ b/packages/react-native/ReactCommon/react/runtime/nativeviewconfig/LegacyUIManagerConstantsProviderBinding.cpp @@ -12,7 +12,7 @@ namespace facebook::react::LegacyUIManagerConstantsProviderBinding { void install( jsi::Runtime& runtime, const std::string& name, - std::function&& provider) { + std::function&& provider) { auto methodName = "RN$LegacyInterop_UIManager_" + name; auto hostFunction = [provider = std::move(provider)]( jsi::Runtime& runtime, @@ -22,7 +22,7 @@ void install( if (count != 0) { throw new jsi::JSError(runtime, "0 arguments expected."); } - return provider(); + return provider(runtime); }; auto jsiFunction = jsi::Function::createFromHostFunction( @@ -34,7 +34,7 @@ void install( void install( jsi::Runtime& runtime, const std::string& name, - std::function&& provider) { + std::function&& provider) { auto methodName = "RN$LegacyInterop_UIManager_" + name; auto hostFunction = [provider = std::move(provider)]( jsi::Runtime& runtime, @@ -49,7 +49,7 @@ void install( throw new jsi::JSError(runtime, "First argument must be string."); } - return provider(args[0].asString(runtime).utf8(runtime)); + return provider(runtime, args[0].asString(runtime).utf8(runtime)); }; auto jsiFunction = jsi::Function::createFromHostFunction( diff --git a/packages/react-native/ReactCommon/react/runtime/nativeviewconfig/LegacyUIManagerConstantsProviderBinding.h b/packages/react-native/ReactCommon/react/runtime/nativeviewconfig/LegacyUIManagerConstantsProviderBinding.h index eadf6f296aec4c..d7758a45bf22c5 100644 --- a/packages/react-native/ReactCommon/react/runtime/nativeviewconfig/LegacyUIManagerConstantsProviderBinding.h +++ b/packages/react-native/ReactCommon/react/runtime/nativeviewconfig/LegacyUIManagerConstantsProviderBinding.h @@ -11,8 +11,6 @@ namespace facebook::react::LegacyUIManagerConstantsProviderBinding { -using ProviderType = std::function; - /* * Installs RN$LegacyInterop_UIManager_getConstants binding into JavaScript * runtime. It is supposed to be used as a substitute to UIManager.getConstants @@ -21,9 +19,9 @@ using ProviderType = std::function; void install( jsi::Runtime& runtime, const std::string& name, - std::function&& provider); + std::function&& provider); void install( jsi::Runtime& runtime, const std::string& name, - std::function&& provider); + std::function&& provider); } // namespace facebook::react::LegacyUIManagerConstantsProviderBinding diff --git a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTLegacyUIManagerConstantsProvider.mm b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTLegacyUIManagerConstantsProvider.mm index 5d8bc5ce83f9af..77933fe9f8891b 100644 --- a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTLegacyUIManagerConstantsProvider.mm +++ b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTLegacyUIManagerConstantsProvider.mm @@ -38,7 +38,6 @@ void installLegacyUIManagerConstantsProviderBinding(jsi::Runtime &runtime) { - auto constantsProvider = [&runtime]() -> jsi::Value { return getConstants(runtime); }; - LegacyUIManagerConstantsProviderBinding::install(runtime, "getConstants", std::move(constantsProvider)); + LegacyUIManagerConstantsProviderBinding::install(runtime, "getConstants", getConstants); } } // namespace facebook::react