Inline-only in kotlin and kotlin.system packages, split Float/Double extension implementations for JVM and JS to make them inline-only in JVM.

This commit is contained in:
Ilya Gorbunov
2016-01-28 21:45:46 +03:00
parent 40fae0463f
commit 56c5758db1
10 changed files with 103 additions and 38 deletions
+33
View File
@@ -0,0 +1,33 @@
package kotlin
/**
* Returns `true` if the specified number is a
* Not-a-Number (NaN) value, `false` otherwise.
*/
public fun Double.isNaN(): Boolean = this != this
/**
* Returns `true` if the specified number is a
* Not-a-Number (NaN) value, `false` otherwise.
*/
public fun Float.isNaN(): Boolean = this != this
/**
* Returns `true` if this value is infinitely large in magnitude.
*/
public fun Double.isInfinite(): Boolean = this == Double.POSITIVE_INFINITY || this == Double.NEGATIVE_INFINITY
/**
* Returns `true` if this value is infinitely large in magnitude.
*/
public 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).
*/
public 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).
*/
public fun Float.isFinite(): Boolean = !isInfinite() && !isNaN()
@@ -9,7 +9,8 @@ package kotlin.system
*
* This method never returns normally.
*/
public fun exitProcess(status: Int): Nothing {
@kotlin.internal.InlineOnly
public inline fun exitProcess(status: Int): Nothing {
System.exit(status)
throw RuntimeException("System.exit returned normally, while it was supposed to halt JVM.")
}
@@ -19,6 +19,7 @@ public fun assert(value: Boolean) {
* Throws an [AssertionError] calculated by [lazyMessage] if the [value] is false
* and runtime assertions have been enabled on the JVM using the *-ea* JVM option.
*/
@kotlin.internal.InlineOnly
public inline fun assert(value: Boolean, lazyMessage: () -> Any) {
@Suppress("INVISIBLE_MEMBER_FROM_INLINE")
if (ASSERTIONS_ENABLED) {
+22 -11
View File
@@ -9,55 +9,66 @@ import java.math.BigInteger
/**
* Enables the use of the `+` operator for [BigInteger] instances.
*/
public operator fun BigInteger.plus(other: BigInteger) : BigInteger = this.add(other)
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.plus(other: BigInteger) : BigInteger = this.add(other)
/**
* Enables the use of the `-` operator for [BigInteger] instances.
*/
public operator fun BigInteger.minus(other: BigInteger) : BigInteger = this.subtract(other)
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.minus(other: BigInteger) : BigInteger = this.subtract(other)
/**
* Enables the use of the `*` operator for [BigInteger] instances.
*/
public operator fun BigInteger.times(other: BigInteger) : BigInteger = this.multiply(other)
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.times(other: BigInteger) : BigInteger = this.multiply(other)
/**
* Enables the use of the `/` operator for [BigInteger] instances.
*/
public operator fun BigInteger.div(other: BigInteger) : BigInteger = this.divide(other)
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.div(other: BigInteger) : BigInteger = this.divide(other)
/**
* Enables the use of the unary `-` operator for [BigInteger] instances.
*/
public operator fun BigInteger.unaryMinus() : BigInteger = this.negate()
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.unaryMinus() : BigInteger = this.negate()
/**
* Enables the use of the `+` operator for [BigDecimal] instances.
*/
public operator fun BigDecimal.plus(other: BigDecimal) : BigDecimal = this.add(other)
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.plus(other: BigDecimal) : BigDecimal = this.add(other)
/**
* Enables the use of the `-` operator for [BigDecimal] instances.
*/
public operator fun BigDecimal.minus(other: BigDecimal) : BigDecimal = this.subtract(other)
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.minus(other: BigDecimal) : BigDecimal = this.subtract(other)
/**
* Enables the use of the `*` operator for [BigDecimal] instances.
*/
public operator fun BigDecimal.times(other: BigDecimal) : BigDecimal = this.multiply(other)
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.times(other: BigDecimal) : BigDecimal = this.multiply(other)
/**
* Enables the use of the `/` operator for [BigDecimal] instances.
*/
public operator fun BigDecimal.div(other: BigDecimal) : BigDecimal = this.divide(other)
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.div(other: BigDecimal) : BigDecimal = this.divide(other)
/**
* Enables the use of the `%` operator for [BigDecimal] instances.
*/
public operator fun BigDecimal.mod(other: BigDecimal) : BigDecimal = this.remainder(other)
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.mod(other: BigDecimal) : BigDecimal = this.remainder(other)
/**
* Enables the use of the unary `-` operator for [BigDecimal] instances.
*/
public operator fun BigDecimal.unaryMinus() : BigDecimal = this.negate()
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.unaryMinus() : BigDecimal = this.negate()
@@ -7,6 +7,7 @@ package kotlin
*
* A zero-based index of current iteration is passed as a parameter to [action].
*/
@kotlin.internal.InlineOnly
public inline fun repeat(times: Int, action: (Int) -> Unit) {
for (index in 0..times - 1) {
action(index)
+13 -6
View File
@@ -1,35 +1,42 @@
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("MathKt")
@file:kotlin.jvm.JvmVersion
package kotlin
/**
* Returns `true` if the specified number is a
* Not-a-Number (NaN) value, `false` otherwise.
*/
public fun Double.isNaN(): Boolean = this != this
@kotlin.internal.InlineOnly
public inline fun Double.isNaN(): Boolean = java.lang.Double.isNaN(this)
/**
* Returns `true` if the specified number is a
* Not-a-Number (NaN) value, `false` otherwise.
*/
public fun Float.isNaN(): Boolean = this != this
@kotlin.internal.InlineOnly
public inline fun Float.isNaN(): Boolean = java.lang.Float.isNaN(this)
/**
* Returns `true` if this value is infinitely large in magnitude.
*/
public fun Double.isInfinite(): Boolean = this == Double.POSITIVE_INFINITY || this == Double.NEGATIVE_INFINITY
@kotlin.internal.InlineOnly
public inline fun Double.isInfinite(): Boolean = java.lang.Double.isInfinite(this)
/**
* Returns `true` if this value is infinitely large in magnitude.
*/
public fun Float.isInfinite(): Boolean = this == Float.POSITIVE_INFINITY || this == Float.NEGATIVE_INFINITY
@kotlin.internal.InlineOnly
public inline fun Float.isInfinite(): Boolean = java.lang.Float.isInfinite(this)
/**
* Returns `true` if the argument is a finite floating-point value; returns `false` otherwise (for `NaN` and infinity arguments).
*/
public fun Double.isFinite(): Boolean = !isInfinite() && !isNaN()
@kotlin.internal.InlineOnly
public inline 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).
*/
public fun Float.isFinite(): Boolean = !isInfinite() && !isNaN()
@kotlin.internal.InlineOnly
public inline fun Float.isFinite(): Boolean = !isInfinite() && !isNaN()
@@ -2,20 +2,21 @@
@file:kotlin.jvm.JvmName("PreconditionsKt")
package kotlin
// TODO should not need this - its here for the JS stuff
import java.lang.IllegalArgumentException
import java.lang.IllegalStateException
/**
* Throws an [IllegalArgumentException] if the [value] is false.
*/
public fun require(value: Boolean): Unit = require(value) { "Failed requirement" }
@kotlin.internal.InlineOnly
public inline fun require(value: Boolean): Unit = require(value) { "Failed requirement" }
/**
* Throws an [IllegalArgumentException] with the result of calling [lazyMessage] if the [value] is false.
*
* @sample test.collections.PreconditionsTest.failingRequireWithLazyMessage
*/
@kotlin.internal.InlineOnly
public inline fun require(value: Boolean, lazyMessage: () -> Any): Unit {
if (!value) {
val message = lazyMessage()
@@ -26,7 +27,8 @@ public inline fun require(value: Boolean, lazyMessage: () -> Any): Unit {
/**
* Throws an [IllegalArgumentException] if the [value] is null. Otherwise returns the not null value.
*/
public fun <T:Any> requireNotNull(value: T?): T = requireNotNull(value) { "Required value was null" }
@kotlin.internal.InlineOnly
public inline fun <T:Any> requireNotNull(value: T?): T = requireNotNull(value) { "Required value was null" }
/**
* Throws an [IllegalArgumentException] with the result of calling [lazyMessage] if the [value] is null. Otherwise
@@ -34,6 +36,7 @@ public fun <T:Any> requireNotNull(value: T?): T = requireNotNull(value) { "Requi
*
* @sample test.collections.PreconditionsTest.requireNotNullWithLazyMessage
*/
@kotlin.internal.InlineOnly
public inline fun <T:Any> requireNotNull(value: T?, lazyMessage: () -> Any): T {
if (value == null) {
val message = lazyMessage()
@@ -46,13 +49,15 @@ public inline fun <T:Any> requireNotNull(value: T?, lazyMessage: () -> Any): T {
/**
* Throws an [IllegalStateException] if the [value] is false.
*/
public fun check(value: Boolean): Unit = check(value) { "Check failed" }
@kotlin.internal.InlineOnly
public inline fun check(value: Boolean): Unit = check(value) { "Check failed" }
/**
* Throws an [IllegalStateException] with the result of calling [lazyMessage] if the [value] is false.
*
* @sample test.collections.PreconditionsTest.failingCheckWithLazyMessage
*/
@kotlin.internal.InlineOnly
public inline fun check(value: Boolean, lazyMessage: () -> Any): Unit {
if (!value) {
val message = lazyMessage()
@@ -64,12 +69,14 @@ public inline fun check(value: Boolean, lazyMessage: () -> Any): Unit {
* Throws an [IllegalStateException] if the [value] is null. Otherwise
* returns the not null value.
*/
public fun <T:Any> checkNotNull(value: T?): T = checkNotNull(value) { "Required value was null" }
@kotlin.internal.InlineOnly
public inline fun <T:Any> checkNotNull(value: T?): T = checkNotNull(value) { "Required value was null" }
/**
* Throws an [IllegalStateException] with the result of calling [lazyMessage] if the [value] is null. Otherwise
* returns the not null value.
*/
@kotlin.internal.InlineOnly
public inline fun <T:Any> checkNotNull(value: T?, lazyMessage: () -> Any): T {
if (value == null) {
val message = lazyMessage()
@@ -85,4 +92,5 @@ public inline fun <T:Any> checkNotNull(value: T?, lazyMessage: () -> Any): T {
*
* @sample test.collections.PreconditionsTest.error
*/
public fun error(message: Any): Nothing = throw IllegalStateException(message.toString())
@kotlin.internal.InlineOnly
public inline fun error(message: Any): Nothing = throw IllegalStateException(message.toString())
+10 -2
View File
@@ -10,38 +10,46 @@ public class NotImplementedError(message: String = "An operation is not implemen
/**
* Always throws [NotImplementedError] stating that operation is not implemented.
*/
public fun TODO(): Nothing = throw NotImplementedError()
@kotlin.internal.InlineOnly
public inline fun TODO(): Nothing = throw NotImplementedError()
/**
* Always throws [NotImplementedError] stating that operation is not implemented.
*
* @param reason a string explaining why the implementation is missing.
*/
public fun TODO(reason: String): Nothing = throw NotImplementedError("An operation is not implemented: $reason")
@kotlin.internal.InlineOnly
public inline fun TODO(reason: String): Nothing = throw NotImplementedError("An operation is not implemented: $reason")
/**
* Calls the specified function [block] and returns its result.
*/
@kotlin.internal.InlineOnly
public inline fun <R> run(block: () -> R): R = block()
/**
* Calls the specified function [block] with `this` value as its receiver and returns its result.
*/
@kotlin.internal.InlineOnly
public inline fun <T, R> T.run(block: T.() -> R): R = block()
/**
* Calls the specified function [block] with the given [receiver] as its receiver and returns its result.
*/
@kotlin.internal.InlineOnly
public inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()
/**
* Calls the specified function [block] with `this` value as its receiver and returns `this` value.
*/
@kotlin.internal.InlineOnly
public inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }
/**
* Calls the specified function [block] with `this` value as its argument and returns its result.
*/
@kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R = block(this)
@@ -9,26 +9,20 @@ import java.io.PrintWriter
/**
* Prints the stack trace of this throwable to the standard output.
*/
public fun Throwable.printStackTrace(): Unit {
val jlt = this as java.lang.Throwable
jlt.printStackTrace()
}
@kotlin.internal.InlineOnly
public inline fun Throwable.printStackTrace(): Unit = (this as java.lang.Throwable).printStackTrace()
/**
* Prints the stack trace of this throwable to the specified [writer].
*/
public fun Throwable.printStackTrace(writer: PrintWriter): Unit {
val jlt = this as java.lang.Throwable
jlt.printStackTrace(writer)
}
@kotlin.internal.InlineOnly
public inline fun Throwable.printStackTrace(writer: PrintWriter): Unit = (this as java.lang.Throwable).printStackTrace(writer)
/**
* Prints the stack trace of this throwable to the specified [stream].
*/
public fun Throwable.printStackTrace(stream: PrintStream): Unit {
val jlt = this as java.lang.Throwable
jlt.printStackTrace(stream)
}
@kotlin.internal.InlineOnly
public inline fun Throwable.printStackTrace(stream: PrintStream): Unit = (this as java.lang.Throwable).printStackTrace(stream)
/**
* Returns an array of stack trace elements representing the stack trace
@@ -9,6 +9,7 @@ import kotlin.jvm.internal.unsafe.*
* Executes the given function [block] while holding the monitor of the given object [lock].
*/
@Suppress("DEPRECATION_ERROR")
@kotlin.internal.InlineOnly
public inline fun <R> synchronized(lock: Any, block: () -> R): R {
monitorEnter(lock)
try {