Refactoring of DFGSerializer
This commit is contained in:
+76
-74
@@ -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) {
|
||||||
@@ -229,9 +231,9 @@ internal object DFGSerializer {
|
|||||||
result.writeByte(
|
result.writeByte(
|
||||||
when {
|
when {
|
||||||
external != null -> 1
|
external != null -> 1
|
||||||
public != null -> 2
|
public != null -> 2
|
||||||
private != null -> 3
|
private != null -> 3
|
||||||
else -> 0
|
else -> 0
|
||||||
}.toByte()
|
}.toByte()
|
||||||
)
|
)
|
||||||
external?.write(result)
|
external?.write(result)
|
||||||
@@ -251,9 +253,9 @@ internal object DFGSerializer {
|
|||||||
fun read(data: ArraySlice): Type {
|
fun read(data: ArraySlice): Type {
|
||||||
val tag = data.readByte().toInt()
|
val tag = data.readByte().toInt()
|
||||||
return when (tag) {
|
return when (tag) {
|
||||||
1 -> Type(ExternalType(data), null, null, false)
|
1 -> Type(ExternalType(data), null, null, false)
|
||||||
2 -> Type(null, PublicType(data), null, false)
|
2 -> Type(null, PublicType(data), null, false)
|
||||||
3 -> Type(null, null, PrivateType(data), false)
|
3 -> Type(null, null, PrivateType(data), false)
|
||||||
else -> Type(null, null, null, true)
|
else -> Type(null, null, null, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -300,9 +302,9 @@ internal object DFGSerializer {
|
|||||||
result.writeByte(
|
result.writeByte(
|
||||||
when {
|
when {
|
||||||
external != null -> 1
|
external != null -> 1
|
||||||
public != null -> 2
|
public != null -> 2
|
||||||
private != null -> 3
|
private != null -> 3
|
||||||
else -> 0
|
else -> 0
|
||||||
}.toByte()
|
}.toByte()
|
||||||
)
|
)
|
||||||
external?.write(result)
|
external?.write(result)
|
||||||
@@ -323,9 +325,9 @@ internal object DFGSerializer {
|
|||||||
fun read(data: ArraySlice): FunctionSymbol {
|
fun read(data: ArraySlice): FunctionSymbol {
|
||||||
val tag = data.readByte().toInt()
|
val tag = data.readByte().toInt()
|
||||||
return when (tag) {
|
return when (tag) {
|
||||||
1 -> FunctionSymbol(ExternalFunctionSymbol(data), null, null)
|
1 -> FunctionSymbol(ExternalFunctionSymbol(data), null, null)
|
||||||
2 -> FunctionSymbol(null, PublicFunctionSymbol(data), null)
|
2 -> FunctionSymbol(null, PublicFunctionSymbol(data), null)
|
||||||
3 -> FunctionSymbol(null, null, PrivateFunctionSymbol(data))
|
3 -> FunctionSymbol(null, null, PrivateFunctionSymbol(data))
|
||||||
else -> FunctionSymbol(null, null, null)
|
else -> FunctionSymbol(null, null, null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -497,43 +499,43 @@ internal object DFGSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Node {
|
class Node {
|
||||||
var parameter : Parameter? = null
|
var parameter : Parameter? = null
|
||||||
var const : Const? = null
|
var const : Const? = null
|
||||||
var staticCall : StaticCall? = null
|
var staticCall : StaticCall? = null
|
||||||
var newObject : NewObject? = null
|
var newObject : NewObject? = null
|
||||||
var vtableCall : VtableCall? = null
|
var vtableCall : VtableCall? = null
|
||||||
var itableCall : ItableCall? = null
|
var itableCall : ItableCall? = null
|
||||||
var singleton : Singleton? = null
|
var singleton : Singleton? = null
|
||||||
var fieldRead : FieldRead? = null
|
var fieldRead : FieldRead? = null
|
||||||
var fieldWrite : FieldWrite? = null
|
var fieldWrite : FieldWrite? = null
|
||||||
var variable : Variable? = null
|
var variable : Variable? = null
|
||||||
|
|
||||||
val type get() = when {
|
val type get() = when {
|
||||||
parameter != null -> NodeType.PARAMETER
|
parameter != null -> NodeType.PARAMETER
|
||||||
const != null -> NodeType.CONST
|
const != null -> NodeType.CONST
|
||||||
staticCall != null -> NodeType.STATIC_CALL
|
staticCall != null -> NodeType.STATIC_CALL
|
||||||
newObject != null -> NodeType.NEW_OBJECT
|
newObject != null -> NodeType.NEW_OBJECT
|
||||||
vtableCall != null -> NodeType.VTABLE_CALL
|
vtableCall != null -> NodeType.VTABLE_CALL
|
||||||
itableCall != null -> NodeType.ITABLE_CALL
|
itableCall != null -> NodeType.ITABLE_CALL
|
||||||
singleton != null -> NodeType.SINGLETON
|
singleton != null -> NodeType.SINGLETON
|
||||||
fieldRead != null -> NodeType.FIELD_READ
|
fieldRead != null -> NodeType.FIELD_READ
|
||||||
fieldWrite != null -> NodeType.FIELD_WRITE
|
fieldWrite != null -> NodeType.FIELD_WRITE
|
||||||
variable != null -> NodeType.VARIABLE
|
variable != null -> NodeType.VARIABLE
|
||||||
else -> NodeType.UNKNOWN
|
else -> NodeType.UNKNOWN
|
||||||
}
|
}
|
||||||
|
|
||||||
fun write(result: ArraySlice) {
|
fun write(result: ArraySlice) {
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@@ -571,17 +573,17 @@ internal object DFGSerializer {
|
|||||||
val type = enumValues<NodeType>()[data.readByte().toInt()]
|
val type = enumValues<NodeType>()[data.readByte().toInt()]
|
||||||
val result = Node()
|
val result = Node()
|
||||||
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 -> { }
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user