diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/Renderer.java b/core/src/main/java/org/teavm/backend/javascript/rendering/Renderer.java index 0988d622d..c60bf6223 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/Renderer.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/Renderer.java @@ -158,6 +158,7 @@ public class Renderer implements RenderingManager { public void renderRuntime() throws RenderingException { try { + renderSetCloneMethod(); renderRuntimeCls(); renderRuntimeString(); renderRuntimeUnwrapString(); @@ -172,6 +173,13 @@ public class Renderer implements RenderingManager { } } + private void renderSetCloneMethod() throws IOException { + writer.append("function $rt_setCloneMethod(target, f)").ws().append("{").softNewLine().indent(); + writer.append("target.").appendMethod("clone", Object.class).ws().append('=').ws().append("f;"). + softNewLine().indent(); + writer.outdent().append("}").newLine(); + } + private void renderRuntimeCls() throws IOException { writer.append("function $rt_cls(cls)").ws().append("{").softNewLine().indent(); writer.append("return ").appendMethodBody("java.lang.Class", "getClass", diff --git a/core/src/main/resources/org/teavm/backend/javascript/runtime.js b/core/src/main/resources/org/teavm/backend/javascript/runtime.js index 9c1c2e615..98e1a2525 100644 --- a/core/src/main/resources/org/teavm/backend/javascript/runtime.js +++ b/core/src/main/resources/org/teavm/backend/javascript/runtime.js @@ -139,6 +139,9 @@ function $rt_arraycls(cls) { str += "]"; return str; }; + $rt_setCloneMethod(arraycls.prototype, function () { + return new arraycls(this.data.slice()); + }); var name = "[" + cls.$meta.binaryName; arraycls.$meta = { item : cls, supertypes : [$rt_objcls()], primitive : false, superclass : $rt_objcls(), name : name, binaryName : name, enum : false }; diff --git a/tests/src/test/java/org/teavm/vm/VMTest.java b/tests/src/test/java/org/teavm/vm/VMTest.java index aded39e2b..b246ac10b 100644 --- a/tests/src/test/java/org/teavm/vm/VMTest.java +++ b/tests/src/test/java/org/teavm/vm/VMTest.java @@ -17,6 +17,7 @@ package org.teavm.vm; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; import static org.junit.Assert.fail; import org.junit.Test; import org.junit.runner.RunWith; @@ -393,4 +394,13 @@ public class VMTest { class PathJoint implements FirstPath, SecondPath { } + + @Test + public void cloneArray() { + String[] a = new String[] { "foo" }; + String[] b = a.clone(); + assertNotSame(a, b); + a[0] = "bar"; + assertEquals("foo", b[0]); + } } \ No newline at end of file