Introduce unsafeCast function. Provide more JS-idiomatic emptyArray() implementation.
#KT-14034 Fixed
This commit is contained in:
@@ -26,7 +26,7 @@ internal fun <T> copyToArray(collection: Collection<T>): Array<T> {
|
||||
return if (collection.asDynamic().toArray !== undefined)
|
||||
collection.asDynamic().toArray()
|
||||
else
|
||||
copyToArrayImpl(collection).asDynamic()
|
||||
copyToArrayImpl(collection).unsafeCast<Array<T>>()
|
||||
}
|
||||
|
||||
@JsName("copyToArrayImpl")
|
||||
|
||||
@@ -16,17 +16,22 @@
|
||||
|
||||
package kotlin.js
|
||||
|
||||
inline fun Any?.asDynamic(): dynamic = this
|
||||
public inline fun Any?.asDynamic(): dynamic = this
|
||||
|
||||
/**
|
||||
* Reinterprets this value as a value of the specified type [T] without any actual type checking.
|
||||
*/
|
||||
public inline fun <T> Any?.unsafeCast(): @kotlin.internal.NoInfer T = this.asDynamic()
|
||||
|
||||
// TODO add the support ES6 iterators
|
||||
operator fun dynamic.iterator(): Iterator<dynamic> {
|
||||
val r = this
|
||||
public operator fun dynamic.iterator(): Iterator<dynamic> {
|
||||
val r: Any? = this
|
||||
|
||||
return when {
|
||||
this["iterator"] != null ->
|
||||
this["iterator"]()
|
||||
js("Array.isArray(r)") ->
|
||||
(this as Array<*>).iterator()
|
||||
r.unsafeCast<Array<*>>().iterator()
|
||||
|
||||
else ->
|
||||
(r as Iterable<*>).iterator()
|
||||
|
||||
@@ -14,119 +14,119 @@ import kotlin.comparisons.*
|
||||
* Returns a [List] that wraps the original array.
|
||||
*/
|
||||
public fun <T> Array<out T>.asList(): List<T> {
|
||||
return ArrayList<T>(this as Array<Any?>)
|
||||
return ArrayList<T>(this.unsafeCast<Array<Any?>>())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] that wraps the original array.
|
||||
*/
|
||||
public inline fun ByteArray.asList(): List<Byte> {
|
||||
return (this as Array<Byte>).asList()
|
||||
return this.unsafeCast<Array<Byte>>().asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] that wraps the original array.
|
||||
*/
|
||||
public inline fun ShortArray.asList(): List<Short> {
|
||||
return (this as Array<Short>).asList()
|
||||
return this.unsafeCast<Array<Short>>().asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] that wraps the original array.
|
||||
*/
|
||||
public inline fun IntArray.asList(): List<Int> {
|
||||
return (this as Array<Int>).asList()
|
||||
return this.unsafeCast<Array<Int>>().asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] that wraps the original array.
|
||||
*/
|
||||
public inline fun LongArray.asList(): List<Long> {
|
||||
return (this as Array<Long>).asList()
|
||||
return this.unsafeCast<Array<Long>>().asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] that wraps the original array.
|
||||
*/
|
||||
public inline fun FloatArray.asList(): List<Float> {
|
||||
return (this as Array<Float>).asList()
|
||||
return this.unsafeCast<Array<Float>>().asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] that wraps the original array.
|
||||
*/
|
||||
public inline fun DoubleArray.asList(): List<Double> {
|
||||
return (this as Array<Double>).asList()
|
||||
return this.unsafeCast<Array<Double>>().asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] that wraps the original array.
|
||||
*/
|
||||
public inline fun BooleanArray.asList(): List<Boolean> {
|
||||
return (this as Array<Boolean>).asList()
|
||||
return this.unsafeCast<Array<Boolean>>().asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [List] that wraps the original array.
|
||||
*/
|
||||
public inline fun CharArray.asList(): List<Char> {
|
||||
return (this as Array<Char>).asList()
|
||||
return this.unsafeCast<Array<Char>>().asList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun ByteArray.toTypedArray(): Array<Byte> {
|
||||
return copyOf() as Array<Byte>
|
||||
return copyOf().unsafeCast<Array<Byte>>()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun ShortArray.toTypedArray(): Array<Short> {
|
||||
return copyOf() as Array<Short>
|
||||
return copyOf().unsafeCast<Array<Short>>()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun IntArray.toTypedArray(): Array<Int> {
|
||||
return copyOf() as Array<Int>
|
||||
return copyOf().unsafeCast<Array<Int>>()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun LongArray.toTypedArray(): Array<Long> {
|
||||
return copyOf() as Array<Long>
|
||||
return copyOf().unsafeCast<Array<Long>>()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun FloatArray.toTypedArray(): Array<Float> {
|
||||
return copyOf() as Array<Float>
|
||||
return copyOf().unsafeCast<Array<Float>>()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun DoubleArray.toTypedArray(): Array<Double> {
|
||||
return copyOf() as Array<Double>
|
||||
return copyOf().unsafeCast<Array<Double>>()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun BooleanArray.toTypedArray(): Array<Boolean> {
|
||||
return copyOf() as Array<Boolean>
|
||||
return copyOf().unsafeCast<Array<Boolean>>()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a *typed* object array containing all of the elements of this primitive array.
|
||||
*/
|
||||
public fun CharArray.toTypedArray(): Array<Char> {
|
||||
return copyOf() as Array<Char>
|
||||
return copyOf().unsafeCast<Array<Char>>()
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,7 +21,7 @@ import java.util.*
|
||||
/**
|
||||
* Returns an empty array of the specified type [T].
|
||||
*/
|
||||
public inline fun <reified T> emptyArray(): Array<T> = arrayOfNulls<T>(0) as Array<T>
|
||||
public inline fun <reified T> emptyArray(): Array<T> = js("[]")
|
||||
|
||||
@library
|
||||
public fun <T> arrayOf(vararg elements: T): Array<T> = noImpl
|
||||
@@ -70,7 +70,7 @@ public fun <T> lazy(lock: Any?, initializer: () -> T): Lazy<T> = UnsafeLazyImpl(
|
||||
|
||||
|
||||
internal fun <T> arrayOfNulls(reference: Array<out T>, size: Int): Array<T> {
|
||||
return arrayOfNulls<Any>(size) as Array<T>
|
||||
return arrayOfNulls<Any>(size).unsafeCast<Array<T>>()
|
||||
}
|
||||
|
||||
internal fun arrayCopyResize(source: dynamic, newSize: Int, defaultValue: Any?): dynamic {
|
||||
|
||||
@@ -50,4 +50,4 @@ public fun RegExp.reset() {
|
||||
}
|
||||
|
||||
|
||||
public inline fun RegExpMatch.asArray(): Array<out String?> = this.asDynamic()
|
||||
public inline fun RegExpMatch.asArray(): Array<out String?> = unsafeCast<Array<out String?>>()
|
||||
@@ -27,12 +27,12 @@ fun specialJS(): List<GenericFunction> {
|
||||
returns("List<T>")
|
||||
body(ArraysOfObjects) {
|
||||
"""
|
||||
return ArrayList<T>(this as Array<Any?>)
|
||||
return ArrayList<T>(this.unsafeCast<Array<Any?>>())
|
||||
"""
|
||||
}
|
||||
|
||||
inline(true, ArraysOfPrimitives)
|
||||
body(ArraysOfPrimitives) {"""return (this as Array<T>).asList()"""}
|
||||
body(ArraysOfPrimitives) {"""return this.unsafeCast<Array<T>>().asList()"""}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ fun specialJS(): List<GenericFunction> {
|
||||
}
|
||||
body {
|
||||
"""
|
||||
return copyOf() as Array<T>
|
||||
return copyOf().unsafeCast<Array<T>>()
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user