Anyone having issues returning a native var (e.g. ULongVar) from a suspending function? Im getting a typecast exception from NativePointed to the return type (ULongVar in this instance). Reproducer inside ⬇️
Hauke Radtki
05/21/2021, 7:33 PM
Copy code
@Test
fun suspendReturnNativeVar() {
suspend fun NativePlacement.returnStruct(): ULongVar {
val buffer = allocArray<UByteVar>(sizeOf<ULongVar>())
// Any suspending data fetch function
delay(1)
val reinterpret = buffer.reinterpret<ULongVar>()
reinterpret[0] = 0xAAAAu
return reinterpret.pointed
}
runBlocking {
memScoped {
val v = returnStruct()
assertEquals(0xAAAAu, v.value)
}
}
}
Hauke Radtki
05/21/2021, 7:35 PM
Im getting a
kotlin.ClassCastException: kotlinx.cinterop.NativePointed cannot be cast to kotlinx.cinterop.ULongVarOf
when the coroutine returns to the calling context (when coroutine dispatches the assignment of
val v
)
Hauke Radtki
05/21/2021, 7:35 PM
Removing the suspend keyword (and the delay) makes this work
Hauke Radtki
05/21/2021, 7:43 PM
Tested on 1.4.35 and 1.5.0 with multithreaded coroutines (though no threading involved here)