Refact stdlib generator, add support for different backends

[JS IR BE] Runtime fixes
 * Do not generate external declarations for IR BE
 * Move `arrayToString` helper function out of shared JS stdlib
 * Fix arrays type check for IR BE
This commit is contained in:
Roman Artemev
2018-11-12 14:28:47 +03:00
committed by romanart
parent 05b1a99022
commit c5922bf74b
23 changed files with 1847 additions and 68 deletions
@@ -5,9 +5,60 @@
package kotlin.collections
import kotlin.js.*
// Copied from libraries/stdlib/js/src/kotlin/collections/utils.kt
// Current inliner doesn't rename symbols inside `js` fun
@Suppress("UNUSED_PARAMETER")
internal fun deleteProperty(obj: Any, property: Any) {
js("delete obj[property]")
}
internal fun arrayToString(array: Array<*>) = array.joinToString(", ", "[", "]") { toString(it) }
internal fun <T> Array<out T>.contentDeepHashCodeInternal(): Int {
var result = 1
for (element in this) {
val elementHash = when {
element == null -> 0
isArrayish(element) -> (element.unsafeCast<Array<*>>()).contentDeepHashCodeInternal()
element is UByteArray -> element.contentHashCode()
element is UShortArray -> element.contentHashCode()
element is UIntArray -> element.contentHashCode()
element is ULongArray -> element.contentHashCode()
else -> element.hashCode()
}
result = 31 * result + elementHash
}
return result
}
internal fun <T> T.contentEqualsInternal(other: T): Boolean {
val a = this.asDynamic()
val b = other.asDynamic()
if (a === b) return true
if (!isArrayish(b) || a.length != b.length) return false
for (i in 0 until a.length) {
if (a[i] != b[i]) {
return false
}
}
return true
}
internal fun <T> T.contentHashCodeInternal(): Int {
val a = this.asDynamic()
var result = 1
for (i in 0 until a.length) {
result = result * 31 + hashCode(a[i])
}
return result
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,29 @@
/*
* 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.collections
//
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.js.*
/**
* Reverses elements in the list in-place.
*/
public actual fun <T> MutableList<T>.reverse(): Unit {
val midPoint = (size / 2) - 1
if (midPoint < 0) return
var reverseIndex = lastIndex
for (index in 0..midPoint) {
val tmp = this[index]
this[index] = this[reverseIndex]
this[reverseIndex] = tmp
reverseIndex--
}
}
@@ -0,0 +1,284 @@
/*
* 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.comparisons
//
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.js.*
/**
* Returns the greater of two values.
* If values are equal, returns the first one.
*/
@SinceKotlin("1.1")
public actual fun <T : Comparable<T>> maxOf(a: T, b: T): T {
return if (a >= b) a else b
}
/**
* Returns the greater of two values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun maxOf(a: Byte, b: Byte): Byte {
return Math.max(a.toInt(), b.toInt()).unsafeCast<Byte>()
}
/**
* Returns the greater of two values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun maxOf(a: Short, b: Short): Short {
return Math.max(a.toInt(), b.toInt()).unsafeCast<Short>()
}
/**
* Returns the greater of two values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun maxOf(a: Int, b: Int): Int {
return Math.max(a, b)
}
/**
* Returns the greater of two values.
*/
@SinceKotlin("1.1")
@Suppress("DEPRECATION_ERROR", "NOTHING_TO_INLINE")
public actual inline fun maxOf(a: Long, b: Long): Long {
return if (a >= b) a else b
}
/**
* Returns the greater of two values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun maxOf(a: Float, b: Float): Float {
return Math.max(a, b)
}
/**
* Returns the greater of two values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun maxOf(a: Double, b: Double): Double {
return Math.max(a, b)
}
/**
* Returns the greater of three values.
*/
@SinceKotlin("1.1")
public actual fun <T : Comparable<T>> maxOf(a: T, b: T, c: T): T {
return maxOf(a, maxOf(b, c))
}
/**
* Returns the greater of three values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun maxOf(a: Byte, b: Byte, c: Byte): Byte {
return Math.max(a.toInt(), b.toInt(), c.toInt()).unsafeCast<Byte>()
}
/**
* Returns the greater of three values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun maxOf(a: Short, b: Short, c: Short): Short {
return Math.max(a.toInt(), b.toInt(), c.toInt()).unsafeCast<Short>()
}
/**
* Returns the greater of three values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun maxOf(a: Int, b: Int, c: Int): Int {
return Math.max(a, b, c)
}
/**
* Returns the greater of three values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
public actual inline fun maxOf(a: Long, b: Long, c: Long): Long {
return maxOf(a, maxOf(b, c))
}
/**
* Returns the greater of three values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun maxOf(a: Float, b: Float, c: Float): Float {
return Math.max(a, b, c)
}
/**
* Returns the greater of three values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun maxOf(a: Double, b: Double, c: Double): Double {
return Math.max(a, b, c)
}
/**
* Returns the smaller of two values.
* If values are equal, returns the first one.
*/
@SinceKotlin("1.1")
public actual fun <T : Comparable<T>> minOf(a: T, b: T): T {
return if (a <= b) a else b
}
/**
* Returns the smaller of two values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun minOf(a: Byte, b: Byte): Byte {
return Math.min(a.toInt(), b.toInt()).unsafeCast<Byte>()
}
/**
* Returns the smaller of two values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun minOf(a: Short, b: Short): Short {
return Math.min(a.toInt(), b.toInt()).unsafeCast<Short>()
}
/**
* Returns the smaller of two values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun minOf(a: Int, b: Int): Int {
return Math.min(a, b)
}
/**
* Returns the smaller of two values.
*/
@SinceKotlin("1.1")
@Suppress("DEPRECATION_ERROR", "NOTHING_TO_INLINE")
public actual inline fun minOf(a: Long, b: Long): Long {
return if (a <= b) a else b
}
/**
* Returns the smaller of two values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun minOf(a: Float, b: Float): Float {
return Math.min(a, b)
}
/**
* Returns the smaller of two values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun minOf(a: Double, b: Double): Double {
return Math.min(a, b)
}
/**
* Returns the smaller of three values.
*/
@SinceKotlin("1.1")
public actual fun <T : Comparable<T>> minOf(a: T, b: T, c: T): T {
return minOf(a, minOf(b, c))
}
/**
* Returns the smaller of three values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun minOf(a: Byte, b: Byte, c: Byte): Byte {
return Math.min(a.toInt(), b.toInt(), c.toInt()).unsafeCast<Byte>()
}
/**
* Returns the smaller of three values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun minOf(a: Short, b: Short, c: Short): Short {
return Math.min(a.toInt(), b.toInt(), c.toInt()).unsafeCast<Short>()
}
/**
* Returns the smaller of three values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun minOf(a: Int, b: Int, c: Int): Int {
return Math.min(a, b, c)
}
/**
* Returns the smaller of three values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
public actual inline fun minOf(a: Long, b: Long, c: Long): Long {
return minOf(a, minOf(b, c))
}
/**
* Returns the smaller of three values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun minOf(a: Float, b: Float, c: Float): Float {
return Math.min(a, b, c)
}
/**
* Returns the smaller of three values.
*/
@SinceKotlin("1.1")
@kotlin.internal.InlineOnly
@Suppress("DEPRECATION_ERROR")
public actual inline fun minOf(a: Double, b: Double, c: Double): Double {
return Math.min(a, b, c)
}
@@ -103,14 +103,14 @@ public fun isChar(c: Any): Boolean {
}
// TODO: Distinguish Boolean/Byte and Short/Char
public fun isBooleanArray(a: dynamic): Boolean = isArray(a) && a.asDynamic().`$type$` == "BooleanArray"
public fun isBooleanArray(a: dynamic): Boolean = isArray(a) && a.`$type$` == "BooleanArray"
public fun isByteArray(a: dynamic): Boolean = js("a instanceof Int8Array").unsafeCast<Boolean>()
public fun isShortArray(a: dynamic): Boolean = js("a instanceof Int16Array").unsafeCast<Boolean>()
public fun isCharArray(a: dynamic): Boolean = isArray(a) && a.asDynamic().`$type$` == "CharArray"
public fun isCharArray(a: dynamic): Boolean = isArray(a) && a.`$type$` == "CharArray"
public fun isIntArray(a: dynamic): Boolean = js("a instanceof Int32Array").unsafeCast<Boolean>()
public fun isFloatArray(a: dynamic): Boolean = js("a instanceof Float32Array").unsafeCast<Boolean>()
public fun isDoubleArray(a: dynamic): Boolean = js("a instanceof Float64Array").unsafeCast<Boolean>()
public fun isLongArray(a: dynamic): Boolean = isArray(a) && a.asDynamic().`$type$` == "LongArray"
public fun isLongArray(a: dynamic): Boolean = isArray(a) && a.`$type$` == "LongArray"
internal fun jsIn(x: String, y: dynamic): Boolean = js("x in y")
+16 -1
View File
@@ -288,12 +288,27 @@ if (typeof ArrayBuffer.isView === "undefined") {
}
// Patch sort to work with TypedArrays if needed.
// TODO: consider to remove following function and replace it with `Kotlin.doubleCompareTo` (see misc.js)
var totalOrderComparator = function (a, b) {
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
};
for (var i = 0; i < arrays.length; ++i) {
var TypedArray = arrays[i];
if (typeof TypedArray.prototype.sort === "undefined") {
Object.defineProperty(TypedArray.prototype, 'sort', {
value: function(compareFunction) {
return Array.prototype.sort.call(this, compareFunction);
return Array.prototype.sort.call(this, compareFunction || totalOrderComparator);
}
});
}
@@ -6,7 +6,6 @@
package kotlin.collections
import kotlin.comparisons.naturalOrder
import kotlin.internal.InlineOnly
import kotlin.random.Random
/** Returns the array if it's not `null`, or an empty array otherwise. */
@@ -50,10 +49,6 @@ internal actual fun <T> copyToArrayImpl(collection: Collection<*>, array: Array<
return array
}
@library("arrayToString")
@Suppress("UNUSED_PARAMETER")
internal fun arrayToString(array: Array<*>): String = definedExternally
/**
* Returns an immutable list containing only the specified object [element].
*/
@@ -0,0 +1,10 @@
/*
* 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.collections
@library("arrayToString")
@Suppress("UNUSED_PARAMETER")
internal fun arrayToString(array: Array<*>): String = definedExternally