Skip to content

Commit

Permalink
TurboModule Android: allow RNTester to activate TurboModule system
Browse files Browse the repository at this point in the history
Summary:
If built with `USE_CODEGEN=1` flag set, RNTester now activates the TurboModule system, also using various codegen output from the previous commits.

Note that this is very early integration, and not thoroughly tested yet.

To verify:

```
console.warn('TM enabled?', global.__turboModuleProxy != null);
```

{F337454276}

Changelog: [Internal]

Reviewed By: yungsters

Differential Revision: D23946944

fbshipit-source-id: 5838aeb9ded07b1cc0fcb069535d1c6fb3725973
  • Loading branch information
fkgozali authored and facebook-github-bot committed Sep 26, 2020
1 parent 9dbab5f commit 8d4b5ef
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 47 deletions.
1 change: 1 addition & 0 deletions packages/rn-tester/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ android {
testBuildType System.getProperty('testBuildType', 'debug') // This will later be used to control the test apk build type
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
buildConfigField("boolean", "ENABLE_FABRIC", "$enableFabric")
buildConfigField("boolean", "ENABLE_TURBOMODULE", "$enableCodegen") // If using codegen, assume using TurboModule
}
signingConfigs {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

package com.facebook.react.uiapp;

import static com.facebook.react.uiapp.RNTesterApplication.IS_FABRIC_ENABLED;

import android.content.res.Configuration;
import android.os.Bundle;
import androidx.annotation.Nullable;
Expand All @@ -31,7 +29,7 @@ public RNTesterActivityDelegate(ReactActivity activity, String mainComponentName
@Override
protected ReactRootView createRootView() {
ReactRootView reactRootView = new ReactRootView(getContext());
reactRootView.setIsFabric(IS_FABRIC_ENABLED);
reactRootView.setIsFabric(BuildConfig.ENABLE_FABRIC);
return reactRootView;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@

package com.facebook.react.uiapp;

import static com.facebook.react.uiapp.BuildConfig.ENABLE_FABRIC;

import android.app.Application;
import android.content.Context;
import androidx.annotation.Nullable;
import com.facebook.react.BuildConfig;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JSIModule;
import com.facebook.react.bridge.JSIModulePackage;
import com.facebook.react.bridge.JSIModuleProvider;
import com.facebook.react.bridge.JSIModuleSpec;
import com.facebook.react.bridge.JSIModuleType;
import com.facebook.react.bridge.JavaScriptContextHolder;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.UIManager;
import com.facebook.react.config.ReactFeatureFlags;
import com.facebook.react.fabric.ComponentFactory;
import com.facebook.react.fabric.CoreComponentsRegistry;
import com.facebook.react.fabric.FabricJSIModuleProvider;
import com.facebook.react.fabric.ReactNativeConfig;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.react.turbomodule.core.TurboModuleManager;
import com.facebook.react.views.text.ReactFontManager;
import com.facebook.soloader.SoLoader;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -38,8 +38,6 @@

public class RNTesterApplication extends Application implements ReactApplication {

static final boolean IS_FABRIC_ENABLED = ENABLE_FABRIC;

private final ReactNativeHost mReactNativeHost =
new ReactNativeHost(this) {
@Override
Expand All @@ -65,7 +63,7 @@ public List<ReactPackage> getPackages() {
@Nullable
@Override
protected JSIModulePackage getJSIModulePackage() {
if (!IS_FABRIC_ENABLED) {
if (!BuildConfig.ENABLE_FABRIC && !ReactFeatureFlags.useTurboModules) {
return null;
}

Expand All @@ -75,44 +73,81 @@ public List<JSIModuleSpec> getJSIModules(
final ReactApplicationContext reactApplicationContext,
final JavaScriptContextHolder jsContext) {
List<JSIModuleSpec> specs = new ArrayList<>();
specs.add(
new JSIModuleSpec() {
@Override
public JSIModuleType getJSIModuleType() {
return JSIModuleType.UIManager;
}

@Override
public JSIModuleProvider<UIManager> getJSIModuleProvider() {
ComponentFactory ComponentFactory = new ComponentFactory();
CoreComponentsRegistry.register(ComponentFactory);
return new FabricJSIModuleProvider(
reactApplicationContext,
ComponentFactory,
// TODO: T71362667 add ReactNativeConfig's support in RNTester
new ReactNativeConfig() {
@Override
public boolean getBool(String s) {
return true;
}

@Override
public int getInt64(String s) {
return 0;
}

@Override
public String getString(String s) {
return "";
}

@Override
public double getDouble(String s) {
return 0;
}
});
}
});

// Install the new native module system.
if (ReactFeatureFlags.useTurboModules) {
specs.add(
new JSIModuleSpec() {
@Override
public JSIModuleType getJSIModuleType() {
return JSIModuleType.TurboModuleManager;
}

@Override
public JSIModuleProvider getJSIModuleProvider() {
return new JSIModuleProvider() {
@Override
public JSIModule get() {
ReactInstanceManager reactInstanceManager = getReactInstanceManager();
List<ReactPackage> packages = reactInstanceManager.getPackages();

return new TurboModuleManager(
jsContext,
new RNTesterTurboModuleManagerDelegate(
reactApplicationContext, packages),
reactApplicationContext
.getCatalystInstance()
.getJSCallInvokerHolder(),
reactApplicationContext
.getCatalystInstance()
.getNativeCallInvokerHolder());
}
};
}
});
}

// Install the new renderer.
if (BuildConfig.ENABLE_FABRIC) {
specs.add(
new JSIModuleSpec() {
@Override
public JSIModuleType getJSIModuleType() {
return JSIModuleType.UIManager;
}

@Override
public JSIModuleProvider<UIManager> getJSIModuleProvider() {
ComponentFactory ComponentFactory = new ComponentFactory();
CoreComponentsRegistry.register(ComponentFactory);
return new FabricJSIModuleProvider(
reactApplicationContext,
ComponentFactory,
// TODO: T71362667 add ReactNativeConfig's support in RNTester
new ReactNativeConfig() {
@Override
public boolean getBool(String s) {
return false;
}

@Override
public int getInt64(String s) {
return 0;
}

@Override
public String getString(String s) {
return "";
}

@Override
public double getDouble(String s) {
return 0;
}
});
}
});
}

return specs;
}
Expand All @@ -122,6 +157,8 @@ public double getDouble(String s) {

@Override
public void onCreate() {
// Set `USE_CODEGEN` env var when building RNTester to enable TurboModule.
ReactFeatureFlags.useTurboModules = BuildConfig.ENABLE_TURBOMODULE;
ReactFontManager.getInstance().addCustomFont(this, "Rubik", R.font.rubik);
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,54 @@ std::shared_ptr<TurboModule> RNTesterAppModuleProvider(const std::string moduleN
if (module != nullptr) {
return module;
}

// TODO: fix up the ReactAndroidSpec_ModuleProvider() to avoid the Android prefix.
if (moduleName == "DatePicker") {
return std::make_shared<NativeDatePickerAndroidSpecJSI>(params);
}
if (moduleName == "DialogManager") {
return std::make_shared<NativeDialogManagerAndroidSpecJSI>(params);
}
if (moduleName == "ImageLoader") {
return std::make_shared<NativeImageLoaderAndroidSpecJSI>(params);
}
if (moduleName == "Networking") {
return std::make_shared<NativeNetworkingAndroidSpecJSI>(params);
}
if (moduleName == "Permissions") {
return std::make_shared<NativePermissionsAndroidSpecJSI>(params);
}
if (moduleName == "PlatformConstants") {
return std::make_shared<NativePlatformConstantsAndroidSpecJSI>(params);
}
if (moduleName == "StatusBarManager") {
return std::make_shared<NativeStatusBarManagerAndroidSpecJSI>(params);
}
if (moduleName == "Toast") {
return std::make_shared<NativeToastAndroidSpecJSI>(params);
}

// TODO: handle some special case naming.
if (moduleName == "IntentAndroid") {
return std::make_shared<NativeLinkingSpecJSI>(params);
}

// TODO: Animated module has special cases.
if ("NativeAnimatedModule" == moduleName) {
return std::make_shared<NativeAnimatedModuleSpecJSI>(params);
}
if ("NativeAnimatedTurboModule" == moduleName) {
return std::make_shared<NativeAnimatedTurboModuleSpecJSI>(params);
}

// TODO: handle multiple names for one spec.
if ("AsyncLocalStorage" == moduleName) {
return std::make_shared<NativeAsyncStorageSpecJSI>(params);
}
if ("AsyncSQLiteDBStorage" == moduleName) {
return std::make_shared<NativeAsyncStorageSpecJSI>(params);
}

return ReactAndroidSpec_ModuleProvider(moduleName, params);
}

Expand Down

1 comment on commit 8d4b5ef

@fkgozali
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot_1601078927

Please sign in to comment.