From d21985493ee941b587d459a860e9f0ba4171b32a Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Thu, 26 Nov 2015 16:24:07 +0300 Subject: [PATCH] Mark functions with 'operator' in JS stdlib --- js/js.libraries/src/core/core.kt | 2 +- js/js.libraries/src/core/dynamic.kt | 2 +- js/js.libraries/src/core/json.kt | 4 +- js/js.libraries/src/core/kotlin_special.kt | 54 +++++++++---------- .../src/templates/SpecialJS.kt | 6 +++ 5 files changed, 37 insertions(+), 31 deletions(-) diff --git a/js/js.libraries/src/core/core.kt b/js/js.libraries/src/core/core.kt index ed97c02a1ca..8c94415b3e8 100644 --- a/js/js.libraries/src/core/core.kt +++ b/js/js.libraries/src/core/core.kt @@ -15,7 +15,7 @@ public fun typeof(a: Any?): String = noImpl @native public val undefined: Nothing? = noImpl -@native public fun MutableMap.set(key: K, value: V): V? = noImpl +@native operator fun MutableMap.set(key: K, value: V): V? = noImpl @library public fun println() {} diff --git a/js/js.libraries/src/core/dynamic.kt b/js/js.libraries/src/core/dynamic.kt index ae7f207baff..321b11ddce1 100644 --- a/js/js.libraries/src/core/dynamic.kt +++ b/js/js.libraries/src/core/dynamic.kt @@ -19,7 +19,7 @@ package kotlin.js inline fun Any?.asDynamic(): dynamic = this // TODO add the support ES6 iterators -public fun dynamic.iterator(): Iterator { +operator fun dynamic.iterator(): Iterator { val r = this return when { diff --git a/js/js.libraries/src/core/json.kt b/js/js.libraries/src/core/json.kt index c54ed9fdc87..b231852e7b4 100644 --- a/js/js.libraries/src/core/json.kt +++ b/js/js.libraries/src/core/json.kt @@ -1,8 +1,8 @@ package kotlin.js @native public class Json() { - public fun get(propertyName: String): Any? = noImpl - public fun set(propertyName: String, value: Any?): Unit = noImpl + operator fun get(propertyName: String): Any? = noImpl + operator fun set(propertyName: String, value: Any?): Unit = noImpl } public fun json(vararg pairs: Pair): Json { diff --git a/js/js.libraries/src/core/kotlin_special.kt b/js/js.libraries/src/core/kotlin_special.kt index 4cd0232763c..c0c4cc98102 100644 --- a/js/js.libraries/src/core/kotlin_special.kt +++ b/js/js.libraries/src/core/kotlin_special.kt @@ -285,7 +285,7 @@ public inline fun ShortArray.copyOfRange(fromIndex: Int, toIndex: Int): ShortArr * Returns an array containing all elements of the original array and then the given [element]. */ @Suppress("NOTHING_TO_INLINE") -public inline fun Array.plus(element: T): Array { +public inline operator fun Array.plus(element: T): Array { return this.asDynamic().concat(arrayOf(element)) } @@ -293,7 +293,7 @@ public inline fun Array.plus(element: T): Array { * Returns an array containing all elements of the original array and then the given [element]. */ @Suppress("NOTHING_TO_INLINE") -public inline fun BooleanArray.plus(element: Boolean): BooleanArray { +public inline operator fun BooleanArray.plus(element: Boolean): BooleanArray { return this.asDynamic().concat(arrayOf(element)) } @@ -301,7 +301,7 @@ public inline fun BooleanArray.plus(element: Boolean): BooleanArray { * Returns an array containing all elements of the original array and then the given [element]. */ @Suppress("NOTHING_TO_INLINE") -public inline fun ByteArray.plus(element: Byte): ByteArray { +public inline operator fun ByteArray.plus(element: Byte): ByteArray { return this.asDynamic().concat(arrayOf(element)) } @@ -309,7 +309,7 @@ public inline fun ByteArray.plus(element: Byte): ByteArray { * Returns an array containing all elements of the original array and then the given [element]. */ @Suppress("NOTHING_TO_INLINE") -public inline fun CharArray.plus(element: Char): CharArray { +public inline operator fun CharArray.plus(element: Char): CharArray { return this.asDynamic().concat(arrayOf(element)) } @@ -317,7 +317,7 @@ public inline fun CharArray.plus(element: Char): CharArray { * Returns an array containing all elements of the original array and then the given [element]. */ @Suppress("NOTHING_TO_INLINE") -public inline fun DoubleArray.plus(element: Double): DoubleArray { +public inline operator fun DoubleArray.plus(element: Double): DoubleArray { return this.asDynamic().concat(arrayOf(element)) } @@ -325,7 +325,7 @@ public inline fun DoubleArray.plus(element: Double): DoubleArray { * Returns an array containing all elements of the original array and then the given [element]. */ @Suppress("NOTHING_TO_INLINE") -public inline fun FloatArray.plus(element: Float): FloatArray { +public inline operator fun FloatArray.plus(element: Float): FloatArray { return this.asDynamic().concat(arrayOf(element)) } @@ -333,7 +333,7 @@ public inline fun FloatArray.plus(element: Float): FloatArray { * Returns an array containing all elements of the original array and then the given [element]. */ @Suppress("NOTHING_TO_INLINE") -public inline fun IntArray.plus(element: Int): IntArray { +public inline operator fun IntArray.plus(element: Int): IntArray { return this.asDynamic().concat(arrayOf(element)) } @@ -341,7 +341,7 @@ public inline fun IntArray.plus(element: Int): IntArray { * Returns an array containing all elements of the original array and then the given [element]. */ @Suppress("NOTHING_TO_INLINE") -public inline fun LongArray.plus(element: Long): LongArray { +public inline operator fun LongArray.plus(element: Long): LongArray { return this.asDynamic().concat(arrayOf(element)) } @@ -349,70 +349,70 @@ public inline fun LongArray.plus(element: Long): LongArray { * Returns an array containing all elements of the original array and then the given [element]. */ @Suppress("NOTHING_TO_INLINE") -public inline fun ShortArray.plus(element: Short): ShortArray { +public inline operator fun ShortArray.plus(element: Short): ShortArray { return this.asDynamic().concat(arrayOf(element)) } /** * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. */ -public fun Array.plus(elements: Collection): Array { +public operator fun Array.plus(elements: Collection): Array { return arrayPlusCollection(this, elements) } /** * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. */ -public fun BooleanArray.plus(elements: Collection): BooleanArray { +public operator fun BooleanArray.plus(elements: Collection): BooleanArray { return arrayPlusCollection(this, elements) } /** * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. */ -public fun ByteArray.plus(elements: Collection): ByteArray { +public operator fun ByteArray.plus(elements: Collection): ByteArray { return arrayPlusCollection(this, elements) } /** * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. */ -public fun CharArray.plus(elements: Collection): CharArray { +public operator fun CharArray.plus(elements: Collection): CharArray { return arrayPlusCollection(this, elements) } /** * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. */ -public fun DoubleArray.plus(elements: Collection): DoubleArray { +public operator fun DoubleArray.plus(elements: Collection): DoubleArray { return arrayPlusCollection(this, elements) } /** * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. */ -public fun FloatArray.plus(elements: Collection): FloatArray { +public operator fun FloatArray.plus(elements: Collection): FloatArray { return arrayPlusCollection(this, elements) } /** * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. */ -public fun IntArray.plus(elements: Collection): IntArray { +public operator fun IntArray.plus(elements: Collection): IntArray { return arrayPlusCollection(this, elements) } /** * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. */ -public fun LongArray.plus(elements: Collection): LongArray { +public operator fun LongArray.plus(elements: Collection): LongArray { return arrayPlusCollection(this, elements) } /** * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. */ -public fun ShortArray.plus(elements: Collection): ShortArray { +public operator fun ShortArray.plus(elements: Collection): ShortArray { return arrayPlusCollection(this, elements) } @@ -420,7 +420,7 @@ public fun ShortArray.plus(elements: Collection): ShortArray { * Returns an array containing all elements of the original array and then all elements of the given [elements] array. */ @Suppress("NOTHING_TO_INLINE") -public inline fun Array.plus(elements: Array): Array { +public inline operator fun Array.plus(elements: Array): Array { return this.asDynamic().concat(elements) } @@ -428,7 +428,7 @@ public inline fun Array.plus(elements: Array): Array { * Returns an array containing all elements of the original array and then all elements of the given [elements] array. */ @Suppress("NOTHING_TO_INLINE") -public inline fun BooleanArray.plus(elements: BooleanArray): BooleanArray { +public inline operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray { return this.asDynamic().concat(elements) } @@ -436,7 +436,7 @@ public inline fun BooleanArray.plus(elements: BooleanArray): BooleanArray { * Returns an array containing all elements of the original array and then all elements of the given [elements] array. */ @Suppress("NOTHING_TO_INLINE") -public inline fun ByteArray.plus(elements: ByteArray): ByteArray { +public inline operator fun ByteArray.plus(elements: ByteArray): ByteArray { return this.asDynamic().concat(elements) } @@ -444,7 +444,7 @@ public inline fun ByteArray.plus(elements: ByteArray): ByteArray { * Returns an array containing all elements of the original array and then all elements of the given [elements] array. */ @Suppress("NOTHING_TO_INLINE") -public inline fun CharArray.plus(elements: CharArray): CharArray { +public inline operator fun CharArray.plus(elements: CharArray): CharArray { return this.asDynamic().concat(elements) } @@ -452,7 +452,7 @@ public inline fun CharArray.plus(elements: CharArray): CharArray { * Returns an array containing all elements of the original array and then all elements of the given [elements] array. */ @Suppress("NOTHING_TO_INLINE") -public inline fun DoubleArray.plus(elements: DoubleArray): DoubleArray { +public inline operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray { return this.asDynamic().concat(elements) } @@ -460,7 +460,7 @@ public inline fun DoubleArray.plus(elements: DoubleArray): DoubleArray { * Returns an array containing all elements of the original array and then all elements of the given [elements] array. */ @Suppress("NOTHING_TO_INLINE") -public inline fun FloatArray.plus(elements: FloatArray): FloatArray { +public inline operator fun FloatArray.plus(elements: FloatArray): FloatArray { return this.asDynamic().concat(elements) } @@ -468,7 +468,7 @@ public inline fun FloatArray.plus(elements: FloatArray): FloatArray { * Returns an array containing all elements of the original array and then all elements of the given [elements] array. */ @Suppress("NOTHING_TO_INLINE") -public inline fun IntArray.plus(elements: IntArray): IntArray { +public inline operator fun IntArray.plus(elements: IntArray): IntArray { return this.asDynamic().concat(elements) } @@ -476,7 +476,7 @@ public inline fun IntArray.plus(elements: IntArray): IntArray { * Returns an array containing all elements of the original array and then all elements of the given [elements] array. */ @Suppress("NOTHING_TO_INLINE") -public inline fun LongArray.plus(elements: LongArray): LongArray { +public inline operator fun LongArray.plus(elements: LongArray): LongArray { return this.asDynamic().concat(elements) } @@ -484,7 +484,7 @@ public inline fun LongArray.plus(elements: LongArray): LongArray { * Returns an array containing all elements of the original array and then all elements of the given [elements] array. */ @Suppress("NOTHING_TO_INLINE") -public inline fun ShortArray.plus(elements: ShortArray): ShortArray { +public inline operator fun ShortArray.plus(elements: ShortArray): ShortArray { return this.asDynamic().concat(elements) } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt index dac76485751..6c6d70a253f 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt @@ -91,6 +91,8 @@ fun specialJS(): List { templates add f("plus(element: T)") { + operator(true) + only(ArraysOfObjects, ArraysOfPrimitives) returns("SELF") returns(ArraysOfObjects) { "Array" } @@ -105,6 +107,8 @@ fun specialJS(): List { } templates add f("plus(elements: Collection)") { + operator(true) + only(ArraysOfObjects, ArraysOfPrimitives) returns("SELF") returns(ArraysOfObjects) { "Array" } @@ -118,6 +122,8 @@ fun specialJS(): List { // This overload can cause nulls if array size is expanding, hence different return overload templates add f("plus(elements: SELF)") { + operator(true) + only(ArraysOfObjects, ArraysOfPrimitives) doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] array." } inline(true)