From f20ee6df4bc581739767bdc40c662c6efc6c5f4c Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Fri, 10 Oct 2014 16:50:13 +0400 Subject: [PATCH] JS backend: fixes in ranges: * map all number ranges and progressions; * add missed hashCode and static EMPTY to Range implementations; * add missed hashCode and equals to Progression implementations; * fix type of start and end in CharRange and CharProgression. --- .../translate/context/StandardClasses.java | 15 ++-- js/js.translator/testData/kotlin_lib.js | 68 ++++++++++++++----- 2 files changed, 63 insertions(+), 20 deletions(-) diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/context/StandardClasses.java b/js/js.translator/src/org/jetbrains/k2js/translate/context/StandardClasses.java index 043ece3afe5..2002939e6a1 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/context/StandardClasses.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/context/StandardClasses.java @@ -24,6 +24,7 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe; import org.jetbrains.jet.lang.resolve.name.Name; +import org.jetbrains.jet.lang.types.lang.PrimitiveType; import java.util.Map; @@ -95,8 +96,16 @@ public final class StandardClasses { private static void declareKotlinStandardClasses(@NotNull StandardClasses standardClasses) { standardClasses.declare().forFQ("kotlin.Iterator").kotlinClass("Iterator").methods("next").properties("hasNext"); - standardClasses.declare().forFQ("kotlin.IntRange").kotlinClass("NumberRange") - .methods("iterator", "contains").properties("start", "end", "increment"); + for (PrimitiveType type : PrimitiveType.NUMBER_TYPES) { + if (type == PrimitiveType.CHAR || type == PrimitiveType.LONG) continue; + + String typeName = type.getTypeName().asString(); + standardClasses.declare().forFQ("kotlin." + typeName + "Range").kotlinClass("NumberRange") + .methods("iterator", "contains").properties("start", "end", "increment"); + + standardClasses.declare().forFQ("kotlin." + typeName + "Progression").kotlinClass("NumberProgression") + .methods("iterator", "contains").properties("start", "end", "increment"); + } standardClasses.declare().forFQ("kotlin.LongRange").kotlinClass("LongRange") .methods("iterator", "contains").properties("start", "end", "increment"); @@ -104,8 +113,6 @@ public final class StandardClasses { standardClasses.declare().forFQ("kotlin.CharRange").kotlinClass("CharRange") .methods("iterator", "contains").properties("start", "end", "increment"); - standardClasses.declare().forFQ("kotlin.IntProgression").kotlinClass("NumberProgression") - .methods("iterator", "contains").properties("start", "end", "increment"); standardClasses.declare().forFQ("kotlin.LongProgression").kotlinClass("LongProgression") .methods("iterator", "contains").properties("start", "end", "increment"); diff --git a/js/js.translator/testData/kotlin_lib.js b/js/js.translator/testData/kotlin_lib.js index 040e45a9a40..18f2b849198 100644 --- a/js/js.translator/testData/kotlin_lib.js +++ b/js/js.translator/testData/kotlin_lib.js @@ -613,7 +613,14 @@ isEmpty: function () { return this.start > this.end; }, + hashCode: function() { + return this.isEmpty() ? -1 : (31 * this.start|0 + this.end|0); + }, equals_za3rmp$: isSameNotNullRanges + }, { + object_initializer$: function () { + return { EMPTY : new this(1, 0) }; + } }); Kotlin.NumberProgression = Kotlin.createClassNow(null, @@ -627,7 +634,11 @@ }, isEmpty: function() { return this.increment > 0 ? this.start > this.end : this.start < this.end; - } + }, + hashCode: function() { + return this.isEmpty() ? -1 : (31 * (31 * this.start|0 + this.end|0) + this.increment|0); + }, + equals_za3rmp$: isSameNotNullRanges }); Kotlin.LongRangeIterator = Kotlin.createClassNow(Kotlin.Iterator, @@ -665,8 +676,15 @@ isEmpty: function () { return this.start.compare(this.end) > 0; }, - equals_za3rmp$: isSameNotNullRanges - }); + hashCode: function() { + return this.isEmpty() ? -1 : (31 * this.start.toInt() + this.end.toInt()); + }, + equals_za3rmp$: isSameNotNullRanges + }, { + object_initializer$: function () { + return { EMPTY : new this(Kotlin.Long.ONE, Kotlin.Long.ZERO) }; + } + }); Kotlin.LongProgression = Kotlin.createClassNow(null, function (start, end, increment) { @@ -679,8 +697,12 @@ }, isEmpty: function() { return this.increment.isNegative() ? this.start.compare(this.end) < 0 : this.start.compare(this.end) > 0; - } - }); + }, + hashCode: function() { + return this.isEmpty() ? -1 : (31 * (31 * this.start.toInt() + this.end.toInt()) + this.increment.toInt()); + }, + equals_za3rmp$: isSameNotNullRanges + }); Kotlin.CharRangeIterator = Kotlin.createClassNow(Kotlin.RangeIterator, function (start, end, increment) { @@ -690,40 +712,54 @@ var value = this.i; this.i = this.i + this.increment; return String.fromCharCode(value); - }, + } }); Kotlin.CharRange = Kotlin.createClassNow(null, function (start, end) { - this.start = start.charCodeAt(0); - this.end = end.charCodeAt(0); + this.start = start; + this.startCode = start.charCodeAt(0); + this.end = end; + this.endCode = end.charCodeAt(0); this.increment = 1; }, { contains: function (char) { - var code = char.charCodeAt(0) - return this.start <= code && code <= this.end; + return this.start <= char && char <= this.end; }, iterator: function () { - return new Kotlin.CharRangeIterator(this.start, this.end, this.increment); + return new Kotlin.CharRangeIterator(this.startCode, this.endCode, this.increment); }, isEmpty: function () { return this.start > this.end; }, + hashCode: function() { + return this.isEmpty() ? -1 : (31 * this.startCode|0 + this.endCode|0); + }, equals_za3rmp$: isSameNotNullRanges + }, { + object_initializer$: function () { + return { EMPTY : new this(Kotlin.toChar(1), Kotlin.toChar(0)) }; + } }); - Kotlin.CharNumberProgression = Kotlin.createClassNow(null, + Kotlin.CharProgression = Kotlin.createClassNow(null, function (start, end, increment) { - this.start = start.charCodeAt(0); - this.end = end.charCodeAt(0); + this.start = start; + this.startCode = start.charCodeAt(0); + this.end = end; + this.endCode = end.charCodeAt(0); this.increment = increment; }, { iterator: function () { - return new Kotlin.CharRangeIterator(this.start, this.end, this.increment); + return new Kotlin.CharRangeIterator(this.startCode, this.endCode, this.increment); }, isEmpty: function() { return this.increment > 0 ? this.start > this.end : this.start < this.end; - } + }, + hashCode: function() { + return this.isEmpty() ? -1 : (31 * (31 * this.startCode|0 + this.endCode|0) + this.increment|0); + }, + equals_za3rmp$: isSameNotNullRanges }); /**