I have a Kotlin function written that consumes a List<String>
. The function has an annotation with @JsName
so that I can call this function from JavaScript. I cannot determine though what I am supposed to pass into this function. Passing an JavaScript Array doesn't work because the Kotlin-JS code calls iterator
on the object. Furthermore, the names in the Kotlin standard library are all mangled; so I cannot in any reliable way call say listOf
in JavaScript and pass the results to function.
The question is then how beyond trivial types (numbers, string, etc.) are we supposed to create and pass to objects functions if the Kotlin standard library names are mangled?
You can use ArrayList when interop with JavaScript in Kotlin, since List
is an interface (which isn't in JavaScript) and (one of) its actual implementation is ArrayList
.
As you are dealing with List<String>
, you may do this in JavaScript:
let list = new kotlin.kotlin.collections.ArrayList(["foo","bar","baz"])
And you can pass the list
variable to the functions that needs a List<String>
parameter.
Remarks:
list.toArray()
to convert a List from Kotlin back to JavaScript array, if some of your Kotlin functions returns a List.List
, that takes a MutableList
and call add
, since the library function is mangled. (I want a more proper way of doing this too)Foo
, you should be able to call new <package>.<identifier>...Foo(params)
.