From abdcbf1fb26cb421574b459d02f812da0fb6d2df Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 14 Sep 2017 17:43:51 +0300 Subject: [PATCH] Refactor: use the same arraybuffer to compute double hashcode --- js/js.libraries/src/js/core.js | 34 +--------------------------------- js/js.libraries/src/js/misc.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/js/js.libraries/src/js/core.js b/js/js.libraries/src/js/core.js index 6b3e4b3bf12..73fae6e0600 100644 --- a/js/js.libraries/src/js/core.js +++ b/js/js.libraries/src/js/core.js @@ -46,7 +46,7 @@ Kotlin.hashCode = function (obj) { return getObjectHashCode(obj); } if ("number" === objType) { - return numberHashCode(obj); + return Kotlin.numberHashCode(obj); } if ("boolean" === objType) { return Number(obj) @@ -56,38 +56,6 @@ Kotlin.hashCode = function (obj) { return getStringHashCode(str); }; -var numberHashCode; - -if (typeof ArrayBuffer === "function") { - var bufferForNumberConversion = new ArrayBuffer(8); - var arrayForDoubleConversion = new Float64Array(bufferForNumberConversion); - var arrayForIntegerConversion = new Int32Array(bufferForNumberConversion); - - // Detect endiannes of ArrayBuffer - var lowerIntegerIndex = 0; - var upperIntegerIndex = 1; - (function() { - arrayForDoubleConversion[0] = 1.2; - if (arrayForIntegerConversion[0] !== 0x3FF33333) { - lowerIntegerIndex = 1; - upperIntegerIndex = 0; - } - })(); - numberHashCode = function(obj) { - if ((obj | 0) === obj) { - return obj | 0; - } - else { - arrayForDoubleConversion[0] = obj; - return (arrayForIntegerConversion[lowerIntegerIndex] * 31 | 0) + arrayForIntegerConversion[upperIntegerIndex] | 0; - } - } -} -else { - numberHashCode = function(obj) { - return obj | 0; - } -} Kotlin.toString = function (o) { if (o == null) { diff --git a/js/js.libraries/src/js/misc.js b/js/js.libraries/src/js/misc.js index 3491df9d43b..da39744daeb 100644 --- a/js/js.libraries/src/js/misc.js +++ b/js/js.libraries/src/js/misc.js @@ -97,6 +97,16 @@ function imul(a, b) { bufFloat64[0] = value; return bufInt32[highIndex] & 0x80000000; }; + + Kotlin.numberHashCode = function(obj) { + if ((obj | 0) === obj) { + return obj | 0; + } + else { + bufFloat64[0] = obj; + return (bufInt32[highIndex] * 31 | 0) + bufInt32[lowIndex] | 0; + } + } })();