diff --git a/src/main/java/ru/dbotthepony/kstarbound/lua/LuaJNA.java b/src/main/java/ru/dbotthepony/kstarbound/lua/LuaJNA.java index e8d58154..22f75e5b 100644 --- a/src/main/java/ru/dbotthepony/kstarbound/lua/LuaJNA.java +++ b/src/main/java/ru/dbotthepony/kstarbound/lua/LuaJNA.java @@ -2,11 +2,36 @@ package ru.dbotthepony.kstarbound.lua; import com.sun.jna.Native; import com.sun.jna.Pointer; +import com.sun.jna.ptr.LongByReference; import java.io.File; public final class LuaJNA { - public static native int lua_pcallk(long p, int numArgs, int numResults, int msgh, Pointer ctx, ILua.lua_KFunction callback); + public static native int lua_pcallk(long p, int numArgs, int numResults, int msgh, Pointer ctx, LuaJNADynamic.lua_KFunction callback); + public static native int lua_callk(long p, int numArgs, int numResults, Pointer ctx, LuaJNADynamic.lua_KFunction callback); + + public static native Pointer luaL_newstate(); + public static native void lua_close(long pointer); + + // Стандартные библиотеки + public static native void luaopen_base(long pointer); + public static native void luaopen_package(long pointer); + public static native void luaopen_coroutine(long pointer); + public static native void luaopen_table(long pointer); + public static native void luaopen_io(long pointer); + public static native void luaopen_os(long pointer); + public static native void luaopen_string(long pointer); + public static native void luaopen_math(long pointer); + public static native void luaopen_utf8(long pointer); + public static native void luaopen_debug(long pointer); + + public static native int lua_checkstack(long pointer, int value); + public static native int lua_absindex(long pointer, int value); + public static native int lua_gettop(long pointer); + + public static native Pointer lua_tolstring(long pointer, int index, LongByReference size); + + public static native void lua_setglobal(long pointer, String name); static { Native.register(new File("./lua54.dll").getAbsolutePath()); diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/lua/Lua.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/lua/Lua.kt index 7eff78b2..e0b5166b 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/lua/Lua.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/lua/Lua.kt @@ -19,13 +19,10 @@ private typealias psize_t = LongByReference private typealias lua_KContext = Pointer -interface ILua : Library { - fun luaL_newstate(): Pointer - fun lua_checkstack(luaState: Pointer, amount: Int): Int - fun lua_absindex(luaState: Pointer, value: Int): Int - - fun lua_close(luaState: Pointer) +interface LuaJNADynamic : Library { fun lua_atpanic(luaState: Pointer, fn: lua_CFunction): Pointer + fun lua_load(luaState: Pointer, reader: lua_Reader, data: Pointer?, chunkName: String, mode: String): Int + fun lua_pushlstring(luaState: Pointer, str: Pointer, len: size_t): Pointer? interface lua_CFunction : Callback { fun invoke(luaState: Pointer): Int @@ -35,47 +32,15 @@ interface ILua : Library { fun invoke(luaState: Pointer, data: Pointer?, size: psize_t): Pointer? /* String */ } - fun lua_load(luaState: Pointer, reader: lua_Reader, data: Pointer?, chunkName: String, mode: String): Int - - // Оно является макросом - /* fun lua_pcall(luaState: Pointer, numArgs: Int, numResults: Int, msgh: Int): Int { - return lua_pcallk(luaState, numArgs, numResults, msgh, Pointer.NULL, null) - } */ - interface lua_KFunction : Callback { /* ** Type for continuation functions */ fun invoke(luaState: Pointer, status: Int, ctx: lua_KContext?): Int } - - fun lua_pcallk(luaState: Pointer, numArgs: Int, numResults: Int, msgh: Int, ctx: lua_KContext?, k: lua_KFunction?): Int - fun lua_callk(luaState: Pointer, numArgs: Int, numResults: Int, ctx: lua_KContext?, k: lua_KFunction?): Int - - fun lua_tolstring(luaState: Pointer, index: Int, len: psize_t): Pointer? - - fun lua_pushcclosure(luaState: Pointer, fn: lua_CFunction, n: Int) - - fun lua_setglobal(luaState: Pointer, name: String) - - // Стандартные библиотеки - fun luaopen_base(luaState: Pointer) - fun luaopen_package(luaState: Pointer) - fun luaopen_coroutine(luaState: Pointer) - fun luaopen_table(luaState: Pointer) - fun luaopen_io(luaState: Pointer) - fun luaopen_os(luaState: Pointer) - fun luaopen_string(luaState: Pointer) - fun luaopen_math(luaState: Pointer) - fun luaopen_utf8(luaState: Pointer) - fun luaopen_debug(luaState: Pointer) - - fun lua_pushlstring(luaState: Pointer, str: Pointer, len: size_t): Pointer? - fun lua_error(luaState: Pointer): Int - fun lua_gettop(luaState: Pointer): Int } -val LUA_JNA: ILua = Native.load("lua54", ILua::class.java) +val LUA_JNA: LuaJNADynamic = Native.load("lua54", LuaJNADynamic::class.java) private fun stringToBuffer(str: String): ByteBuffer { val bytes = str.toByteArray(charset = Charsets.UTF_8) @@ -104,11 +69,11 @@ private fun lua_pushlstring(luaState: Pointer, str: String) { } class LuaState : Closeable { - private val pointer = LUA_JNA.luaL_newstate() - private val nativePointer get() = Pointer.nativeValue(pointer) + private val pointer = LuaJNA.luaL_newstate() + private val nativePointer = Pointer.nativeValue(pointer) private var destroyed = false - private val panicHandler = object : ILua.lua_CFunction { + private val panicHandler = object : LuaJNADynamic.lua_CFunction { override fun invoke(luaState: Pointer): Int { throw RuntimeException("$this panicked!") } @@ -120,14 +85,14 @@ class LuaState : Closeable { LUA_JNA.lua_atpanic(pointer, panicHandler) - LUA_JNA.luaopen_base(pointer) - LUA_JNA.luaopen_package(pointer) - LUA_JNA.luaopen_table(pointer) - LUA_JNA.luaopen_coroutine(pointer) - LUA_JNA.luaopen_string(pointer) - LUA_JNA.luaopen_math(pointer) - LUA_JNA.luaopen_utf8(pointer) - LUA_JNA.luaopen_debug(pointer) + LuaJNA.luaopen_base(nativePointer) + LuaJNA.luaopen_package(nativePointer) + LuaJNA.luaopen_table(nativePointer) + LuaJNA.luaopen_coroutine(nativePointer) + LuaJNA.luaopen_string(nativePointer) + LuaJNA.luaopen_math(nativePointer) + LuaJNA.luaopen_utf8(nativePointer) + LuaJNA.luaopen_debug(nativePointer) pushClosure { val build = mutableListOf() @@ -139,7 +104,7 @@ class LuaState : Closeable { LOGGER.info("Lua/print(): {}", build.joinToString("\t")) } - LUA_JNA.lua_setglobal(pointer, "print") + LuaJNA.lua_setglobal(nativePointer, "print") } fun pushClosure(lambda: (state: LuaState) -> Unit) { @@ -159,17 +124,17 @@ class LuaState : Closeable { } fun checkStack(minAmount: Int): Boolean { - return LUA_JNA.lua_checkstack(pointer, minAmount) > 0 + return LuaJNA.lua_checkstack(nativePointer, minAmount) > 0 } fun absoluteIndex(index: Int): Int { - return LUA_JNA.lua_absindex(pointer, index) + return LuaJNA.lua_absindex(nativePointer, index) } fun load(code: String, chunkName: String = "main chunk") { val buf = stringToBuffer(code) - throwLoadError(LUA_JNA.lua_load(pointer, object : ILua.lua_Reader { + throwLoadError(LUA_JNA.lua_load(pointer, object : LuaJNADynamic.lua_Reader { override fun invoke(luaState: Pointer, data: Pointer?, size: psize_t): Pointer? { if (buf.remaining() == 0) { size.value = 0 @@ -196,7 +161,7 @@ class LuaState : Closeable { fun getString(index: Int = -1, limit: Long = 4096): String? { val len = psize_t() - val p = LUA_JNA.lua_tolstring(pointer, absoluteIndex(index), len) ?: return null + val p = LuaJNA.lua_tolstring(nativePointer, absoluteIndex(index), len) ?: return null if (len.value == 0L) { return "" @@ -211,14 +176,14 @@ class LuaState : Closeable { return readBytes.toString(charset = Charsets.UTF_8) } - val stackTop get() = LUA_JNA.lua_gettop(pointer) + val stackTop get() = LuaJNA.lua_gettop(nativePointer) override fun close() { if (destroyed) { throw IllegalStateException("Already destroyed") } - LUA_JNA.lua_close(pointer) + LuaJNA.lua_close(nativePointer) destroyed = true }