Widen byte/short to ints in ObjectMethods (#853)

This commit is contained in:
Jonathan Coates 2023-11-08 09:28:13 +00:00 committed by GitHub
parent 5b3c462ab8
commit 9469e4c0b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 19 deletions

View File

@ -20,6 +20,7 @@ import org.teavm.dependency.BootstrapMethodSubstitutor;
import org.teavm.dependency.DynamicCallSite; import org.teavm.dependency.DynamicCallSite;
import org.teavm.model.BasicBlock; import org.teavm.model.BasicBlock;
import org.teavm.model.MethodHandle; import org.teavm.model.MethodHandle;
import org.teavm.model.PrimitiveType;
import org.teavm.model.ValueType; import org.teavm.model.ValueType;
import org.teavm.model.emit.ConditionEmitter; import org.teavm.model.emit.ConditionEmitter;
import org.teavm.model.emit.ConditionProducer; import org.teavm.model.emit.ConditionProducer;
@ -177,7 +178,16 @@ public class ObjectMethodsSubstitutor implements BootstrapMethodSubstitutor {
String fieldTitle = (index == 0 ? "" : ", ") + fieldName + "="; String fieldTitle = (index == 0 ? "" : ", ") + fieldName + "=";
resultVar = resultVar.invokeVirtual("append", StringBuilder.class, pe.constant(fieldTitle)); resultVar = resultVar.invokeVirtual("append", StringBuilder.class, pe.constant(fieldTitle));
ValueEmitter thisField = InvokeDynamicUtil.invoke(pe, getter, thisVar); ValueEmitter thisField = InvokeDynamicUtil.invoke(pe, getter, thisVar);
if (!(getter.getValueType() instanceof ValueType.Primitive)) { if (getter.getValueType() instanceof ValueType.Primitive) {
PrimitiveType primitive = ((ValueType.Primitive) getter.getValueType()).getKind();
switch (primitive) {
case BYTE:
case SHORT:
thisField = thisField.cast(int.class);
break;
default:
}
} else {
thisField = thisField.cast(Object.class); thisField = thisField.cast(Object.class);
} }
resultVar = resultVar.invokeVirtual("append", StringBuilder.class, thisField); resultVar = resultVar.invokeVirtual("append", StringBuilder.class, thisField);

View File

@ -42,7 +42,7 @@ public class RecordTest {
@Test @Test
public void toStringMethod() { public void toStringMethod() {
String s = new B(2, "q", 3L).toString(); String s = new B(2, "q", 3L, (byte) 4, (short) 5).toString();
int index = 0; int index = 0;
@ -50,33 +50,28 @@ public class RecordTest {
assertTrue(index >= 0); assertTrue(index >= 0);
++index; ++index;
index = s.indexOf("x", index); index = checkRecordKey(s, "a", "2", index);
index = checkRecordKey(s, "b", "q", index);
index = checkRecordKey(s, "c", "3", index);
index = checkRecordKey(s, "d", "4", index);
checkRecordKey(s, "e", "5", index);
}
private static int checkRecordKey(String s, String key, String value, int index) {
index = s.indexOf(key, index);
assertTrue(index > 0); assertTrue(index > 0);
++index; ++index;
index = s.indexOf("2", index); index = s.indexOf(value, index);
assertTrue(index > 0); assertTrue(index > 0);
++index; ++index;
index = s.indexOf("y", index); return index;
assertTrue(index > 0);
++index;
index = s.indexOf("q", index);
assertTrue(index > 0);
++index;
index = s.indexOf("z", index);
assertTrue(index > 0);
++index;
index = s.indexOf("3", index);
assertTrue(index > 0);
} }
record A(int x, String y) { record A(int x, String y) {
} }
record B(int x, String y, Long z) { record B(int a, String b, Long c, byte d, short e) {
} }
} }