All objects should be loaded by Eval and not directly

This commit is contained in:
Andrey Breslav
2013-10-06 16:30:30 +04:00
parent c5cddd42cb
commit 9b2190e1b9
5 changed files with 16 additions and 4 deletions
+2 -1
View File
@@ -19,6 +19,7 @@ class UnsupportedByteCodeException(message: String) : RuntimeException(message)
trait Eval {
fun loadClass(classType: Type): Value
fun loadString(str: String): Value
fun newInstance(classType: Type): Value
fun checkCast(value: Value, targetType: Type): Value
fun isInsetanceOf(value: Value, targetType: Type): Boolean
@@ -83,7 +84,7 @@ class SingleInstructionInterpreter(private val eval: Eval) : Interpreter<Value>(
is Float -> float(cst)
is Long -> long(cst)
is Double -> double(cst)
is String -> obj(cst)
is String -> eval.loadString(cst)
is Type -> {
val sort = (cst as Type).getSort()
when (sort) {
+1 -1
View File
@@ -97,7 +97,7 @@ fun interpreterLoop(
IRETURN, LRETURN, FRETURN, DRETURN, ARETURN -> {
val value = frame.getStack(0)!!
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")
val coerced = when (expectedType.getSort()) {
+1 -1
View File
@@ -65,7 +65,7 @@ fun int(v: Int) = IntValue(v, Type.INT_TYPE)
fun long(v: Long) = LongValue(v)
fun float(v: Float) = FloatValue(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"))
@@ -36,6 +36,14 @@ class TestData {
return 2.0d;
}
static Object returnNull() {
return null;
}
static String returnString() {
return "str";
}
static int variable() {
int i = 153;
return i;
@@ -131,4 +139,5 @@ class TestData {
}
return i;
}
}
+3 -1
View File
@@ -58,7 +58,7 @@ fun doTest(ownerClass: Class<TestData>, methodNode: MethodNode): TestCase? {
Type.LONG -> ValueReturned(long(result as Long))
Type.DOUBLE -> ValueReturned(double(result as Double))
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 -> {
println("Unsupported result type: $returnType")
return null
@@ -92,6 +92,8 @@ object REFLECTION_EVAL : Eval {
override fun loadClass(classType: Type): Value {
throw UnsupportedOperationException()
}
override fun loadString(str: String): Value = ObjectValue(str, Type.getType(javaClass<String>()))
override fun newInstance(classType: Type): Value {
throw UnsupportedOperationException()
}