1

At first sight, it seems easy using kotlin collections from js. Because kotlin multiplatform and its libraries (kotlinx.collections, etc) generate shared libraries that are interoperable between both js and kotlin. If we call new ArrayList() from javascript code it will construct a MutableList that is interoperable with kotlin-multiplatform code but this mutableList will not be usable directly in the js code because of mangling. Indeed when creating mutableList from js it does generates this kind of object (mutablelist)

 let mutableList = new kt.kotlin.collections.ArrayList(["first", "second", "third"]);
 console.log("js mutable list: ", mutableList);

and this give me this following log: enter image description here

So as you can see all properties have mangling (hd7ov6$_0) and sadly no getters. (Getters/Setters with good name are generated thanks @JsName("name"), and it seems to be the only way to avoid mangling on properties and functions name.

So I ask the community and the kotlin team, how can such mangling still be on MPP, and will it change thanks the IR compiler and the new 1.6.20 version that allows interfaces and enum js export?

Also, is there good workaround ? The only one I see is sadly to reimplement a MutableList based on ArrayList, but the array composed by ArrayList is private. And as some ArrayList implementations methods lose the private array reference, I can't have a reference array on the base class.

Hope someone can help with a proper way to avoid this mangling... Thanks;)

nicolidz
  • 462
  • 4
  • 10
  • 1
    Why do you need to use `kt.kotlin.collections.ArrayList` in `JS` side? Why can't you use JS array? Kotlin code should accept that as `kotlin array`. Kotlin collections are not yet properly usable for JS interop – shaktiman_droid Dec 22 '21 at 19:13
  • Simply because I have a kotlin-multiplatform "shared logic library" use by a kotlin server and a js client and I want them to be fully interoperable. So I can't use Array on js side because the commun kotlin multiplatform code require some ArrayList (MutableList). Thus, the only way I found to make it functional is to implement and JsExport my own Collection based on ArrayList. However and sadly this should not be the way to go and "native" kotlin collections should be JsExportable without mangling. – nicolidz Dec 22 '21 at 22:05
  • 1
    See this issue - https://youtrack.jetbrains.com/issue/KT-34995 – vanyochek Dec 28 '21 at 12:15

0 Answers0