From 678cfbf45eea56f43c590559e011576ef1f5081f Mon Sep 17 00:00:00 2001 From: sergeych Date: Sun, 7 Dec 2025 21:50:29 +0100 Subject: [PATCH] fix #85 lynon empty list encoding --- .../kotlin/net/sergeych/lynon/LynonEncoder.kt | 8 +++++--- lynglib/src/commonTest/kotlin/ScriptTest.kt | 7 +++++++ lynglib/src/jvmTest/kotlin/LynonTests.kt | 13 +++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lynglib/src/commonMain/kotlin/net/sergeych/lynon/LynonEncoder.kt b/lynglib/src/commonMain/kotlin/net/sergeych/lynon/LynonEncoder.kt index 737e78e..225add5 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lynon/LynonEncoder.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lynon/LynonEncoder.kt @@ -127,9 +127,11 @@ open class LynonEncoder(val bout: BitOutput, val settings: LynonSettings = Lynon */ suspend fun encodeAnyList(scope: Scope, list: List,fixedSize: Boolean = false) { if( list.isEmpty()) { - // any-typed, empty, save space - putBit(0) - encodeUnsigned(0u) + // Empty list: we still need to write the heterogenous/homogenous flag bit + // but must NOT write the size when fixedSize is provided, as decoder will + // use the provided size and won't read it from the stream. + putBit(0) // treat empty lists as heterogeneous + if (!fixedSize) encodeUnsigned(0u) } else { val objClass = list[0].objClass diff --git a/lynglib/src/commonTest/kotlin/ScriptTest.kt b/lynglib/src/commonTest/kotlin/ScriptTest.kt index 8bc04c2..c761d6f 100644 --- a/lynglib/src/commonTest/kotlin/ScriptTest.kt +++ b/lynglib/src/commonTest/kotlin/ScriptTest.kt @@ -4013,6 +4013,13 @@ class ScriptTest { assertEquals( TestJson4(TestEnum.One), x) } + @Test + fun testStringLast() = runTest { + eval(""" + assertEquals('t', "assert".last()) + """.trimIndent()) + } + @Test fun testLogicalNot() = runTest { eval(""" diff --git a/lynglib/src/jvmTest/kotlin/LynonTests.kt b/lynglib/src/jvmTest/kotlin/LynonTests.kt index 89634cb..f597c5d 100644 --- a/lynglib/src/jvmTest/kotlin/LynonTests.kt +++ b/lynglib/src/jvmTest/kotlin/LynonTests.kt @@ -778,6 +778,19 @@ class Wallet( id, ownerKey, balance=0, createdAt=Instant.now().truncateToSecond( assert( Lynon.encode(Poin3(1,2)).size <= 110) """.trimIndent()) } + + @Test + fun testLyngDecodeResult() = runTest { + val a = eval(""" + [Map(),Map(),Map()] + """.trimIndent()) + val b = lynonDecodeAny(Scope(), lynonEncodeAny(Scope(), a)) + assertEquals(a, b ) + // val c = b.decodeSerializable() +// println(c) + } + + }