diff --git a/libraries/stdlib/wasm/builtins/kotlin/Array.kt b/libraries/stdlib/wasm/builtins/kotlin/Array.kt index 1872d4191e1..31c6f07af3e 100644 --- a/libraries/stdlib/wasm/builtins/kotlin/Array.kt +++ b/libraries/stdlib/wasm/builtins/kotlin/Array.kt @@ -41,7 +41,7 @@ public class Array constructor(size: Int) { */ @Suppress("UNCHECKED_CAST") public operator fun get(index: Int): T { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) return storage.get(index) as T } @@ -56,7 +56,7 @@ public class Array constructor(size: Int) { * where the behavior is unspecified. */ public operator fun set(index: Int, value: T) { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) storage.set(index, value) } diff --git a/libraries/stdlib/wasm/builtins/kotlin/Arrays.kt b/libraries/stdlib/wasm/builtins/kotlin/Arrays.kt index 52bedde2bc5..28a8c955a9d 100644 --- a/libraries/stdlib/wasm/builtins/kotlin/Arrays.kt +++ b/libraries/stdlib/wasm/builtins/kotlin/Arrays.kt @@ -15,12 +15,12 @@ public class ByteArray(size: Int) { } public operator fun get(index: Int): Byte { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) return storage.get(index) } public operator fun set(index: Int, value: Byte) { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) storage.set(index, value) } @@ -45,12 +45,12 @@ public class CharArray(size: Int) { } public operator fun get(index: Int): Char { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) return storage.get(index) } public operator fun set(index: Int, value: Char) { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) storage.set(index, value) } @@ -76,12 +76,12 @@ public class ShortArray(size: Int) { } public operator fun get(index: Int): Short { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) return storage.get(index) } public operator fun set(index: Int, value: Short) { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) storage.set(index, value) } @@ -107,12 +107,12 @@ public class IntArray(size: Int) { } public operator fun get(index: Int): Int { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) return storage.get(index) } public operator fun set(index: Int, value: Int) { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) storage.set(index, value) } @@ -138,12 +138,12 @@ public class LongArray(size: Int) { } public operator fun get(index: Int): Long { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) return storage.get(index) } public operator fun set(index: Int, value: Long) { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) storage.set(index, value) } @@ -168,12 +168,12 @@ public class FloatArray(size: Int) { } public operator fun get(index: Int): Float { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) return storage.get(index) } public operator fun set(index: Int, value: Float) { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) storage.set(index, value) } @@ -198,12 +198,12 @@ public class DoubleArray(size: Int) { } public operator fun get(index: Int): Double { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) return storage.get(index) } public operator fun set(index: Int, value: Double) { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) storage.set(index, value) } @@ -228,12 +228,12 @@ public class BooleanArray(size: Int) { } public operator fun get(index: Int): Boolean { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) return storage.get(index).reinterpretAsInt().reinterpretAsBoolean() } public operator fun set(index: Int, value: Boolean) { - if (index < 0 || index >= storage.len()) throw IndexOutOfBoundsException() + rangeCheck(index, storage.len()) storage.set(index, value.toInt().reinterpretAsByte()) } diff --git a/libraries/stdlib/wasm/internal/kotlin/wasm/internal/ThrowHelpers.kt b/libraries/stdlib/wasm/internal/kotlin/wasm/internal/ThrowHelpers.kt index 022d7b22384..b6cb65a9c38 100644 --- a/libraries/stdlib/wasm/internal/kotlin/wasm/internal/ThrowHelpers.kt +++ b/libraries/stdlib/wasm/internal/kotlin/wasm/internal/ThrowHelpers.kt @@ -25,6 +25,10 @@ internal fun throwNoBranchMatchedException(): Nothing { throw NoWhenBranchMatchedException() } +internal fun rangeCheck(index: Int, size: Int) { + if (index < 0 || index >= size) throw IndexOutOfBoundsException() +} + @PublishedApi internal fun throwUninitializedPropertyAccessException(name: String): Nothing { throw UninitializedPropertyAccessException("lateinit property $name has not been initialized")