Skip to content

Commit

Permalink
cxx module autolinking without codegen (#43054)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #43054

Changelog: [iOS][Android][Added] Experimental macro to autolink C++ turbomodules

This implementation is inspired by RCT_EXPORT_MODULE on iOS. We keep a global data structure that maps module names to a lambda that returns the C++ turbomodule. This will come with a hit to startup time. the only way to avoid that is a solution that does static analysis of the list of C++ turbomodules being linked to the library.

Reviewed By: fkgozali

Differential Revision: D53602544

fbshipit-source-id: 8ea49fa576dc718f44b1595b68ab7c606c2db605
  • Loading branch information
philIip authored and facebook-github-bot committed Feb 20, 2024
1 parent 28da7f9 commit 01d704d
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <fbjni/fbjni.h>
#include <jsi/jsi.h>

#include <ReactCommon/CxxTurboModuleUtils.h>
#include <ReactCommon/JavaInteropTurboModule.h>
#include <ReactCommon/TurboCxxModule.h>
#include <ReactCommon/TurboModuleBinding.h>
Expand Down Expand Up @@ -168,6 +169,14 @@ TurboModuleProviderFunctionType TurboModuleManager::createTurboModuleProvider(
return cxxModule;
}

auto& cxxTurboModuleMapProvider = globalExportedCxxTurboModuleMap();
auto it = cxxTurboModuleMapProvider.find(name);
if (it != cxxTurboModuleMapProvider.end()) {
auto turboModule = it->second(jsCallInvoker);
turboModuleCache->insert({name, turboModule});
return turboModule;
}

static auto getTurboLegacyCxxModule =
javaPart->getClass()
->getMethod<jni::alias_ref<CxxModuleWrapper::javaobject>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 "CxxTurboModuleUtils.h"

namespace facebook::react {

std::unordered_map<
std::string,
std::function<
std::shared_ptr<TurboModule>(std::shared_ptr<CallInvoker> jsInvoker)>>&
globalExportedCxxTurboModuleMap() {
static std::unordered_map<
std::string,
std::function<std::shared_ptr<TurboModule>(
std::shared_ptr<CallInvoker> jsInvoker)>>
map;
return map;
}

void registerCxxModuleToGlobalModuleMap(
std::string name,
std::function<std::shared_ptr<TurboModule>(
std::shared_ptr<CallInvoker> jsInvoker)> moduleProviderFunc) {
globalExportedCxxTurboModuleMap()[name] = moduleProviderFunc;
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 <string>
#include <unordered_map>

#include <ReactCommon/CallInvoker.h>
#include <ReactCommon/TurboModule.h>

namespace facebook::react {

std::unordered_map<
std::string,
std::function<
std::shared_ptr<TurboModule>(std::shared_ptr<CallInvoker> jsInvoker)>>&
globalExportedCxxTurboModuleMap();

void registerCxxModuleToGlobalModuleMap(
std::string name,
std::function<std::shared_ptr<TurboModule>(
std::shared_ptr<CallInvoker> jsInvoker)> moduleProviderFunc);

} // namespace facebook::react

/*
* You can use this macro to register your C++ TurboModule in your .cpp
* implementation if you do not have access to getTurboModule:jsInvoker:
* callback. This will register the module before main() is called,
* so it will incur a startup cost.
*
* RCT_EXPORT_CXX_MODULE_EXPERIMENTAL(ModuleExample) becomes:
*
* #pragma clang diagnostic push
* #pragma clang diagnostic ignored "-Wglobal-constructors"
* struct ModuleExampleLoad {
* ModuleExampleLoad() {
* facebook::react::registerCxxModule(name,
* [&](std::shared_ptr<facebook::react::CallInvoker> jsInvoker) {
* return
* std::make_shared<facebook::react::ModuleExample>(jsInvoker);
* });
* }
* };
* static ModuleExampleLoad moduleExampleLoad;
* #pragma clang diagnostic pop
*
*/
#define RCT_EXPORT_CXX_MODULE_EXPERIMENTAL(name) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wglobal-constructors\"") struct \
name##Load { \
name##Load() { \
facebook::react::registerCxxModuleToGlobalModuleMap( \
#name, \
[&](std::shared_ptr<facebook::react::CallInvoker> jsInvoker) { \
return std::make_shared<facebook::react::name>(jsInvoker); \
}); \
} \
}; \
static name##Load _##name##Load; \
_Pragma("clang diagnostic pop")
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#import <React/RCTPerformanceLogger.h>
#import <React/RCTRuntimeExecutorModule.h>
#import <React/RCTUtils.h>
#import <ReactCommon/CxxTurboModuleUtils.h>
#import <ReactCommon/TurboCxxModule.h>
#import <ReactCommon/TurboModulePerfLogger.h>
#import <ReactCommon/TurboModuleUtils.h>
Expand Down Expand Up @@ -325,6 +326,14 @@ - (instancetype)initWithBridgeProxy:(RCTBridgeProxy *)bridgeProxy
TurboModulePerfLogger::moduleCreateFail(moduleName, moduleId);
}

auto &cxxTurboModuleMapProvider = globalExportedCxxTurboModuleMap();
auto it = cxxTurboModuleMapProvider.find(moduleName);
if (it != cxxTurboModuleMapProvider.end()) {
auto turboModule = it->second(_jsInvoker);
_turboModuleCache.insert({moduleName, turboModule});
return turboModule;
}

/**
* Step 2: Look for platform-specific modules.
*/
Expand Down

0 comments on commit 01d704d

Please sign in to comment.