From 6a957746f6a2fc280bd6b30724d4fe10b4841841 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Fri, 3 Jul 2026 01:41:41 -0700 Subject: [PATCH] Add MapBufferList entry type to MapBuffer (#57360) Summary: Pull Request resolved: https://github.com/react/react-native/pull/57360 Introduces a dedicated `MapBufferList` `DataType` for an ordered array of nested MapBuffers, instead of overloading the `Map` type for lists. This makes a list of MapBuffers self-describing and distinguishable from a single nested `Map` (they were byte-distinct in payload but previously shared the `Map` type tag). Updates the C++ builder (`putMapBufferList`), the Kotlin `MapBuffer` interface, `ReadableMapBuffer`, and `WritableMapBuffer`, and adds cross-language JNI round-trip coverage in the serialization instrumentation test. Changelog: [Android][Added] - Add a dedicated `MapBufferList` type to `MapBuffer` for ordered lists of nested `MapBuffer`s landed-with-radar-review Reviewed By: zeyap Differential Revision: D109848477 fbshipit-source-id: 7f590d5999d0cc4ee2d9d28cc34ae9220e442e18 --- .../react-native/ReactAndroid/api/ReactAndroid.api | 2 ++ .../com/facebook/react/common/mapbuffer/MapBuffer.kt | 12 ++++++++++-- .../react/common/mapbuffer/ReadableMapBuffer.kt | 9 ++++++++- .../react/common/mapbuffer/WritableMapBuffer.kt | 3 +++ .../ReactCommon/react/renderer/mapbuffer/MapBuffer.h | 4 ++++ .../react/renderer/mapbuffer/MapBufferBuilder.cpp | 5 +++-- .../cxx-api/api-snapshots/ReactAndroidDebugCxx.api | 1 + .../cxx-api/api-snapshots/ReactAndroidNewarchCxx.api | 1 + .../cxx-api/api-snapshots/ReactAndroidReleaseCxx.api | 1 + scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api | 1 + .../cxx-api/api-snapshots/ReactAppleNewarchCxx.api | 1 + .../cxx-api/api-snapshots/ReactAppleReleaseCxx.api | 1 + .../cxx-api/api-snapshots/ReactCommonDebugCxx.api | 1 + .../cxx-api/api-snapshots/ReactCommonNewarchCxx.api | 1 + .../cxx-api/api-snapshots/ReactCommonReleaseCxx.api | 1 + 15 files changed, 39 insertions(+), 5 deletions(-) diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 3eae29cea63..c4e2927d156 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -1687,6 +1687,7 @@ public final class com/facebook/react/common/mapbuffer/MapBuffer$DataType : java public static final field INT_BUFFER Lcom/facebook/react/common/mapbuffer/MapBuffer$DataType; public static final field LONG Lcom/facebook/react/common/mapbuffer/MapBuffer$DataType; public static final field MAP Lcom/facebook/react/common/mapbuffer/MapBuffer$DataType; + public static final field MAP_BUFFER_LIST Lcom/facebook/react/common/mapbuffer/MapBuffer$DataType; public static final field STRING Lcom/facebook/react/common/mapbuffer/MapBuffer$DataType; public static fun getEntries ()Lkotlin/enums/EnumEntries; public static fun valueOf (Ljava/lang/String;)Lcom/facebook/react/common/mapbuffer/MapBuffer$DataType; @@ -1701,6 +1702,7 @@ public abstract interface class com/facebook/react/common/mapbuffer/MapBuffer$En public abstract fun getIntValue ()I public abstract fun getKey ()I public abstract fun getLongValue ()J + public abstract fun getMapBufferListValue ()Ljava/util/List; public abstract fun getMapBufferValue ()Lcom/facebook/react/common/mapbuffer/MapBuffer; public abstract fun getStringValue ()Ljava/lang/String; public abstract fun getType ()Lcom/facebook/react/common/mapbuffer/MapBuffer$DataType; diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/MapBuffer.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/MapBuffer.kt index 17db9cd7a1d..feba0dc97d6 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/MapBuffer.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/MapBuffer.kt @@ -48,6 +48,7 @@ public interface MapBuffer : Iterable { LONG, INT_BUFFER, DOUBLE_BUFFER, + MAP_BUFFER_LIST, } /** @@ -153,8 +154,8 @@ public interface MapBuffer : Iterable { public fun getMapBuffer(key: Int): MapBuffer /** - * Provides parsed [List] value if the entry for given key exists with [DataType.MAP] - * type + * Provides parsed [List] value if the entry for given key exists with + * [DataType.MAP_BUFFER_LIST] type * * @param key key to lookup [List] value for * @return value associated with the requested key @@ -253,5 +254,12 @@ public interface MapBuffer : Iterable { * @throws IllegalStateException if the data type doesn't match [DataType.DOUBLE_BUFFER] */ public val doubleBufferValue: DoubleArray + + /** + * Entry value represented as [List] + * + * @throws IllegalStateException if the data type doesn't match [DataType.MAP_BUFFER_LIST] + */ + public val mapBufferListValue: List } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/ReadableMapBuffer.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/ReadableMapBuffer.kt index 35ed2959005..875873bfb0c 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/ReadableMapBuffer.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/ReadableMapBuffer.kt @@ -209,7 +209,7 @@ private constructor( readMapBufferValue(getTypedValueOffsetForKey(key, MapBuffer.DataType.MAP)) override fun getMapBufferList(key: Int): List = - readMapBufferListValue(getTypedValueOffsetForKey(key, MapBuffer.DataType.MAP)) + readMapBufferListValue(getTypedValueOffsetForKey(key, MapBuffer.DataType.MAP_BUFFER_LIST)) override fun getIntBuffer(key: Int): IntArray = readIntBufferValue(getTypedValueOffsetForKey(key, MapBuffer.DataType.INT_BUFFER)) @@ -255,6 +255,7 @@ private constructor( MapBuffer.DataType.MAP -> append(entry.mapBufferValue.toString()) MapBuffer.DataType.INT_BUFFER -> append(entry.intBufferValue.contentToString()) MapBuffer.DataType.DOUBLE_BUFFER -> append(entry.doubleBufferValue.contentToString()) + MapBuffer.DataType.MAP_BUFFER_LIST -> append(entry.mapBufferListValue.toString()) } } } @@ -349,6 +350,12 @@ private constructor( assertType(MapBuffer.DataType.DOUBLE_BUFFER) return readDoubleBufferValue(bucketOffset + VALUE_OFFSET) } + + override val mapBufferListValue: List + get() { + assertType(MapBuffer.DataType.MAP_BUFFER_LIST) + return readMapBufferListValue(bucketOffset + VALUE_OFFSET) + } } public companion object { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/WritableMapBuffer.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/WritableMapBuffer.kt index be6895b25fa..dea593ca6e7 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/WritableMapBuffer.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/WritableMapBuffer.kt @@ -206,6 +206,9 @@ internal class WritableMapBuffer : MapBuffer { override val doubleBufferValue: DoubleArray get() = verifyValue(key, values.valueAt(index)) + + override val mapBufferListValue: List + get() = verifyValue(key, values.valueAt(index)) } /* diff --git a/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBuffer.h b/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBuffer.h index 2d920af86e8..ae89b674b6a 100644 --- a/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBuffer.h +++ b/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBuffer.h @@ -112,6 +112,10 @@ class MapBuffer { // array within the dynamic data section. IntBuffer = 6, DoubleBuffer = 7, + // A homogeneous, ordered array of nested MapBuffers. Distinct from `Map` so + // that a list of MapBuffers is self-describing (a single Map and a list are + // byte-distinct in payload but previously shared the `Map` type tag). + MapBufferList = 8, }; explicit MapBuffer(std::vector data); diff --git a/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp b/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp index c0e77bdbb5d..8ee37c1c01e 100644 --- a/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp +++ b/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp @@ -153,10 +153,11 @@ void MapBufferBuilder::putMapBufferList( mapBufferSize); } - // Store Key and pointer to the string + // Store Key and pointer to the list. Uses the dedicated MapBufferList type so + // the entry is self-describing and distinguishable from a single Map. storeKeyValue( key, - MapBuffer::DataType::Map, + MapBuffer::DataType::MapBufferList, reinterpret_cast(&offset), INT_SIZE); } diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api index a3c11b134bd..8022b30807e 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api @@ -3264,6 +3264,7 @@ enum facebook::react::MapBuffer::DataType : uint16_t { IntBuffer, Long, Map, + MapBufferList, String, } diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api index ffe4f90614c..c241745f95f 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api @@ -3173,6 +3173,7 @@ enum facebook::react::MapBuffer::DataType : uint16_t { IntBuffer, Long, Map, + MapBufferList, String, } diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api index 82b7241990e..73beff563c8 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api @@ -3261,6 +3261,7 @@ enum facebook::react::MapBuffer::DataType : uint16_t { IntBuffer, Long, Map, + MapBufferList, String, } diff --git a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api index 8efdf4143c4..0522de96cd2 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api @@ -5499,6 +5499,7 @@ enum facebook::react::MapBuffer::DataType : uint16_t { IntBuffer, Long, Map, + MapBufferList, String, } diff --git a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api index 6c7799c8dc3..e961ff1cf98 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api @@ -5423,6 +5423,7 @@ enum facebook::react::MapBuffer::DataType : uint16_t { IntBuffer, Long, Map, + MapBufferList, String, } diff --git a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api index 276c824bec1..f5fd13c275c 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api @@ -5496,6 +5496,7 @@ enum facebook::react::MapBuffer::DataType : uint16_t { IntBuffer, Long, Map, + MapBufferList, String, } diff --git a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api index 676107ead62..5ec53a2f575 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api @@ -2185,6 +2185,7 @@ enum facebook::react::MapBuffer::DataType : uint16_t { IntBuffer, Long, Map, + MapBufferList, String, } diff --git a/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api index 69b7eb0ca5f..2b67e1c5b4f 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api @@ -2121,6 +2121,7 @@ enum facebook::react::MapBuffer::DataType : uint16_t { IntBuffer, Long, Map, + MapBufferList, String, } diff --git a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api index 5d77f4412ff..2f7f3c22e51 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api @@ -2182,6 +2182,7 @@ enum facebook::react::MapBuffer::DataType : uint16_t { IntBuffer, Long, Map, + MapBufferList, String, }