Control Duration init block assertions on JVM with -ea option

On other platforms enable them unconditionally
This commit is contained in:
Ilya Gorbunov
2021-09-23 12:56:20 +03:00
committed by Space
parent 21a9198fff
commit 7666377565
5 changed files with 15 additions and 6 deletions
@@ -7,6 +7,8 @@ package kotlin.time
import kotlin.native.internal.GCUnsafeCall
internal actual inline val durationAssertionsEnabled: Boolean get() = true
@GCUnsafeCall("Kotlin_DurationValue_formatToExactDecimals")
internal actual external fun formatToExactDecimals(value: Double, decimals: Int): String
@@ -8,6 +8,8 @@ package kotlin.time
import kotlin.js.json
import kotlin.math.*
internal actual inline val durationAssertionsEnabled: Boolean get() = true
internal actual fun formatToExactDecimals(value: Double, decimals: Int): String {
val rounded = if (decimals == 0) {
value
@@ -9,6 +9,8 @@ import java.math.RoundingMode
import java.text.DecimalFormat
import kotlin.concurrent.getOrSet
internal actual val durationAssertionsEnabled: Boolean = Duration::class.java.desiredAssertionStatus()
private val precisionFormats = Array(4) { ThreadLocal<DecimalFormat>() }
private fun createFormatForDecimals(decimals: Int) = DecimalFormat("0").apply {
+8 -6
View File
@@ -38,12 +38,13 @@ public value class Duration internal constructor(private val rawValue: Long) : C
private val storageUnit get() = if (isInNanos()) DurationUnit.NANOSECONDS else DurationUnit.MILLISECONDS
init {
// TODO: disable assertions in final version
if (isInNanos()) {
if (value !in -MAX_NANOS..MAX_NANOS) throw AssertionError("$value ns is out of nanoseconds range")
} else {
if (value !in -MAX_MILLIS..MAX_MILLIS) throw AssertionError("$value ms is out of milliseconds range")
if (value in -MAX_NANOS_IN_MILLIS..MAX_NANOS_IN_MILLIS) throw AssertionError("$value ms is denormalized")
if (durationAssertionsEnabled) {
if (isInNanos()) {
if (value !in -MAX_NANOS..MAX_NANOS) throw AssertionError("$value ns is out of nanoseconds range")
} else {
if (value !in -MAX_MILLIS..MAX_MILLIS) throw AssertionError("$value ms is out of milliseconds range")
if (value in -MAX_NANOS_IN_MILLIS..MAX_NANOS_IN_MILLIS) throw AssertionError("$value ms is denormalized")
}
}
}
@@ -1421,6 +1422,7 @@ private fun durationOfMillisNormalized(millis: Long) =
durationOfMillis(millis.coerceIn(-MAX_MILLIS, MAX_MILLIS))
}
internal expect val durationAssertionsEnabled: Boolean
internal expect fun formatToExactDecimals(value: Double, decimals: Int): String
internal expect fun formatUpToDecimals(value: Double, decimals: Int): String
@@ -4,5 +4,6 @@
*/
package kotlin.time
internal actual inline val durationAssertionsEnabled: Boolean get() = true
internal actual fun formatToExactDecimals(value: Double, decimals: Int): String = TODO("Wasm stdlib: Duration")
internal actual fun formatUpToDecimals(value: Double, decimals: Int): String = TODO("Wasm stdlib: Duration")