Difficulty with Interop of Array of Pointers

Hello,

I have an API I’d like to use with the following declaration:

typealias PLFLT = Double
typealias PLFLTVar = DoubleVarOf<PLFLT>
typealias PLFLT_VECTOR = CPointer<PLFLTVar>
fun c_plline(n: PLINT, x: PLFLT_VECTOR?, y: PLFLT_VECTOR?)

I’m trying to call this function like this:

val Array<Point>.x get() = map { p -> p.x }
val Array<Point>.y get() = map { p -> p.y }

fun line(points: List<Point>)
{
    memScoped {
        val bufferX = allocArray<PLFLT>(points.size)
        val bufferY = allocArray<PLFLT>(points.size)
        points.x.forEachIndexed { index, value -> bufferX[index] = value }
        points.y.forEachIndexed { index, value -> bufferY[index] = value }
        c_plline(points.size, bufferX, bufferY)
    }
}

That doesn’t work because:

  • the pointer doesn’t accept the operator;
  • allocArray() returns a CArrayPointer; not a CPointer

I have also tried to use cValuesOf() unsuccessfully.

Not sure how to do it properly. I’d appreciate any suggestions.

Never mind. The problem had nothing to do with the interop. It was the Array property extension that had to be List. And PLFLT had to be PLFLTVar.

Btw, CArrayPointer is just a typealias fro CPointer.

https://github.com/JetBrains/kotlin-native/blob/master/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Types.kt#L457