如何从C原生代码访问与操作Kotlin的Map、List及Array?
Hey there! I’ve worked with Kotlin/Native’s C interop quite a bit, so let’s walk through how to handle Kotlin Map, List, and Array from your C code. Here’s a clear, step-by-step breakdown tailored to your scenario:
Since you already have your enum keys accessible in C, here’s how to use them to index the Kotlin Map:
Find the generated Map types and helpers in your header
Your auto-generated header will include a struct for your specific Map type (e.g.,typedef struct KotlinMap_YourEnum_YourValueType KotlinMap_YourEnum_YourValueType;— the exact name depends on your Kotlin type declarations). Look for helper functions prefixed withkotlin_map_; you’ll need at least:kotlin_object* kotlin_map_get(KotlinMap_YourEnum_YourValueType* map, YourEnum key);- If you need to iterate through the Map, you’ll also see
kotlin_map_iterator*,kotlin_map_iterator_has_next,kotlin_map_iterator_next_key, andkotlin_map_iterator_next_value.
Access your Map instance
Assume you’ve already obtained a pointer to your Map’s kref (either from a Kotlin function return value, a global exported variable, etc.):KotlinMap_YourEnum_YourValueType* my_kotlin_map = ...;Retrieve values using enum keys
Call thekotlin_map_getfunction with your Map pointer and target enum key. Cast the result to your value type’s struct pointer (since the return is a generickotlin_object*):YourValueType* target_value = (YourValueType*)kotlin_map_get(my_kotlin_map, YOUR_TARGET_ENUM_KEY);- Always check for
NULLhere — if the key doesn’t exist in the Map, the function will return a null pointer. - For iteration, initialize an iterator and loop through entries like this:
kotlin_map_iterator* iter = kotlin_map_iterator(my_kotlin_map); while (kotlin_map_iterator_has_next(iter)) { YourEnum key = (YourEnum)kotlin_map_iterator_next_key(iter); YourValueType* value = (YourValueType*)kotlin_map_iterator_next_value(iter); // Do something with key/value } kotlin_release(iter); // Don't forget to clean up the iterator!
- Always check for
The pattern for List and Array is similar, with slight differences based on whether the collection is fixed-size (Array) or mutable (List/MutableList):
For Kotlin Arrays
- Your header will have a struct like
typedef struct KotlinArray_YourElementType KotlinArray_YourElementType; - Key helper functions:
int kotlin_array_size(KotlinArray_YourElementType* array);— Get the array lengthYourElementType* kotlin_array_get(KotlinArray_YourElementType* array, int index);— Access elements by 0-based index
- Example usage:
KotlinArray_YourElementType* my_array = ...; int array_len = kotlin_array_size(my_array); for (int i = 0; i < array_len; i++) { YourElementType* element = kotlin_array_get(my_array, i); // Work with the element }
For Kotlin Lists
- Mutable or immutable Lists will have a struct like
typedef struct KotlinList_YourElementType KotlinList_YourElementType; - Core helper functions match Array, plus extra functions for mutable Lists (if exposed in Kotlin):
int kotlin_list_size(KotlinList_YourElementType* list);YourElementType* kotlin_list_get(KotlinList_YourElementType* list, int index);- Mutable List extras:
void kotlin_list_add(KotlinList_YourElementType* list, YourElementType* element);,void kotlin_list_remove_at(KotlinList_YourElementType* list, int index);
- Same index-based access rules apply — always check the size before accessing to avoid out-of-bounds errors.
- Handle NULL pointers rigorously: Kotlin nullable types translate to nullable pointers in C. Never dereference a pointer without checking if it’s
NULLfirst. - Manage memory properly: Most returned objects from collection getters are "borrowed" (you don’t own them), but if you retain an object (using
kotlin_retain), you must release it withkotlin_releaseto avoid leaks. The generated header will note ownership rules for each function. - Lean on your generated header: All the functions you need are auto-generated based on your Kotlin code. If you can’t find a helper, double-check the header — it’s the single source of truth for what’s available.
- Keep enums in sync: The integer values of your C enum exactly match the order of entries in your Kotlin enum. Don’t reorder or modify the C enum without updating the Kotlin side, or you’ll get mismatched keys.
内容的提问来源于stack exchange,提问作者philtherobot

