How can I convert <this Kotlin/JS code> to Wasm? ...
# webassembly
a
How can I convert this Kotlin/JS code to Wasm?
Copy code
internal actual fun identityHashCode(any: Any?): IdentityHashCode {
    if (any == null) return IdentityHashCode(0)
    val dyn = any.asDynamic()
    if (dyn[IDENTITY_HASH_CODE_SYMBOL] === undefined) {
        dyn[IDENTITY_HASH_CODE_SYMBOL] = lastIdentityHashCodeId++
    }
    val hc = dyn[IDENTITY_HASH_CODE_SYMBOL].unsafeCast<Int>()
    return IdentityHashCode(hc)
}
I miss dynamic in Wasm :(
s
You can’t add new properties to Wasm objects at runtime, but you can use JS WeakMap to associate values like this
Copy code
external interface IdentityHashCodeMap {
    fun get(x: JsReference<Any>): Int
    fun set(x: JsReference<Any>, value: Int): Unit
    fun has(x: JsReference<Any>): Boolean
}

val identityHashCodes: IdentityHashCodeMap = js("new WeakMap()")

var lastIdentityHashCodeId = 0

fun identityHashCode(any: Any): Int {
    val x = any.toJsReference()
    if (!identityHashCodes.has(x)) {
        identityHashCodes.set(x, lastIdentityHashCodeId++)
    }
    return identityHashCodes.get(x)
}
very nice 1
a
Thanks so much! A perfect substitute.