From 12e321daf07d312c6b4947e306a449a7985ac19a Mon Sep 17 00:00:00 2001
From: Parsa Nasirimehr
Date: Mon, 13 Jan 2025 08:44:05 -0800
Subject: [PATCH] chore(Android): Migrate Hermes Executor to Kotlin (#48617)
Summary:
Migrating HermesExecutor and it's factory to Kotlin. Not sure if the TAG in HermesExecutorFactory is needed anymore or not, but the rest of the changes are pretty bog standard
## Changelog:
[INTERNAL] [FIXED] - Migrate HermesExecutor and HermesExecutorFactory to Kotlin
Pull Request resolved: https://github.com/facebook/react-native/pull/48617
Test Plan:
`./gradlew test`:
Reviewed By: tdn120
Differential Revision: D68094681
Pulled By: cortinico
fbshipit-source-id: 16eae5c7c24886421cbd2cbf213295134a9c01cf
---
.../hermes/reactexecutor/HermesExecutor.java | 50 ----------------
.../hermes/reactexecutor/HermesExecutor.kt | 53 +++++++++++++++++
.../reactexecutor/HermesExecutorFactory.java | 57 -------------------
.../reactexecutor/HermesExecutorFactory.kt | 42 ++++++++++++++
4 files changed, 95 insertions(+), 107 deletions(-)
delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutor.java
create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutor.kt
delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutorFactory.java
create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutorFactory.kt
diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutor.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutor.java
deleted file mode 100644
index 2073bb4ffdb..00000000000
--- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutor.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright (c) Meta Platforms, Inc. and 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.hermes.reactexecutor;
-
-import com.facebook.jni.HybridData;
-import com.facebook.react.bridge.JavaScriptExecutor;
-import com.facebook.react.common.build.ReactBuildConfig;
-import com.facebook.soloader.SoLoader;
-import javax.annotation.Nullable;
-
-public class HermesExecutor extends JavaScriptExecutor {
- private static String mode_;
-
- static {
- loadLibrary();
- }
-
- public static void loadLibrary() throws UnsatisfiedLinkError {
- if (mode_ == null) {
- // libhermes must be loaded explicitly to invoke its JNI_OnLoad.
- SoLoader.loadLibrary("hermes");
- SoLoader.loadLibrary("hermes_executor");
- // libhermes_executor is built differently for Debug & Release so we load the proper mode.
- mode_ = ReactBuildConfig.DEBUG ? "Debug" : "Release";
- }
- }
-
- HermesExecutor(@Nullable RuntimeConfig config, boolean enableDebugger, String debuggerName) {
- super(
- config == null
- ? initHybridDefaultConfig(enableDebugger, debuggerName)
- : initHybrid(enableDebugger, debuggerName, config.getHeapSizeMB()));
- }
-
- @Override
- public String getName() {
- return "HermesExecutor" + mode_;
- }
-
- private static native HybridData initHybridDefaultConfig(
- boolean enableDebugger, String debuggerName);
-
- private static native HybridData initHybrid(
- boolean enableDebugger, String debuggerName, long heapSizeMB);
-}
diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutor.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutor.kt
new file mode 100644
index 00000000000..a71d0ed9188
--- /dev/null
+++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutor.kt
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) Meta Platforms, Inc. and 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.hermes.reactexecutor
+
+import com.facebook.jni.HybridData
+import com.facebook.react.bridge.JavaScriptExecutor
+import com.facebook.react.common.build.ReactBuildConfig
+import com.facebook.soloader.SoLoader
+
+public class HermesExecutor
+internal constructor(config: RuntimeConfig?, enableDebugger: Boolean, debuggerName: String) :
+ JavaScriptExecutor(
+ config?.let { initHybrid(enableDebugger, debuggerName, it.heapSizeMB) }
+ ?: initHybridDefaultConfig(enableDebugger, debuggerName)) {
+
+ override fun getName(): String = "HermesExecutor$mode"
+
+ public companion object {
+ private var mode: String? = null
+
+ init {
+ loadLibrary()
+ }
+
+ @JvmStatic
+ @Throws(UnsatisfiedLinkError::class)
+ public fun loadLibrary() {
+ if (mode == null) {
+ // libhermes must be loaded explicitly to invoke its JNI_OnLoad.
+ SoLoader.loadLibrary("hermes")
+ SoLoader.loadLibrary("hermes_executor")
+ // libhermes_executor is built differently for Debug & Release so we load the proper mode.
+ mode = if (ReactBuildConfig.DEBUG) "Debug" else "Release"
+ }
+ }
+
+ private external fun initHybridDefaultConfig(
+ enableDebugger: Boolean,
+ debuggerName: String
+ ): HybridData?
+
+ private external fun initHybrid(
+ enableDebugger: Boolean,
+ debuggerName: String,
+ heapSizeMB: Long
+ ): HybridData?
+ }
+}
diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutorFactory.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutorFactory.java
deleted file mode 100644
index a51d5825a26..00000000000
--- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutorFactory.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Copyright (c) Meta Platforms, Inc. and 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.hermes.reactexecutor;
-
-import com.facebook.hermes.instrumentation.HermesSamplingProfiler;
-import com.facebook.react.bridge.JavaScriptExecutor;
-import com.facebook.react.bridge.JavaScriptExecutorFactory;
-
-public class HermesExecutorFactory implements JavaScriptExecutorFactory {
- private static final String TAG = "Hermes";
-
- private final RuntimeConfig mConfig;
- private boolean mEnableDebugger = true;
- private String mDebuggerName = "";
-
- public HermesExecutorFactory() {
- this(null);
- }
-
- public HermesExecutorFactory(RuntimeConfig config) {
- mConfig = config;
- }
-
- public void setEnableDebugger(boolean enableDebugger) {
- mEnableDebugger = enableDebugger;
- }
-
- public void setDebuggerName(String debuggerName) {
- mDebuggerName = debuggerName;
- }
-
- @Override
- public JavaScriptExecutor create() {
- return new HermesExecutor(mConfig, mEnableDebugger, mDebuggerName);
- }
-
- @Override
- public void startSamplingProfiler() {
- HermesSamplingProfiler.enable();
- }
-
- @Override
- public void stopSamplingProfiler(String filename) {
- HermesSamplingProfiler.dumpSampledTraceToFile(filename);
- HermesSamplingProfiler.disable();
- }
-
- @Override
- public String toString() {
- return "JSIExecutor+HermesRuntime";
- }
-}
diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutorFactory.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutorFactory.kt
new file mode 100644
index 00000000000..8faf7799981
--- /dev/null
+++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/HermesExecutorFactory.kt
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) Meta Platforms, Inc. and 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.hermes.reactexecutor
+
+import com.facebook.hermes.instrumentation.HermesSamplingProfiler.disable
+import com.facebook.hermes.instrumentation.HermesSamplingProfiler.dumpSampledTraceToFile
+import com.facebook.hermes.instrumentation.HermesSamplingProfiler.enable
+import com.facebook.react.bridge.JavaScriptExecutor
+import com.facebook.react.bridge.JavaScriptExecutorFactory
+
+public class HermesExecutorFactory
+@JvmOverloads
+public constructor(private val config: RuntimeConfig? = null) : JavaScriptExecutorFactory {
+ private var enableDebugger = true
+ private var debuggerName = ""
+
+ public fun setEnableDebugger(enableDebugger: Boolean) {
+ this.enableDebugger = enableDebugger
+ }
+
+ public fun setDebuggerName(debuggerName: String) {
+ this.debuggerName = debuggerName
+ }
+
+ override fun create(): JavaScriptExecutor = HermesExecutor(config, enableDebugger, debuggerName)
+
+ override fun startSamplingProfiler() {
+ enable()
+ }
+
+ override fun stopSamplingProfiler(filename: String) {
+ dumpSampledTraceToFile(filename)
+ disable()
+ }
+
+ override fun toString(): String = "JSIExecutor+HermesRuntime"
+}