Does Kotlin Native have a way to get Kotlin object...
# kotlin-native
n
Does Kotlin Native have a way to get Kotlin objects out of a callback as output? Below is the defined callback:
Copy code
fun runFunction(argPtr: COpaquePointer?): Int = memScoped {
    val tmpCmd = argPtr?.asStableRef<Command>()?.get() ?: throw IllegalStateException("Command cannot be null")
    val argv =
        if (tmpCmd.args.isEmpty()) arrayOf(tmpCmd.filePath, "").toCStringArray(this)
        else arrayOf(tmpCmd.filePath, *tmpCmd.args.toTypedArray()).toCStringArray(this)
    val status = alloc<IntVar>()
    val fileDescriptors = allocArray<IntVar>(2)
    createPipe(fileDescriptors)
    val pid = fork()

    if (pid == 0) {
        close(fileDescriptors[READ_POS])
        dup2(fileDescriptors[WRITE_POS], STDOUT_FILENO)
        dup2(fileDescriptors[WRITE_POS], STDERR_FILENO)
        execvp(tmpCmd.filePath, argv)
    }

    close(fileDescriptors[WRITE_POS])
    val file = openFileDescriptor(fileDescriptors[READ_POS])
    tmpCmd.output += file.readLines(10u)
    file.close()
    wait_for_process(status.ptr)
    status.value
}
In the callback the command output is stored in tmpCmd as a StableRef ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlinx.cinterop/-stable-ref/ ). Why isn't the changed state of the StableRef accessible outside of the callback?
Some modifications have been made to see if using an AtomicReference works for getting output out of the callback. There is an AtomicReference object defined for the command output:
Copy code
val cmdOutput = AtomicReference(emptyArray<String>())
When the value of cmdOutput is changed inside the callback the new value can be printed out in the callback. However if the new value of cmdOutput is accessed outside of the callback then the value is empty, as though nothing has changed.