Refactoring of DFGSerializer
This commit is contained in:
+30
-28
@@ -39,8 +39,6 @@ internal object DFGSerializer {
|
|||||||
index += 4
|
index += 4
|
||||||
}
|
}
|
||||||
|
|
||||||
fun writeNullableInt(value: Int?) = writeNullable(value) { this.writeInt(it) }
|
|
||||||
|
|
||||||
fun writeLong(value: Long) {
|
fun writeLong(value: Long) {
|
||||||
ensureSize(8)
|
ensureSize(8)
|
||||||
theUnsafe.putLong(array, byteArrayDataOffset + index, value)
|
theUnsafe.putLong(array, byteArrayDataOffset + index, value)
|
||||||
@@ -53,6 +51,16 @@ internal object DFGSerializer {
|
|||||||
index++
|
index++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline fun <T> writeNullable(value: T?, valueWriter: ArraySlice.(T) -> Unit) {
|
||||||
|
writeBoolean(value != null)
|
||||||
|
if (value != null)
|
||||||
|
this.valueWriter(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun writeNullableInt(value: Int?) = writeNullable(value) { this.writeInt(it) }
|
||||||
|
|
||||||
|
fun writeNullableString(s: String?) = writeNullable(s) { writeString(it) }
|
||||||
|
|
||||||
//------------Read------------------------------------------------------------------//
|
//------------Read------------------------------------------------------------------//
|
||||||
|
|
||||||
fun readByte(): Byte {
|
fun readByte(): Byte {
|
||||||
@@ -65,8 +73,6 @@ internal object DFGSerializer {
|
|||||||
return theUnsafe.getInt(array, byteArrayDataOffset + index).also { index += 4 }
|
return theUnsafe.getInt(array, byteArrayDataOffset + index).also { index += 4 }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun readNullableInt() = readNullable { this.readInt() }
|
|
||||||
|
|
||||||
fun readLong(): Long {
|
fun readLong(): Long {
|
||||||
checkSize(8)
|
checkSize(8)
|
||||||
return theUnsafe.getLong(array, byteArrayDataOffset + index).also { index += 8 }
|
return theUnsafe.getLong(array, byteArrayDataOffset + index).also { index += 8 }
|
||||||
@@ -77,6 +83,13 @@ internal object DFGSerializer {
|
|||||||
return theUnsafe.getBoolean(array, byteArrayDataOffset + index).also { index++ }
|
return theUnsafe.getBoolean(array, byteArrayDataOffset + index).also { index++ }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline fun <T> readNullable(valueReader: ArraySlice.() -> T) =
|
||||||
|
if (readBoolean()) this.valueReader() else null
|
||||||
|
|
||||||
|
fun readNullableInt() = readNullable { this.readInt() }
|
||||||
|
|
||||||
|
fun readNullableString() = readNullable { readString() }
|
||||||
|
|
||||||
//------------Write arrays------------------------------------------------------------------//
|
//------------Write arrays------------------------------------------------------------------//
|
||||||
|
|
||||||
fun writeIntArray(source: IntArray) {
|
fun writeIntArray(source: IntArray) {
|
||||||
@@ -96,7 +109,10 @@ internal object DFGSerializer {
|
|||||||
index += dataSize
|
index += dataSize
|
||||||
}
|
}
|
||||||
|
|
||||||
fun writeNullableString(s: String?) = writeNullable(s) { writeString(it) }
|
inline fun <reified T> writeArray(array: Array<T>, itemWriter: ArraySlice.(T) -> Unit) {
|
||||||
|
writeInt(array.size)
|
||||||
|
array.forEach { this.itemWriter(it) }
|
||||||
|
}
|
||||||
|
|
||||||
//------------Read arrays------------------------------------------------------------------//
|
//------------Read arrays------------------------------------------------------------------//
|
||||||
|
|
||||||
@@ -122,24 +138,10 @@ internal object DFGSerializer {
|
|||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
fun readNullableString() = readNullable { readString() }
|
|
||||||
|
|
||||||
inline fun <reified T> readArray(itemReader: ArraySlice.() -> T) =
|
inline fun <reified T> readArray(itemReader: ArraySlice.() -> T) =
|
||||||
Array(readInt()) { this.itemReader() }
|
Array(readInt()) { this.itemReader() }
|
||||||
|
|
||||||
inline fun <reified T> writeArray(array: Array<T>, itemWriter: ArraySlice.(T) -> Unit) {
|
//------------Resizing------------------------------------------------------------------//
|
||||||
writeInt(array.size)
|
|
||||||
array.forEach { this.itemWriter(it) }
|
|
||||||
}
|
|
||||||
|
|
||||||
inline fun <T> readNullable(valueReader: ArraySlice.() -> T) =
|
|
||||||
if (readBoolean()) this.valueReader() else null
|
|
||||||
|
|
||||||
inline fun <T> writeNullable(value: T?, valueWriter: ArraySlice.(T) -> Unit) {
|
|
||||||
writeBoolean(value != null)
|
|
||||||
if (value != null)
|
|
||||||
this.valueWriter(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun trim() {
|
fun trim() {
|
||||||
if (array.size > index) {
|
if (array.size > index) {
|
||||||
@@ -526,13 +528,13 @@ internal object DFGSerializer {
|
|||||||
result.writeByte(type.ordinal.toByte())
|
result.writeByte(type.ordinal.toByte())
|
||||||
parameter ?.write(result)
|
parameter ?.write(result)
|
||||||
const ?.write(result)
|
const ?.write(result)
|
||||||
staticCall ?.write(result)
|
staticCall?.write(result)
|
||||||
newObject ?.write(result)
|
newObject ?.write(result)
|
||||||
vtableCall ?.write(result)
|
vtableCall?.write(result)
|
||||||
itableCall ?.write(result)
|
itableCall?.write(result)
|
||||||
singleton ?.write(result)
|
singleton ?.write(result)
|
||||||
fieldRead ?.write(result)
|
fieldRead ?.write(result)
|
||||||
fieldWrite ?.write(result)
|
fieldWrite?.write(result)
|
||||||
variable ?.write(result)
|
variable ?.write(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -573,13 +575,13 @@ internal object DFGSerializer {
|
|||||||
when (type) {
|
when (type) {
|
||||||
NodeType.PARAMETER -> result.parameter = Parameter (data)
|
NodeType.PARAMETER -> result.parameter = Parameter (data)
|
||||||
NodeType.CONST -> result.const = Const (data)
|
NodeType.CONST -> result.const = Const (data)
|
||||||
NodeType.STATIC_CALL -> result.staticCall = StaticCall (data)
|
NodeType.STATIC_CALL -> result.staticCall = StaticCall(data)
|
||||||
NodeType.NEW_OBJECT -> result.newObject = NewObject (data)
|
NodeType.NEW_OBJECT -> result.newObject = NewObject (data)
|
||||||
NodeType.VTABLE_CALL -> result.vtableCall = VtableCall (data)
|
NodeType.VTABLE_CALL -> result.vtableCall = VtableCall(data)
|
||||||
NodeType.ITABLE_CALL -> result.itableCall = ItableCall (data)
|
NodeType.ITABLE_CALL -> result.itableCall = ItableCall(data)
|
||||||
NodeType.SINGLETON -> result.singleton = Singleton (data)
|
NodeType.SINGLETON -> result.singleton = Singleton (data)
|
||||||
NodeType.FIELD_READ -> result.fieldRead = FieldRead (data)
|
NodeType.FIELD_READ -> result.fieldRead = FieldRead (data)
|
||||||
NodeType.FIELD_WRITE -> result.fieldWrite = FieldWrite (data)
|
NodeType.FIELD_WRITE -> result.fieldWrite = FieldWrite(data)
|
||||||
NodeType.VARIABLE -> result.variable = Variable (data)
|
NodeType.VARIABLE -> result.variable = Variable (data)
|
||||||
else -> { }
|
else -> { }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user