Reduce the amount of C++ code in user space for RN-Tester

Summary:
This diff reduces the amount of C++ code in user space by:
- Moving all the C++ implementation of the .defaults package inside a `react_newarchdefaults` shared library
- Exposing only the entrypoint logic inside the RN-Tester's `OnLoad.cpp` file.

Changelog:
[Android] [Changed] - Reduce the amount of C++ code in user space for New Architecture

Reviewed By: cipolleschi

Differential Revision: D39381820

fbshipit-source-id: 9c4b5596b67b5a7ee58824319c80e325348ed06c
This commit is contained in:
Nicola Corti
2022-09-12 12:48:43 -07:00
committed by Facebook GitHub Bot
parent 90e7f510dc
commit e89bd4a375
19 changed files with 245 additions and 132 deletions
@@ -15,9 +15,8 @@ import com.facebook.react.fabric.ComponentFactory
* A utility class that provides users a ComponentRegistry they can customize with a C++
* implementation of its native methods.
*
* Please note that you need to provide a native implementation for the method initHybrid for this
* class, making sure the Java Descriptor is:
* Lcom/facebook/react/defaults/DefaultComponentsRegistry;
* This class works together with the [DefaultNativeEntryPoint] and it's C++ implementation is
* hosted inside the React Native framwork
*/
@DoNotStrip
class DefaultComponentsRegistry
@@ -0,0 +1,39 @@
/*
* 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.defaults
import com.facebook.jni.HybridData
import com.facebook.proguard.annotations.DoNotStrip
import com.facebook.soloader.SoLoader
/**
* A utility class that serves as an entry point for users to register all the custom Fabric
* Components and Turbo Native Modules.
*
* This class needs to be invoked as `DefaultNativeEntryPoint.load("...")` by passing the name of
* the dynamic library to load.
*
* This class works together with the [DefaultNativeEntryPoint] and it's C++ implementation is
* hosted inside the React Native framework
*/
@DoNotStrip
class DefaultNativeEntryPoint @DoNotStrip private constructor() {
@DoNotStrip private val hybridData: HybridData = initHybrid()
@DoNotStrip private external fun initHybrid(): HybridData
companion object {
@JvmStatic
fun load(dynamicLibraryName: String) {
SoLoader.loadLibrary("react_newarchdefaults")
SoLoader.loadLibrary(dynamicLibraryName)
DefaultNativeEntryPoint()
}
}
}
@@ -17,9 +17,8 @@ import com.facebook.react.bridge.ReactApplicationContext
* A utility class that allows you to simplify the setup of a
* [ReactPackageTurboModuleManagerDelegate] for new apps in Open Source.
*
* Please note that you need to provide a native implementation for the method initHybrid for this
* class, making sure the Java Descriptor is:
* Lcom/facebook/react/defaults/DefaultTurboModuleManagerDelegate;
* This class works together with the [DefaultNativeEntryPoint] and it's C++ implementation is
* hosted inside the React Native framwork
*/
class DefaultTurboModuleManagerDelegate
private constructor(context: ReactApplicationContext, packages: List<ReactPackage>) :
+1
View File
@@ -101,6 +101,7 @@ add_react_android_subdir(src/main/jni/react/uimanager)
add_react_android_subdir(src/main/jni/react/mapbuffer)
add_react_android_subdir(src/main/jni/react/reactnativeblob)
add_react_android_subdir(src/main/jni/react/fabric)
add_react_android_subdir(src/main/jni/react/newarchdefaults)
add_react_android_subdir(src/main/jni/react/hermes/reactexecutor)
add_react_android_subdir(src/main/jni/react/hermes/instrumentation/)
@@ -0,0 +1,22 @@
# 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.
cmake_minimum_required(VERSION 3.13)
set(CMAKE_VERBOSE_MAKEFILE on)
add_compile_options(-fexceptions -frtti -std=c++17 -Wall -DLOG_TAG=\"ReactNative\")
file(GLOB react_newarchdefaults_SRC CONFIGURE_DEPENDS *.cpp)
add_library(react_newarchdefaults SHARED ${react_newarchdefaults_SRC})
target_include_directories(react_newarchdefaults PUBLIC .)
target_link_libraries(react_newarchdefaults
fbjni
fabricjni
react_nativemodule_core
react_codegen_rncore
jsi)
@@ -0,0 +1,70 @@
/*
* 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 "DefaultComponentsRegistry.h"
#include <CoreComponentsRegistry.h>
#include <fbjni/fbjni.h>
#include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h>
#include <react/renderer/components/rncore/ComponentDescriptors.h>
namespace facebook {
namespace react {
std::function<void(std::shared_ptr<ComponentDescriptorProviderRegistry const>)>
DefaultComponentsRegistry::registerComponentDescriptorsFromEntryPoint{};
DefaultComponentsRegistry::DefaultComponentsRegistry(ComponentFactory *delegate)
: delegate_(delegate) {}
std::shared_ptr<ComponentDescriptorProviderRegistry const>
DefaultComponentsRegistry::sharedProviderRegistry() {
auto providerRegistry = CoreComponentsRegistry::sharedProviderRegistry();
(DefaultComponentsRegistry::registerComponentDescriptorsFromEntryPoint)(
providerRegistry);
return providerRegistry;
}
jni::local_ref<DefaultComponentsRegistry::jhybriddata>
DefaultComponentsRegistry::initHybrid(
jni::alias_ref<jclass>,
ComponentFactory *delegate) {
auto instance = makeCxxInstance(delegate);
auto buildRegistryFunction =
[](EventDispatcher::Weak const &eventDispatcher,
ContextContainer::Shared const &contextContainer)
-> ComponentDescriptorRegistry::Shared {
auto registry = DefaultComponentsRegistry::sharedProviderRegistry()
->createComponentDescriptorRegistry(
{eventDispatcher, contextContainer});
auto mutableRegistry =
std::const_pointer_cast<ComponentDescriptorRegistry>(registry);
mutableRegistry->setFallbackComponentDescriptor(
std::make_shared<UnimplementedNativeViewComponentDescriptor>(
ComponentDescriptorParameters{
eventDispatcher, contextContainer, nullptr}));
return registry;
};
delegate->buildRegistryFunction = buildRegistryFunction;
return instance;
}
void DefaultComponentsRegistry::registerNatives() {
registerHybrid({
makeNativeMethod("initHybrid", DefaultComponentsRegistry::initHybrid),
});
}
} // namespace react
} // namespace facebook
@@ -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 <ComponentFactory.h>
#include <fbjni/fbjni.h>
#include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h>
#include <react/renderer/componentregistry/ComponentDescriptorRegistry.h>
#include <react/renderer/core/ConcreteComponentDescriptor.h>
namespace facebook {
namespace react {
class DefaultComponentsRegistry
: public facebook::jni::HybridClass<DefaultComponentsRegistry> {
public:
constexpr static auto kJavaDescriptor =
"Lcom/facebook/react/defaults/DefaultComponentsRegistry;";
static void registerNatives();
static std::function<void(
std::shared_ptr<ComponentDescriptorProviderRegistry const>)>
registerComponentDescriptorsFromEntryPoint;
DefaultComponentsRegistry(ComponentFactory *delegate);
private:
friend HybridBase;
static std::shared_ptr<ComponentDescriptorProviderRegistry const>
sharedProviderRegistry();
const ComponentFactory *delegate_;
static jni::local_ref<jhybriddata> initHybrid(
jni::alias_ref<jclass>,
ComponentFactory *delegate);
};
} // namespace react
} // namespace facebook
@@ -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 "DefaultTurboModuleManagerDelegate.h"
#include <rncore.h>
namespace facebook {
namespace react {
std::function<std::shared_ptr<TurboModule>(
const std::string &,
const JavaTurboModule::InitParams &)>
DefaultTurboModuleManagerDelegate::moduleProvidersFromEntryPoint{nullptr};
jni::local_ref<DefaultTurboModuleManagerDelegate::jhybriddata>
DefaultTurboModuleManagerDelegate::initHybrid(jni::alias_ref<jhybridobject>) {
return makeCxxInstance();
}
void DefaultTurboModuleManagerDelegate::registerNatives() {
registerHybrid({
makeNativeMethod(
"initHybrid", DefaultTurboModuleManagerDelegate::initHybrid),
});
}
std::shared_ptr<TurboModule> DefaultTurboModuleManagerDelegate::getTurboModule(
const std::string &name,
const std::shared_ptr<CallInvoker> &jsInvoker) {
// Not implemented yet: provide pure-C++ NativeModules here.
return nullptr;
}
std::shared_ptr<TurboModule> DefaultTurboModuleManagerDelegate::getTurboModule(
const std::string &name,
const JavaTurboModule::InitParams &params) {
auto resolvedModule = (DefaultTurboModuleManagerDelegate::
moduleProvidersFromEntryPoint)(name, params);
if (resolvedModule != nullptr) {
return resolvedModule;
}
return rncore_ModuleProvider(name, params);
}
} // namespace react
} // namespace facebook
@@ -0,0 +1,48 @@
/*
* 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 <memory>
#include <string>
#include <ReactCommon/JavaTurboModule.h>
#include <ReactCommon/TurboModule.h>
#include <ReactCommon/TurboModuleManagerDelegate.h>
#include <fbjni/fbjni.h>
namespace facebook {
namespace react {
class DefaultTurboModuleManagerDelegate : public jni::HybridClass<
DefaultTurboModuleManagerDelegate,
TurboModuleManagerDelegate> {
public:
static constexpr auto kJavaDescriptor =
"Lcom/facebook/react/defaults/DefaultTurboModuleManagerDelegate;";
static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jhybridobject>);
static void registerNatives();
static std::function<std::shared_ptr<TurboModule>(
const std::string &,
const JavaTurboModule::InitParams &)>
moduleProvidersFromEntryPoint;
std::shared_ptr<TurboModule> getTurboModule(
const std::string &name,
const std::shared_ptr<CallInvoker> &jsInvoker) override;
std::shared_ptr<TurboModule> getTurboModule(
const std::string &name,
const JavaTurboModule::InitParams &params) override;
private:
friend HybridBase;
using HybridBase::HybridBase;
};
} // namespace react
} // namespace facebook
@@ -0,0 +1,18 @@
/*
* 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 <fbjni/fbjni.h>
#include "DefaultComponentsRegistry.h"
#include "DefaultTurboModuleManagerDelegate.h"
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
return facebook::jni::initialize(vm, [] {
facebook::react::DefaultTurboModuleManagerDelegate::registerNatives();
facebook::react::DefaultComponentsRegistry::registerNatives();
});
}