Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added additional builder method receiving arguments for using jsc or hermes to correctly decide which DSO to load at app startup. #33952

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.packagerconnection.RequestHandler;
import com.facebook.react.uimanager.UIImplementationProvider;
import com.facebook.react.util.JSInterpreter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -66,18 +68,11 @@ public class ReactInstanceManagerBuilder {
private @Nullable Map<String, RequestHandler> mCustomPackagerCommandHandlers;
private @Nullable ReactPackageTurboModuleManagerDelegate.Builder mTMMDelegateBuilder;
private @Nullable SurfaceDelegateFactory mSurfaceDelegateFactory;
private Boolean hermesEnabled = null;
private JSInterpreter jsEngine = JSInterpreter.OLD_LOGIC;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
private JSInterpreter jsEngine = JSInterpreter.OLD_LOGIC;
private JSInterpreter jsInterpreter = JSInterpreter.OLD_LOGIC;

Let's use jsInterpreter everywhere to keep it aligned with the enum class name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


/* package protected Deafault Constructor */
ReactInstanceManagerBuilder() {}

/**
* Constructor that takes a Boolean specifying whether to load hermes or jsc
*/
ReactInstanceManagerBuilder(Boolean hermesEnabled) {
this.hermesEnabled = hermesEnabled;
}

/** Sets a provider of {@link UIImplementation}. Uses default provider if null is passed. */
public ReactInstanceManagerBuilder setUIImplementationProvider(
@Nullable UIImplementationProvider uiImplementationProvider) {
Expand Down Expand Up @@ -134,6 +129,30 @@ public ReactInstanceManagerBuilder setJSBundleLoader(JSBundleLoader jsBundleLoad
return this;
}

/**
* Sets the jsEngine as JSC or HERMES as per the setJsEngineAsHermes call
* Uses the enum {@link JSInterpreter}
* @param jsEngine
*/
private void setJSEngine(JSInterpreter jsEngine){
this.jsEngine = jsEngine;
}

/**
* Utility setter to set the required JSEngine as HERMES or JSC
* Defaults to OLD_LOGIC if not called by the host app
* @param hermesEnabled
* hermesEnabled = true sets the JS Engine as HERMES and JSC otherwise
*/
public void setJsEngineAsHermes(boolean hermesEnabled){
if(hermesEnabled){
setJSEngine(JSInterpreter.HERMES);
}
else{
setJSEngine(JSInterpreter.JSC);
}
}

/**
* Path to your app's main module on Metro. This is used when reloading JS during development. All
* paths are relative to the root folder the packager is serving files from. Examples: {@code
Expand Down Expand Up @@ -356,15 +375,15 @@ private JavaScriptExecutorFactory getDefaultJSExecutorFactory(
String appName, String deviceName, Context applicationContext) {

// Relying solely on try catch block and loading jsc even when
// project is using hermes can lead to launchtime crashes expecially in
// project is using hermes can lead to launch-time crashes especially in
// monorepo architectures and hybrid apps using both native android
// and react native.
// So we can use the value of enableHermes receved by the constructor
// So we can use the value of enableHermes received by the constructor
// to decide which library to load at launch

// if nothing is specified, use old loading method
// else load the required engine
if (hermesEnabled == null) {
if (jsEngine == JSInterpreter.OLD_LOGIC) {
try {
// If JSC is included, use it as normal
initializeSoLoaderIfNecessary(applicationContext);
Expand All @@ -377,7 +396,7 @@ private JavaScriptExecutorFactory getDefaultJSExecutorFactory(
HermesExecutor.loadLibrary();
return new HermesExecutorFactory();
}
} else if (hermesEnabled == true) {
} else if (jsEngine == JSInterpreter.HERMES) {
HermesExecutor.loadLibrary();
return new HermesExecutorFactory();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.facebook.react.util;

/**
* An enum that specifies the JS Interpreter to be used in the app
* Old Logic uses the legacy code
* JSC/HERMES loads the respective engine using the revamped logic
*/
public enum JSInterpreter {
OLD_LOGIC,
JSC,
HERMES
}