Skip to content

Commit

Permalink
Migrate NativeArray classes to Kotlin
Browse files Browse the repository at this point in the history
Summary:
# Changelog:
[Internal] -

This converts the vertical of NativeArray/ReadableNativeArray/WritableNativeArray classes to Kotlin.

Differential Revision: D57327835
  • Loading branch information
rshest authored and facebook-github-bot committed May 14, 2024
1 parent 2cc3ba1 commit e60cd87
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 273 deletions.
12 changes: 10 additions & 2 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -951,10 +951,14 @@ public class com/facebook/react/bridge/ModuleSpec {
}

public abstract class com/facebook/react/bridge/NativeArray : com/facebook/react/bridge/NativeArrayInterface {
protected static final field Companion Lcom/facebook/react/bridge/NativeArray$Companion;
protected fun <init> (Lcom/facebook/jni/HybridData;)V
public fun toString ()Ljava/lang/String;
}

protected final class com/facebook/react/bridge/NativeArray$Companion {
}

public abstract interface class com/facebook/react/bridge/NativeArrayInterface {
public abstract fun toString ()Ljava/lang/String;
}
Expand Down Expand Up @@ -1378,6 +1382,7 @@ public abstract interface class com/facebook/react/bridge/ReadableMapKeySetItera
}

public class com/facebook/react/bridge/ReadableNativeArray : com/facebook/react/bridge/NativeArray, com/facebook/react/bridge/ReadableArray {
public static final field Companion Lcom/facebook/react/bridge/ReadableNativeArray$Companion;
protected fun <init> (Lcom/facebook/jni/HybridData;)V
public fun equals (Ljava/lang/Object;)Z
public synthetic fun getArray (I)Lcom/facebook/react/bridge/ReadableArray;
Expand All @@ -1386,7 +1391,6 @@ public class com/facebook/react/bridge/ReadableNativeArray : com/facebook/react/
public fun getDouble (I)D
public fun getDynamic (I)Lcom/facebook/react/bridge/Dynamic;
public fun getInt (I)I
public static fun getJNIPassCounter ()I
public fun getLong (I)J
public synthetic fun getMap (I)Lcom/facebook/react/bridge/ReadableMap;
public fun getMap (I)Lcom/facebook/react/bridge/ReadableNativeMap;
Expand All @@ -1398,6 +1402,10 @@ public class com/facebook/react/bridge/ReadableNativeArray : com/facebook/react/
public fun toArrayList ()Ljava/util/ArrayList;
}

public final class com/facebook/react/bridge/ReadableNativeArray$Companion {
public final fun getJniPassCounter ()I
}

public class com/facebook/react/bridge/ReadableNativeMap : com/facebook/react/bridge/NativeMap, com/facebook/react/bridge/ReadableMap {
protected fun <init> (Lcom/facebook/jni/HybridData;)V
public fun equals (Ljava/lang/Object;)Z
Expand Down Expand Up @@ -1523,7 +1531,7 @@ public abstract interface class com/facebook/react/bridge/WritableMap : com/face
public abstract fun putString (Ljava/lang/String;Ljava/lang/String;)V
}

public class com/facebook/react/bridge/WritableNativeArray : com/facebook/react/bridge/ReadableNativeArray, com/facebook/react/bridge/WritableArray {
public final class com/facebook/react/bridge/WritableNativeArray : com/facebook/react/bridge/ReadableNativeArray, com/facebook/react/bridge/WritableArray {
public fun <init> ()V
public fun pushArray (Lcom/facebook/react/bridge/ReadableArray;)V
public fun pushBoolean (Z)V
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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

/** Base class for an array whose members are stored in native code (C++). */
@DoNotStrip
public abstract class NativeArray
protected constructor(@field:DoNotStrip private val mHybridData: HybridData?) :
NativeArrayInterface {
external override fun toString(): String

protected companion object {
init {
ReactBridge.staticInit()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ public interface ReadableArray {

public fun size(): Int

public fun toArrayList(): ArrayList<Any>
public fun toArrayList(): ArrayList<Any?>
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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.infer.annotation.Assertions
import com.facebook.jni.HybridData
import com.facebook.proguard.annotations.DoNotStripAny
import java.util.Arrays

/**
* Implementation of a NativeArray that allows read-only access to its members. This will generally
* be constructed and filled in native code so you shouldn't construct one yourself.
*/
@DoNotStripAny
public open class ReadableNativeArray protected constructor(hybridData: HybridData?) :
NativeArray(hybridData), ReadableArray {

private val localArray: Array<Any?> by
lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
// WriteOnce but not in the constructor fields
jniPassCounter++
Assertions.assertNotNull(importArray())
}

private external fun importArray(): Array<Any?>

private val localTypeArray: Array<ReadableType> by
lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
// WriteOnce but not in the constructor fields
jniPassCounter++
val tempArray = Assertions.assertNotNull(importTypeArray())
Arrays.copyOf(tempArray, tempArray.size, Array<ReadableType>::class.java)
}

private external fun importTypeArray(): Array<Any>

override fun size(): Int = localArray.size

override fun isNull(index: Int): Boolean = localArray[index] == null

override fun getBoolean(index: Int): Boolean = localArray[index] as Boolean

override fun getDouble(index: Int): Double = localArray[index] as Double

override fun getInt(index: Int): Int = (localArray[index] as Double).toInt()

override fun getLong(index: Int): Long = localArray[index] as Long

override fun getString(index: Int): String = localArray[index] as String

override fun getArray(index: Int): ReadableNativeArray = localArray[index] as ReadableNativeArray

override fun getMap(index: Int): ReadableNativeMap = localArray[index] as ReadableNativeMap

override fun getType(index: Int): ReadableType = localTypeArray[index]

override fun getDynamic(index: Int): Dynamic = DynamicFromArray.create(this, index)

override fun hashCode(): Int = localArray.hashCode()

override fun equals(other: Any?): Boolean =
if (other !is ReadableNativeArray) false else Arrays.deepEquals(localArray, other.localArray)

override fun toArrayList(): ArrayList<Any?> {
val arrayList = ArrayList<Any?>()
for (i in 0 until size()) {
when (getType(i)) {
ReadableType.Null -> arrayList.add(null)
ReadableType.Boolean -> arrayList.add(getBoolean(i))
ReadableType.Number -> arrayList.add(getDouble(i))
ReadableType.String -> arrayList.add(getString(i))
ReadableType.Map -> arrayList.add(getMap(i).toHashMap())
ReadableType.Array -> arrayList.add(getArray(i).toArrayList())
else -> throw IllegalArgumentException("Could not convert object at index: $i.")
}
}
return arrayList
}

public companion object {
init {
ReactBridge.staticInit()
}

public var jniPassCounter: Int = 0
private set
}
}
Loading

0 comments on commit e60cd87

Please sign in to comment.