From 627a49329d6e612fafc6a046ce1842ace3ef5d31 Mon Sep 17 00:00:00 2001 From: KotlinCrypto Date: Fri, 27 Feb 2026 13:08:26 -0500 Subject: [PATCH] Remove unnecessary Int8Array type definition --- .../random/internal/js/_JsCrypto.kt | 6 +-- .../random/internal/js/_JsInt8Array.kt | 28 ------------ .../random/internal/js/JsInt8Array.kt | 45 ------------------- .../random/internal/js/JsUint8Array.kt | 2 - .../random/internal/js/_JsCrypto.kt | 4 +- .../random/internal/js/_JsInt8Array.kt | 24 ---------- 6 files changed, 4 insertions(+), 105 deletions(-) delete mode 100644 library/crypto-rand/src/jsMain/kotlin/org/kotlincrypto/random/internal/js/_JsInt8Array.kt delete mode 100644 library/crypto-rand/src/jsWasmJsMain/kotlin/org/kotlincrypto/random/internal/js/JsInt8Array.kt delete mode 100644 library/crypto-rand/src/wasmJsMain/kotlin/org/kotlincrypto/random/internal/js/_JsInt8Array.kt diff --git a/library/crypto-rand/src/jsMain/kotlin/org/kotlincrypto/random/internal/js/_JsCrypto.kt b/library/crypto-rand/src/jsMain/kotlin/org/kotlincrypto/random/internal/js/_JsCrypto.kt index 9904888..ec458f6 100644 --- a/library/crypto-rand/src/jsMain/kotlin/org/kotlincrypto/random/internal/js/_JsCrypto.kt +++ b/library/crypto-rand/src/jsMain/kotlin/org/kotlincrypto/random/internal/js/_JsCrypto.kt @@ -15,7 +15,7 @@ **/ package org.kotlincrypto.random.internal.js -import kotlin.math.min +import kotlin.js.unsafeCast internal actual val IS_NODE_JS: Boolean by lazy { isNodeJs() } @@ -25,13 +25,13 @@ internal actual fun jsCryptoNode(): JsCrypto = js(CODE_JS_CRYPTO_NODE) private fun isNodeJs(): Boolean = js(CODE_IS_NODE_JS) internal actual fun ByteArray.cryptoRandFill(procure: (JsUint8Array) -> Unit) { - val a = asJsInt8Array().asJsUint8Array() + val a = JsUint8Array.new(unsafeCast().buffer) if (a.length < JS_CRYPTO_MAX_FILL) return procure(a) var remainder = size var offset = 0 while (remainder > 0) { - val len = min(JS_CRYPTO_MAX_FILL, remainder) + val len = minOf(JS_CRYPTO_MAX_FILL, remainder) val s = a.subarray(start = offset, end = offset + len) procure(s) offset += len diff --git a/library/crypto-rand/src/jsMain/kotlin/org/kotlincrypto/random/internal/js/_JsInt8Array.kt b/library/crypto-rand/src/jsMain/kotlin/org/kotlincrypto/random/internal/js/_JsInt8Array.kt deleted file mode 100644 index 9a22c0a..0000000 --- a/library/crypto-rand/src/jsMain/kotlin/org/kotlincrypto/random/internal/js/_JsInt8Array.kt +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2025 KotlinCrypto - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - **/ -@file:Suppress("NOTHING_TO_INLINE") - -package org.kotlincrypto.random.internal.js - -import kotlin.js.unsafeCast - -internal actual fun jsInt8Array(length: Int): JsInt8Array = js(CODE_JS_NEW_INT8_LENGTH) -internal actual fun jsInt8Array(buffer: JsArrayBufferLike): JsInt8Array = js(CODE_JS_NEW_INT8_BUFFER) - -internal actual fun jsInt8ArrayGet(array: JsInt8Array, index: Int): Byte = js(CODE_JS_ARRAY_GET) -internal actual fun jsInt8ArraySet(array: JsInt8Array, index: Int, value: Byte) { js(CODE_JS_ARRAY_SET) } - -internal inline fun ByteArray.asJsInt8Array(): JsInt8Array = unsafeCast() diff --git a/library/crypto-rand/src/jsWasmJsMain/kotlin/org/kotlincrypto/random/internal/js/JsInt8Array.kt b/library/crypto-rand/src/jsWasmJsMain/kotlin/org/kotlincrypto/random/internal/js/JsInt8Array.kt deleted file mode 100644 index 78a3bc0..0000000 --- a/library/crypto-rand/src/jsWasmJsMain/kotlin/org/kotlincrypto/random/internal/js/JsInt8Array.kt +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2025 KotlinCrypto - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - **/ -@file:Suppress("NOTHING_TO_INLINE", "UNUSED") - -package org.kotlincrypto.random.internal.js - -import kotlin.js.JsName - -@JsName("Int8Array") -internal open external class JsInt8Array: JsTypedArrayLike { - override val buffer: JsArrayBufferLike - override val length: Int - override fun subarray(start: Int, end: Int): JsInt8Array - internal companion object -} - -internal inline fun JsInt8Array.Companion.new(length: Int): JsInt8Array = jsInt8Array(length) -internal inline fun JsInt8Array.Companion.new(buffer: JsArrayBufferLike): JsInt8Array = jsInt8Array(buffer) - -internal inline operator fun JsInt8Array.get(index: Int): Byte = jsInt8ArrayGet(this, index) -internal inline operator fun JsInt8Array.set(index: Int, value: Byte) { jsInt8ArraySet(this, index, value) } - -internal inline fun JsInt8Array.asJsUint8Array(): JsUint8Array = JsUint8Array.new(buffer) - -internal const val CODE_JS_NEW_INT8_LENGTH = "new Int8Array(length)" -internal const val CODE_JS_NEW_INT8_BUFFER = "new Int8Array(buffer)" - -internal expect fun jsInt8Array(length: Int): JsInt8Array -internal expect fun jsInt8Array(buffer: JsArrayBufferLike): JsInt8Array - -internal expect fun jsInt8ArrayGet(array: JsInt8Array, index: Int): Byte -internal expect fun jsInt8ArraySet(array: JsInt8Array, index: Int, value: Byte) diff --git a/library/crypto-rand/src/jsWasmJsMain/kotlin/org/kotlincrypto/random/internal/js/JsUint8Array.kt b/library/crypto-rand/src/jsWasmJsMain/kotlin/org/kotlincrypto/random/internal/js/JsUint8Array.kt index 558069e..d89e822 100644 --- a/library/crypto-rand/src/jsWasmJsMain/kotlin/org/kotlincrypto/random/internal/js/JsUint8Array.kt +++ b/library/crypto-rand/src/jsWasmJsMain/kotlin/org/kotlincrypto/random/internal/js/JsUint8Array.kt @@ -33,8 +33,6 @@ internal inline fun JsUint8Array.Companion.new(buffer: JsArrayBufferLike): JsUin internal inline operator fun JsUint8Array.get(index: Int): Byte = jsUint8ArrayGet(this, index).toByte() internal inline operator fun JsUint8Array.set(index: Int, value: UByte) { jsUint8ArraySet(this, index, value.toByte()) } -internal inline fun JsUint8Array.asJsInt8Array(): JsInt8Array = JsInt8Array.new(buffer) - internal const val CODE_JS_NEW_UINT8_LENGTH = "new Uint8Array(length)" internal const val CODE_JS_NEW_UINT8_BUFFER = "new Uint8Array(buffer)" diff --git a/library/crypto-rand/src/wasmJsMain/kotlin/org/kotlincrypto/random/internal/js/_JsCrypto.kt b/library/crypto-rand/src/wasmJsMain/kotlin/org/kotlincrypto/random/internal/js/_JsCrypto.kt index bfd29da..189c3d6 100644 --- a/library/crypto-rand/src/wasmJsMain/kotlin/org/kotlincrypto/random/internal/js/_JsCrypto.kt +++ b/library/crypto-rand/src/wasmJsMain/kotlin/org/kotlincrypto/random/internal/js/_JsCrypto.kt @@ -17,8 +17,6 @@ package org.kotlincrypto.random.internal.js -import kotlin.math.min - internal actual val IS_NODE_JS: Boolean by lazy { isNodeJs() } internal actual fun jsCryptoBrowser(): JsCrypto = js(CODE_JS_CRYPTO_BROWSER) @@ -32,7 +30,7 @@ internal actual fun ByteArray.cryptoRandFill(procure: (JsUint8Array) -> Unit) { // Kotlin/WasmJs does not provide access to ByteArray.storage // (i.e. the WasmByteArray), so we must always use a buffer and // copy back and forth. - val buf = JsUint8Array.new(length = min(size, BUFFER_SIZE)) + val buf = JsUint8Array.new(length = minOf(size, BUFFER_SIZE)) try { var remainder = size diff --git a/library/crypto-rand/src/wasmJsMain/kotlin/org/kotlincrypto/random/internal/js/_JsInt8Array.kt b/library/crypto-rand/src/wasmJsMain/kotlin/org/kotlincrypto/random/internal/js/_JsInt8Array.kt deleted file mode 100644 index faa6351..0000000 --- a/library/crypto-rand/src/wasmJsMain/kotlin/org/kotlincrypto/random/internal/js/_JsInt8Array.kt +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2025 KotlinCrypto - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - **/ -@file:Suppress("OPT_IN_USAGE") - -package org.kotlincrypto.random.internal.js - -internal actual fun jsInt8Array(length: Int): JsInt8Array = js(CODE_JS_NEW_INT8_LENGTH) -internal actual fun jsInt8Array(buffer: JsArrayBufferLike): JsInt8Array = js(CODE_JS_NEW_INT8_BUFFER) - -internal actual fun jsInt8ArrayGet(array: JsInt8Array, index: Int): Byte = js(CODE_JS_ARRAY_GET) -internal actual fun jsInt8ArraySet(array: JsInt8Array, index: Int, value: Byte) { js(CODE_JS_ARRAY_SET) }