Fix concatenation with byte and short in JVM 9+

Fix #522
This commit is contained in:
Alexey Andreev 2020-09-08 15:03:19 +03:00
parent 17098495f2
commit d97dce4650
2 changed files with 25 additions and 1 deletions

View File

@ -23,6 +23,7 @@ import org.teavm.model.RuntimeConstant;
import org.teavm.model.ValueType; import org.teavm.model.ValueType;
import org.teavm.model.emit.ProgramEmitter; import org.teavm.model.emit.ProgramEmitter;
import org.teavm.model.emit.ValueEmitter; import org.teavm.model.emit.ValueEmitter;
import org.teavm.model.instructions.IntegerSubtype;
public class StringConcatFactorySubstitutor implements BootstrapMethodSubstitutor { public class StringConcatFactorySubstitutor implements BootstrapMethodSubstitutor {
private ReferenceCache referenceCache = new ReferenceCache(); private ReferenceCache referenceCache = new ReferenceCache();
@ -99,7 +100,20 @@ public class StringConcatFactorySubstitutor implements BootstrapMethodSubstituto
} }
private ValueEmitter appendArgument(ValueEmitter sb, ValueType type, ValueEmitter argument) { private ValueEmitter appendArgument(ValueEmitter sb, ValueType type, ValueEmitter argument) {
if (!(type instanceof ValueType.Primitive)) { if (type instanceof ValueType.Primitive) {
switch (((ValueType.Primitive) type).getKind()) {
case BYTE:
argument = argument.castToInteger(IntegerSubtype.BYTE);
type = ValueType.INTEGER;
break;
case SHORT:
argument = argument.castToInteger(IntegerSubtype.SHORT);
type = ValueType.INTEGER;
break;
default:
break;
}
} else {
type = ValueType.object("java.lang.Object"); type = ValueType.object("java.lang.Object");
} }
MethodReference method = referenceCache.getCached(new MethodReference(STRING_BUILDER, "append", type, MethodReference method = referenceCache.getCached(new MethodReference(STRING_BUILDER, "append", type,

View File

@ -181,12 +181,22 @@ public class VMTest {
public void stringConcat() { public void stringConcat() {
assertEquals("(23)", surroundWithParentheses(23)); assertEquals("(23)", surroundWithParentheses(23));
assertEquals("(42)", surroundWithParentheses(42)); assertEquals("(42)", surroundWithParentheses(42));
assertEquals("(17)", surroundWithParentheses((byte) 17));
assertEquals("(19)", surroundWithParentheses((short) 19));
} }
private String surroundWithParentheses(int value) { private String surroundWithParentheses(int value) {
return "(" + value + ")"; return "(" + value + ")";
} }
private String surroundWithParentheses(byte value) {
return "(" + value + ")";
}
private String surroundWithParentheses(short value) {
return "(" + value + ")";
}
@Test @Test
public void variableReadInCatchBlock() { public void variableReadInCatchBlock() {
int n = foo(); int n = foo();