Reorganize stdlib-js sources specific to the current JS backend

Move kotlin-stdlib-js project and the sources specific to the current backend to 'stdlib/js-v1' directory,
but leave sources that can be shared with the new IR backend in the common 'stdlib/js' location
with exception for 'stdlib/js/src/generated', which is used exclusively for current backend.
This simplifies sourceset configuration when building stdlib with the new backend.
This commit is contained in:
Svyatoslav Kuzmich
2019-04-11 16:25:40 +03:00
parent 9dd9efd4aa
commit b1d303b027
70 changed files with 72 additions and 115 deletions
@@ -0,0 +1,46 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. 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 (typeOf(a)) {
"number" -> when {
typeOf(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 =
js("a < b ? -1 : a > b ? 1 : 0").unsafeCast<Int>()
fun doubleCompareTo(a: dynamic, b: dynamic): Int =
js("""
if (a < b) return -1;
if (a > b) return 1;
if (a === b) {
if (a !== 0) return 0;
var ia = 1 / a;
return ia === 1 / b ? 0 : (ia < 0 ? -1 : 1);
}
return a !== a ? (b !== b ? 0 : 1) : -1
""").unsafeCast<Int>()