WASM: Implement string hashcode

This commit is contained in:
Igor Laevsky
2021-07-30 18:49:40 +03:00
committed by TeamCityServer
parent c526145a48
commit af865544ff
3 changed files with 15 additions and 4 deletions

View File

@@ -1,5 +1,3 @@
// DONT_TARGET_EXACT_BACKEND: WASM
// WASM_MUTE_REASON: String.hashCode()
// IGNORE_BACKEND: NATIVE
fun test(s: String) {

View File

@@ -11728,6 +11728,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/primitiveTypes/rangeTo.kt");
}
@TestMetadata("stringEqualsHashCodeToString.kt")
public void testStringEqualsHashCodeToString() throws Exception {
runTest("compiler/testData/codegen/box/primitiveTypes/stringEqualsHashCodeToString.kt");
}
@TestMetadata("unboxComparable.kt")
public void testUnboxComparable() throws Exception {
runTest("compiler/testData/codegen/box/primitiveTypes/unboxComparable.kt");

View File

@@ -56,8 +56,16 @@ public class String internal constructor(internal val chars: CharArray) : Compar
public override fun toString(): String = this
// TODO: this is not nice
public override fun hashCode(): Int = 10
private var cachedHash: Int = 0
public override fun hashCode(): Int {
if (cachedHash != 0 || this.isEmpty())
return cachedHash
var hash = 0
for (c in chars)
hash = 31 * hash + c.toInt()
cachedHash = hash
return cachedHash
}
}
internal fun stringLiteral(startAddr: Int, length: Int) = String(unsafeRawMemoryToCharArray(startAddr, length))