From fb1c4320b6ccb44186224064eb89b0756516867d Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Fri, 21 Apr 2017 16:41:18 +0300 Subject: [PATCH] KJS: check hashCode property only for objects to avoid extra boxing for primitive values Also in Nashorn everything has hashCode including primitive types, so the result can be different at Nashorn and at other engines. --- .../codegen/box/dataClasses/hashCode/boolean.kt | 4 +++- js/js.libraries/src/js/core.js | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/compiler/testData/codegen/box/dataClasses/hashCode/boolean.kt b/compiler/testData/codegen/box/dataClasses/hashCode/boolean.kt index afc41bbe2e7..e231e75a57b 100644 --- a/compiler/testData/codegen/box/dataClasses/hashCode/boolean.kt +++ b/compiler/testData/codegen/box/dataClasses/hashCode/boolean.kt @@ -1,5 +1,7 @@ data class A(val a: Boolean) fun box() : String { - return if( A(true).hashCode()==1 && A(false).hashCode()==0 ) "OK" else "fail" + if (A(true).hashCode() != 1) return "fail1" + if (A(false).hashCode() !=0) return "fail2" + return "OK" } diff --git a/js/js.libraries/src/js/core.js b/js/js.libraries/src/js/core.js index 36b202d0244..6b3e4b3bf12 100644 --- a/js/js.libraries/src/js/core.js +++ b/js/js.libraries/src/js/core.js @@ -38,14 +38,14 @@ Kotlin.hashCode = function (obj) { if (obj == null) { return 0; } - if ("function" === typeof obj.hashCode) { - return obj.hashCode(); - } var objType = typeof obj; - if ("object" === objType || "function" === objType) { + if ("object" === objType) { + return "function" === typeof obj.hashCode ? obj.hashCode() : getObjectHashCode(obj); + } + if ("function" === objType) { return getObjectHashCode(obj); } - else if ("number" === objType) { + if ("number" === objType) { return numberHashCode(obj); } if ("boolean" === objType) {