Edoardo Luppi
07/04/2024, 9:32 AMidentityHashCode()
mechanism in K/JS?Artem Kobzar
07/04/2024, 9:37 AMvar index = 0
val jsWeakHashMap = js("new WeakMap()")
fun identityHashCode(object: Any): Int =
if (jsWeakHashMap.has(object))
jsWeakHashMap.get(object)
else
(index + 1).also { jsWeakHashMap.set(object, it) }
Edoardo Luppi
07/04/2024, 9:38 AMEdoardo Luppi
07/04/2024, 9:41 AMEdoardo Luppi
07/04/2024, 9:52 AM// identityHashCode ---------------------------------------------------------
This is how they do it now with codegen
https://github.com/scala-js/scala-js/blob/9f5cc98f7f8f1d93a07ca6592aea2e953488b062[…]c/main/scala/org/scalajs/linker/backend/emitter/CoreJSLib.scalaArtem Kobzar
07/04/2024, 9:54 AMEdoardo Luppi
07/04/2024, 9:57 AMWeakMap
is not available they use a magic field
val hash = x.asInstanceOf[js.Dynamic].selectDynamic("$idHashCode$0")
Which, as far is can tell, is simply set to nextIDHashCode()
Edoardo Luppi
07/04/2024, 10:00 AM42
when the class is sealed. I don't get that part.Artem Kobzar
07/04/2024, 10:02 AMEdoardo Luppi
07/04/2024, 10:09 AMcalculateRandomHash
In Scala/JS it's only a counter++
The difference between K/JS and Scala/JS is only the WeakMap
usage if it's available?Edoardo Luppi
07/04/2024, 10:10 AMEdoardo Luppi
07/04/2024, 10:14 AMArtem Kobzar
07/04/2024, 10:17 AMEdoardo Luppi
07/04/2024, 10:19 AMSo, you can emulate it by yourself using just the regular JS MapThat was my initial idea, but what about the environments where a JS
Map
isn't available?Artem Kobzar
07/04/2024, 10:22 AMEdoardo Luppi
07/04/2024, 10:39 AMEdoardo Luppi
07/04/2024, 12:34 PMkotlin.native.identityHashCode
function in stdlib.
A common function in stdlib for all targets seems doable 👀Edoardo Luppi
07/04/2024, 12:51 PM