From 5888b0e8462cd08f69e4be9a9e83d029557c8d78 Mon Sep 17 00:00:00 2001 From: David Wursteisen Date: Wed, 20 Mar 2024 23:45:37 +0100 Subject: [PATCH] Bug fixes - ws.list not filtered correctly - debug.log crashing with table - new() not doing a deep copy of elements. --- .../com/github/minigdx/tiny/lua/DebugLib.kt | 40 +++++++++---------- .../com/github/minigdx/tiny/lua/StdLib.kt | 26 ++++++++---- .../github/minigdx/tiny/lua/WorkspaceLib.kt | 2 +- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/DebugLib.kt b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/DebugLib.kt index 80aea156..9efbf71a 100644 --- a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/DebugLib.kt +++ b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/DebugLib.kt @@ -189,12 +189,30 @@ class DebugLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction( } } + private fun formatValue(arg: LuaValue, recursiveSecurity: MutableSet = mutableSetOf()): String = if (arg.istable()) { + val table = arg as LuaTable + if (recursiveSecurity.contains(table.hashCode())) { + "table[<${table.hashCode()}>]" + } else { + recursiveSecurity.add(table.hashCode()) + val keys = table.keys() + val str = keys.joinToString(" ") { + it.optjstring("nil") + ":" + formatValue(table[it], recursiveSecurity) + } + "table[$str]" + } + } else if (arg.isfunction()) { + "function(" + (0 until arg.narg()).joinToString(", ") { "arg" } + ")" + } else { + arg.toString() + } + @TinyFunction("Log a message on the screen.", example = DEBUG_EXAMPLE) internal inner class log : TwoArgFunction() { @TinyCall("Log a message on the screen.") override fun call(@TinyArg("str") arg1: LuaValue, @TinyArg("color") arg2: LuaValue): LuaValue { - val message = arg1.optjstring("")!! + val message = formatValue(arg1) val color = arg2.optjstring("#32CD32")!! resourceAccess.debug(DebugMessage(message, color)) return NIL @@ -207,32 +225,12 @@ class DebugLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction( @TinyFunction("Log a message into the console.", example = DEBUG_EXAMPLE) internal inner class console : OneArgFunction() { - private val recursiveSecurity = mutableSetOf() - @TinyCall("Log a message into the console.") override fun call(@TinyArg("str") arg: LuaValue): LuaValue { val str = formatValue(arg) - recursiveSecurity.clear() println("\uD83D\uDC1B $str") return NIL } - - private fun formatValue(arg: LuaValue): String = if (arg.istable()) { - val table = arg as LuaTable - if (recursiveSecurity.contains(table.hashCode())) { - "table[<${table.hashCode()}>]" - } else { - recursiveSecurity.add(table.hashCode()) - val keys = table.keys() - val str = keys.map { it.optjstring("nil") + ":" + formatValue(table.get(it)) } - .joinToString(" ") - "table[$str]" - } - } else if (arg.isfunction()) { - "function(" + (0 until arg.narg()).map { "arg" }.joinToString(", ") + ")" - } else { - arg.toString() - } } @TinyFunction("Draw a rectangle on the screen", example = DEBUG_ENABLED_EXAMPLE) diff --git a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/StdLib.kt b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/StdLib.kt index b9891517..c7cced77 100644 --- a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/StdLib.kt +++ b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/StdLib.kt @@ -46,19 +46,29 @@ class StdLib( @TinyCall("Create new instance of class using default values.") override fun call(@TinyArg("class") arg1: LuaValue, @TinyArg("default") arg2: LuaValue): LuaValue { val default = if (arg2.istable()) { - val result = LuaTable() - val toCopy = arg2.checktable()!! - toCopy.keys().forEach { key -> - result.set(key, toCopy.get(key)) - } - result + arg2.checktable()!!.deepCopy() } else { LuaTable() } - default.setmetatable(arg1) - arg1.rawset("__index", arg1) + val reference = arg1.checktable()!!.deepCopy() + default.setmetatable(reference) + reference.rawset("__index", reference) return default } + + private fun LuaTable.deepCopy(): LuaTable { + val result = LuaTable() + this.keys().forEach { key -> + var value = this[key] + value = if(value.istable()) { + value.checktable()!!.deepCopy() + } else { + value + } + result[key] = value + } + return result + } } @TinyFunction( diff --git a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/WorkspaceLib.kt b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/WorkspaceLib.kt index 8f7171c3..434583c5 100644 --- a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/WorkspaceLib.kt +++ b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/WorkspaceLib.kt @@ -107,7 +107,7 @@ class WorkspaceLib( val ext = arg.optjstring(null).let { it?.lowercase() } val result = LuaTable() resources.forEach { - if ((ext == null || it.name.endsWith(ext))) { + if ((ext == null || it.extension.endsWith(ext))) { result.insert(0, valueOf(it.name)) } }