refs #35 refac to get caching out of Obj; it is decoder/encoder matter now

This commit is contained in:
sergeych committed 2025-07-19 14:19:50 +03:00
1 parent 405ff2ec2b
commit 20181c63a1
4 files changed
+70 -68

No files matched your search

@@ -84,9 +84,9 @@ data class ObjString(val value: String) : Obj() {
override suspend fun lynonType(): LynonType = LynonType.String
override suspend fun serialize(scope: Scope, encoder: LynonEncoder, lynonType: LynonType?) {
if( lynonType == null )
encoder.encodeCached(this) { encoder.encodeBinaryData(value.encodeToByteArray()) }
else
// if( lynonType == null )
// encoder.encodeCached(this) { encoder.encodeBinaryData(value.encodeToByteArray()) }
// else
encoder.encodeBinaryData(value.encodeToByteArray())
}
@@ -94,11 +94,12 @@ data class ObjString(val value: String) : Obj() {
companion object {
val type = object : ObjClass("String") {
override suspend fun deserialize(scope: Scope, decoder: LynonDecoder, lynonType: LynonType?): Obj =
if( lynonType == null )
decoder.decodeCached {
ObjString(decoder.unpackBinaryData().decodeToString())
}
else ObjString(decoder.unpackBinaryData().decodeToString())
// if( lynonType == null )
// decoder.decodeCached {
// ObjString(decoder.unpackBinaryData().decodeToString())
// }
// else
ObjString(decoder.unpackBinaryData().decodeToString())
}.apply {
addFn("toInt") {
ObjInt(thisAs<ObjString>().value.toLong())
@@ -39,9 +39,8 @@ open class LynonDecoder(val bin: BitInput, val settings: LynonSettings = LynonSe
type.objClass.deserialize(scope, this, type)
}
// todo: rewrite/remove?
suspend fun unpackObject(scope: Scope, type: ObjClass): Obj {
return type.deserialize(scope, this, null)
suspend fun decodeObject(scope: Scope, type: ObjClass): Obj {
return decodeCached { type.deserialize(scope, this, null) }
}
fun unpackBinaryData(): ByteArray = bin.decompress()
@@ -65,8 +65,10 @@ open class LynonEncoder(val bout: BitOutput, val settings: LynonSettings = Lynon
bout.putBits(type.ordinal.toULong(), 4)
}
suspend fun encodeObj(scope: Scope, obj: Obj) {
suspend fun encodeObject(scope: Scope, obj: Obj) {
encodeCached(obj) {
obj.serialize(scope, this, null)
}
}
fun encodeBinaryData(data: ByteArray) {