KStarbound/src/main/kotlin/ru/dbotthepony/kstarbound/lua/JVM2LuaWrapper.kt

57 lines
1.6 KiB
Kotlin

package ru.dbotthepony.kstarbound.lua
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.FUNCTION)
annotation class LuaExposed
interface ILuaIndex {
fun luaIndex(index: Any?): Any?
}
interface ILuaNewIndex {
fun luaIndex(index: Any?, value: Any?)
}
class JVM2LuaWrapper<R : Any>(val value: R) {
private abstract class LuaBinding<in R : Any> {
abstract fun invoke(receiver: R, values: Array<Any?>): Any?
abstract fun read(receiver: R): Any?
abstract fun write(receiver: R, value: Any?)
}
private class LuaFunctionBinding<in R : Any>(private val func: (R, Array<Any?>) -> Any?) : LuaBinding<R>() {
override fun invoke(receiver: R, values: Array<Any?>): Any? {
return func.invoke(receiver, values)
}
override fun read(receiver: R) {
throw UnsupportedOperationException("attempt to index a function value")
}
override fun write(receiver: R, value: Any?) {
throw UnsupportedOperationException("attempt to new index a function value")
}
}
private class LuaPropertyBinding<in R : Any>(private val read: (R) -> Any?, private val write: ((R, Any?) -> Unit)?) : LuaBinding<R>() {
override fun invoke(receiver: R, values: Array<Any?>): Any? {
throw UnsupportedOperationException("attempt to call a value")
}
override fun read(receiver: R): Any? {
return read.invoke(receiver)
}
override fun write(receiver: R, value: Any?) {
if (write == null)
throw UnsupportedOperationException("attempt to new index a read-only value")
write.invoke(receiver, value)
}
}
companion object {
//private val decls =
}
}