JS backend: add support for Long type

This commit is contained in:
Michael Nedzelsky
2014-09-23 19:00:16 +04:00
parent d1d722546e
commit dca6e31519
24 changed files with 643 additions and 134 deletions
+97 -10
View File
@@ -85,7 +85,7 @@
return "[" + a.join(", ") + "]";
};
Kotlin.compareTo = function(a, b) {
Kotlin.compareTo = function (a, b) {
var type = typeof a;
if (type == "number" || type == "string") {
return a < b ? -1 : a > b ? 1 : 0;
@@ -93,22 +93,46 @@
return a.compareTo_za3rmp$(b);
};
Kotlin.primitiveCompareTo = function(a, b) {
Kotlin.primitiveCompareTo = function (a, b) {
return a < b ? -1 : a > b ? 1 : 0;
};
Kotlin.isNumber = function (a) {
return typeof a == "number";
return typeof a == "number" || a instanceof Kotlin.Long;
};
Kotlin.toShort = function(a) {
Kotlin.toInt = function (a) {
return a | 0;
};
Kotlin.toShort = function (a) {
return (a & 0xFFFF) << 16 >> 16;
};
Kotlin.toByte = function(a) {
Kotlin.toByte = function (a) {
return (a & 0xFF) << 24 >> 24;
};
Kotlin.numberToLong = function (a) {
return a instanceof Kotlin.Long ? a : Kotlin.Long.fromNumber(a);
};
Kotlin.numberToInt = function (a) {
return a instanceof Kotlin.Long ? a.toInt() : (a | 0);
};
Kotlin.numberToShort = function (a) {
return Kotlin.toShort(Kotlin.numberToInt(a));
};
Kotlin.numberToByte = function (a) {
return Kotlin.toByte(Kotlin.numberToInt(a));
};
Kotlin.numberToDouble = function (a) {
return +a;
};
Kotlin.intUpto = function (from, to) {
return new Kotlin.NumberRange(from, to);
};
@@ -530,6 +554,15 @@
}
});
function isSameNotNullRanges(other) {
var classObject = this.constructor;
if (this instanceof classObject && other instanceof classObject) {
return this.isEmpty() && other.isEmpty() ||
(this.start === other.start && this.end === other.end && this.increment === other.increment);
}
return false;
}
Kotlin.NumberRange = Kotlin.createClassNow(null,
function (start, end) {
this.start = start;
@@ -545,11 +578,7 @@
isEmpty: function () {
return this.start > this.end;
},
equals_za3rmp$: function(other) {
if (other == null)
return false;
return this.start === other.start && this.end === other.end && this.increment === other.increment;
}
equals_za3rmp$: isSameNotNullRanges
});
Kotlin.NumberProgression = Kotlin.createClassNow(null,
@@ -566,6 +595,58 @@
}
});
Kotlin.LongRangeIterator = Kotlin.createClassNow(Kotlin.Iterator,
function (start, end, increment) {
this.start = start;
this.end = end;
this.increment = increment;
this.i = start;
}, {
next: function () {
var value = this.i;
this.i = this.i.add(this.increment);
return value;
},
hasNext: function () {
if (this.increment.isNegative())
return this.i.compare(this.end) >= 0;
else
return this.i.compare(this.end) <= 0;
}
});
Kotlin.LongRange = Kotlin.createClassNow(null,
function (start, end) {
this.start = start;
this.end = end;
this.increment = Kotlin.Long.ONE;
}, {
contains: function (number) {
return this.start.compare(number) <= 0 && number.compare(this.end) <= 0;
},
iterator: function () {
return new Kotlin.LongRangeIterator(this.start, this.end, this.increment);
},
isEmpty: function () {
return this.start.compare(this.end) > 0;
},
equals_za3rmp$: isSameNotNullRanges
});
Kotlin.LongProgression = Kotlin.createClassNow(null,
function (start, end, increment) {
this.start = start;
this.end = end;
this.increment = increment;
}, {
iterator: function () {
return new Kotlin.LongRangeIterator(this.start, this.end, this.increment);
},
isEmpty: function() {
return this.increment.isNegative() ? this.start.compare(this.end) < 0 : this.start.compare(this.end) > 0;
}
});
/**
* @interface
* @template T
@@ -703,6 +784,12 @@
});
};
Kotlin.longArrayOfSize = function (size) {
return Kotlin.arrayFromFun(size, function () {
return Kotlin.Long.ZERO;
});
};
Kotlin.arrayFromFun = function (size, initFun) {
var result = new Array(size);
for (var i = 0; i < size; i++) {
+11
View File
@@ -826,4 +826,15 @@
Kotlin.Long.prototype.valueOf = function() {
return this.toNumber();
};
Kotlin.Long.prototype.plus = function() {
return this;
};
Kotlin.Long.prototype.minus = Kotlin.Long.prototype.negate;
Kotlin.Long.prototype.inv = Kotlin.Long.prototype.not;
Kotlin.Long.prototype.rangeTo = function (other) {
return new Kotlin.LongRange(this, other);
};
}(Kotlin));
@@ -5,7 +5,7 @@ native fun eval(arg: String): Any? = noImpl
fun test(testName: String, actual: Any, expectedAsString: String) {
val expected = eval("[$expectedAsString]")
if (actual != expected) throw Fail("Wrong result for '$testName' function. Expected: $expected | Actual: $actual")
assertEquals(expected, actual)
}
fun box(): String {
@@ -16,8 +16,8 @@ fun box(): String {
test("byteArray", byteArray(0, 1, 2, 3, 4), "0, 1, 2, 3, 4")
test("shortArray", shortArray(0, 1, 2, 3, 4), "0, 1, 2, 3, 4")
test("intArray,", intArray(0, 1, 2, 3, 4), "0, 1, 2, 3, 4")
test("longArray", longArray(0, 1, 2, 3, 4), "0, 1, 2, 3, 4")
test("floatArray", floatArray(0.0.toFloat(), 1.0.toFloat(), 2.0.toFloat(), 3.0.toFloat(), 4.0.toFloat()), "0.0, 1.0, 2.0, 3.0, 4.0")
test("longArray", longArray(0, 1, 2, 3, 4), "Kotlin.Long.fromInt(0), Kotlin.Long.fromInt(1), Kotlin.Long.fromInt(2), Kotlin.Long.fromInt(3), Kotlin.Long.fromInt(4)")
test("floatArray", floatArray(0.0f, 1.0f, 2.0f, 3.0f, 4.0f), "0.0, 1.0, 2.0, 3.0, 4.0")
test("doubleArray", doubleArray(0.0, 1.1, 2.2, 3.3, 4.4), "0.0, 1.1, 2.2, 3.3, 4.4")
}
catch (e: Fail) {