napperley
01/03/2023, 12:45 AMIgnat Beresnev
01/03/2023, 1:42 AMIs it possible to call Kotlin code from a callback on the C sideHaven't tried it, but I remember reading about it in the docs. Looks like you can convert a Kotlin function to a C pointer and use it as a callback https://kotlinlang.org/docs/native-c-interop.html#callbacks
napperley
01/03/2023, 1:45 AMJeff Lockhart
01/03/2023, 3:22 AMJeff Lockhart
01/03/2023, 3:24 AMnapperley
01/03/2023, 4:28 AMJeff Lockhart
01/03/2023, 4:32 AMextern
and calling that.vbsteven
01/03/2023, 11:15 AMstaticCFunction
as mentioned by others. Use staticCFunction on the kotlin side and put it into a val
, and then pass yourVal.CPointer()
as the "callback" function pointer.
The lambda cannot capture values, but typically a C function for configuring a callback will take a void * data
pointer which will be passed as an argument to the callback. You can use a cinterop StableRef
to create a pointer to your Kotlin data, and in the staticCFunction lambda, get your kotlin data back.vbsteven
01/03/2023, 11:33 AM