diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index a9041187ae309d..b3c8cd59907944 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -1411,7 +1411,7 @@ public class com/facebook/react/bridge/ReadableNativeMap : com/facebook/react/br public fun getDynamic (Ljava/lang/String;)Lcom/facebook/react/bridge/Dynamic; public fun getEntryIterator ()Ljava/util/Iterator; public fun getInt (Ljava/lang/String;)I - public static fun getJNIPassCounter ()I + public static final fun getJNIPassCounter ()I public fun getLong (Ljava/lang/String;)J public synthetic fun getMap (Ljava/lang/String;)Lcom/facebook/react/bridge/ReadableMap; public fun getMap (Ljava/lang/String;)Lcom/facebook/react/bridge/ReadableNativeMap; @@ -1539,7 +1539,7 @@ public final class com/facebook/react/bridge/WritableNativeArray : com/facebook/ public fun pushString (Ljava/lang/String;)V } -public class com/facebook/react/bridge/WritableNativeMap : com/facebook/react/bridge/ReadableNativeMap, com/facebook/react/bridge/WritableMap { +public final class com/facebook/react/bridge/WritableNativeMap : com/facebook/react/bridge/ReadableNativeMap, com/facebook/react/bridge/WritableMap { public fun ()V public fun copy ()Lcom/facebook/react/bridge/WritableMap; public fun merge (Lcom/facebook/react/bridge/ReadableMap;)V @@ -5979,140 +5979,6 @@ public final class com/facebook/react/util/RNLog { public static final fun w (Lcom/facebook/react/bridge/ReactContext;Ljava/lang/String;)V } -public class com/facebook/react/viewmanagers/ActivityIndicatorViewManagerDelegate : com/facebook/react/uimanager/BaseViewManagerDelegate { - public fun (Lcom/facebook/react/uimanager/BaseViewManagerInterface;)V - public fun setProperty (Landroid/view/View;Ljava/lang/String;Ljava/lang/Object;)V -} - -public abstract interface class com/facebook/react/viewmanagers/ActivityIndicatorViewManagerInterface { - public abstract fun setAnimating (Landroid/view/View;Z)V - public abstract fun setColor (Landroid/view/View;Ljava/lang/Integer;)V - public abstract fun setHidesWhenStopped (Landroid/view/View;Z)V - public abstract fun setSize (Landroid/view/View;Ljava/lang/String;)V -} - -public class com/facebook/react/viewmanagers/AndroidDrawerLayoutManagerDelegate : com/facebook/react/uimanager/BaseViewManagerDelegate { - public fun (Lcom/facebook/react/uimanager/BaseViewManagerInterface;)V - public fun receiveCommand (Landroid/view/View;Ljava/lang/String;Lcom/facebook/react/bridge/ReadableArray;)V - public fun setProperty (Landroid/view/View;Ljava/lang/String;Ljava/lang/Object;)V -} - -public abstract interface class com/facebook/react/viewmanagers/AndroidDrawerLayoutManagerInterface { - public abstract fun closeDrawer (Landroid/view/View;)V - public abstract fun openDrawer (Landroid/view/View;)V - public abstract fun setDrawerBackgroundColor (Landroid/view/View;Ljava/lang/Integer;)V - public abstract fun setDrawerLockMode (Landroid/view/View;Ljava/lang/String;)V - public abstract fun setDrawerPosition (Landroid/view/View;Ljava/lang/String;)V - public abstract fun setDrawerWidth (Landroid/view/View;Ljava/lang/Float;)V - public abstract fun setKeyboardDismissMode (Landroid/view/View;Ljava/lang/String;)V - public abstract fun setStatusBarBackgroundColor (Landroid/view/View;Ljava/lang/Integer;)V -} - -public class com/facebook/react/viewmanagers/AndroidHorizontalScrollContentViewManagerDelegate : com/facebook/react/uimanager/BaseViewManagerDelegate { - public fun (Lcom/facebook/react/uimanager/BaseViewManagerInterface;)V - public fun setProperty (Landroid/view/View;Ljava/lang/String;Ljava/lang/Object;)V -} - -public abstract interface class com/facebook/react/viewmanagers/AndroidHorizontalScrollContentViewManagerInterface { - public abstract fun setRemoveClippedSubviews (Landroid/view/View;Z)V -} - -public class com/facebook/react/viewmanagers/AndroidProgressBarManagerDelegate : com/facebook/react/uimanager/BaseViewManagerDelegate { - public fun (Lcom/facebook/react/uimanager/BaseViewManagerInterface;)V - public fun setProperty (Landroid/view/View;Ljava/lang/String;Ljava/lang/Object;)V -} - -public abstract interface class com/facebook/react/viewmanagers/AndroidProgressBarManagerInterface { - public abstract fun setAnimating (Landroid/view/View;Z)V - public abstract fun setColor (Landroid/view/View;Ljava/lang/Integer;)V - public abstract fun setIndeterminate (Landroid/view/View;Z)V - public abstract fun setProgress (Landroid/view/View;D)V - public abstract fun setStyleAttr (Landroid/view/View;Ljava/lang/String;)V - public abstract fun setTestID (Landroid/view/View;Ljava/lang/String;)V - public abstract fun setTypeAttr (Landroid/view/View;Ljava/lang/String;)V -} - -public class com/facebook/react/viewmanagers/AndroidSwipeRefreshLayoutManagerDelegate : com/facebook/react/uimanager/BaseViewManagerDelegate { - public fun (Lcom/facebook/react/uimanager/BaseViewManagerInterface;)V - public fun receiveCommand (Landroid/view/View;Ljava/lang/String;Lcom/facebook/react/bridge/ReadableArray;)V - public fun setProperty (Landroid/view/View;Ljava/lang/String;Ljava/lang/Object;)V -} - -public abstract interface class com/facebook/react/viewmanagers/AndroidSwipeRefreshLayoutManagerInterface { - public abstract fun setColors (Landroid/view/View;Lcom/facebook/react/bridge/ReadableArray;)V - public abstract fun setEnabled (Landroid/view/View;Z)V - public abstract fun setNativeRefreshing (Landroid/view/View;Z)V - public abstract fun setProgressBackgroundColor (Landroid/view/View;Ljava/lang/Integer;)V - public abstract fun setProgressViewOffset (Landroid/view/View;F)V - public abstract fun setRefreshing (Landroid/view/View;Z)V - public abstract fun setSize (Landroid/view/View;Ljava/lang/String;)V -} - -public class com/facebook/react/viewmanagers/AndroidSwitchManagerDelegate : com/facebook/react/uimanager/BaseViewManagerDelegate { - public fun (Lcom/facebook/react/uimanager/BaseViewManagerInterface;)V - public fun receiveCommand (Landroid/view/View;Ljava/lang/String;Lcom/facebook/react/bridge/ReadableArray;)V - public fun setProperty (Landroid/view/View;Ljava/lang/String;Ljava/lang/Object;)V -} - -public abstract interface class com/facebook/react/viewmanagers/AndroidSwitchManagerInterface { - public abstract fun setDisabled (Landroid/view/View;Z)V - public abstract fun setEnabled (Landroid/view/View;Z)V - public abstract fun setNativeValue (Landroid/view/View;Z)V - public abstract fun setOn (Landroid/view/View;Z)V - public abstract fun setThumbColor (Landroid/view/View;Ljava/lang/Integer;)V - public abstract fun setThumbTintColor (Landroid/view/View;Ljava/lang/Integer;)V - public abstract fun setTrackColorForFalse (Landroid/view/View;Ljava/lang/Integer;)V - public abstract fun setTrackColorForTrue (Landroid/view/View;Ljava/lang/Integer;)V - public abstract fun setTrackTintColor (Landroid/view/View;Ljava/lang/Integer;)V - public abstract fun setValue (Landroid/view/View;Z)V -} - -public class com/facebook/react/viewmanagers/DebuggingOverlayManagerDelegate : com/facebook/react/uimanager/BaseViewManagerDelegate { - public fun (Lcom/facebook/react/uimanager/BaseViewManagerInterface;)V - public fun receiveCommand (Landroid/view/View;Ljava/lang/String;Lcom/facebook/react/bridge/ReadableArray;)V - public fun setProperty (Landroid/view/View;Ljava/lang/String;Ljava/lang/Object;)V -} - -public abstract interface class com/facebook/react/viewmanagers/DebuggingOverlayManagerInterface { - public abstract fun clearElementsHighlights (Landroid/view/View;)V - public abstract fun highlightElements (Landroid/view/View;Lcom/facebook/react/bridge/ReadableArray;)V - public abstract fun highlightTraceUpdates (Landroid/view/View;Lcom/facebook/react/bridge/ReadableArray;)V -} - -public class com/facebook/react/viewmanagers/ModalHostViewManagerDelegate : com/facebook/react/uimanager/BaseViewManagerDelegate { - public fun (Lcom/facebook/react/uimanager/BaseViewManagerInterface;)V - public fun setProperty (Landroid/view/View;Ljava/lang/String;Ljava/lang/Object;)V -} - -public abstract interface class com/facebook/react/viewmanagers/ModalHostViewManagerInterface { - public abstract fun setAnimated (Landroid/view/View;Z)V - public abstract fun setAnimationType (Landroid/view/View;Ljava/lang/String;)V - public abstract fun setHardwareAccelerated (Landroid/view/View;Z)V - public abstract fun setIdentifier (Landroid/view/View;I)V - public abstract fun setPresentationStyle (Landroid/view/View;Ljava/lang/String;)V - public abstract fun setStatusBarTranslucent (Landroid/view/View;Z)V - public abstract fun setSupportedOrientations (Landroid/view/View;Lcom/facebook/react/bridge/ReadableArray;)V - public abstract fun setTransparent (Landroid/view/View;Z)V - public abstract fun setVisible (Landroid/view/View;Z)V -} - -public class com/facebook/react/viewmanagers/SafeAreaViewManagerDelegate : com/facebook/react/uimanager/BaseViewManagerDelegate { - public fun (Lcom/facebook/react/uimanager/BaseViewManagerInterface;)V - public fun setProperty (Landroid/view/View;Ljava/lang/String;Ljava/lang/Object;)V -} - -public abstract interface class com/facebook/react/viewmanagers/SafeAreaViewManagerInterface { -} - -public class com/facebook/react/viewmanagers/UnimplementedNativeViewManagerDelegate : com/facebook/react/uimanager/BaseViewManagerDelegate { - public fun (Lcom/facebook/react/uimanager/BaseViewManagerInterface;)V - public fun setProperty (Landroid/view/View;Ljava/lang/String;Ljava/lang/Object;)V -} - -public abstract interface class com/facebook/react/viewmanagers/UnimplementedNativeViewManagerInterface { - public abstract fun setName (Landroid/view/View;Ljava/lang/String;)V -} - public final class com/facebook/react/views/common/ContextUtils { public static final field INSTANCE Lcom/facebook/react/views/common/ContextUtils; public static final fun findContextOfType (Landroid/content/Context;Ljava/lang/Class;)Ljava/lang/Object; diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeMap.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeMap.java deleted file mode 100644 index fdf9504f37de50..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeMap.java +++ /dev/null @@ -1,28 +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; - -/** Base class for a Map whose keys and values are stored in native code (C++). */ -@DoNotStrip -public abstract class NativeMap { - static { - ReactBridge.staticInit(); - } - - public NativeMap(HybridData hybridData) { - mHybridData = hybridData; - } - - @Override - public native String toString(); - - @DoNotStrip private HybridData mHybridData; -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeMap.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeMap.kt new file mode 100644 index 00000000000000..0400ed715dcd03 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeMap.kt @@ -0,0 +1,23 @@ +/* + * 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 a Map whose keys and values are stored in native code (C++). */ +@DoNotStrip +public abstract class NativeMap(@field:DoNotStrip private val mHybridData: HybridData?) { + external override fun toString(): String + + private companion object { + init { + ReactBridge.staticInit() + } + } +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java deleted file mode 100644 index 7e796f7cdaf7f5..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java +++ /dev/null @@ -1,298 +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 androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import com.facebook.infer.annotation.Assertions; -import com.facebook.jni.HybridData; -import com.facebook.proguard.annotations.DoNotStrip; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -/** - * Implementation of a read-only map in native memory. This will generally be constructed and filled - * in native code so you shouldn't construct one yourself. - */ -@DoNotStrip -public class ReadableNativeMap extends NativeMap implements ReadableMap { - static { - ReactBridge.staticInit(); - } - - protected ReadableNativeMap(HybridData hybridData) { - super(hybridData); - } - - private @Nullable String[] mKeys; - private @Nullable HashMap mLocalMap; - private @Nullable HashMap mLocalTypeMap; - private static int mJniCallCounter; - - public static int getJNIPassCounter() { - return mJniCallCounter; - } - - private void ensureKeysAreImported() { - if (mKeys != null) { - return; - } - synchronized (this) { - mKeys = Assertions.assertNotNull(importKeys()); - mJniCallCounter++; - } - } - - private HashMap getLocalMap() { - if (mLocalMap != null) { - return mLocalMap; - } - synchronized (this) { - ensureKeysAreImported(); - if (mLocalMap == null) { - Object[] values = Assertions.assertNotNull(importValues()); - mJniCallCounter++; - int length = mKeys.length; - mLocalMap = new HashMap<>(length); - for (int i = 0; i < length; i++) { - mLocalMap.put(mKeys[i], values[i]); - } - } - } - return mLocalMap; - } - - private native String[] importKeys(); - - private native Object[] importValues(); - - private @NonNull HashMap getLocalTypeMap() { - if (mLocalTypeMap != null) { - return mLocalTypeMap; - } - synchronized (this) { - ensureKeysAreImported(); - if (mLocalTypeMap == null) { - Object[] types = Assertions.assertNotNull(importTypes()); - mJniCallCounter++; - int length = mKeys.length; - mLocalTypeMap = new HashMap<>(length); - for (int i = 0; i < length; i++) { - mLocalTypeMap.put(mKeys[i], (ReadableType) types[i]); - } - } - } - return mLocalTypeMap; - } - - private native Object[] importTypes(); - - @Override - public boolean hasKey(@NonNull String name) { - return getLocalMap().containsKey(name); - } - - @Override - public boolean isNull(@NonNull String name) { - if (getLocalMap().containsKey(name)) { - return getLocalMap().get(name) == null; - } - throw new NoSuchKeyException(name); - } - - private @NonNull Object getValue(@NonNull String name) { - if (hasKey(name) && !(isNull(name))) { - return Assertions.assertNotNull(getLocalMap().get(name)); - } - throw new NoSuchKeyException(name); - } - - private T getValue(String name, Class type) { - Object value = getValue(name); - checkInstance(name, value, type); - return (T) value; - } - - private @Nullable Object getNullableValue(String name) { - if (hasKey(name)) { - return getLocalMap().get(name); - } - return null; - } - - private @Nullable T getNullableValue(String name, Class type) { - Object value = getNullableValue(name); - checkInstance(name, value, type); - return (T) value; - } - - private void checkInstance(String name, Object value, Class type) { - if (value != null && !type.isInstance(value)) { - throw new UnexpectedNativeTypeException( - "Value for " - + name - + " cannot be cast from " - + value.getClass().getSimpleName() - + " to " - + type.getSimpleName()); - } - } - - @Override - public boolean getBoolean(@NonNull String name) { - return getValue(name, Boolean.class).booleanValue(); - } - - @Override - public double getDouble(@NonNull String name) { - return getValue(name, Double.class).doubleValue(); - } - - @Override - public int getInt(@NonNull String name) { - // All numbers coming out of native are doubles, so cast here then truncate - return getValue(name, Double.class).intValue(); - } - - @Override - public long getLong(@NonNull String name) { - return getValue(name, Long.class).longValue(); - } - - @Override - public @Nullable String getString(@NonNull String name) { - return getNullableValue(name, String.class); - } - - @Override - public @Nullable ReadableArray getArray(@NonNull String name) { - return getNullableValue(name, ReadableArray.class); - } - - @Override - public @Nullable ReadableNativeMap getMap(@NonNull String name) { - return getNullableValue(name, ReadableNativeMap.class); - } - - @Override - public @NonNull ReadableType getType(@NonNull String name) { - if (getLocalTypeMap().containsKey(name)) { - return Assertions.assertNotNull(getLocalTypeMap().get(name)); - } - throw new NoSuchKeyException(name); - } - - @Override - public @NonNull Dynamic getDynamic(@NonNull String name) { - return DynamicFromMap.create(this, name); - } - - @Override - public @NonNull Iterator> getEntryIterator() { - synchronized (this) { - ensureKeysAreImported(); - - final String[] iteratorKeys = mKeys; - final Object[] iteratorValues = Assertions.assertNotNull(importValues()); - mJniCallCounter++; - - return new Iterator>() { - int currentIndex = 0; - - @Override - public boolean hasNext() { - return currentIndex < iteratorKeys.length; - } - - @Override - public Map.Entry next() { - final int index = currentIndex++; - return new Map.Entry() { - @Override - public String getKey() { - return iteratorKeys[index]; - } - - @Override - public Object getValue() { - return iteratorValues[index]; - } - - @Override - public Object setValue(Object value) { - throw new UnsupportedOperationException( - "Can't set a value while iterating over a ReadableNativeMap"); - } - }; - } - }; - } - } - - @Override - public @NonNull ReadableMapKeySetIterator keySetIterator() { - ensureKeysAreImported(); - final String[] iteratorKeys = mKeys; - return new ReadableMapKeySetIterator() { - int currentIndex = 0; - - @Override - public boolean hasNextKey() { - return currentIndex < iteratorKeys.length; - } - - @Override - public String nextKey() { - return iteratorKeys[currentIndex++]; - } - }; - } - - @Override - public int hashCode() { - return getLocalMap().hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof ReadableNativeMap)) { - return false; - } - ReadableNativeMap other = (ReadableNativeMap) obj; - return getLocalMap().equals(other.getLocalMap()); - } - - @Override - public @NonNull HashMap toHashMap() { - // we can almost just return getLocalMap(), but we need to convert nested arrays and maps to the - // correct types first - HashMap hashMap = new HashMap<>(getLocalMap()); - Iterator iterator = hashMap.keySet().iterator(); - - while (iterator.hasNext()) { - String key = (String) iterator.next(); - switch (getType(key)) { - case Null: - case Boolean: - case Number: - case String: - break; - case Map: - hashMap.put(key, Assertions.assertNotNull(getMap(key)).toHashMap()); - break; - case Array: - hashMap.put(key, Assertions.assertNotNull(getArray(key)).toArrayList()); - break; - default: - throw new IllegalArgumentException("Could not convert object with key: " + key + "."); - } - } - return hashMap; - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.kt new file mode 100644 index 00000000000000..6c1b1619e8ca7d --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.kt @@ -0,0 +1,195 @@ +/* + * 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 android.annotation.SuppressLint +import com.facebook.infer.annotation.Assertions +import com.facebook.jni.HybridData +import com.facebook.proguard.annotations.DoNotStripAny + +/** + * Implementation of a read-only map in native memory. This will generally be constructed and filled + * in native code so you shouldn't construct one yourself. + */ +@DoNotStripAny +public open class ReadableNativeMap protected constructor(hybridData: HybridData?) : + NativeMap(hybridData), ReadableMap { + private val keys: Array by + lazy(LazyThreadSafetyMode.SYNCHRONIZED) { importKeys().also { jniPassCounter++ } } + + private val localMap: HashMap by + lazy(LazyThreadSafetyMode.SYNCHRONIZED) { + val length = keys.size + val res = HashMap(length) + val values = importValues() + for (i in 0 until length) { + res[keys[i]] = values[i] + } + jniPassCounter++ + res + } + + private val localTypeMap: HashMap by + lazy(LazyThreadSafetyMode.SYNCHRONIZED) { + val length = keys.size + val res = HashMap(length) + val types = importTypes() + for (i in 0 until length) { + res[keys[i]] = types[i] as ReadableType + } + jniPassCounter++ + res + } + + private external fun importKeys(): Array + + private external fun importValues(): Array + + private external fun importTypes(): Array + + override fun hasKey(name: String): Boolean = localMap.containsKey(name) + + override fun isNull(name: String): Boolean { + if (localMap.containsKey(name)) { + return localMap[name] == null + } + throw NoSuchKeyException(name) + } + + @SuppressLint("ReflectionMethodUse") + private inline fun checkInstance(name: String, instance: Any?, type: Class): T = + instance as? T + ?: throw UnexpectedNativeTypeException( + "Value for $name cannot be cast from ${instance?.javaClass?.simpleName ?: "NULL"} to ${type.simpleName}") + + private fun getValue(name: String): Any { + if (hasKey(name)) { + return Assertions.assertNotNull(localMap[name]) + } + throw NoSuchKeyException(name) + } + + private inline fun getValue(name: String, type: Class): T = + checkInstance(name, getValue(name), type) + + private fun getNullableValue(name: String): Any? = localMap.get(name) + + private inline fun getNullableValue(name: String, type: Class): T? { + val res = getNullableValue(name) + if (res == null) { + return null + } else { + return checkInstance(name, res, type) + } + } + + override fun getBoolean(name: String): Boolean = getValue(name, Boolean::class.java) + + override fun getDouble(name: String): Double = getValue(name, Double::class.java) + + // All numbers coming out of native are doubles, so cast here then truncate + override fun getInt(name: String): Int = getValue(name, Double::class.java).toInt() + + override fun getLong(name: String): Long = getValue(name, Long::class.java) + + override fun getString(name: String): String? = getNullableValue(name, String::class.java) + + override fun getArray(name: String): ReadableArray? = + getNullableValue(name, ReadableArray::class.java) + + override fun getMap(name: String): ReadableNativeMap? = + getNullableValue(name, ReadableNativeMap::class.java) + + override fun getType(name: String): ReadableType { + if (localTypeMap.containsKey(name)) { + return Assertions.assertNotNull(localTypeMap[name]) + } + throw NoSuchKeyException(name) + } + + override fun getDynamic(name: String): Dynamic = DynamicFromMap.create(this, name) + + override fun getEntryIterator(): Iterator> { + synchronized(this) { + val iteratorKeys = keys + val iteratorValues = importValues() + jniPassCounter++ + return object : Iterator> { + var currentIndex = 0 + + override fun hasNext(): Boolean { + return currentIndex < iteratorKeys.size + } + + override fun next(): Map.Entry { + val index = currentIndex++ + return object : MutableMap.MutableEntry { + override val key: String + get() = iteratorKeys[index] + + override val value: Any + get() = iteratorValues[index] + + override fun setValue(newValue: Any): Any { + throw UnsupportedOperationException( + "Can't set a value while iterating over a ReadableNativeMap") + } + } + } + } + } + } + + override fun keySetIterator(): ReadableMapKeySetIterator { + val iteratorKeys = keys + return object : ReadableMapKeySetIterator { + var currentIndex = 0 + + override fun hasNextKey(): Boolean = currentIndex < iteratorKeys.size + + override fun nextKey(): String = iteratorKeys[currentIndex++] + } + } + + override fun hashCode(): Int = localMap.hashCode() + + override fun equals(other: Any?): Boolean = + if (other !is ReadableNativeMap) { + false + } else localMap == other.localMap + + override fun toHashMap(): HashMap { + // we can almost just return getLocalMap(), but we need to convert nested arrays and maps to the + // correct types first + val hashMap = HashMap(localMap) + val iterator: Iterator<*> = hashMap.keys.iterator() + while (iterator.hasNext()) { + val key = iterator.next() as String + when (getType(key)) { + ReadableType.Null, + ReadableType.Boolean, + ReadableType.Number, + ReadableType.String -> {} + ReadableType.Map -> hashMap[key] = Assertions.assertNotNull(getMap(key)).toHashMap() + ReadableType.Array -> hashMap[key] = Assertions.assertNotNull(getArray(key)).toArrayList() + else -> throw IllegalArgumentException("Could not convert object with key: $key.") + } + } + return hashMap + } + + private companion object { + init { + ReactBridge.staticInit() + } + + private var jniPassCounter: Int = 0 + + @JvmStatic public fun getJNIPassCounter(): Int = jniPassCounter + } +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/WritableNativeMap.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/WritableNativeMap.java deleted file mode 100644 index 228b3065de7a39..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/WritableNativeMap.java +++ /dev/null @@ -1,84 +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 androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import com.facebook.infer.annotation.Assertions; -import com.facebook.jni.HybridData; -import com.facebook.proguard.annotations.DoNotStrip; - -/** - * Implementation of a write-only map stored in native memory. Use {@link Arguments#createMap()} if - * you need to stub out creating this class in a test. TODO(5815532): Check if consumed on read - */ -@DoNotStrip -public class WritableNativeMap extends ReadableNativeMap implements WritableMap { - static { - ReactBridge.staticInit(); - } - - @Override - public native void putBoolean(@NonNull String key, boolean value); - - @Override - public native void putDouble(@NonNull String key, double value); - - @Override - public native void putInt(@NonNull String key, int value); - - @Override - public native void putLong(@NonNull String key, long value); - - @Override - public native void putNull(@NonNull String key); - - @Override - public native void putString(@NonNull String key, @Nullable String value); - - @Override - public void putMap(@NonNull String key, @Nullable ReadableMap value) { - Assertions.assertCondition( - value == null || value instanceof ReadableNativeMap, "Illegal type provided"); - putNativeMap(key, (ReadableNativeMap) value); - } - - // Note: this consumes the map so do not reuse it. - @Override - public void putArray(@NonNull String key, @Nullable ReadableArray value) { - Assertions.assertCondition( - value == null || value instanceof ReadableNativeArray, "Illegal type provided"); - putNativeArray(key, (ReadableNativeArray) value); - } - - // Note: this **DOES NOT** consume the source map - @Override - public void merge(@NonNull ReadableMap source) { - Assertions.assertCondition(source instanceof ReadableNativeMap, "Illegal type provided"); - mergeNativeMap((ReadableNativeMap) source); - } - - @Override - public WritableMap copy() { - final WritableNativeMap target = new WritableNativeMap(); - target.merge(this); - return target; - } - - public WritableNativeMap() { - super(initHybrid()); - } - - private static native HybridData initHybrid(); - - private native void putNativeMap(String key, ReadableNativeMap value); - - private native void putNativeArray(String key, ReadableNativeArray value); - - private native void mergeNativeMap(ReadableNativeMap source); -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/WritableNativeMap.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/WritableNativeMap.kt new file mode 100644 index 00000000000000..af90cae5eba8c1 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/WritableNativeMap.kt @@ -0,0 +1,69 @@ +/* + * 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.DoNotStrip + +/** + * Implementation of a write-only map stored in native memory. Use [Arguments.createMap] if you need + * to stub out creating this class in a test. TODO(5815532): Check if consumed on read + */ +@DoNotStrip +public class WritableNativeMap : ReadableNativeMap(initHybrid()), WritableMap { + external override fun putBoolean(key: String, value: Boolean) + + external override fun putDouble(key: String, value: Double) + + external override fun putInt(key: String, value: Int) + + external override fun putLong(key: String, value: Long) + + external override fun putNull(key: String) + + external override fun putString(key: String, value: String?) + + override fun putMap(key: String, value: ReadableMap?) { + Assertions.assertCondition(value == null || value is ReadableNativeMap, "Illegal type provided") + putNativeMap(key, value as ReadableNativeMap?) + } + + // Note: this consumes the map so do not reuse it. + override fun putArray(key: String, value: ReadableArray?) { + Assertions.assertCondition( + value == null || value is ReadableNativeArray, "Illegal type provided") + putNativeArray(key, value as ReadableNativeArray?) + } + + // Note: this **DOES NOT** consume the source map + override fun merge(source: ReadableMap) { + Assertions.assertCondition(source is ReadableNativeMap, "Illegal type provided") + mergeNativeMap(source as ReadableNativeMap) + } + + override fun copy(): WritableMap { + val target = WritableNativeMap() + target.merge(this) + return target + } + + private external fun putNativeMap(key: String, value: ReadableNativeMap?) + + private external fun putNativeArray(key: String, value: ReadableNativeArray?) + + private external fun mergeNativeMap(source: ReadableNativeMap) + + private companion object { + init { + ReactBridge.staticInit() + } + + @JvmStatic private external fun initHybrid(): HybridData? + } +}