forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cxx module autolinking without codegen
Summary: see https://fb.workplace.com/groups/2158787827656681/posts/2296646383870824/?comment_id=2303006146568181 for context >hi everyone, update on how this directional development is going. currently, RuntimeExecutor appears to fulfill ExpoModules needs and has been integrated successfully on iOS. however, there is still a question on how to migrate callsites that require the runtime "on demand" when they are already on the JS thread. example from reanimated here (https://github.com/.../react.../blob/main/apple/REAModule.mm). >Samuel and Pieter helped brainstorm a couple of options (exposing the raw runtime pointer, C++ turbomodules, deobfuscating the runtime pointer in the turbomodule invocation flow), but i felt like C++ turbomodule was the least hacky and had the most long-term prospects of these options. >since the blocker is autolinking, i prototyped a macro similar to RCT_EXPORT_MODULE for cxx modules only and i would like to get everyone's comments: D53602544. Nicola is investigating android atm. this implementation is inspired by RCT_EXPORT_MODULE, 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 codegen solution. this is a temporary solution because we don't have a proper autolinking solution for ios modules that doesn't require changes in user land. Differential Revision: D53602544
- Loading branch information
1 parent
e4135e9
commit f80b712
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...ages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/CxxTurboModuleUtils.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)>>& | ||
cxxTurboModuleMap() { | ||
static std::unordered_map< | ||
std::string, | ||
std::function<std::shared_ptr<TurboModule>( | ||
std::shared_ptr<CallInvoker> jsInvoker)>> | ||
map; | ||
return map; | ||
} | ||
|
||
void registerCxxModule( | ||
std::string name, | ||
std::function<std::shared_ptr<TurboModule>( | ||
std::shared_ptr<CallInvoker> jsInvoker)> moduleProviderFunc) { | ||
cxxTurboModuleMap()[name] = moduleProviderFunc; | ||
} | ||
|
||
} // namespace facebook::react |
63 changes: 63 additions & 0 deletions
63
packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/CxxTurboModuleUtils.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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 <cassert> | ||
#include <string> | ||
#include <unordered_map> | ||
|
||
#include <jsi/jsi.h> | ||
|
||
#include <ReactCommon/CallInvoker.h> | ||
#include <ReactCommon/CallbackWrapper.h> | ||
#include <ReactCommon/TurboModule.h> | ||
|
||
namespace facebook::react { | ||
|
||
std::unordered_map< | ||
std::string, | ||
std::function< | ||
std::shared_ptr<TurboModule>(std::shared_ptr<CallInvoker> jsInvoker)>>& | ||
cxxTurboModuleMap(); | ||
|
||
void registerCxxModule( | ||
std::string name, | ||
std::function<std::shared_ptr<TurboModule>( | ||
std::shared_ptr<CallInvoker> jsInvoker)> moduleProviderFunc); | ||
|
||
} // namespace facebook::react | ||
|
||
#define RCT_EXPORT_CXX_MODULE_EXPERIMENTAL(name) \ | ||
_Pragma("clang diagnostic push") \ | ||
_Pragma("clang diagnostic ignored \"-Wglobal-constructors\"") struct \ | ||
name##Load { \ | ||
name##Load() { \ | ||
facebook::react::registerCxxModule( \ | ||
#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") | ||
|
||
// RCT_EXPORT_CXX_MODULE(NativeCxxModuleExample) turns into the following: | ||
// #pragma clang diagnostic push | ||
// #pragma clang diagnostic ignored "-Wglobal-constructors" | ||
// struct NativeCxxModuleExampleLoad { | ||
// NativeCxxModuleExampleLoad() { | ||
// facebook::react::registerCxxModule(name, | ||
// [&](std::shared_ptr<facebook::react::CallInvoker> jsInvoker) { | ||
// return | ||
// std::make_shared<facebook::react::NativeCxxModuleExample>(jsInvoker); | ||
// }); | ||
// } | ||
// }; | ||
// constexpr static NativeCxxModuleExampleLoad nativeCxxModuleExampleLoad; | ||
// #pragma clang diagnostic pop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters