|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.common.mapbuffer |
| 9 | + |
| 10 | +import android.util.SparseArray |
| 11 | +import com.facebook.proguard.annotations.DoNotStrip |
| 12 | +import com.facebook.react.common.mapbuffer.MapBuffer.Companion.KEY_RANGE |
| 13 | +import com.facebook.react.common.mapbuffer.MapBuffer.DataType |
| 14 | +import javax.annotation.concurrent.NotThreadSafe |
| 15 | + |
| 16 | +/** |
| 17 | + * Implementation of writeable Java-only MapBuffer, which can be used to send information through |
| 18 | + * JNI. |
| 19 | + * |
| 20 | + * See [MapBuffer] for more details |
| 21 | + */ |
| 22 | +@NotThreadSafe |
| 23 | +@DoNotStrip |
| 24 | +class WritableMapBuffer : MapBuffer { |
| 25 | + private val values: SparseArray<Any> = SparseArray<Any>() |
| 26 | + |
| 27 | + /* |
| 28 | + * Write methods |
| 29 | + */ |
| 30 | + |
| 31 | + /** |
| 32 | + * Adds a boolean value for given key to the MapBuffer. |
| 33 | + * @param key entry key |
| 34 | + * @param value entry value |
| 35 | + * @throws IllegalArgumentException if key is out of [UShort] range |
| 36 | + */ |
| 37 | + fun put(key: Int, value: Boolean): WritableMapBuffer = putInternal(key, value) |
| 38 | + |
| 39 | + /** |
| 40 | + * Adds an int value for given key to the MapBuffer. |
| 41 | + * @param key entry key |
| 42 | + * @param value entry value |
| 43 | + * @throws IllegalArgumentException if key is out of [UShort] range |
| 44 | + */ |
| 45 | + fun put(key: Int, value: Int): WritableMapBuffer = putInternal(key, value) |
| 46 | + |
| 47 | + /** |
| 48 | + * Adds a double value for given key to the MapBuffer. |
| 49 | + * @param key entry key |
| 50 | + * @param value entry value |
| 51 | + * @throws IllegalArgumentException if key is out of [UShort] range |
| 52 | + */ |
| 53 | + fun put(key: Int, value: Double): WritableMapBuffer = putInternal(key, value) |
| 54 | + |
| 55 | + /** |
| 56 | + * Adds a string value for given key to the MapBuffer. |
| 57 | + * @param key entry key |
| 58 | + * @param value entry value |
| 59 | + * @throws IllegalArgumentException if key is out of [UShort] range |
| 60 | + */ |
| 61 | + fun put(key: Int, value: String): WritableMapBuffer = putInternal(key, value) |
| 62 | + |
| 63 | + /** |
| 64 | + * Adds a [MapBuffer] value for given key to the current MapBuffer. |
| 65 | + * @param key entry key |
| 66 | + * @param value entry value |
| 67 | + * @throws IllegalArgumentException if key is out of [UShort] range |
| 68 | + */ |
| 69 | + fun put(key: Int, value: MapBuffer): WritableMapBuffer = putInternal(key, value) |
| 70 | + |
| 71 | + private fun putInternal(key: Int, value: Any): WritableMapBuffer { |
| 72 | + require(key in KEY_RANGE) { |
| 73 | + "Only integers in [${UShort.MIN_VALUE};${UShort.MAX_VALUE}] range are allowed for keys." |
| 74 | + } |
| 75 | + |
| 76 | + values.put(key, value) |
| 77 | + return this |
| 78 | + } |
| 79 | + |
| 80 | + /* |
| 81 | + * Read methods |
| 82 | + */ |
| 83 | + |
| 84 | + override val count: Int |
| 85 | + get() = values.size() |
| 86 | + |
| 87 | + override fun contains(key: Int): Boolean = values.get(key) != null |
| 88 | + |
| 89 | + override fun getKeyOffset(key: Int): Int = values.indexOfKey(key) |
| 90 | + |
| 91 | + override fun entryAt(offset: Int): MapBuffer.Entry = MapBufferEntry(offset) |
| 92 | + |
| 93 | + override fun getType(key: Int): DataType { |
| 94 | + val value = values.get(key) |
| 95 | + require(value != null) { "Key not found: $key" } |
| 96 | + return value.dataType(key) |
| 97 | + } |
| 98 | + |
| 99 | + override fun getBoolean(key: Int): Boolean = verifyValue(key, values.get(key)) |
| 100 | + |
| 101 | + override fun getInt(key: Int): Int = verifyValue(key, values.get(key)) |
| 102 | + |
| 103 | + override fun getDouble(key: Int): Double = verifyValue(key, values.get(key)) |
| 104 | + |
| 105 | + override fun getString(key: Int): String = verifyValue(key, values.get(key)) |
| 106 | + |
| 107 | + override fun getMapBuffer(key: Int): MapBuffer = verifyValue(key, values.get(key)) |
| 108 | + |
| 109 | + /** Generalizes verification of the value types based on the requested type. */ |
| 110 | + private inline fun <reified T> verifyValue(key: Int, value: Any?): T { |
| 111 | + require(value != null) { "Key not found: $key" } |
| 112 | + check(value is T) { |
| 113 | + "Expected ${T::class.java} for key: $key, found ${value.javaClass} instead." |
| 114 | + } |
| 115 | + return value |
| 116 | + } |
| 117 | + |
| 118 | + private fun Any.dataType(key: Int): DataType { |
| 119 | + return when (val value = this) { |
| 120 | + is Boolean -> DataType.BOOL |
| 121 | + is Int -> DataType.INT |
| 122 | + is Double -> DataType.DOUBLE |
| 123 | + is String -> DataType.STRING |
| 124 | + is MapBuffer -> DataType.MAP |
| 125 | + else -> throw IllegalStateException("Key $key has value of unknown type: ${value.javaClass}") |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + override fun iterator(): Iterator<MapBuffer.Entry> = |
| 130 | + object : Iterator<MapBuffer.Entry> { |
| 131 | + var count = 0 |
| 132 | + override fun hasNext(): Boolean = count < values.size() |
| 133 | + override fun next(): MapBuffer.Entry = MapBufferEntry(count++) |
| 134 | + } |
| 135 | + |
| 136 | + private inner class MapBufferEntry(private val index: Int) : MapBuffer.Entry { |
| 137 | + override val key: Int = values.keyAt(index) |
| 138 | + override val type: DataType = values.valueAt(index).dataType(key) |
| 139 | + override val booleanValue: Boolean |
| 140 | + get() = verifyValue(key, values.valueAt(index)) |
| 141 | + override val intValue: Int |
| 142 | + get() = verifyValue(key, values.valueAt(index)) |
| 143 | + override val doubleValue: Double |
| 144 | + get() = verifyValue(key, values.valueAt(index)) |
| 145 | + override val stringValue: String |
| 146 | + get() = verifyValue(key, values.valueAt(index)) |
| 147 | + override val mapBufferValue: MapBuffer |
| 148 | + get() = verifyValue(key, values.valueAt(index)) |
| 149 | + } |
| 150 | + |
| 151 | + /* |
| 152 | + * JNI hooks |
| 153 | + */ |
| 154 | + |
| 155 | + @DoNotStrip |
| 156 | + @Suppress("UNUSED") |
| 157 | + /** JNI hook for MapBuffer to retrieve sorted keys from this class. */ |
| 158 | + private fun getKeys(): IntArray = IntArray(values.size()) { values.keyAt(it) } |
| 159 | + |
| 160 | + @DoNotStrip |
| 161 | + @Suppress("UNUSED") |
| 162 | + /** JNI hook for MapBuffer to retrieve sorted values from this class. */ |
| 163 | + private fun getValues(): Array<Any> = Array(values.size()) { values.valueAt(it) } |
| 164 | + |
| 165 | + companion object { |
| 166 | + init { |
| 167 | + ReadableMapBufferSoLoader.staticInit() |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments