[Wasm] Implement Any::hashCode()
This commit is contained in:
committed by
TeamCityServer
parent
9e00a2c5b4
commit
c7cfa97748
+1
@@ -267,6 +267,7 @@ class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorV
|
|||||||
val klassId = context.referenceClassId(klass.symbol)
|
val klassId = context.referenceClassId(klass.symbol)
|
||||||
|
|
||||||
body.buildConstI32Symbol(klassId)
|
body.buildConstI32Symbol(klassId)
|
||||||
|
body.buildConstI32(0) // Any::_hashCode
|
||||||
generateExpression(call.getValueArgument(0)!!)
|
generateExpression(call.getValueArgument(0)!!)
|
||||||
body.buildGetGlobal(context.referenceClassRTT(klass.symbol))
|
body.buildGetGlobal(context.referenceClassRTT(klass.symbol))
|
||||||
body.buildStructNew(structTypeName)
|
body.buildStructNew(structTypeName)
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ package kotlin
|
|||||||
|
|
||||||
import kotlin.wasm.internal.*
|
import kotlin.wasm.internal.*
|
||||||
|
|
||||||
|
import kotlin.random.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The root of the Kotlin class hierarchy. Every Kotlin class has [Any] as a superclass.
|
* The root of the Kotlin class hierarchy. Every Kotlin class has [Any] as a superclass.
|
||||||
*/
|
*/
|
||||||
@@ -37,8 +39,12 @@ public open class Any {
|
|||||||
* * Whenever it is invoked on the same object more than once, the `hashCode` method must consistently return the same integer, provided no information used in `equals` comparisons on the object is modified.
|
* * Whenever it is invoked on the same object more than once, the `hashCode` method must consistently return the same integer, provided no information used in `equals` comparisons on the object is modified.
|
||||||
* * If two objects are equal according to the `equals()` method, then calling the `hashCode` method on each of the two objects must produce the same integer result.
|
* * If two objects are equal according to the `equals()` method, then calling the `hashCode` method on each of the two objects must produce the same integer result.
|
||||||
*/
|
*/
|
||||||
// TODO: Implement
|
internal var _hashCode: Int = 0
|
||||||
public open fun hashCode(): Int = 100
|
public open fun hashCode(): Int {
|
||||||
|
if (_hashCode == 0)
|
||||||
|
_hashCode = Random.nextInt(1, Int.MAX_VALUE)
|
||||||
|
return _hashCode
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a string representation of the object.
|
* Returns a string representation of the object.
|
||||||
|
|||||||
@@ -59,15 +59,14 @@ public class String private constructor(internal val chars: CharArray) : Compara
|
|||||||
|
|
||||||
public override fun toString(): String = this
|
public override fun toString(): String = this
|
||||||
|
|
||||||
private var cachedHash: Int = 0
|
|
||||||
public override fun hashCode(): Int {
|
public override fun hashCode(): Int {
|
||||||
if (cachedHash != 0 || this.isEmpty())
|
if (_hashCode != 0 || this.isEmpty())
|
||||||
return cachedHash
|
return _hashCode
|
||||||
var hash = 0
|
var hash = 0
|
||||||
for (c in chars)
|
for (c in chars)
|
||||||
hash = 31 * hash + c.toInt()
|
hash = 31 * hash + c.toInt()
|
||||||
cachedHash = hash
|
_hashCode = hash
|
||||||
return cachedHash
|
return _hashCode
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user