Coerce object types on return

This commit is contained in:
Andrey Breslav
2013-10-06 17:44:37 +04:00
parent 8d29047204
commit cec2111c29
2 changed files with 13 additions and 2 deletions
+9 -2
View File
@@ -8,6 +8,7 @@ import org.objectweb.asm.Opcodes.*
import org.objectweb.asm.tree.analysis.Interpreter
import org.objectweb.asm.tree.JumpInsnNode
import org.objectweb.asm.tree.VarInsnNode
import org.objectweb.asm.util.Printer
trait InterpreterResult {
fun toString(): String
@@ -97,8 +98,14 @@ fun interpreterLoop(
IRETURN, LRETURN, FRETURN, DRETURN, ARETURN -> {
val value = frame.getStack(0)!!
val expectedType = Type.getReturnType(m.desc)
if (value != NULL_VALUE && value.asmType != expectedType) {
assert(insnOpcode == IRETURN, "Only ints should be coerced")
if (expectedType.getSort() == Type.OBJECT) {
val coerced = if (value != NULL_VALUE && value.asmType != expectedType)
ObjectValue(value.obj, expectedType)
else value
return ValueReturned(coerced)
}
if (value.asmType != expectedType) {
assert(insnOpcode == IRETURN, "Only ints should be coerced: " + Printer.OPCODES[insnOpcode])
val coerced = when (expectedType.getSort()) {
Type.BOOLEAN -> boolean(value.boolean)
@@ -44,6 +44,10 @@ class TestData {
return "str";
}
static Object returnStringAsObject() {
return "str";
}
static int variable() {
int i = 153;
return i;