Skip to content

Commit

Permalink
Provide easy registration of C++ TurboModules in rn-tester Android (#…
Browse files Browse the repository at this point in the history
…35225)

Summary:
Pull Request resolved: #35225

Changelog:
[Android] [Changed] - Provide easy registration of C++ TurboModules in rn-tester Android

Reviewed By: javache

Differential Revision: D41069277

fbshipit-source-id: 450de9302df2916acf324d4c316996b185b2833f
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Nov 8, 2022
1 parent dac6806 commit 9cb0261
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
15 changes: 12 additions & 3 deletions ReactAndroid/cmake-utils/default-app-setup/OnLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ void registerComponents(
rncli_registerProviders(registry);
}

std::shared_ptr<TurboModule> provideModules(
std::shared_ptr<TurboModule> cxxModuleProvider(
const std::string &name,
const std::shared_ptr<CallInvoker> &jsInvoker) {
// Not implemented yet: provide pure-C++ NativeModules here.
return nullptr;
}

std::shared_ptr<TurboModule> javaModuleProvider(
const std::string &name,
const JavaTurboModule::InitParams &params) {
// Here you can provide your own module provider for TurboModules coming from
Expand All @@ -70,8 +77,10 @@ std::shared_ptr<TurboModule> provideModules(

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
return facebook::jni::initialize(vm, [] {
facebook::react::DefaultTurboModuleManagerDelegate::
moduleProvidersFromEntryPoint = &facebook::react::provideModules;
facebook::react::DefaultTurboModuleManagerDelegate::cxxModuleProvider =
&facebook::react::cxxModuleProvider;
facebook::react::DefaultTurboModuleManagerDelegate::javaModuleProvider =
&facebook::react::javaModuleProvider;
facebook::react::DefaultComponentsRegistry::
registerComponentDescriptorsFromEntryPoint =
&facebook::react::registerComponents;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
namespace facebook {
namespace react {

std::function<std::shared_ptr<TurboModule>(
const std::string &,
const std::shared_ptr<CallInvoker> &)>
DefaultTurboModuleManagerDelegate::cxxModuleProvider{nullptr};

std::function<std::shared_ptr<TurboModule>(
const std::string &,
const JavaTurboModule::InitParams &)>
DefaultTurboModuleManagerDelegate::moduleProvidersFromEntryPoint{nullptr};
DefaultTurboModuleManagerDelegate::javaModuleProvider{nullptr};

jni::local_ref<DefaultTurboModuleManagerDelegate::jhybriddata>
DefaultTurboModuleManagerDelegate::initHybrid(jni::alias_ref<jhybridobject>) {
Expand All @@ -32,17 +37,21 @@ void DefaultTurboModuleManagerDelegate::registerNatives() {
std::shared_ptr<TurboModule> DefaultTurboModuleManagerDelegate::getTurboModule(
const std::string &name,
const std::shared_ptr<CallInvoker> &jsInvoker) {
// Not implemented yet: provide pure-C++ NativeModules here.
auto moduleProvider = DefaultTurboModuleManagerDelegate::cxxModuleProvider;
if (moduleProvider) {
return moduleProvider(name, jsInvoker);
}
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;
auto moduleProvider = DefaultTurboModuleManagerDelegate::javaModuleProvider;
if (moduleProvider) {
if (auto resolvedModule = moduleProvider(name, params)) {
return resolvedModule;
}
}
return rncore_ModuleProvider(name, params);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ class DefaultTurboModuleManagerDelegate : public jni::HybridClass<

static void registerNatives();

static std::function<std::shared_ptr<TurboModule>(
const std::string &,
const std::shared_ptr<CallInvoker> &)>
cxxModuleProvider;

static std::function<std::shared_ptr<TurboModule>(
const std::string &,
const JavaTurboModule::InitParams &)>
moduleProvidersFromEntryPoint;
javaModuleProvider;

std::shared_ptr<TurboModule> getTurboModule(
const std::string &name,
Expand Down
14 changes: 11 additions & 3 deletions packages/rn-tester/android/app/src/main/jni/OnLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ void registerComponents(
RNTMyNativeViewComponentDescriptor>());
}

std::shared_ptr<TurboModule> provideModules(
std::shared_ptr<TurboModule> cxxModuleProvider(
const std::string &name,
const std::shared_ptr<CallInvoker> &jsInvoker) {
return nullptr;
}

std::shared_ptr<TurboModule> javaModuleProvider(
const std::string &name,
const JavaTurboModule::InitParams &params) {
auto module = AppSpecs_ModuleProvider(name, params);
Expand All @@ -42,8 +48,10 @@ std::shared_ptr<TurboModule> provideModules(

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
return facebook::jni::initialize(vm, [] {
facebook::react::DefaultTurboModuleManagerDelegate::
moduleProvidersFromEntryPoint = &facebook::react::provideModules;
facebook::react::DefaultTurboModuleManagerDelegate::cxxModuleProvider =
&facebook::react::cxxModuleProvider;
facebook::react::DefaultTurboModuleManagerDelegate::javaModuleProvider =
&facebook::react::javaModuleProvider;
facebook::react::DefaultComponentsRegistry::
registerComponentDescriptorsFromEntryPoint =
&facebook::react::registerComponents;
Expand Down

0 comments on commit 9cb0261

Please sign in to comment.