toString() supported for values and results
This commit is contained in:
@@ -10,11 +10,21 @@ import org.objectweb.asm.tree.analysis.Interpreter
|
|||||||
import org.objectweb.asm.tree.JumpInsnNode
|
import org.objectweb.asm.tree.JumpInsnNode
|
||||||
import org.objectweb.asm.tree.VarInsnNode
|
import org.objectweb.asm.tree.VarInsnNode
|
||||||
|
|
||||||
trait InterpreterResult
|
trait InterpreterResult {
|
||||||
object NOTHING_DONE: InterpreterResult
|
fun toString(): String
|
||||||
class ExceptionThrown(val exception: Value): InterpreterResult
|
}
|
||||||
class ValueReturned(val result: Value): InterpreterResult
|
|
||||||
class AbnormalTermination(val message: String): InterpreterResult
|
class ExceptionThrown(val exception: Value): InterpreterResult {
|
||||||
|
override fun toString(): String = "Thrown $exception"
|
||||||
|
}
|
||||||
|
|
||||||
|
class ValueReturned(val result: Value): InterpreterResult {
|
||||||
|
override fun toString(): String = "Returned $result"
|
||||||
|
}
|
||||||
|
|
||||||
|
class AbnormalTermination(val message: String): InterpreterResult {
|
||||||
|
override fun toString(): String = "Terminated abnormally: $message"
|
||||||
|
}
|
||||||
|
|
||||||
trait InterpretationEventHandler {
|
trait InterpretationEventHandler {
|
||||||
|
|
||||||
@@ -42,7 +52,7 @@ fun interpreterLoop(
|
|||||||
handler: InterpretationEventHandler = InterpretationEventHandler.NONE
|
handler: InterpretationEventHandler = InterpretationEventHandler.NONE
|
||||||
): InterpreterResult {
|
): InterpreterResult {
|
||||||
val firstInsn = m.instructions.getFirst()
|
val firstInsn = m.instructions.getFirst()
|
||||||
if (firstInsn == null) return NOTHING_DONE
|
if (firstInsn == null) throw IllegalArgumentException("Empty method")
|
||||||
|
|
||||||
var currentInsn = firstInsn!!
|
var currentInsn = firstInsn!!
|
||||||
|
|
||||||
|
|||||||
@@ -7,36 +7,45 @@ trait Value : org.objectweb.asm.tree.analysis.Value {
|
|||||||
val asmType: Type
|
val asmType: Type
|
||||||
val valid: Boolean
|
val valid: Boolean
|
||||||
override fun getSize(): Int = asmType.getSize()
|
override fun getSize(): Int = asmType.getSize()
|
||||||
|
|
||||||
|
override fun toString(): String
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class AbstractValue(
|
abstract class AbstractValue(
|
||||||
override val asmType: Type
|
override val asmType: Type,
|
||||||
|
private val asString: String
|
||||||
) : Value {
|
) : Value {
|
||||||
override val valid = true
|
override val valid = true
|
||||||
|
|
||||||
|
override fun toString() = asString
|
||||||
}
|
}
|
||||||
|
|
||||||
object NOT_A_VALUE: Value {
|
object NOT_A_VALUE: Value {
|
||||||
override val asmType = Type.getType("<invalid>")
|
override val asmType = Type.getType("<invalid>")
|
||||||
override val valid = false
|
override val valid = false
|
||||||
override fun getSize(): Int = 1
|
override fun getSize(): Int = 1
|
||||||
|
|
||||||
|
override fun toString() = "NOT_A_VALUE"
|
||||||
}
|
}
|
||||||
|
|
||||||
object VOID_VALUE: Value {
|
object VOID_VALUE: Value {
|
||||||
override val asmType: Type = Type.VOID_TYPE
|
override val asmType: Type = Type.VOID_TYPE
|
||||||
override val valid: Boolean = false
|
override val valid: Boolean = false
|
||||||
|
override fun toString() = "VOID_VALUE"
|
||||||
}
|
}
|
||||||
|
|
||||||
class NotInitialized(override val asmType: Type): Value {
|
class NotInitialized(override val asmType: Type): Value {
|
||||||
override val valid = false
|
override val valid = false
|
||||||
|
override fun toString() = "NotInitialized: $asmType"
|
||||||
}
|
}
|
||||||
|
|
||||||
class IntValue(val value: Int, asmType: Type): AbstractValue(asmType)
|
class IntValue(val value: Int, asmType: Type): AbstractValue(asmType, "$value")
|
||||||
class LongValue(val value: Long): AbstractValue(Type.LONG_TYPE)
|
class LongValue(val value: Long): AbstractValue(Type.LONG_TYPE, "$value")
|
||||||
class FloatValue(val value: Float): AbstractValue(Type.FLOAT_TYPE)
|
class FloatValue(val value: Float): AbstractValue(Type.FLOAT_TYPE, "$value")
|
||||||
class DoubleValue(val value: Double): AbstractValue(Type.DOUBLE_TYPE)
|
class DoubleValue(val value: Double): AbstractValue(Type.DOUBLE_TYPE, "$value")
|
||||||
class ObjectValue(val value: Any?, asmType: Type): AbstractValue(asmType)
|
class ObjectValue(val value: Any?, asmType: Type): AbstractValue(asmType, "$value")
|
||||||
|
|
||||||
class LabelValue(val value: LabelNode): AbstractValue(Type.VOID_TYPE)
|
class LabelValue(val value: LabelNode): AbstractValue(Type.VOID_TYPE, "label: ${value.getLabel()}")
|
||||||
|
|
||||||
fun boolean(v: Boolean) = IntValue(if (v) 1 else 0, Type.BOOLEAN_TYPE)
|
fun boolean(v: Boolean) = IntValue(if (v) 1 else 0, Type.BOOLEAN_TYPE)
|
||||||
fun byte(v: Byte) = IntValue(v.toInt(), Type.BYTE_TYPE)
|
fun byte(v: Byte) = IntValue(v.toInt(), Type.BYTE_TYPE)
|
||||||
|
|||||||
Reference in New Issue
Block a user