Migrate JavaScriptContextHolder & JavaScriptExecutor to Kotlin (#49909)

Summary:
Migrate com.facebook.react.bridge JavaScriptContextHolder & JavaScriptExecutor to Kotlin.

In this PR I also marked the return of HermesExecutor.initHybridDefaultConfig as non-nullable to keep the conversion without logic changes. I checked that this was changed unintentionally to nullable in 12e321daf0 but I think it was not supposed to be.

## Changelog:

[INTERNAL] - Migrate com.facebook.react.bridge JavaScriptContextHolder & JavaScriptExecutor to Kotlin

Pull Request resolved: https://github.com/facebook/react-native/pull/49909

Test Plan:
```bash
yarn test-android
yarn android
```

Reviewed By: javache

Differential Revision: D70871012

Pulled By: Abbondanzo

fbshipit-source-id: 1c15927056baf166e520e896e92bebf664fa0229
This commit is contained in:
Mateo Guzmán
2025-03-10 11:08:44 -07:00
committed by Facebook GitHub Bot
parent 8b33668c43
commit 0b2b91d532
5 changed files with 43 additions and 52 deletions
@@ -892,10 +892,10 @@ public final class com/facebook/react/bridge/JavaOnlyMap$Companion {
public final fun of ([Ljava/lang/Object;)Lcom/facebook/react/bridge/JavaOnlyMap;
}
public class com/facebook/react/bridge/JavaScriptContextHolder {
public final class com/facebook/react/bridge/JavaScriptContextHolder {
public fun <init> (J)V
public fun clear ()V
public fun get ()J
public final fun clear ()V
public final fun get ()J
}
public abstract class com/facebook/react/bridge/JavaScriptExecutor {
@@ -42,7 +42,7 @@ public class HermesExecutor internal constructor(enableDebugger: Boolean, debugg
private external fun initHybridDefaultConfig(
enableDebugger: Boolean,
debuggerName: String
): HybridData?
): HybridData
@DoNotStrip
@JvmStatic
@@ -50,6 +50,6 @@ public class HermesExecutor internal constructor(enableDebugger: Boolean, debugg
enableDebugger: Boolean,
debuggerName: String,
heapSizeMB: Long
): HybridData?
): HybridData
}
}
@@ -5,29 +5,21 @@
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.bridge;
package com.facebook.react.bridge
import androidx.annotation.GuardedBy;
import androidx.annotation.GuardedBy
/**
* Wrapper for JavaScriptContext native pointer. CatalystInstanceImpl creates this on demand, and
* will call clear() before destroying the VM. People who need the raw JavaScriptContext pointer can
* synchronize on this wrapper object to guarantee that it will not be destroyed.
*/
public class JavaScriptContextHolder {
@GuardedBy("this")
private long mContext;
public class JavaScriptContextHolder
public constructor(@field:GuardedBy("this") private var context: Long) {
@GuardedBy("this") public fun get(): Long = context
public JavaScriptContextHolder(long context) {
mContext = context;
}
@GuardedBy("this")
public long get() {
return mContext;
}
public synchronized void clear() {
mContext = 0;
@Synchronized
public fun clear() {
context = 0
}
}
@@ -1,31 +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.react.bridge;
import com.facebook.jni.HybridData;
import com.facebook.proguard.annotations.DoNotStrip;
@DoNotStrip
public abstract class JavaScriptExecutor {
private final HybridData mHybridData;
protected JavaScriptExecutor(HybridData hybridData) {
mHybridData = hybridData;
}
/**
* Close this executor and cleanup any resources that it was using. No further calls are expected
* after this. TODO mhorowitz: This may no longer be used; check and delete if possible.
*/
public void close() {
mHybridData.resetNative();
}
/** Returns the name of the executor, identifying the underlying runtime. */
public abstract String getName();
}
@@ -0,0 +1,30 @@
/*
* 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.react.bridge
import com.facebook.jni.HybridData
import com.facebook.proguard.annotations.DoNotStripAny
@DoNotStripAny
public abstract class JavaScriptExecutor
protected constructor(
// fbjni looks for the exact name "mHybridData":
// https://github.com/facebookincubator/fbjni/blob/7b7efda0d49b956acf1d3307510e3c73fc55b404/cxx/fbjni/detail/Hybrid.h#L310
@Suppress("NoHungarianNotation") private val mHybridData: HybridData
) {
/**
* Close this executor and cleanup any resources that it was using. No further calls are expected
* after this. TODO mhorowitz: This may no longer be used; check and delete if possible.
*/
public open fun close() {
mHybridData.resetNative()
}
/** Returns the name of the executor, identifying the underlying runtime. */
public abstract fun getName(): String
}