From 32fbf69738c5b5ed6ef61869e5fee1d347652423 Mon Sep 17 00:00:00 2001 From: Igor Yakovlev Date: Fri, 28 Jan 2022 19:50:45 +0100 Subject: [PATCH] [WASM] Std code clean up --- .../stdlib/wasm/src/kotlin/Comparator.kt | 18 +++ libraries/stdlib/wasm/src/kotlin/Kotlin.kt | 104 ------------------ libraries/stdlib/wasm/src/kotlin/Lazy.kt | 24 ++++ .../wasm/src/kotlin/{util => }/Numbers.kt | 76 ++++++++++++- 4 files changed, 117 insertions(+), 105 deletions(-) create mode 100644 libraries/stdlib/wasm/src/kotlin/Comparator.kt delete mode 100644 libraries/stdlib/wasm/src/kotlin/Kotlin.kt create mode 100644 libraries/stdlib/wasm/src/kotlin/Lazy.kt rename libraries/stdlib/wasm/src/kotlin/{util => }/Numbers.kt (66%) diff --git a/libraries/stdlib/wasm/src/kotlin/Comparator.kt b/libraries/stdlib/wasm/src/kotlin/Comparator.kt new file mode 100644 index 00000000000..da9c4bee5a9 --- /dev/null +++ b/libraries/stdlib/wasm/src/kotlin/Comparator.kt @@ -0,0 +1,18 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package kotlin + +/** + * Provides a comparison function for imposing a total ordering between instances of the type [T]. + */ +public actual fun interface Comparator { + /** + * Compares its two arguments for order. Returns zero if the arguments are equal, + * a negative number if the first argument is less than the second, or a positive number + * if the first argument is greater than the second. + */ + public actual fun compare(a: T, b: T): Int +} \ No newline at end of file diff --git a/libraries/stdlib/wasm/src/kotlin/Kotlin.kt b/libraries/stdlib/wasm/src/kotlin/Kotlin.kt deleted file mode 100644 index ae29e3993d0..00000000000 --- a/libraries/stdlib/wasm/src/kotlin/Kotlin.kt +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package kotlin - -import kotlin.annotation.AnnotationTarget.FIELD -import kotlin.annotation.AnnotationTarget.PROPERTY -import kotlin.wasm.internal.wasm_f32_reinterpret_i32 -import kotlin.wasm.internal.wasm_f64_reinterpret_i64 -import kotlin.wasm.internal.wasm_i32_reinterpret_f32 -import kotlin.wasm.internal.wasm_i64_reinterpret_f64 - - -/** - * Provides a comparison function for imposing a total ordering between instances of the type [T]. - */ -public actual fun interface Comparator { - /** - * Compares its two arguments for order. Returns zero if the arguments are equal, - * a negative number if the first argument is less than the second, or a positive number - * if the first argument is greater than the second. - */ - public actual fun compare(a: T, b: T): Int -} - -// From numbers.kt - -actual fun Double.isNaN(): Boolean = this != this -actual fun Float.isNaN(): Boolean = this != this -actual fun Double.isInfinite(): Boolean = (this == Double.POSITIVE_INFINITY) || (this == Double.NEGATIVE_INFINITY) -actual fun Float.isInfinite(): Boolean = (this == Float.POSITIVE_INFINITY) || (this == Float.NEGATIVE_INFINITY) -actual fun Double.isFinite(): Boolean = !isInfinite() && !isNaN() -actual fun Float.isFinite(): Boolean = !isInfinite() && !isNaN() - -/** - * Returns a bit representation of the specified floating-point value as [Long] - * according to the IEEE 754 floating-point "double format" bit layout. - */ -@SinceKotlin("1.2") -public actual fun Double.toBits(): Long = if (isNaN()) Double.NaN.toRawBits() else toRawBits() - -/** - * Returns a bit representation of the specified floating-point value as [Long] - * according to the IEEE 754 floating-point "double format" bit layout, - * preserving `NaN` values exact layout. - */ -@SinceKotlin("1.2") -public actual fun Double.toRawBits(): Long = wasm_i64_reinterpret_f64(this) - -/** - * Returns the [Double] value corresponding to a given bit representation. - */ -@SinceKotlin("1.2") -public actual fun Double.Companion.fromBits(bits: Long): Double = wasm_f64_reinterpret_i64(bits) - -/** - * Returns a bit representation of the specified floating-point value as [Int] - * according to the IEEE 754 floating-point "single format" bit layout. - */ -@SinceKotlin("1.2") -public actual fun Float.toBits(): Int = if (isNaN()) Float.NaN.toRawBits() else toRawBits() - -/** - * Returns a bit representation of the specified floating-point value as [Int] - * according to the IEEE 754 floating-point "single format" bit layout, - * preserving `NaN` values exact layout. - */ -@SinceKotlin("1.2") -public actual fun Float.toRawBits(): Int = wasm_i32_reinterpret_f32(this) - -/** - * Returns the [Float] value corresponding to a given bit representation. - */ -@SinceKotlin("1.2") -public actual fun Float.Companion.fromBits(bits: Int): Float = wasm_f32_reinterpret_i32(bits) - - -// From concurrent.kt - -// TODO: promote to error? Otherwise it gets to JVM part -//@Deprecated("Use Volatile annotation from kotlin.jvm package", ReplaceWith("kotlin.jvm.Volatile"), level = DeprecationLevel.WARNING) -//public typealias Volatile = kotlin.jvm.Volatile - -// from lazy.kt - -/** - * Creates a new instance of the [Lazy] that uses the specified initialization function [initializer]. - */ -public actual fun lazy(initializer: () -> T): Lazy = UnsafeLazyImpl(initializer) - -/** - * Creates a new instance of the [Lazy] that uses the specified initialization function [initializer]. - * - * The [mode] parameter is ignored. */ -public actual fun lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy = UnsafeLazyImpl(initializer) - -/** - * Creates a new instance of the [Lazy] that uses the specified initialization function [initializer]. - * - * The [lock] parameter is ignored. - */ -public actual fun lazy(lock: Any?, initializer: () -> T): Lazy = UnsafeLazyImpl(initializer) diff --git a/libraries/stdlib/wasm/src/kotlin/Lazy.kt b/libraries/stdlib/wasm/src/kotlin/Lazy.kt new file mode 100644 index 00000000000..e75ad1604cc --- /dev/null +++ b/libraries/stdlib/wasm/src/kotlin/Lazy.kt @@ -0,0 +1,24 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package kotlin + +/** + * Creates a new instance of the [Lazy] that uses the specified initialization function [initializer]. + */ +public actual fun lazy(initializer: () -> T): Lazy = UnsafeLazyImpl(initializer) + +/** + * Creates a new instance of the [Lazy] that uses the specified initialization function [initializer]. + * + * The [mode] parameter is ignored. */ +public actual fun lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy = UnsafeLazyImpl(initializer) + +/** + * Creates a new instance of the [Lazy] that uses the specified initialization function [initializer]. + * + * The [lock] parameter is ignored. + */ +public actual fun lazy(lock: Any?, initializer: () -> T): Lazy = UnsafeLazyImpl(initializer) \ No newline at end of file diff --git a/libraries/stdlib/wasm/src/kotlin/util/Numbers.kt b/libraries/stdlib/wasm/src/kotlin/Numbers.kt similarity index 66% rename from libraries/stdlib/wasm/src/kotlin/util/Numbers.kt rename to libraries/stdlib/wasm/src/kotlin/Numbers.kt index ed88c869f2a..c8c7b6c590c 100644 --- a/libraries/stdlib/wasm/src/kotlin/util/Numbers.kt +++ b/libraries/stdlib/wasm/src/kotlin/Numbers.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -132,3 +132,77 @@ public actual fun Long.rotateLeft(bitCount: Int): Long = @kotlin.internal.InlineOnly public actual inline fun Long.rotateRight(bitCount: Int): Long = shl(64 - bitCount) or ushr(bitCount) + +/** + * Returns `true` if the specified number is a + * Not-a-Number (NaN) value, `false` otherwise. + */ +actual fun Double.isNaN(): Boolean = this != this + +/** + * Returns `true` if the specified number is a + * Not-a-Number (NaN) value, `false` otherwise. + */ +actual fun Float.isNaN(): Boolean = this != this + +/** + * Returns `true` if this value is infinitely large in magnitude. + */ +actual fun Double.isInfinite(): Boolean = (this == Double.POSITIVE_INFINITY) || (this == Double.NEGATIVE_INFINITY) + +/** + * Returns `true` if this value is infinitely large in magnitude. + */ +actual fun Float.isInfinite(): Boolean = (this == Float.POSITIVE_INFINITY) || (this == Float.NEGATIVE_INFINITY) + +/** + * Returns `true` if the argument is a finite floating-point value; returns `false` otherwise (for `NaN` and infinity arguments). + */ +actual fun Double.isFinite(): Boolean = !isInfinite() && !isNaN() + +/** + * Returns `true` if the argument is a finite floating-point value; returns `false` otherwise (for `NaN` and infinity arguments). + */ +actual fun Float.isFinite(): Boolean = !isInfinite() && !isNaN() + +/** + * Returns a bit representation of the specified floating-point value as [Long] + * according to the IEEE 754 floating-point "double format" bit layout. + */ +@SinceKotlin("1.2") +public actual fun Double.toBits(): Long = if (isNaN()) Double.NaN.toRawBits() else toRawBits() + +/** + * Returns a bit representation of the specified floating-point value as [Long] + * according to the IEEE 754 floating-point "double format" bit layout, + * preserving `NaN` values exact layout. + */ +@SinceKotlin("1.2") +public actual fun Double.toRawBits(): Long = wasm_i64_reinterpret_f64(this) + +/** + * Returns the [Double] value corresponding to a given bit representation. + */ +@SinceKotlin("1.2") +public actual fun Double.Companion.fromBits(bits: Long): Double = wasm_f64_reinterpret_i64(bits) + +/** + * Returns a bit representation of the specified floating-point value as [Int] + * according to the IEEE 754 floating-point "single format" bit layout. + */ +@SinceKotlin("1.2") +public actual fun Float.toBits(): Int = if (isNaN()) Float.NaN.toRawBits() else toRawBits() + +/** + * Returns a bit representation of the specified floating-point value as [Int] + * according to the IEEE 754 floating-point "single format" bit layout, + * preserving `NaN` values exact layout. + */ +@SinceKotlin("1.2") +public actual fun Float.toRawBits(): Int = wasm_i32_reinterpret_f32(this) + +/** + * Returns the [Float] value corresponding to a given bit representation. + */ +@SinceKotlin("1.2") +public actual fun Float.Companion.fromBits(bits: Int): Float = wasm_f32_reinterpret_i32(bits) \ No newline at end of file