Fix T[].clone()

This commit is contained in:
Alexey Andreev 2017-11-03 21:58:48 +03:00
parent 90cc2c4677
commit 344cb4e42d
3 changed files with 21 additions and 0 deletions

View File

@ -158,6 +158,7 @@ public class Renderer implements RenderingManager {
public void renderRuntime() throws RenderingException { public void renderRuntime() throws RenderingException {
try { try {
renderSetCloneMethod();
renderRuntimeCls(); renderRuntimeCls();
renderRuntimeString(); renderRuntimeString();
renderRuntimeUnwrapString(); 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 { private void renderRuntimeCls() throws IOException {
writer.append("function $rt_cls(cls)").ws().append("{").softNewLine().indent(); writer.append("function $rt_cls(cls)").ws().append("{").softNewLine().indent();
writer.append("return ").appendMethodBody("java.lang.Class", "getClass", writer.append("return ").appendMethodBody("java.lang.Class", "getClass",

View File

@ -139,6 +139,9 @@ function $rt_arraycls(cls) {
str += "]"; str += "]";
return str; return str;
}; };
$rt_setCloneMethod(arraycls.prototype, function () {
return new arraycls(this.data.slice());
});
var name = "[" + cls.$meta.binaryName; var name = "[" + cls.$meta.binaryName;
arraycls.$meta = { item : cls, supertypes : [$rt_objcls()], primitive : false, superclass : $rt_objcls(), arraycls.$meta = { item : cls, supertypes : [$rt_objcls()], primitive : false, superclass : $rt_objcls(),
name : name, binaryName : name, enum : false }; name : name, binaryName : name, enum : false };

View File

@ -17,6 +17,7 @@ package org.teavm.vm;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -393,4 +394,13 @@ public class VMTest {
class PathJoint implements FirstPath, SecondPath { 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]);
}
} }