refs #35 serialize any: null, int, real, boolean, Instant. Added unary minus as general operator, not only to numbers. Instant truncation (nice for serialization)
This commit is contained in:
1 parent
cffe4eaffc
commit
6ab438b1f6
14 files changed
+208
-51
No files matched your search
@@ -1,36 +1,57 @@
|
||||
package net.sergeych.lynon
|
||||
|
||||
import kotlinx.datetime.Instant
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.obj.Obj
|
||||
import net.sergeych.lyng.obj.ObjClass
|
||||
import net.sergeych.lyng.obj.ObjInt
|
||||
import net.sergeych.lyng.obj.ObjNull
|
||||
import net.sergeych.lyng.obj.*
|
||||
|
||||
open class LynonDecoder(val bin: BitInput,val settings: LynonSettings = LynonSettings.default) {
|
||||
open class LynonDecoder(val bin: BitInput, val settings: LynonSettings = LynonSettings.default) {
|
||||
|
||||
val cache = mutableListOf<Obj>()
|
||||
|
||||
inline fun decodeCached(f: LynonDecoder.() -> Obj): Obj {
|
||||
return if( bin.getBit() == 0 ) {
|
||||
return if (bin.getBit() == 0) {
|
||||
// unpack and cache
|
||||
f().also {
|
||||
if( settings.shouldCache(it) ) cache.add(it)
|
||||
if (settings.shouldCache(it)) cache.add(it)
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// get cache reference
|
||||
val size = sizeInBits(cache.size)
|
||||
val id = bin.getBitsOrNull(size)?.toInt() ?: throw RuntimeException("Invalid object id: unexpected end of stream")
|
||||
if( id >= cache.size ) throw RuntimeException("Invalid object id: $id should be in 0..<${cache.size}")
|
||||
val id = bin.getBitsOrNull(size)?.toInt()
|
||||
?: throw RuntimeException("Invalid object id: unexpected end of stream")
|
||||
if (id >= cache.size) throw RuntimeException("Invalid object id: $id should be in 0..<${cache.size}")
|
||||
cache[id]
|
||||
}
|
||||
}
|
||||
|
||||
fun decodeAny(scope: Scope): Obj = decodeCached {
|
||||
val type = LynonType.entries[bin.getBits(4).toInt()]
|
||||
return when(type) {
|
||||
return when (type) {
|
||||
LynonType.Null -> ObjNull
|
||||
LynonType.Int0 -> ObjInt.Zero
|
||||
LynonType.IntPositive -> ObjInt(bin.unpackUnsigned().toLong())
|
||||
LynonType.IntNegative -> ObjInt(-bin.unpackUnsigned().toLong())
|
||||
LynonType.Bool -> ObjBool(bin.getBit() == 1)
|
||||
LynonType.Real -> ObjReal(bin.unpackDouble())
|
||||
LynonType.Instant -> {
|
||||
val mode = LynonSettings.InstantTruncateMode.entries[bin.getBits(2).toInt()]
|
||||
when (mode) {
|
||||
LynonSettings.InstantTruncateMode.Microsecond -> ObjInstant(
|
||||
Instant.fromEpochSeconds(
|
||||
bin.unpackSigned(), bin.unpackUnsigned().toInt() * 1000
|
||||
)
|
||||
)
|
||||
LynonSettings.InstantTruncateMode.Millisecond -> ObjInstant(
|
||||
Instant.fromEpochMilliseconds(
|
||||
bin.unpackSigned()
|
||||
)
|
||||
)
|
||||
LynonSettings.InstantTruncateMode.Second -> ObjInstant(
|
||||
Instant.fromEpochSeconds(bin.unpackSigned())
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
scope.raiseNotImplemented("lynon type $type")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user