Skip to content

Commit

Permalink
Update getDefaultJSExecutorFactory to load Hermes first and fallback …
Browse files Browse the repository at this point in the history
…to JSC (#41914)

Summary:
Pull Request resolved: #41914

If the user does not specify which engine they're using, we still default to loading JSC first and then attempting to load Hermes.
This has a small performance hit (as we attempt to load an existing library) + it prints an inactionable log for the user every time.

Changelog:
[Android] [Fixed] - Update getDefaultJSExecutorFactory to load Hermes first and fallback to JSC

Reviewed By: luluwu2032

Differential Revision: D52080545

fbshipit-source-id: 95f37304d713da7d7079eabbd2dfdf230d29a1b9
  • Loading branch information
cortinico authored and facebook-github-bot committed Dec 13, 2023
1 parent 46d304a commit 70a757f
Showing 1 changed file with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -373,31 +373,28 @@ public ReactInstanceManager build() {
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 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 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
initializeSoLoaderIfNecessary(applicationContext);
// Hermes has been enabled by default in OSS since React Native 0.70.
// If the user hasn't specified a JSEngineResolutionAlgorithm,
// we attempt to load Hermes first, and fallback to JSC if we can't resolve the library.
if (mJSEngineResolutionAlgorithm == null) {
FLog.w(
TAG,
"You're not setting the JS Engine Resolution Algorithm. "
+ "We'll try to load JSC first, and if it fails we'll fallback to Hermes");
try {
// If JSC is included, use it as normal
initializeSoLoaderIfNecessary(applicationContext);
JSCExecutor.loadLibrary();
return new JSCExecutorFactory(appName, deviceName);
} catch (UnsatisfiedLinkError jscE) {
if (jscE.getMessage().contains("__cxa_bad_typeid")) {
throw jscE;
}
HermesExecutor.loadLibrary();
return new HermesExecutorFactory();
} catch (UnsatisfiedLinkError ignoredHermesError) {
try {
JSCExecutor.loadLibrary();
return new JSCExecutorFactory(appName, deviceName);
} catch (UnsatisfiedLinkError jscError) {
FLog.e(
TAG,
"Unable to load neither the Hermes nor the JSC native library. "
+ "Your application is not built correctly and will fail to execute");
if (jscError.getMessage().contains("__cxa_bad_typeid")) {
throw jscError;
}
return null;
}
}
} else if (mJSEngineResolutionAlgorithm == JSEngineResolutionAlgorithm.HERMES) {
HermesExecutor.loadLibrary();
Expand Down

0 comments on commit 70a757f

Please sign in to comment.