39 lines
817 B
Kotlin
39 lines
817 B
Kotlin
package ru.dbotthepony.kstarbound.lua
|
|
|
|
import ru.dbotthepony.kstarbound.Starbound
|
|
import java.io.Closeable
|
|
|
|
class LuaHandle(private val parent: LuaHandleThread, val handle: Int) : Closeable {
|
|
private val cleanables = ArrayList<Runnable>()
|
|
|
|
var isValid = true
|
|
private set
|
|
|
|
init {
|
|
val parent = parent
|
|
val handle = handle
|
|
|
|
cleanables.add(Starbound.CLEANER.register(this) {
|
|
parent.freeHandle(handle)
|
|
}::clean)
|
|
|
|
}
|
|
|
|
fun push(into: LuaThread) {
|
|
check(isValid) { "Tried to use NULL handle!" }
|
|
parent.thread.push()
|
|
parent.thread.copy(handle, -1)
|
|
parent.thread.moveStackValuesOnto(into)
|
|
}
|
|
|
|
fun onClose(cleanable: Runnable) {
|
|
check(isValid) { "No longer valid" }
|
|
cleanables.add(cleanable)
|
|
}
|
|
|
|
override fun close() {
|
|
if (!isValid) return
|
|
cleanables.forEach { it.run() }
|
|
isValid = false
|
|
}
|
|
} |