Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement js and was identityHashCode family of implementations with WeakMap #1752

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,28 @@ package androidx.compose.material.internal

import androidx.compose.runtime.NoLiveLiterals

// a copy from compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/Expect.kt

private var nextHash = 1
private const val IDENTITY_HASHCODE_FIELD = "kotlinIdentityHashcodeValue$"

private external interface WeakMap {
fun set(key: Any, value: Int)
fun get(key: Any): Int?
}

private val weakMap: WeakMap = js("new WeakMap()")
@NoLiveLiterals
private fun memoizeIdentityHashCode(instance: Any?): Int {
private fun memoizeIdentityHashCode(instance: Any): Int {
val value = nextHash++

val descriptor = js("new Object()")
descriptor.value = value
descriptor.writable = false
descriptor.configurable = false
descriptor.enumerable = false

js("Object").defineProperty(instance, IDENTITY_HASHCODE_FIELD, descriptor)
weakMap.set(instance, value)

return value
}

// For the reference check the identityHashCode in compose:runtime
internal actual fun identityHashCode(instance: Any?): Int {
if (instance == null) {
return 0
}

val hashCode = instance.asDynamic()[IDENTITY_HASHCODE_FIELD]
if (hashCode != null) {
return hashCode
}

return when (jsTypeOf(instance)) {
"object", "function" -> memoizeIdentityHashCode(instance)
else -> throw IllegalArgumentException(
"identity hash code for ${jsTypeOf(instance)} is not supported"
)
}
}
return weakMap.get(instance) ?: memoizeIdentityHashCode(instance)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,31 @@

package androidx.compose.material.internal

// for details please see the same implementation in runtime
import androidx.compose.runtime.NoLiveLiterals

private var nextHash = 1

private external interface WeakMap {
fun set(key: JsAny, value: Int)
fun get(key: JsAny): Int?
}

private val weakMap: WeakMap = js("new WeakMap()")
@NoLiveLiterals
private fun memoizeIdentityHashCode(instance: JsAny): Int {
val value = nextHash++

weakMap.set(instance.toJsReference(), value)

return value
}

// For the reference check the identityHashCode in compose:runtime
internal actual fun identityHashCode(instance: Any?): Int {
if (instance == null) {
return 0
}
return instance.hashCode()

val jsRef = instance.toJsReference()
return weakMap.get(jsRef) ?: memoizeIdentityHashCode(jsRef)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,28 @@ package androidx.compose.material3.adaptive.layout.internal

import androidx.compose.runtime.NoLiveLiterals

// a copy from compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/Expect.kt

private var nextHash = 1
private const val IDENTITY_HASHCODE_FIELD = "kotlinIdentityHashcodeValue$"

private external interface WeakMap {
fun set(key: Any, value: Int)
fun get(key: Any): Int?
}

private val weakMap: WeakMap = js("new WeakMap()")
@NoLiveLiterals
private fun memoizeIdentityHashCode(instance: Any?): Int {
private fun memoizeIdentityHashCode(instance: Any): Int {
val value = nextHash++

val descriptor = js("new Object()")
descriptor.value = value
descriptor.writable = false
descriptor.configurable = false
descriptor.enumerable = false

js("Object").defineProperty(instance, IDENTITY_HASHCODE_FIELD, descriptor)
weakMap.set(instance, value)

return value
}

// For the reference check the identityHashCode in compose:runtime
internal actual fun identityHashCode(instance: Any?): Int {
if (instance == null) {
return 0
}

val hashCode = instance.asDynamic()[IDENTITY_HASHCODE_FIELD]
if (hashCode != null) {
return hashCode
}

return when (jsTypeOf(instance)) {
"object", "function" -> memoizeIdentityHashCode(instance)
else -> throw IllegalArgumentException(
"identity hash code for ${jsTypeOf(instance)} is not supported"
)
}
return weakMap.get(instance) ?: memoizeIdentityHashCode(instance)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,31 @@

package androidx.compose.material3.adaptive.layout.internal

// for details please see the same implementation in runtime
import androidx.compose.runtime.NoLiveLiterals

private var nextHash = 1

private external interface WeakMap {
fun set(key: JsAny, value: Int)
fun get(key: JsAny): Int?
}

private val weakMap: WeakMap = js("new WeakMap()")
@NoLiveLiterals
private fun memoizeIdentityHashCode(instance: JsAny): Int {
val value = nextHash++

weakMap.set(instance.toJsReference(), value)

return value
}

// For the reference check the identityHashCode in compose:runtime
internal actual fun identityHashCode(instance: Any?): Int {
if (instance == null) {
return 0
}
return instance.hashCode()

val jsRef = instance.toJsReference()
return weakMap.get(jsRef) ?: memoizeIdentityHashCode(jsRef)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,28 @@ package androidx.compose.material3.common.internal

import androidx.compose.runtime.NoLiveLiterals

// a copy from compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/Expect.kt

private var nextHash = 1
private const val IDENTITY_HASHCODE_FIELD = "kotlinIdentityHashcodeValue$"

private external interface WeakMap {
fun set(key: Any, value: Int)
fun get(key: Any): Int?
}

private val weakMap: WeakMap = js("new WeakMap()")
@NoLiveLiterals
private fun memoizeIdentityHashCode(instance: Any?): Int {
private fun memoizeIdentityHashCode(instance: Any): Int {
val value = nextHash++

val descriptor = js("new Object()")
descriptor.value = value
descriptor.writable = false
descriptor.configurable = false
descriptor.enumerable = false

js("Object").defineProperty(instance, IDENTITY_HASHCODE_FIELD, descriptor)
weakMap.set(instance, value)

return value
}

// For the reference check the identityHashCode in compose:runtime
internal actual fun identityHashCode(instance: Any?): Int {
if (instance == null) {
return 0
}

val hashCode = instance.asDynamic()[IDENTITY_HASHCODE_FIELD]
if (hashCode != null) {
return hashCode
}

return when (jsTypeOf(instance)) {
"object", "function" -> memoizeIdentityHashCode(instance)
else -> throw IllegalArgumentException(
"identity hash code for ${jsTypeOf(instance)} is not supported"
)
}
}
return weakMap.get(instance) ?: memoizeIdentityHashCode(instance)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,31 @@

package androidx.compose.material3.common.internal

import androidx.compose.runtime.NoLiveLiterals

private var nextHash = 1

private external interface WeakMap {
fun set(key: JsAny, value: Int)
fun get(key: JsAny): Int?
}

private val weakMap: WeakMap = js("new WeakMap()")
@NoLiveLiterals
private fun memoizeIdentityHashCode(instance: JsAny): Int {
val value = nextHash++

weakMap.set(instance.toJsReference(), value)

return value
}

// For the reference check the identityHashCode in compose:runtime
internal actual fun identityHashCode(instance: Any?): Int {
if (instance == null) {
return 0
}
return instance.hashCode()

val jsRef = instance.toJsReference()
return weakMap.get(jsRef) ?: memoizeIdentityHashCode(jsRef)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,28 @@ package androidx.compose.material3.internal

import androidx.compose.runtime.NoLiveLiterals

// a copy from compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/Expect.kt

private var nextHash = 1
private const val IDENTITY_HASHCODE_FIELD = "kotlinIdentityHashcodeValue$"

private external interface WeakMap {
fun set(key: Any, value: Int)
fun get(key: Any): Int?
}

private val weakMap: WeakMap = js("new WeakMap()")
@NoLiveLiterals
private fun memoizeIdentityHashCode(instance: Any?): Int {
private fun memoizeIdentityHashCode(instance: Any): Int {
val value = nextHash++

val descriptor = js("new Object()")
descriptor.value = value
descriptor.writable = false
descriptor.configurable = false
descriptor.enumerable = false

js("Object").defineProperty(instance, IDENTITY_HASHCODE_FIELD, descriptor)
weakMap.set(instance, value)

return value
}

// For the reference check the identityHashCode in compose:runtime
internal actual fun identityHashCode(instance: Any?): Int {
if (instance == null) {
return 0
}

val hashCode = instance.asDynamic()[IDENTITY_HASHCODE_FIELD]
if (hashCode != null) {
return hashCode
}

return when (jsTypeOf(instance)) {
"object", "function" -> memoizeIdentityHashCode(instance)
else -> throw IllegalArgumentException(
"identity hash code for ${jsTypeOf(instance)} is not supported"
)
}
}
return weakMap.get(instance) ?: memoizeIdentityHashCode(instance)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,31 @@

package androidx.compose.material3.internal

// TODO https://youtrack.jetbrains.com/issue/COMPOSE-789/CfW-properly-implement-identityHashCode-for-k-wasm
import androidx.compose.runtime.NoLiveLiterals

private var nextHash = 1

private external interface WeakMap {
fun set(key: JsAny, value: Int)
fun get(key: JsAny): Int?
}

private val weakMap: WeakMap = js("new WeakMap()")
@NoLiveLiterals
private fun memoizeIdentityHashCode(instance: JsAny): Int {
val value = nextHash++

weakMap.set(instance.toJsReference(), value)

return value
}

// For the reference check the identityHashCode in compose:runtime
internal actual fun identityHashCode(instance: Any?): Int {
if (instance == null) {
return 0
}
return instance.hashCode()

val jsRef = instance.toJsReference()
return weakMap.get(jsRef) ?: memoizeIdentityHashCode(jsRef)
}
Loading