Skip to content

Commit

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

Summary:
Pull Request resolved: facebook#35225

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

Differential Revision: D41069277

fbshipit-source-id: 9d8957fa2c9bebcdcf9c5ea576351444cdda5f50
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Nov 7, 2022
1 parent 7a327d9 commit df569d1
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> moduleProvider(
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::moduleProvider =
&facebook::react::moduleProvider;
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::moduleProvider{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::moduleProvider;
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;
moduleProvider;

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> moduleProvider(
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::moduleProvider =
&facebook::react::moduleProvider;
facebook::react::DefaultComponentsRegistry::
registerComponentDescriptorsFromEntryPoint =
&facebook::react::registerComponents;
Expand Down

0 comments on commit df569d1

Please sign in to comment.