Implement support for legacy CxxModules

Summary:
## Background
Legacy Cxx NativeModules are implemented as Hybrid classes. Essentially, when a Cxx NativeModule is requested, you instantiate its hybrid class, which then creates a C++ counterpart. Then, the bridge uses the C++ counterpart's `getModule()` method to obtain ownership of the Cxx NativeModule.

## Summary
This diff implements backwards-compability for Cxx NativeModules.

If a Cxx NativeModule implements the `TurboModule` interface, then when the module is requested by name, we:
1. Instantiate its Java hybrid class, createing a C++ counterpart.
3. Obtain the CxxModule from the C++ counterpart using `getModule()` and use it to create a `TurboCxxModule` instance (this forwards all JavaScript method calls to the CxxModule) inside `TurboModuleManager`.
5. Return this `TurboCxxModule` to JS.

Reviewed By: mdvacca

Differential Revision: D15252041

fbshipit-source-id: cdbb62632d7a8735f7687daf62de63df9e3ad2c5
This commit is contained in:
Ramanpreet Nara 2019-05-22 13:11:53 -07:00 коммит произвёл Facebook Github Bot
Родитель 37583bd6e8
Коммит 5127ac5c2a
2 изменённых файлов: 16 добавлений и 3 удалений

Просмотреть файл

@ -12,7 +12,7 @@
#include <jsi/jsi.h>
#include <jsireact/TurboModuleBinding.h>
#include <jsireact/TurboCxxModule.h>
#include <react/jni/JMessageQueueThread.h>
#include "TurboModuleManager.h"
@ -55,13 +55,18 @@ void TurboModuleManager::installJSIBindings() {
return; // Runtime doesn't exist when attached to Chrome debugger.
}
TurboModuleBinding::install(*runtime_, std::make_shared<TurboModuleBinding>(
[this](const std::string &name) {
[this](const std::string &name) -> std::shared_ptr<TurboModule> {
auto cxxModule = turboModuleManagerDelegate_->cthis()->getTurboModule(name, jsCallInvoker_);
if (cxxModule) {
return cxxModule;
}
const auto moduleInstance = getJavaModule(name);
auto legacyCxxModule = getLegacyCxxJavaModule(name);
if (legacyCxxModule) {
return std::make_shared<react::TurboCxxModule>(legacyCxxModule->cthis()->getModule(), jsCallInvoker_);
}
auto moduleInstance = getJavaModule(name);
if (moduleInstance) {
return turboModuleManagerDelegate_->cthis()->getTurboModule(name, moduleInstance, jsCallInvoker_);
@ -80,5 +85,11 @@ jni::global_ref<JTurboModule> TurboModuleManager::getJavaModule(std::string name
return module;
}
jni::global_ref<CxxModuleWrapper::javaobject> TurboModuleManager::getLegacyCxxJavaModule(std::string name) {
static auto method = turboModuleManagerDelegate_->getClass()->getMethod<jni::alias_ref<CxxModuleWrapper::javaobject>(const std::string&)>("getLegacyCxxModule");
auto module = jni::make_global(method(turboModuleManagerDelegate_.get(), name));
return module;
}
} // namespace react
} // namespace facebook

Просмотреть файл

@ -12,6 +12,7 @@
#include <jsi/jsi.h>
#include <jsireact/TurboModule.h>
#include <jsireact/JavaTurboModule.h>
#include <react/jni/CxxModuleWrapper.h>
#include <react/jni/JMessageQueueThread.h>
#include <jsireact/JSCallInvokerHolder.h>
#include <jsireact/TurboModuleManagerDelegate.h>
@ -37,6 +38,7 @@ private:
jni::global_ref<TurboModuleManagerDelegate::javaobject> turboModuleManagerDelegate_;
jni::global_ref<JTurboModule> getJavaModule(std::string name);
jni::global_ref<CxxModuleWrapper::javaobject> getLegacyCxxJavaModule(std::string name);
void installJSIBindings();
explicit TurboModuleManager(
jni::alias_ref<TurboModuleManager::jhybridobject> jThis,