From 9e9b27fe79d3786148929993e1b3b1c89bdafea8 Mon Sep 17 00:00:00 2001 From: Anton Bannykh Date: Tue, 10 Jul 2018 17:07:41 +0300 Subject: [PATCH] JS IR: bug fixes --- libraries/stdlib/js/irRuntime/core.kt | 6 +++++- libraries/stdlib/js/irRuntime/longjs.kt | 14 +++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/libraries/stdlib/js/irRuntime/core.kt b/libraries/stdlib/js/irRuntime/core.kt index 9e30fa8eaaf..22b94afcebb 100644 --- a/libraries/stdlib/js/irRuntime/core.kt +++ b/libraries/stdlib/js/irRuntime/core.kt @@ -67,6 +67,10 @@ fun hashCode(obj: dynamic): Int { /** @const */ var OBJECT_HASH_CODE_PROPERTY_NAME = "kotlinHashCodeValue${"$"}"; + var byteBuffer = new ArrayBuffer(8); + var bufFloat64 = new Float64Array(byteBuffer); + var bufInt32 = new Int32Array(byteBuffer); + function getObjectHashCode(obj) { if (!(OBJECT_HASH_CODE_PROPERTY_NAME in obj)) { var hash = (Math.random() * POW_2_32) | 0; // Make 32-bit singed integer. @@ -90,7 +94,7 @@ fun hashCode(obj: dynamic): Int { } else { bufFloat64[0] = obj; - return (bufInt32[highIndex] * 31 | 0) + bufInt32[lowIndex] | 0; + return (bufInt32[1] * 31 | 0) + bufInt32[0] | 0; } } diff --git a/libraries/stdlib/js/irRuntime/longjs.kt b/libraries/stdlib/js/irRuntime/longjs.kt index ac2212927d8..cbf82e39a10 100644 --- a/libraries/stdlib/js/irRuntime/longjs.kt +++ b/libraries/stdlib/js/irRuntime/longjs.kt @@ -61,7 +61,7 @@ internal fun Long.toString(radix: Int): String { if (rem.isZero()) { return digits + result } else { - while (digits.length < 6) { + while (js("digits.length").unsafeCast() < 6) { digits = "0" + digits } result = digits + result @@ -109,7 +109,7 @@ internal fun Long.add(other: Long): Long { val a48 = high ushr 16 val a32 = high and 0xFFFF - val a16 = high ushr 16 + val a16 = low ushr 16 val a00 = low and 0xFFFF val b48 = other.high ushr 16 @@ -253,20 +253,20 @@ internal fun Long.divide(other: Long): Long { // Approximate the result of division. This may be a little greater or // smaller than the actual value. val approxDouble = rem.toNumber() / other.toNumber() - var approx = js("Math.max(1, Math.floor(approxDouble))").unsafeCast() + var approx2 = js("Math.max(1, Math.floor(approxDouble))").unsafeCast() // We will tweak the approximate result by changing it in the 48-th digit or // the smallest non-fractional digit, whichever is larger. - val log2 = js("Math.ceil(Math.log(approx) / Math.LN2)").unsafeCast() + val log2 = js("Math.ceil(Math.log(approx2) / Math.LN2)").unsafeCast() val delta = if (log2 <= 48) 1.0 else js("Math.pow(2, log2 - 48)").unsafeCast() // Decrease the approximation until it is smaller than the remainder. Note // that if it is too large, the product overflows and is negative. - var approxRes = fromNumber(approx) + var approxRes = fromNumber(approx2) var approxRem = approxRes.multiply(other) while (approxRem.isNegative() || approxRem.greaterThan(rem)) { - approx -= delta - approxRes = fromNumber(approx) + approx2 -= delta + approxRes = fromNumber(approx2) approxRem = approxRes.multiply(other) }