mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-18 12:12:15 +08:00
Move Android TurboModules to github
Summary: This commit moves all the turbo module files for Android to Github. Note that gradle build is not yet enabled. Sample Turbo Modules will be added in a later commit. Other missing features - Support for `CxxModule` - Remove usage of folly::dynamic for arguments and result conversion - Support for Promise return types. Reviewed By: mdvacca Differential Revision: D13647438 fbshipit-source-id: 5f1188556d6c64bfa2b2fd2146ac72b0fb456891
This commit is contained in:
committed by
Facebook Github Bot
parent
8a8a11b77a
commit
e6f7d69428
@@ -0,0 +1,25 @@
|
||||
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_android_library")
|
||||
|
||||
rn_android_library(
|
||||
name = "core",
|
||||
srcs = glob(
|
||||
[
|
||||
"*.java",
|
||||
],
|
||||
),
|
||||
required_for_source_only_abi = True,
|
||||
visibility = [
|
||||
"PUBLIC",
|
||||
],
|
||||
deps = [
|
||||
react_native_target("java/com/facebook/react/turbomodule/core/interfaces:interfaces"),
|
||||
react_native_dep("java/com/facebook/systrace:systrace"),
|
||||
react_native_dep("libraries/soloader/java/com/facebook/soloader:soloader"),
|
||||
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
|
||||
react_native_dep("third-party/java/jsr-305:jsr-305"),
|
||||
react_native_target("java/com/facebook/react/turbomodule/core/jni:jni"),
|
||||
react_native_target("java/com/facebook/debug/holder:holder"),
|
||||
react_native_target("java/com/facebook/react/bridge:interfaces"),
|
||||
react_native_target("java/com/facebook/react/bridge:bridge"),
|
||||
],
|
||||
)
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.turbomodule.core;
|
||||
|
||||
import com.facebook.jni.HybridData;
|
||||
import com.facebook.proguard.annotations.DoNotStrip;
|
||||
import com.facebook.react.bridge.JSIModule;
|
||||
import com.facebook.react.bridge.JavaScriptContextHolder;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.queue.MessageQueueThread;
|
||||
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
|
||||
import com.facebook.soloader.SoLoader;
|
||||
|
||||
/**
|
||||
* This is the main class and entry point for TurboModules.
|
||||
* Note that this is a hybrid class, and has a C++ counterpart
|
||||
* This class installs the JSI bindings. It also implements the method to get a Java module, that the C++ counterpart calls.
|
||||
*/
|
||||
public class TurboModuleManager implements JSIModule {
|
||||
static {
|
||||
SoLoader.loadLibrary("turbomodulejsijni");
|
||||
}
|
||||
|
||||
private final ReactApplicationContext mReactApplicationContext;
|
||||
|
||||
@DoNotStrip
|
||||
@SuppressWarnings("unused")
|
||||
private final HybridData mHybridData;
|
||||
private final ModuleProvider mModuleProvider;
|
||||
|
||||
public TurboModuleManager(
|
||||
ReactApplicationContext reactApplicationContext, JavaScriptContextHolder jsContext, ModuleProvider moduleProvider) {
|
||||
mReactApplicationContext = reactApplicationContext;
|
||||
MessageQueueThread jsMessageQueueThread =
|
||||
mReactApplicationContext
|
||||
.getCatalystInstance()
|
||||
.getReactQueueConfiguration()
|
||||
.getJSQueueThread();
|
||||
mHybridData = initHybrid(jsContext.get(), jsMessageQueueThread);
|
||||
mModuleProvider = moduleProvider;
|
||||
}
|
||||
|
||||
@DoNotStrip
|
||||
@SuppressWarnings("unused")
|
||||
protected TurboModule getJavaModule(String name) {
|
||||
return mModuleProvider.getModule(name, mReactApplicationContext);
|
||||
}
|
||||
|
||||
protected native HybridData initHybrid(long jsContext, MessageQueueThread jsQueue);
|
||||
|
||||
protected native void installJSIBindings();
|
||||
|
||||
public void installBindings() {
|
||||
installJSIBindings();
|
||||
}
|
||||
|
||||
protected ReactApplicationContext getReactApplicationContext() {
|
||||
return mReactApplicationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() {}
|
||||
|
||||
@Override
|
||||
public void onCatalystInstanceDestroy() {}
|
||||
|
||||
/** All applications must implement this interface, and provide the Java TurboModule class */
|
||||
public interface ModuleProvider {
|
||||
TurboModule getModule(String name, ReactApplicationContext reactApplicationContext);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
load("//tools/build_defs/oss:rn_defs.bzl", "rn_android_library")
|
||||
|
||||
rn_android_library(
|
||||
name = "interfaces",
|
||||
srcs = [
|
||||
"TurboModule.java",
|
||||
],
|
||||
required_for_source_only_abi = True,
|
||||
visibility = [
|
||||
"PUBLIC",
|
||||
],
|
||||
)
|
||||
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.turbomodule.core.interfaces;
|
||||
|
||||
/**
|
||||
* All turbo modules should inherit from this interface
|
||||
*/
|
||||
public interface TurboModule {
|
||||
/** When CatalystInstance is destroyed, this method will be called. All implementing TurboModules can perform cleanup here. */
|
||||
void invalidate();
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
load("@fbsource//tools/build_defs:glob_defs.bzl", "subdir_glob")
|
||||
load("@fbsource//tools/build_defs/oss:rn_defs.bzl", "ANDROID", "react_native_target", "react_native_xplat_target", "rn_xplat_cxx_library")
|
||||
|
||||
rn_xplat_cxx_library(
|
||||
name = "jni",
|
||||
srcs = glob(["**/*.cpp"]),
|
||||
header_namespace = "",
|
||||
exported_headers = subdir_glob(
|
||||
[
|
||||
("", "**/*.h"),
|
||||
],
|
||||
prefix = "jsireact",
|
||||
),
|
||||
compiler_flags = [
|
||||
"-fexceptions",
|
||||
"-frtti",
|
||||
"-std=c++14",
|
||||
"-Wall",
|
||||
],
|
||||
force_static = True,
|
||||
platforms = ANDROID,
|
||||
preprocessor_flags = [
|
||||
"-DLOG_TAG=\"ReactNative\"",
|
||||
"-DWITH_FBSYSTRACE=1",
|
||||
],
|
||||
visibility = [
|
||||
"PUBLIC",
|
||||
],
|
||||
deps = [
|
||||
react_native_target("jni/react/jni:jni"),
|
||||
"xplat//jsi:JSIDynamic",
|
||||
"xplat//jsi:jsi",
|
||||
],
|
||||
exported_deps = [
|
||||
"xplat//jsi:jsi",
|
||||
react_native_xplat_target("turbomodule/core:core"),
|
||||
],
|
||||
)
|
||||
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <fb/fbjni.h>
|
||||
#include <jsi/jsi.h>
|
||||
|
||||
#include <jsireact/TurboModuleBinding.h>
|
||||
|
||||
#include <react/jni/JMessageQueueThread.h>
|
||||
|
||||
#include "TurboModuleManager.h"
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
static JTurboModuleProviderFunctionType moduleProvider_ = nullptr;
|
||||
|
||||
TurboModuleManager::TurboModuleManager(
|
||||
jni::alias_ref<TurboModuleManager::javaobject> jThis,
|
||||
jsi::Runtime* rt,
|
||||
std::shared_ptr<JMessageQueueThread> jsMessageQueueThread
|
||||
):
|
||||
javaPart_(make_global(jThis)),
|
||||
runtime_(rt),
|
||||
jsMessageQueueThread_(jsMessageQueueThread)
|
||||
{}
|
||||
|
||||
jni::local_ref<TurboModuleManager::jhybriddata> TurboModuleManager::initHybrid(
|
||||
jni::alias_ref<jhybridobject> jThis,
|
||||
jlong jsContext,
|
||||
jni::alias_ref<JavaMessageQueueThread::javaobject> jsQueue
|
||||
) {
|
||||
auto sharedJSMessageQueueThread = std::make_shared<JMessageQueueThread> (jsQueue);
|
||||
return makeCxxInstance(jThis, (jsi::Runtime *) jsContext, sharedJSMessageQueueThread);
|
||||
}
|
||||
|
||||
void TurboModuleManager::registerNatives() {
|
||||
registerHybrid({
|
||||
makeNativeMethod("initHybrid", TurboModuleManager::initHybrid),
|
||||
makeNativeMethod("installJSIBindings", TurboModuleManager::installJSIBindings),
|
||||
});
|
||||
}
|
||||
|
||||
void TurboModuleManager::installJSIBindings() {
|
||||
if (!runtime_) {
|
||||
return; // Runtime doesn't exist when attached to Chrome debugger.
|
||||
}
|
||||
TurboModuleBinding::install(*runtime_, std::make_shared<TurboModuleBinding>(
|
||||
[this](const std::string &name) {
|
||||
const auto moduleInstance = getJavaModule(name);
|
||||
const auto jsInvoker = std::make_shared<react::JSCallInvoker>(jsMessageQueueThread_);
|
||||
return moduleProvider_(name, moduleInstance, jsInvoker);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
jni::global_ref<JTurboModule> TurboModuleManager::getJavaModule(std::string name) {
|
||||
static auto method = javaClassStatic()->getMethod<jni::alias_ref<JTurboModule>(const std::string&)>("getJavaModule");
|
||||
return make_global(method(javaPart_.get(), name));
|
||||
}
|
||||
|
||||
void TurboModuleManager::setModuleProvider(JTurboModuleProviderFunctionType fn) {
|
||||
moduleProvider_ = fn;
|
||||
}
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its 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 <memory>
|
||||
#include <fb/fbjni.h>
|
||||
#include <jsi/jsi.h>
|
||||
#include <jsireact/TurboModule.h>
|
||||
#include <jsireact/JavaTurboModule.h>
|
||||
#include <react/jni/JMessageQueueThread.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
using JTurboModuleProviderFunctionType = std::function<std::shared_ptr<TurboModule>(
|
||||
const std::string &name, jni::global_ref<JTurboModule> moduleInstance, std::shared_ptr<JSCallInvoker> jsInvoker)>;
|
||||
|
||||
class TurboModuleManager : public jni::HybridClass<TurboModuleManager> {
|
||||
public:
|
||||
static auto constexpr kJavaDescriptor = "Lcom/facebook/react/turbomodule/core/TurboModuleManager;";
|
||||
static jni::local_ref<jhybriddata> initHybrid(
|
||||
jni::alias_ref<jhybridobject> jThis,
|
||||
jlong jsContext,
|
||||
jni::alias_ref<JavaMessageQueueThread::javaobject> jsQueue
|
||||
);
|
||||
static void registerNatives();
|
||||
static void setModuleProvider(JTurboModuleProviderFunctionType moduleProvider);
|
||||
private:
|
||||
friend HybridBase;
|
||||
jni::global_ref<TurboModuleManager::javaobject> javaPart_;
|
||||
jsi::Runtime* runtime_;
|
||||
std::shared_ptr<JMessageQueueThread> jsMessageQueueThread_;
|
||||
|
||||
jni::global_ref<JTurboModule> getJavaModule(std::string name);
|
||||
void installJSIBindings();
|
||||
explicit TurboModuleManager(
|
||||
jni::alias_ref<TurboModuleManager::jhybridobject> jThis,
|
||||
jsi::Runtime* rt,
|
||||
std::shared_ptr<JMessageQueueThread> jsMessageQueueThread
|
||||
);
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
Reference in New Issue
Block a user