diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/CppComponentRegistry.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/CppComponentRegistry.java new file mode 100644 index 00000000000..0a14322c649 --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/CppComponentRegistry.java @@ -0,0 +1,28 @@ +/* + * 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. + */ + +package com.facebook.react.fabric; + +import androidx.annotation.NonNull; +import com.facebook.jni.HybridData; +import com.facebook.proguard.annotations.DoNotStrip; + +@DoNotStrip +public class CppComponentRegistry { + + static { + FabricSoLoader.staticInit(); + } + + @NonNull private final HybridData mHybridData; + + private static native HybridData initHybrid(); + + private CppComponentRegistry(HybridData hybridData) { + mHybridData = hybridData; + } +} diff --git a/ReactAndroid/src/main/jni/react/fabric/ComponentRegistryResolver.cpp b/ReactAndroid/src/main/jni/react/fabric/ComponentRegistryResolver.cpp new file mode 100644 index 00000000000..5560590ac21 --- /dev/null +++ b/ReactAndroid/src/main/jni/react/fabric/ComponentRegistryResolver.cpp @@ -0,0 +1,51 @@ +/* + * 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 "ComponentRegistryResolver.h" + +#include + +namespace facebook { +namespace react { + +void ComponentRegistryResolver::addComponentManager( + std::string name, + bool isRootComponent, + std::function( + const std::string &name)> f) { + ComponentResolverData data = {isRootComponent, std::move(f)}; + resolverMap_.insert({name, data}); +} + +bool ComponentRegistryResolver::containsComponentManager( + std::string componentName) const { + return resolverMap_.contains(componentName); +} + +std::shared_ptr +ComponentRegistryResolver::getComponentManager( + std::string componentName) const { + auto iterator = resolverMap_.find(componentName); + if (iterator != resolverMap_.end()) { + return iterator->second.getComponentManagerFunction(componentName); + } + + return nullptr; +} + +bool ComponentRegistryResolver::isRootComponent( + std::string componentName) const { + auto iterator = resolverMap_.find(componentName); + if (iterator != resolverMap_.end()) { + return iterator->second.isRootComponent; + } + + return false; +} + +} // namespace react +} // namespace facebook diff --git a/ReactAndroid/src/main/jni/react/fabric/ComponentRegistryResolver.h b/ReactAndroid/src/main/jni/react/fabric/ComponentRegistryResolver.h new file mode 100644 index 00000000000..67eb046f8ff --- /dev/null +++ b/ReactAndroid/src/main/jni/react/fabric/ComponentRegistryResolver.h @@ -0,0 +1,47 @@ +/* + * 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. + */ + +#pragma once + +#include +#include + +namespace facebook { +namespace react { + +struct ComponentResolverData { + bool isRootComponent; + std::function( + const std::string &name)> + getComponentManagerFunction; +}; + +using ComponentResolverMap = butter::map; + +class ComponentRegistryResolver { + public: + ComponentRegistryResolver(){}; + + void addComponentManager( + std::string name, + bool isRootComponent, + std::function( + const std::string &name)> f); + + bool containsComponentManager(std::string componentName) const; + + bool isRootComponent(std::string componentName) const; + + std::shared_ptr getComponentManager( + std::string name) const; + + private: + butter::map resolverMap_{}; +}; + +} // namespace react +} // namespace facebook diff --git a/ReactAndroid/src/main/jni/react/fabric/CppComponentRegistry.cpp b/ReactAndroid/src/main/jni/react/fabric/CppComponentRegistry.cpp new file mode 100644 index 00000000000..b1f845a09d6 --- /dev/null +++ b/ReactAndroid/src/main/jni/react/fabric/CppComponentRegistry.cpp @@ -0,0 +1,73 @@ +/* + * 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 "CppComponentRegistry.h" +#include +#include +#include +#include + +using namespace facebook::jsi; + +namespace facebook { +namespace react { + +jni::local_ref +CppComponentRegistry::initHybrid(jni::alias_ref) { + return makeCxxInstance(); +} + +void CppComponentRegistry::registerNatives() { + registerHybrid({ + makeNativeMethod("initHybrid", CppComponentRegistry::initHybrid), + }); +} + +void CppComponentRegistry::addComponentManager( + std::string name, + bool isRootComponent, + std::function( + const std::string &name)> f) { + componentManagerResolver_.addComponentManager(name, isRootComponent, f); +} + +bool CppComponentRegistry::containsComponentManager(std::string name) const { + return componentManagerResolver_.containsComponentManager(name); +} + +std::shared_ptr +CppComponentRegistry::getComponentManager(const std::string &name) const { + return componentManagerResolver_.getComponentManager(name); +} + +std::shared_ptr +CppComponentRegistry::getComponentInstance(Tag tag) const { + return components_[tag]; +} + +bool CppComponentRegistry::isRootComponent(std::string name) const { + return componentManagerResolver_.isRootComponent(name); +} + +std::shared_ptr +CppComponentRegistry::createComponentInstance( + const std::string &componentName, + Tag tag, + Props::Shared initialProps) const { + // TODO: cache component managers + auto componentManager = getComponentManager(componentName); + auto component = componentManager->createComponent(tag, initialProps); + components_[tag] = component; + return component; +} + +void CppComponentRegistry::deleteComponentInstance(Tag tag) const { + components_.erase(tag); +} + +} // namespace react +} // namespace facebook diff --git a/ReactAndroid/src/main/jni/react/fabric/CppComponentRegistry.h b/ReactAndroid/src/main/jni/react/fabric/CppComponentRegistry.h new file mode 100644 index 00000000000..f417b386e36 --- /dev/null +++ b/ReactAndroid/src/main/jni/react/fabric/CppComponentRegistry.h @@ -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. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace facebook { +namespace react { + +class Instance; + +class CppComponentRegistry : public jni::HybridClass { + public: + constexpr static const char *const kJavaDescriptor = + "Lcom/facebook/react/fabric/CppComponentRegistry;"; + + static void registerNatives(); + + void addComponentManager( + std::string name, + bool isRootComponent, + std::function( + const std::string &name)> f); + + bool isRootComponent(std::string name) const; + + bool containsComponentManager(std::string name) const; + + std::shared_ptr getComponentManager( + const std::string &name) const; + + std::shared_ptr getComponentInstance( + Tag tag) const; + + std::shared_ptr createComponentInstance( + const std::string &componentName, + Tag tag, + Props::Shared initialProps) const; + + void deleteComponentInstance(Tag tag) const; + + private: + static jni::local_ref initHybrid(jni::alias_ref); + ComponentRegistryResolver componentManagerResolver_{}; + mutable butter::map> + components_{}; +}; + +} // namespace react +} // namespace facebook