379cb08226
Function 'kotlin.js.js' is to be redesigned in JS IR backend, partially because it is a hard feature to support. Current implementation is unstable and can cause problems around inlining and name generator. Luckily most of its use-cases can be covered by simpler features like dynamic expressions and external declarations. Thus we are reducing it's usage in stdlib to make IR backend more stable in current state. JavaScript features that can't be covered by dynamic expression are implemented in 'jsOperators.kt' file respectively for each backend: - 'internal inline' function which calls 'js' function inside for current pre-IR backend - 'internal' function with '_hack' parameters for JS IR backend which will be later intinsicified in a compiler
61 lines
1.4 KiB
Kotlin
61 lines
1.4 KiB
Kotlin
/*
|
|
* Copyright 2010-2018 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.js
|
|
|
|
|
|
// Adopted from misc.js
|
|
|
|
fun compareTo(a: dynamic, b: dynamic): Int = when (jsTypeOf(a)) {
|
|
"number" -> when {
|
|
jsTypeOf(b) == "number" ->
|
|
doubleCompareTo(a, b)
|
|
b is Long ->
|
|
doubleCompareTo(a, b.toDouble())
|
|
else ->
|
|
primitiveCompareTo(a, b)
|
|
}
|
|
|
|
"string", "boolean" -> primitiveCompareTo(a, b)
|
|
|
|
else -> compareToDoNotIntrinsicify(a, b)
|
|
}
|
|
|
|
@DoNotIntrinsify
|
|
private fun <T : Comparable<T>> compareToDoNotIntrinsicify(a: Comparable<T>, b: T) =
|
|
a.compareTo(b)
|
|
|
|
fun primitiveCompareTo(a: dynamic, b: dynamic): Int =
|
|
when {
|
|
a < b -> -1
|
|
a > b -> 1
|
|
else -> 0
|
|
}
|
|
|
|
fun doubleCompareTo(a: dynamic, b: dynamic): Int =
|
|
when {
|
|
a < b -> -1
|
|
a > b -> 1
|
|
|
|
a === b -> {
|
|
if (a !== 0)
|
|
0
|
|
else {
|
|
val ia = 1.asDynamic() / a
|
|
if (ia === 1.asDynamic() / b) {
|
|
0
|
|
} else if (ia < 0) {
|
|
-1
|
|
} else {
|
|
1
|
|
}
|
|
}
|
|
}
|
|
|
|
a !== a ->
|
|
if (b !== b) 0 else 1
|
|
|
|
else -> -1
|
|
} |