Reformat stdlib: js part

#KT-5558
This commit is contained in:
Ilya Gorbunov
2018-04-23 15:30:51 +03:00
parent e3883d8d6d
commit add09e0f2e
18 changed files with 91 additions and 88 deletions
+2 -1
View File
@@ -7,7 +7,8 @@ package kotlin
public actual interface Comparator<T> {
@JsName("compare") actual fun compare(a: T, b: T): Int
@JsName("compare")
actual fun compare(a: T, b: T): Int
}
public actual inline fun <T> Comparator(crossinline comparison: (a: T, b: T) -> Int): Comparator<T> = object : Comparator<T> {
+5 -10
View File
@@ -90,8 +90,7 @@ internal fun noWhenBranchMatched(): Nothing = throw NoWhenBranchMatchedException
internal fun subSequence(c: CharSequence, startIndex: Int, endIndex: Int): CharSequence {
if (c is String) {
return c.substring(startIndex, endIndex)
}
else {
} else {
return c.asDynamic().`subSequence_vux9f0$`(startIndex, endIndex)
}
}
@@ -100,8 +99,7 @@ internal fun subSequence(c: CharSequence, startIndex: Int, endIndex: Int): CharS
internal fun captureStack(baseClass: JsClass<in Throwable>, instance: Throwable) {
if (js("Error").captureStackTrace) {
js("Error").captureStackTrace(instance, instance::class.js);
}
else {
} else {
instance.asDynamic().stack = js("new Error()").stack;
}
}
@@ -111,8 +109,7 @@ internal fun newThrowable(message: String?, cause: Throwable?): Throwable {
val throwable = js("new Error()")
throwable.message = if (jsTypeOf(message) == "undefined") {
if (cause != null) cause.toString() else null
}
else {
} else {
message
}
throwable.cause = cause
@@ -151,8 +148,7 @@ internal inline fun <T> concat(args: Array<T>): T {
val arr = args[i]
if (arr !is Array<*>) {
typed[i] = js("[]").slice.call(arr)
}
else {
} else {
typed[i] = arr
}
}
@@ -182,8 +178,7 @@ internal fun <T> primitiveArrayConcat(a: T, b: T): T {
val args: Array<T> = js("arguments")
if (a is Array<*> && a.asDynamic().`$type$` === undefined) {
return concat(args)
}
else {
} else {
var size = 0
for (i in args.indices) {
size += args[i].asDynamic().length as Int
@@ -95,6 +95,7 @@ public actual fun <T> MutableList<T>.shuffle(): Unit {
this[j] = copy
}
}
private fun rand(upperBound: Int) = floor(kotlin.js.Math.random() * upperBound).toInt()
/**
@@ -143,8 +144,8 @@ internal actual inline fun <K, V> Map<out K, V>.toSingletonMap(): Map<K, V> = th
@Suppress("NOTHING_TO_INLINE")
internal actual inline fun <T> Array<out T>.copyToArrayOfAny(isVarargs: Boolean): Array<out Any?> =
if (isVarargs)
// no need to copy vararg array in JS
this
else
this.copyOf()
if (isVarargs)
// no need to copy vararg array in JS
this
else
this.copyOf()
@@ -8,6 +8,7 @@ package kotlin
@Deprecated("Use Synchronized annotation from kotlin.jvm package", ReplaceWith("kotlin.jvm.Synchronized"), level = DeprecationLevel.WARNING)
public typealias Synchronized = kotlin.jvm.Synchronized
@Deprecated("Use Volatile annotation from kotlin.jvm package", ReplaceWith("kotlin.jvm.Volatile"), level = DeprecationLevel.WARNING)
public typealias Volatile = kotlin.jvm.Volatile
+4 -2
View File
@@ -4,6 +4,7 @@
*/
package kotlin.coroutines.experimental
import kotlin.coroutines.experimental.intrinsics.*
@JsName("CoroutineImpl")
@@ -38,13 +39,14 @@ internal abstract class CoroutineImpl(private val resultContinuation: Continuati
private val UNDECIDED: Any? = Any()
private val RESUMED: Any? = Any()
private class Fail(val exception: Throwable)
@PublishedApi
internal actual class SafeContinuation<in T>
internal actual constructor(
private val delegate: Continuation<T>,
initialResult: Any?
private val delegate: Continuation<T>,
initialResult: Any?
) : Continuation<T> {
@PublishedApi
@@ -11,24 +11,24 @@ import kotlin.coroutines.experimental.Continuation
@Suppress("UNCHECKED_CAST")
@kotlin.internal.InlineOnly
public actual inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrReturn(
completion: Continuation<T>
completion: Continuation<T>
): Any? = this.asDynamic()(completion, false)
@SinceKotlin("1.1")
@Suppress("UNCHECKED_CAST")
@kotlin.internal.InlineOnly
public actual inline fun <R, T> (suspend R.() -> T).startCoroutineUninterceptedOrReturn(
receiver: R,
completion: Continuation<T>
receiver: R,
completion: Continuation<T>
): Any? = this.asDynamic()(receiver, completion, false)
@SinceKotlin("1.1")
public actual fun <R, T> (suspend R.() -> T).createCoroutineUnchecked(
receiver: R,
completion: Continuation<T>
receiver: R,
completion: Continuation<T>
): Continuation<Unit> = this.asDynamic()(receiver, completion, true).facade
@SinceKotlin("1.1")
public actual fun <T> (suspend () -> T).createCoroutineUnchecked(
completion: Continuation<T>
completion: Continuation<T>
): Continuation<Unit> = this.asDynamic()(completion, true).facade
@@ -20,5 +20,5 @@ public fun Document.createElement(name: String, init: Element.() -> Unit): Eleme
* The element is initialized with the speicifed [init] function.
*/
public fun Element.appendElement(name: String, init: Element.() -> Unit): Element =
ownerDocument!!.createElement(name, init).also { appendChild(it) }
ownerDocument!!.createElement(name, init).also { appendChild(it) }
@@ -11,5 +11,6 @@ private class EventListenerHandler(private val handler: (Event) -> Unit) : Event
public override fun handleEvent(e: Event) {
handler(e)
}
public override fun toString(): String = "EventListenerHandler($handler)"
}
+2 -2
View File
@@ -11,9 +11,9 @@ package kotlin.collections
*/
@SinceKotlin("1.1")
public actual fun <T, K> Grouping<T, K>.eachCount(): Map<K, Int> =
fold(0) { acc, _ -> acc + 1 }
fold(0) { acc, _ -> acc + 1 }
/**
/*
/**
* Groups elements from the [Grouping] source by key and sums values provided by the [valueSelector] function for elements in each group.
*
+4 -1
View File
@@ -371,6 +371,7 @@ public actual inline fun sign(x: Double): Double = nativeMath.sign(x)
@SinceKotlin("1.2")
@InlineOnly
public actual inline fun min(a: Double, b: Double): Double = nativeMath.min(a, b)
/**
* Returns the greater of two values.
*
@@ -502,7 +503,7 @@ public actual fun Double.nextTowards(to: Double): Double = when {
this.isNaN() || to.isNaN() -> Double.NaN
to == this -> to
to > this -> this.nextUp()
else /* to < this */-> this.nextDown()
else /* to < this */ -> this.nextDown()
}
@@ -901,6 +902,7 @@ public actual inline fun sign(x: Float): Float = nativeMath.sign(x.toDouble()).t
@SinceKotlin("1.2")
@InlineOnly
public actual inline fun min(a: Float, b: Float): Float = nativeMath.min(a, b)
/**
* Returns the greater of two values.
*
@@ -970,6 +972,7 @@ public actual inline val Float.sign: Float get() = nativeMath.sign(this.toDouble
@SinceKotlin("1.2")
@InlineOnly
public actual inline fun Float.withSign(sign: Float): Float = this.toDouble().withSign(sign.toDouble()).toFloat()
/**
* Returns this value with the sign bit same as of the [sign] value.
*/
@@ -6,7 +6,6 @@
package kotlin.text
/**
* Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise.
*/
@@ -97,7 +96,7 @@ public actual fun String.toDoubleOrNull(): Double? = (+(this.asDynamic())).unsaf
public actual inline fun String.toFloatOrNull(): Float? = toDoubleOrNull().unsafeCast<Float?>()
private fun String.isNaN(): Boolean = when(this.toLowerCase()) {
private fun String.isNaN(): Boolean = when (this.toLowerCase()) {
"nan", "+nan", "-nan" -> true
else -> false
}
@@ -107,7 +106,7 @@ private fun String.isNaN(): Boolean = when(this.toLowerCase()) {
*/
@PublishedApi
internal actual fun checkRadix(radix: Int): Int {
if(radix !in 2..36) {
if (radix !in 2..36) {
throw IllegalArgumentException("radix $radix was not in valid range 2..36")
}
return radix
+4 -3
View File
@@ -14,6 +14,7 @@ import kotlin.internal.LowPriorityInOverloadResolution
public open external class Promise<out T>(executor: (resolve: (T) -> Unit, reject: (Throwable) -> Unit) -> Unit) {
@LowPriorityInOverloadResolution
public open fun <S> then(onFulfilled: ((T) -> S)?): Promise<S>
@LowPriorityInOverloadResolution
public open fun <S> then(onFulfilled: ((T) -> S)?, onRejected: ((Throwable) -> S)?): Promise<S>
@@ -33,14 +34,14 @@ public open external class Promise<out T>(executor: (resolve: (T) -> Unit, rejec
// It's workaround for KT-19672 since we can fix it properly until KT-11265 isn't fixed.
inline fun <T, S> Promise<Promise<T>>.then(
noinline onFulfilled: ((T) -> S)?
noinline onFulfilled: ((T) -> S)?
): Promise<S> {
return this.unsafeCast<Promise<T>>().then(onFulfilled)
}
inline fun <T, S> Promise<Promise<T>>.then(
noinline onFulfilled: ((T) -> S)?,
noinline onRejected: ((Throwable) -> S)?
noinline onFulfilled: ((T) -> S)?,
noinline onRejected: ((Throwable) -> S)?
): Promise<S> {
return this.unsafeCast<Promise<T>>().then(onFulfilled, onRejected)
}
+1 -1
View File
@@ -8,4 +8,4 @@ package kotlin.ranges
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
public inline operator fun Float.rangeTo(that: Float): ClosedFloatingPointRange<Float> =
this.toDouble().rangeTo(that.toDouble()).unsafeCast<ClosedFloatingPointRange<Float>>()
this.toDouble().rangeTo(that.toDouble()).unsafeCast<ClosedFloatingPointRange<Float>>()
@@ -8,7 +8,7 @@ package kotlin.reflect.js.internal
import kotlin.reflect.*
internal abstract class KClassImpl<T : Any>(
internal open val jClass: JsClass<T>
internal open val jClass: JsClass<T>
) : KClass<T> {
override val annotations: List<Annotation>
get() = TODO()
@@ -65,9 +65,9 @@ internal class SimpleKClassImpl<T : Any>(jClass: JsClass<T>) : KClassImpl<T>(jCl
}
internal class PrimitiveKClassImpl<T : Any>(
jClass: JsClass<T>,
private val givenSimpleName: String,
private val isInstanceFunction: (Any?) -> Boolean
jClass: JsClass<T>,
private val givenSimpleName: String,
private val isInstanceFunction: (Any?) -> Boolean
) : KClassImpl<T>(jClass) {
override fun equals(other: Any?): Boolean {
if (other !is PrimitiveKClassImpl<*>) return false
@@ -19,10 +19,10 @@ internal object PrimitiveClasses {
val nothingClass = NothingKClassImpl
@JsName("booleanClass")
val booleanClass = PrimitiveKClassImpl(js("Boolean").unsafeCast<JsClass<Boolean>>(), "Boolean", { it is Boolean })
val booleanClass = PrimitiveKClassImpl(js("Boolean").unsafeCast<JsClass<Boolean>>(), "Boolean", { it is Boolean })
@JsName("byteClass")
val byteClass = PrimitiveKClassImpl(js("Number").unsafeCast<JsClass<Byte>>(), "Byte", { it is Byte })
val byteClass = PrimitiveKClassImpl(js("Number").unsafeCast<JsClass<Byte>>(), "Byte", { it is Byte })
@JsName("shortClass")
val shortClass = PrimitiveKClassImpl(js("Number").unsafeCast<JsClass<Short>>(), "Short", { it is Short })
@@ -10,39 +10,40 @@ import kotlin.reflect.js.internal.*
@JsName("getKClass")
internal fun <T : Any> getKClass(jClass: JsClass<T>): KClass<T> = getOrCreateKClass(jClass)
@JsName("getKClassFromExpression")
internal fun <T : Any> getKClassFromExpression(e: T): KClass<T> =
when (jsTypeOf(e)) {
"string" -> PrimitiveClasses.stringClass
"number" -> if (js("e | 0") === e) PrimitiveClasses.intClass else PrimitiveClasses.doubleClass
"boolean" -> PrimitiveClasses.booleanClass
"function" -> PrimitiveClasses.functionClass(e.asDynamic().length)
else -> {
when {
e is BooleanArray -> PrimitiveClasses.booleanArrayClass
e is CharArray -> PrimitiveClasses.charArrayClass
e is ByteArray -> PrimitiveClasses.byteArrayClass
e is ShortArray -> PrimitiveClasses.shortArrayClass
e is IntArray -> PrimitiveClasses.intArrayClass
e is LongArray -> PrimitiveClasses.longArrayClass
e is FloatArray -> PrimitiveClasses.floatArrayClass
e is DoubleArray -> PrimitiveClasses.doubleArrayClass
e is KClass<*> -> KClass::class
e is Array<*> -> PrimitiveClasses.arrayClass
else -> {
val constructor = js("Object").getPrototypeOf(e).constructor
when {
constructor === js("Object") -> PrimitiveClasses.anyClass
constructor === js("Error") -> PrimitiveClasses.throwableClass
else -> {
val jsClass: JsClass<T> = constructor
getOrCreateKClass(jsClass)
}
when (jsTypeOf(e)) {
"string" -> PrimitiveClasses.stringClass
"number" -> if (js("e | 0") === e) PrimitiveClasses.intClass else PrimitiveClasses.doubleClass
"boolean" -> PrimitiveClasses.booleanClass
"function" -> PrimitiveClasses.functionClass(e.asDynamic().length)
else -> {
when {
e is BooleanArray -> PrimitiveClasses.booleanArrayClass
e is CharArray -> PrimitiveClasses.charArrayClass
e is ByteArray -> PrimitiveClasses.byteArrayClass
e is ShortArray -> PrimitiveClasses.shortArrayClass
e is IntArray -> PrimitiveClasses.intArrayClass
e is LongArray -> PrimitiveClasses.longArrayClass
e is FloatArray -> PrimitiveClasses.floatArrayClass
e is DoubleArray -> PrimitiveClasses.doubleArrayClass
e is KClass<*> -> KClass::class
e is Array<*> -> PrimitiveClasses.arrayClass
else -> {
val constructor = js("Object").getPrototypeOf(e).constructor
when {
constructor === js("Object") -> PrimitiveClasses.anyClass
constructor === js("Error") -> PrimitiveClasses.throwableClass
else -> {
val jsClass: JsClass<T> = constructor
getOrCreateKClass(jsClass)
}
}
}
}
}.unsafeCast<KClass<T>>()
}
}.unsafeCast<KClass<T>>()
private fun <T : Any> getOrCreateKClass(jClass: JsClass<T>): KClass<T> {
if (jClass === js("String")) return PrimitiveClasses.stringClass.unsafeCast<KClass<T>>()
@@ -54,12 +55,10 @@ private fun <T : Any> getOrCreateKClass(jClass: JsClass<T>): KClass<T> {
val kClass = SimpleKClassImpl(jClass)
metadata.`$kClass$` = kClass
kClass
}
else {
} else {
metadata.`$kClass$`
}
}
else {
} else {
SimpleKClassImpl(jClass)
}
}
+3 -3
View File
@@ -71,7 +71,8 @@ public actual class Regex actual constructor(pattern: String, options: Set<Regex
/** Returns a sequence of all occurrences of a regular expression within the [input] string, beginning at the specified [startIndex].
*/
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun findAll(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult> = generateSequence({ find(input, startIndex) }, { match -> match.next() })
public actual fun findAll(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult> =
generateSequence({ find(input, startIndex) }, { match -> match.next() })
/**
* Attempts to match the entire [input] CharSequence against the pattern.
@@ -110,8 +111,7 @@ public actual class Regex actual constructor(pattern: String, options: Set<Regex
sb.append(transform(foundMatch))
lastStart = foundMatch.range.endInclusive + 1
match = foundMatch.next()
}
while (lastStart < length && match != null)
} while (lastStart < length && match != null)
if (lastStart < length) {
sb.append(input, lastStart, length)
+13 -13
View File
@@ -9,6 +9,7 @@ import kotlin.js.RegExp
@kotlin.internal.InlineOnly
internal actual inline fun String.nativeIndexOf(ch: Char, fromIndex: Int): Int = nativeIndexOf(ch.toString(), fromIndex)
@kotlin.internal.InlineOnly
internal actual inline fun String.nativeLastIndexOf(ch: Char, fromIndex: Int): Int = nativeLastIndexOf(ch.toString(), fromIndex)
@@ -43,7 +44,6 @@ public fun String.endsWith(suffix: String, ignoreCase: Boolean = false): Boolean
}
public fun String.matches(regex: String): Boolean {
val result = this.match(regex)
return result != null && result.size != 0
@@ -53,17 +53,17 @@ public actual fun CharSequence.isBlank(): Boolean = length == 0 || (if (this is
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun String?.equals(other: String?, ignoreCase: Boolean = false): Boolean =
if (this == null)
other == null
else if (!ignoreCase)
this == other
else
other != null && this.toLowerCase() == other.toLowerCase()
if (this == null)
other == null
else if (!ignoreCase)
this == other
else
other != null && this.toLowerCase() == other.toLowerCase()
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun CharSequence.regionMatches(thisOffset: Int, other: CharSequence, otherOffset: Int, length: Int, ignoreCase: Boolean = false): Boolean
= regionMatchesImpl(thisOffset, other, otherOffset, length, ignoreCase)
public actual fun CharSequence.regionMatches(thisOffset: Int, other: CharSequence, otherOffset: Int, length: Int, ignoreCase: Boolean = false): Boolean =
regionMatchesImpl(thisOffset, other, otherOffset, length, ignoreCase)
/**
@@ -116,16 +116,16 @@ public actual fun CharSequence.repeat(n: Int): String {
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String =
nativeReplace(RegExp(Regex.escape(oldValue), if (ignoreCase) "gi" else "g"), Regex.escapeReplacement(newValue))
nativeReplace(RegExp(Regex.escape(oldValue), if (ignoreCase) "gi" else "g"), Regex.escapeReplacement(newValue))
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String =
nativeReplace(RegExp(Regex.escape(oldChar.toString()), if (ignoreCase) "gi" else "g"), newChar.toString())
nativeReplace(RegExp(Regex.escape(oldChar.toString()), if (ignoreCase) "gi" else "g"), newChar.toString())
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun String.replaceFirst(oldValue: String, newValue: String, ignoreCase: Boolean = false): String =
nativeReplace(RegExp(Regex.escape(oldValue), if (ignoreCase) "i" else ""), Regex.escapeReplacement(newValue))
nativeReplace(RegExp(Regex.escape(oldValue), if (ignoreCase) "i" else ""), Regex.escapeReplacement(newValue))
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun String.replaceFirst(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String =
nativeReplace(RegExp(Regex.escape(oldChar.toString()), if (ignoreCase) "i" else ""), newChar.toString())
nativeReplace(RegExp(Regex.escape(oldChar.toString()), if (ignoreCase) "i" else ""), newChar.toString())