All objects should be loaded by Eval and not directly
This commit is contained in:
@@ -19,6 +19,7 @@ class UnsupportedByteCodeException(message: String) : RuntimeException(message)
|
|||||||
|
|
||||||
trait Eval {
|
trait Eval {
|
||||||
fun loadClass(classType: Type): Value
|
fun loadClass(classType: Type): Value
|
||||||
|
fun loadString(str: String): Value
|
||||||
fun newInstance(classType: Type): Value
|
fun newInstance(classType: Type): Value
|
||||||
fun checkCast(value: Value, targetType: Type): Value
|
fun checkCast(value: Value, targetType: Type): Value
|
||||||
fun isInsetanceOf(value: Value, targetType: Type): Boolean
|
fun isInsetanceOf(value: Value, targetType: Type): Boolean
|
||||||
@@ -83,7 +84,7 @@ class SingleInstructionInterpreter(private val eval: Eval) : Interpreter<Value>(
|
|||||||
is Float -> float(cst)
|
is Float -> float(cst)
|
||||||
is Long -> long(cst)
|
is Long -> long(cst)
|
||||||
is Double -> double(cst)
|
is Double -> double(cst)
|
||||||
is String -> obj(cst)
|
is String -> eval.loadString(cst)
|
||||||
is Type -> {
|
is Type -> {
|
||||||
val sort = (cst as Type).getSort()
|
val sort = (cst as Type).getSort()
|
||||||
when (sort) {
|
when (sort) {
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ fun interpreterLoop(
|
|||||||
IRETURN, LRETURN, FRETURN, DRETURN, ARETURN -> {
|
IRETURN, LRETURN, FRETURN, DRETURN, ARETURN -> {
|
||||||
val value = frame.getStack(0)!!
|
val value = frame.getStack(0)!!
|
||||||
val expectedType = Type.getReturnType(m.desc)
|
val expectedType = Type.getReturnType(m.desc)
|
||||||
if (value.asmType != expectedType) {
|
if (value != NULL_VALUE && value.asmType != expectedType) {
|
||||||
assert(insnOpcode == IRETURN, "Only ints should be coerced")
|
assert(insnOpcode == IRETURN, "Only ints should be coerced")
|
||||||
|
|
||||||
val coerced = when (expectedType.getSort()) {
|
val coerced = when (expectedType.getSort()) {
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ fun int(v: Int) = IntValue(v, Type.INT_TYPE)
|
|||||||
fun long(v: Long) = LongValue(v)
|
fun long(v: Long) = LongValue(v)
|
||||||
fun float(v: Float) = FloatValue(v)
|
fun float(v: Float) = FloatValue(v)
|
||||||
fun double(v: Double) = DoubleValue(v)
|
fun double(v: Double) = DoubleValue(v)
|
||||||
fun obj<T>(v: T, t: Type = if (v != null) Type.getType(v.javaClass) else Type.getType(javaClass<Any>())) = ObjectValue(v, t)
|
//fun obj<T>(v: T, t: Type = if (v != null) Type.getType(v.javaClass) else Type.getType(javaClass<Any>())) = ObjectValue(v, t)
|
||||||
|
|
||||||
val NULL_VALUE = ObjectValue(null, Type.getObjectType("null"))
|
val NULL_VALUE = ObjectValue(null, Type.getObjectType("null"))
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,14 @@ class TestData {
|
|||||||
return 2.0d;
|
return 2.0d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Object returnNull() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
static String returnString() {
|
||||||
|
return "str";
|
||||||
|
}
|
||||||
|
|
||||||
static int variable() {
|
static int variable() {
|
||||||
int i = 153;
|
int i = 153;
|
||||||
return i;
|
return i;
|
||||||
@@ -131,4 +139,5 @@ class TestData {
|
|||||||
}
|
}
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ fun doTest(ownerClass: Class<TestData>, methodNode: MethodNode): TestCase? {
|
|||||||
Type.LONG -> ValueReturned(long(result as Long))
|
Type.LONG -> ValueReturned(long(result as Long))
|
||||||
Type.DOUBLE -> ValueReturned(double(result as Double))
|
Type.DOUBLE -> ValueReturned(double(result as Double))
|
||||||
Type.FLOAT -> ValueReturned(float(result as Float))
|
Type.FLOAT -> ValueReturned(float(result as Float))
|
||||||
Type.OBJECT -> ValueReturned(obj(result))
|
Type.OBJECT -> ValueReturned(if (result == null) NULL_VALUE else ObjectValue(result, returnType))
|
||||||
else -> {
|
else -> {
|
||||||
println("Unsupported result type: $returnType")
|
println("Unsupported result type: $returnType")
|
||||||
return null
|
return null
|
||||||
@@ -92,6 +92,8 @@ object REFLECTION_EVAL : Eval {
|
|||||||
override fun loadClass(classType: Type): Value {
|
override fun loadClass(classType: Type): Value {
|
||||||
throw UnsupportedOperationException()
|
throw UnsupportedOperationException()
|
||||||
}
|
}
|
||||||
|
override fun loadString(str: String): Value = ObjectValue(str, Type.getType(javaClass<String>()))
|
||||||
|
|
||||||
override fun newInstance(classType: Type): Value {
|
override fun newInstance(classType: Type): Value {
|
||||||
throw UnsupportedOperationException()
|
throw UnsupportedOperationException()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user