Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,9 @@ public abstract interface class com/facebook/react/common/mapbuffer/MapBuffer :
public abstract fun getBoolean (I)Z
public abstract fun getCount ()I
public abstract fun getDouble (I)D
public abstract fun getDoubleBuffer (I)[D
public abstract fun getInt (I)I
public abstract fun getIntBuffer (I)[I
public abstract fun getKeyOffset (I)I
public abstract fun getLong (I)J
public abstract fun getMapBuffer (I)Lcom/facebook/react/common/mapbuffer/MapBuffer;
Expand All @@ -1680,9 +1682,12 @@ public final class com/facebook/react/common/mapbuffer/MapBuffer$Companion {
public final class com/facebook/react/common/mapbuffer/MapBuffer$DataType : java/lang/Enum {
public static final field BOOL Lcom/facebook/react/common/mapbuffer/MapBuffer$DataType;
public static final field DOUBLE Lcom/facebook/react/common/mapbuffer/MapBuffer$DataType;
public static final field DOUBLE_BUFFER Lcom/facebook/react/common/mapbuffer/MapBuffer$DataType;
public static final field INT Lcom/facebook/react/common/mapbuffer/MapBuffer$DataType;
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;
Expand All @@ -1691,10 +1696,13 @@ public final class com/facebook/react/common/mapbuffer/MapBuffer$DataType : java

public abstract interface class com/facebook/react/common/mapbuffer/MapBuffer$Entry {
public abstract fun getBooleanValue ()Z
public abstract fun getDoubleBufferValue ()[D
public abstract fun getDoubleValue ()D
public abstract fun getIntBufferValue ()[I
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;
Expand All @@ -1708,7 +1716,9 @@ public final class com/facebook/react/common/mapbuffer/ReadableMapBuffer : com/f
public fun getBoolean (I)Z
public fun getCount ()I
public fun getDouble (I)D
public fun getDoubleBuffer (I)[D
public fun getInt (I)I
public fun getIntBuffer (I)[I
public fun getKeyOffset (I)I
public fun getLong (I)J
public synthetic fun getMapBuffer (I)Lcom/facebook/react/common/mapbuffer/MapBuffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public interface MapBuffer : Iterable<MapBuffer.Entry> {
STRING,
MAP,
LONG,
INT_BUFFER,
DOUBLE_BUFFER,
MAP_BUFFER_LIST,
}

/**
Expand Down Expand Up @@ -151,8 +154,8 @@ public interface MapBuffer : Iterable<MapBuffer.Entry> {
public fun getMapBuffer(key: Int): MapBuffer

/**
* Provides parsed [List<MapBuffer>] value if the entry for given key exists with [DataType.MAP]
* type
* Provides parsed [List<MapBuffer>] value if the entry for given key exists with
* [DataType.MAP_BUFFER_LIST] type
*
* @param key key to lookup [List<MapBuffer>] value for
* @return value associated with the requested key
Expand All @@ -161,6 +164,30 @@ public interface MapBuffer : Iterable<MapBuffer.Entry> {
*/
public fun getMapBufferList(key: Int): List<MapBuffer>

/**
* Provides parsed [IntArray] value if the entry for given key exists with [DataType.INT_BUFFER]
* type. This is a compact representation of a homogeneous list of ints with no per-element
* key/type overhead.
*
* @param key key to lookup the [IntArray] value for
* @return value associated with the requested key
* @throws IllegalArgumentException if the key doesn't exist
* @throws IllegalStateException if the data type doesn't match
*/
public fun getIntBuffer(key: Int): IntArray

/**
* Provides parsed [DoubleArray] value if the entry for given key exists with
* [DataType.DOUBLE_BUFFER] type. This is a compact representation of a homogeneous list of
* doubles with no per-element key/type overhead.
*
* @param key key to lookup the [DoubleArray] value for
* @return value associated with the requested key
* @throws IllegalArgumentException if the key doesn't exist
* @throws IllegalStateException if the data type doesn't match
*/
public fun getDoubleBuffer(key: Int): DoubleArray

/** Iterable entry representing parsed MapBuffer values */
public interface Entry {
/**
Expand Down Expand Up @@ -213,5 +240,26 @@ public interface MapBuffer : Iterable<MapBuffer.Entry> {
* @throws IllegalStateException if the data type doesn't match [DataType.MAP]
*/
public val mapBufferValue: MapBuffer

/**
* Entry value represented as [IntArray]
*
* @throws IllegalStateException if the data type doesn't match [DataType.INT_BUFFER]
*/
public val intBufferValue: IntArray

/**
* Entry value represented as [DoubleArray]
*
* @throws IllegalStateException if the data type doesn't match [DataType.DOUBLE_BUFFER]
*/
public val doubleBufferValue: DoubleArray

/**
* Entry value represented as [List<MapBuffer>]
*
* @throws IllegalStateException if the data type doesn't match [DataType.MAP_BUFFER_LIST]
*/
public val mapBufferListValue: List<MapBuffer>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ private constructor(
ReadableMapBuffer(buffer.duplicate().apply { position(offset) }, offset)

private fun readHeader() {
// byte order
val storedAlignment = buffer.short
if (storedAlignment.toInt() != ALIGNMENT) {
buffer.order(ByteOrder.LITTLE_ENDIAN)
}
// count
count = readUnsignedShort(buffer.position()).toInt()
// The C++ writer always serializes in little-endian byte order. ByteBuffer
// defaults to big-endian and duplicate() resets the order, so set it
// explicitly on every instance, including nested clones.
buffer.order(ByteOrder.LITTLE_ENDIAN)
count = readUnsignedShort(offsetToMapBuffer).toInt()
}

/**
Expand Down Expand Up @@ -122,26 +120,27 @@ private constructor(
return readIntValue(bufferPosition) == 1
}

// Dynamic-data entries store [offset][byteLength] in the bucket's 8-byte
// value: getInt(bufferPosition) is the offset, getInt(bufferPosition + 4) is
// the byte length. The dynamic data section itself carries no length prefix.
private fun readStringValue(bufferPosition: Int): String {
val offset = offsetForDynamicData + buffer.getInt(bufferPosition)
val sizeOfString = buffer.getInt(offset)
val sizeOfString = buffer.getInt(bufferPosition + Int.SIZE_BYTES)
val result = ByteArray(sizeOfString)
val stringOffset = offset + Int.SIZE_BYTES
buffer.position(stringOffset)
buffer[result, 0, sizeOfString]
buffer.position(offset)
buffer.get(result, 0, sizeOfString)
return String(result)
}

private fun readMapBufferValue(position: Int): ReadableMapBuffer {
val offset = offsetForDynamicData + buffer.getInt(position)
return cloneWithOffset(offset + Int.SIZE_BYTES)
return cloneWithOffset(offset)
}

private fun readMapBufferListValue(position: Int): List<ReadableMapBuffer> {
val readMapBufferList = arrayListOf<ReadableMapBuffer>()
var offset = offsetForDynamicData + buffer.getInt(position)
val sizeMapBufferList = buffer.getInt(offset)
offset += Int.SIZE_BYTES
val offset = offsetForDynamicData + buffer.getInt(position)
val sizeMapBufferList = buffer.getInt(position + Int.SIZE_BYTES)
var curLen = 0
while (curLen < sizeMapBufferList) {
val sizeMapBuffer = buffer.getInt(offset + curLen)
Expand All @@ -152,6 +151,20 @@ private constructor(
return readMapBufferList
}

private fun readIntBufferValue(bufferPosition: Int): IntArray {
val offset = offsetForDynamicData + buffer.getInt(bufferPosition)
val byteLength = buffer.getInt(bufferPosition + Int.SIZE_BYTES)
val count = byteLength / Int.SIZE_BYTES
return IntArray(count) { i -> buffer.getInt(offset + i * Int.SIZE_BYTES) }
}

private fun readDoubleBufferValue(bufferPosition: Int): DoubleArray {
val offset = offsetForDynamicData + buffer.getInt(bufferPosition)
val byteLength = buffer.getInt(bufferPosition + Int.SIZE_BYTES)
val count = byteLength / Double.SIZE_BYTES
return DoubleArray(count) { i -> buffer.getDouble(offset + i * Double.SIZE_BYTES) }
}

private fun getKeyOffsetForBucketIndex(bucketIndex: Int): Int {
return offsetToMapBuffer + HEADER_SIZE + BUCKET_SIZE * bucketIndex
}
Expand Down Expand Up @@ -191,7 +204,13 @@ private constructor(
readMapBufferValue(getTypedValueOffsetForKey(key, MapBuffer.DataType.MAP))

override fun getMapBufferList(key: Int): List<ReadableMapBuffer> =
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))

override fun getDoubleBuffer(key: Int): DoubleArray =
readDoubleBufferValue(getTypedValueOffsetForKey(key, MapBuffer.DataType.DOUBLE_BUFFER))

override fun hashCode(): Int {
buffer.rewind()
Expand Down Expand Up @@ -229,6 +248,9 @@ private constructor(
append('"')
}
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())
}
}
}
Expand Down Expand Up @@ -311,16 +333,31 @@ private constructor(
assertType(MapBuffer.DataType.MAP)
return readMapBufferValue(bucketOffset + VALUE_OFFSET)
}

override val intBufferValue: IntArray
get() {
assertType(MapBuffer.DataType.INT_BUFFER)
return readIntBufferValue(bucketOffset + VALUE_OFFSET)
}

override val doubleBufferValue: DoubleArray
get() {
assertType(MapBuffer.DataType.DOUBLE_BUFFER)
return readDoubleBufferValue(bucketOffset + VALUE_OFFSET)
}

override val mapBufferListValue: List<MapBuffer>
get() {
assertType(MapBuffer.DataType.MAP_BUFFER_LIST)
return readMapBufferListValue(bucketOffset + VALUE_OFFSET)
}
}

public companion object {
// Value used to verify if the data is serialized with LittleEndian order.
private const val ALIGNMENT = 0xFE

// 8 bytes = 2 (alignment) + 2 (count) + 4 (size)
private const val HEADER_SIZE = 8
// 2 bytes = 2 (count)
private const val HEADER_SIZE = 2

// 10 bytes = 2 (key) + 2 (type) + 8 (value)
// 12 bytes = 2 (key) + 2 (type) + 8 (value)
private const val BUCKET_SIZE = 12

// 2 bytes = 2 (key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ internal class WritableMapBuffer : MapBuffer {

override fun getMapBufferList(key: Int): List<MapBuffer> = verifyValue(key, values.get(key))

override fun getIntBuffer(key: Int): IntArray = verifyValue(key, values.get(key))

override fun getDoubleBuffer(key: Int): DoubleArray = verifyValue(key, values.get(key))

/** Generalizes verification of the value types based on the requested type. */
private inline fun <reified T> verifyValue(key: Int, value: Any?): T {
require(value != null) { "Key not found: $key" }
Expand Down Expand Up @@ -176,6 +180,15 @@ internal class WritableMapBuffer : MapBuffer {

override val mapBufferValue: MapBuffer
get() = verifyValue(key, values.valueAt(index))

override val intBufferValue: IntArray
get() = verifyValue(key, values.valueAt(index))

override val doubleBufferValue: DoubleArray
get() = verifyValue(key, values.valueAt(index))

override val mapBufferListValue: List<MapBuffer>
get() = verifyValue(key, values.valueAt(index))
}

/*
Expand Down
Loading
Loading