diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java index 909017701..f715944e9 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java @@ -247,7 +247,7 @@ public class TObject { } else { RuntimeArray array = (RuntimeArray) self; copy = Allocator.allocateArray(cls, array.size).toStructure(); - int itemSize = (cls.itemType.flags & RuntimeClass.PRIMITIVE) == 0 ? 4 : cls.itemType.size; + int itemSize = (cls.itemType.flags & RuntimeClass.PRIMITIVE) == 0 ? Address.sizeOf() : cls.itemType.size; Address headerSize = Address.align(Address.fromInt(Structure.sizeOf(RuntimeArray.class)), itemSize); size = itemSize * array.size + headerSize.toInt(); } diff --git a/core/src/main/java/org/teavm/backend/c/generate/ClassGenerator.java b/core/src/main/java/org/teavm/backend/c/generate/ClassGenerator.java index f9053e076..67fdbcff6 100644 --- a/core/src/main/java/org/teavm/backend/c/generate/ClassGenerator.java +++ b/core/src/main/java/org/teavm/backend/c/generate/ClassGenerator.java @@ -772,7 +772,7 @@ public class ClassGenerator { for (int i = 1; i < ranges.size(); ++i) { lower = ranges.get(i - 1).upper; upper = ranges.get(i).lower; - isSupertypeWriter.println("if (tag >= " + lower + " || tag < " + upper + ") return INT32_C(0);"); + isSupertypeWriter.println("if (tag >= " + lower + " && tag < " + upper + ") return INT32_C(0);"); } isSupertypeWriter.println("return INT32_C(1);"); diff --git a/core/src/main/java/org/teavm/backend/c/generate/CodeGenerationVisitor.java b/core/src/main/java/org/teavm/backend/c/generate/CodeGenerationVisitor.java index 04e0fae56..e989af519 100644 --- a/core/src/main/java/org/teavm/backend/c/generate/CodeGenerationVisitor.java +++ b/core/src/main/java/org/teavm/backend/c/generate/CodeGenerationVisitor.java @@ -66,6 +66,7 @@ import org.teavm.model.ClassReader; import org.teavm.model.MethodReader; import org.teavm.model.MethodReference; import org.teavm.model.ValueType; +import org.teavm.model.classes.VirtualTable; import org.teavm.runtime.Allocator; import org.teavm.runtime.ExceptionHandling; import org.teavm.runtime.RuntimeClass; @@ -140,6 +141,28 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor { return; } + case MODULO: { + switch (expr.getType()) { + case FLOAT: + writer.print("fmodf("); + expr.getFirstOperand().acceptVisitor(this); + writer.print(", "); + expr.getSecondOperand().acceptVisitor(this); + writer.print(")"); + return; + case DOUBLE: + writer.print("fmod("); + expr.getFirstOperand().acceptVisitor(this); + writer.print(", "); + expr.getSecondOperand().acceptVisitor(this); + writer.print(")"); + return; + default: + break; + } + break; + } + default: break; } @@ -372,28 +395,47 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor { break; } case DYNAMIC: { - String receiver = "recv_" + temporaryReceiverLevel++; - maxTemporaryReceiverLevel = Math.max(maxTemporaryReceiverLevel, temporaryReceiverLevel); - writer.print("((").print(receiver).print(" = "); - expr.getArguments().get(0).acceptVisitor(this); + VirtualTable vtable = context.getVirtualTableProvider().lookup(expr.getMethod().getClassName()); + if (vtable == null || !vtable.getEntries().containsKey(expr.getMethod().getDescriptor())) { + writer.print("("); + for (Expr arg : expr.getArguments()) { + arg.acceptVisitor(this); + writer.print(", "); + } + printDefaultValue(expr.getMethod().getReturnType()); + writer.print(")"); + } else { + String receiver = "recv_" + temporaryReceiverLevel++; + maxTemporaryReceiverLevel = Math.max(maxTemporaryReceiverLevel, temporaryReceiverLevel); + writer.print("((").print(receiver).print(" = "); + expr.getArguments().get(0).acceptVisitor(this); - writer.print("), METHOD(") - .print(receiver).print(", ") - .print(names.forClassClass(expr.getMethod().getClassName())).print(", ") - .print(names.forVirtualMethod(expr.getMethod())) - .print(")(").print(receiver); - for (int i = 1; i < expr.getArguments().size(); ++i) { - writer.print(", "); - expr.getArguments().get(i).acceptVisitor(this); + writer.print("), METHOD(") + .print(receiver).print(", ") + .print(names.forClassClass(expr.getMethod().getClassName())).print(", ") + .print(names.forVirtualMethod(expr.getMethod())) + .print(")(").print(receiver); + for (int i = 1; i < expr.getArguments().size(); ++i) { + writer.print(", "); + expr.getArguments().get(i).acceptVisitor(this); + } + writer.print("))"); + + temporaryReceiverLevel--; } - writer.print("))"); - - temporaryReceiverLevel--; break; } } } + private void printDefaultValue(ValueType type) { + if (type instanceof ValueType.Primitive) { + writer.print("0"); + } else { + writer.print("NULL"); + } + } + private void processInclude(AnnotationContainerReader container) { AnnotationReader annot = container.get(Include.class.getName()); if (annot == null) { diff --git a/tests/src/test/java/org/teavm/classlib/java/util/VectorTest.java b/tests/src/test/java/org/teavm/classlib/java/util/VectorTest.java index 626b86a74..255e5b6ed 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/VectorTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/VectorTest.java @@ -52,6 +52,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.Arrays; @@ -326,9 +327,11 @@ public class VectorTest { Enumeration orgNum = tVector.elements(); Enumeration cnum = v.elements(); + int index = 0; while (orgNum.hasMoreElements()) { assertTrue("Not enough elements copied", cnum.hasMoreElements()); - assertTrue("Vector cloned improperly, elements do not match", orgNum.nextElement() == cnum.nextElement()); + assertSame("Vector cloned improperly, element " + index++ + " does not match", + orgNum.nextElement(), cnum.nextElement()); } assertTrue("Not enough elements copied", !cnum.hasMoreElements()); }