Iterating through Kotlin map from C native export

Viewed 76

We have a Kotlin package that we native build and export to C. We have the header file with all the nested struct and pinned-style pointers.

In the Kotlin code, there is a Map which we want to access. We can get a hold of the Kotlin package enum (the key of the Map), but what's the C code for actually indexing into the "kref kotlin Map object" to get to the value in the map?

Basically, we'd like to know how to manipulate Map, List and Array from C code. Actual steps and/or reference doc would be appreciated.

1 Answers

Kotlin/Native compiler does not export any of the collection's functions to the native library API. This decision was taken some time ago, with the idea to minimize the verbosity of the library header. However, this leads to the problem you faced.

Right now, the recommended approach is to write wrapper functions in your Kotlin code.

For an example of this approach, please see this ticket at the Kotlin issue tracker. I also recommend subscribing to it, to get the updates on the problem's state ASAP.

Posting this in case the ticket won't be available for someone:

fun getListElement(list: List<Any?>, index: Int) = list.get(index)
/// function accessing the list element by index
Related