From 8e99b4589ef083602e316ebe62920922a31da736 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Sat, 22 Mar 2014 11:32:39 +0400 Subject: [PATCH] Fixes System.arraycopy behavior when src == dest --- .../teavm/classlib/java/lang/SystemNativeGenerator.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java index bd50aba77..81b66a994 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java @@ -74,9 +74,17 @@ public class SystemNativeGenerator implements Generator, DependencyPlugin { String dest = context.getParameterName(3); String destPos = context.getParameterName(4); String length = context.getParameterName(5); + writer.append("if (" + src + " !== " + dest + " || " + destPos + " < " + srcPos + ") {").indent().newLine(); writer.append("for (var i = 0; i < " + length + "; i = (i + 1) | 0) {").indent().softNewLine(); writer.append(dest + ".data[" + destPos + "++] = " + src + ".data[" + srcPos + "++];").softNewLine(); writer.outdent().append("}").softNewLine(); + writer.outdent().append("} else {").indent().softNewLine(); + writer.append(srcPos + " = (" + srcPos + " + " + length + ") | 0;").softNewLine(); + writer.append(destPos + " = (" + destPos + " + " + length + ") | 0;").softNewLine(); + writer.append("for (var i = 0; i < " + length + "; i = (i + 1) | 0) {").indent().softNewLine(); + writer.append(dest + ".data[--" + destPos + "] = " + src + ".data[--" + srcPos + "];").softNewLine(); + writer.outdent().append("}").softNewLine(); + writer.outdent().append("}").softNewLine(); } private void generateCurrentTimeMillis(SourceWriter writer) throws IOException {