Introduce CxxReactPackage

Summary:
CxxReactPackage is supposed to be the way apps register C++-only turbo modules with React Native.

Applications are meant to subclass this jni::HybridObject.

Since this is a jni::HybridObject, applications are meant to create this CxxReactPackage in java, and initialize it with java dependencies.

React Native will reach into its c++ part, and use it create C++-only turbo modules.

NOTE: This is a **temporary** abstraction meant to unblock the stable API effort of removing the turbomodulemanagerdelegate builder from ReactHostDelegate:

https://www.internalfb.com/code/fbsource/[e7efced3018f6178b7187a2358f3b76d40e2b43c]/xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostDelegate.kt?lines=50-51

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D51166523

fbshipit-source-id: 51a22411239fbba32f3a70cc363e59947c2782dc
This commit is contained in:
Ramanpreet Nara
2023-11-19 18:46:12 -08:00
committed by Facebook GitHub Bot
parent 1394cf7da7
commit a2558d15ab
8 changed files with 122 additions and 0 deletions
@@ -102,6 +102,7 @@ internal object NdkConfiguratorUtils {
"**/libreact_debug.so",
"**/libreact_nativemodule_core.so",
"**/libreact_newarchdefaults.so",
"**/libreact_cxxreactpackage.so",
"**/libreact_render_componentregistry.so",
"**/libreact_render_core.so",
"**/libreact_render_debug.so",
@@ -105,6 +105,10 @@ final def preparePrefab = tasks.register("preparePrefab", PreparePrefabHeadersTa
"react_newarchdefaults",
new Pair("src/main/jni/react/newarchdefaults", "")
),
new PrefabPreprocessingEntry(
"react_cxxreactpackage",
new Pair("src/main/jni/react/runtime/cxxreactpackage", "")
),
new PrefabPreprocessingEntry(
"react_render_animations",
new Pair("../ReactCommon/react/renderer/animations/", "react/renderer/animations/")
@@ -530,6 +534,7 @@ android {
"react_utils",
"react_render_componentregistry",
"react_newarchdefaults",
"react_cxxreactpackage",
"react_render_animations",
"react_render_core",
"react_render_graphics",
@@ -650,6 +655,9 @@ android {
react_newarchdefaults {
headers(new File(prefabHeadersDir, "react_newarchdefaults").absolutePath)
}
react_cxxreactpackage {
headers(new File(prefabHeadersDir, "react_cxxreactpackage").absolutePath)
}
react_render_animations {
headers(new File(prefabHeadersDir, "react_render_animations").absolutePath)
}
@@ -71,6 +71,7 @@ add_library(react_debug ALIAS ReactAndroid::react_debug)
add_library(react_utils ALIAS ReactAndroid::react_utils)
add_library(react_render_componentregistry ALIAS ReactAndroid::react_render_componentregistry)
add_library(react_newarchdefaults ALIAS ReactAndroid::react_newarchdefaults)
add_library(react_cxxreactpackage ALIAS ReactAndroid::react_cxxreactpackage)
add_library(react_render_core ALIAS ReactAndroid::react_render_core)
add_library(react_render_graphics ALIAS ReactAndroid::react_render_graphics)
add_library(rrc_view ALIAS ReactAndroid::rrc_view)
@@ -99,6 +100,7 @@ target_link_libraries(${CMAKE_PROJECT_NAME}
react_utils # prefab ready
react_nativemodule_core # prefab ready
react_newarchdefaults # prefab ready
react_cxxreactpackage # prefab ready
react_render_componentregistry # prefab ready
react_render_core # prefab ready
react_render_debug # prefab ready
@@ -0,0 +1,36 @@
/*
* 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.
*/
// TODO(170197717): Consider moving this to runtime.cxx, or just runtime
package com.facebook.react.runtime.cxxreactpackage
import com.facebook.jni.HybridData
import com.facebook.proguard.annotations.DoNotStrip
import com.facebook.react.common.annotations.UnstableReactNativeAPI
import com.facebook.soloader.SoLoader
/** CxxReactPackage is used to register C++ Turbo Modules with React Native. */
@UnstableReactNativeAPI()
abstract class CxxReactPackage {
@DoNotStrip @SuppressWarnings("unused") private val hybridData: HybridData
protected abstract fun initHybrid(): HybridData
protected constructor() {
maybeLoadOtherSoLibraries()
hybridData = initHybrid()
}
@Synchronized protected open fun maybeLoadOtherSoLibraries(): Unit {}
companion object {
init {
SoLoader.loadLibrary("react_cxxreactpackage")
}
}
}
@@ -121,6 +121,7 @@ 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/)
add_react_android_subdir(src/main/jni/react/runtime/cxxreactpackage)
add_react_android_subdir(src/main/jni/react/runtime/jni)
add_react_android_subdir(src/main/jni/react/runtime/hermes/jni)
add_react_android_subdir(src/main/jni/react/runtime/jsc/jni)
@@ -0,0 +1,29 @@
# 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
-Wno-unused-lambda-capture
-std=c++20)
#########################
### cxxreactpackage ###
#########################
add_library(react_cxxreactpackage SHARED ReactCommon/OnLoad.cpp)
target_include_directories(react_cxxreactpackage
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(react_cxxreactpackage
fb
fbjni)
@@ -0,0 +1,31 @@
/*
* 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 <fbjni/fbjni.h>
#include <memory>
namespace facebook::react {
class TurboModule;
class CallInvoker;
class CxxReactPackage : public jni::HybridClass<CxxReactPackage> {
public:
static auto constexpr kJavaDescriptor =
"Lcom/facebook/react/runtime/cxxreactpackage/CxxReactPackage;";
virtual std::shared_ptr<TurboModule> getModule(
const std::string& name,
const std::shared_ptr<CallInvoker>& jsInvoker) = 0;
private:
friend HybridBase;
};
} // namespace facebook::react
@@ -0,0 +1,14 @@
/*
* 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 "CxxReactPackage.h"
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
return facebook::jni::initialize(vm, [] {});
}