napperley
07/21/2022, 2:16 AMfun 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?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.