From 567fc6b1403368c563e522776238e1ba6cf54815 Mon Sep 17 00:00:00 2001 From: Igor Yakovlev Date: Wed, 3 Jan 2024 15:12:47 +0100 Subject: [PATCH] [Wasm/WASI] Make std random generator 64bit seeded Fixed #KT-60962 --- .../wasm/wasi/src/kotlin/random/PlatformRandom.kt | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/libraries/stdlib/wasm/wasi/src/kotlin/random/PlatformRandom.kt b/libraries/stdlib/wasm/wasi/src/kotlin/random/PlatformRandom.kt index 55758085736..f32fb290f8b 100644 --- a/libraries/stdlib/wasm/wasi/src/kotlin/random/PlatformRandom.kt +++ b/libraries/stdlib/wasm/wasi/src/kotlin/random/PlatformRandom.kt @@ -17,20 +17,18 @@ import kotlin.wasm.unsafe.withScopedMemoryAllocator * seed a pseudo-random number generator, rather than to provide the random data directly. */ @WasmImport("wasi_snapshot_preview1", "random_get") -//TODO: Better to use 64-bit Long as a seed if possible. (KT-60962) -internal external fun wasiRawRandomGet(address: Int, size: Int): Int +private external fun wasiRawRandomGet(address: Int, size: Int): Int -private fun wasiRandomGet(): Int { +private fun wasiRandomGet(): Long { withScopedMemoryAllocator { allocator -> - val memory = allocator.allocate(Int.SIZE_BYTES) - val ret = wasiRawRandomGet(memory.address.toInt(), Int.SIZE_BYTES) + val memory = allocator.allocate(Long.SIZE_BYTES) + val ret = wasiRawRandomGet(memory.address.toInt(), Long.SIZE_BYTES) return if (ret == 0) { - memory.loadInt() + memory.loadLong() } else { throw WasiError(WasiErrorCode.values()[ret]) } } } -internal actual fun defaultPlatformRandom(): Random = - Random(wasiRandomGet()) \ No newline at end of file +internal actual fun defaultPlatformRandom(): Random = Random(wasiRandomGet()) \ No newline at end of file