Repairs Arrays.get

This commit is contained in:
konsoletyper 2014-03-17 14:36:20 +04:00
parent 91a7a9ba22
commit e27e2b32bc
3 changed files with 35 additions and 33 deletions

View File

@ -23,6 +23,7 @@ import org.teavm.dependency.DependencyPlugin;
import org.teavm.dependency.MethodDependency;
import org.teavm.javascript.ni.Generator;
import org.teavm.javascript.ni.GeneratorContext;
import org.teavm.model.ClassReader;
import org.teavm.model.MethodDescriptor;
import org.teavm.model.MethodReference;
import org.teavm.model.ValueType;
@ -32,12 +33,12 @@ import org.teavm.model.ValueType;
* @author Alexey Andreev
*/
public class ArrayNativeGenerator implements Generator, DependencyPlugin {
private static final MethodReference valueOfIntMethod = new MethodReference("java.lang.Integer",
"valueOf", ValueType.INTEGER, ValueType.object("java.lang.Integer"));
private static final MethodReference valueOfCharMethod = new MethodReference("java.lang.Character",
"valueOf", ValueType.CHARACTER, ValueType.object("java.lang.Character"));
private static final String[] primitives = { "Byte", "Short", "Char", "Int", "Long", "Float", "Double",
"Boolean" };
private static final String[] primitiveWrappers = { "Byte", "Short", "Character", "Integer", "Long",
"Float", "Double", "Boolean" };
private static final ValueType[] primitiveTypes = { ValueType.BYTE, ValueType.SHORT, ValueType.CHARACTER,
ValueType.INTEGER, ValueType.LONG, ValueType.FLOAT, ValueType.DOUBLE, ValueType.BOOLEAN };
@Override
public void methodAchieved(DependencyChecker checker, MethodDependency method) {
@ -111,22 +112,38 @@ public class ArrayNativeGenerator implements Generator, DependencyPlugin {
String array = context.getParameterName(1);
writer.append("var item = " + array + ".data[" + context.getParameterName(2) + "];").softNewLine();
writer.append("var type = " + array + ".constructor.$meta.item;").softNewLine();
writer.append("if (type === $rt_intcls()) {").indent().softNewLine();
writer.append("item = ").appendMethodBody(valueOfIntMethod).append("(item);").softNewLine();
writer.outdent().append("} else if (type === $rt_charcls()) {").indent().softNewLine();
writer.append("item = ").appendMethodBody(valueOfCharMethod).append("(item);").softNewLine();
writer.outdent().append("}").softNewLine();
for (int i = 0; i < primitives.length; ++i) {
String wrapper = "java.lang." + primitiveWrappers[i];
MethodReference methodRef = new MethodReference(wrapper, "valueOf",
primitiveTypes[i], ValueType.object(wrapper));
ClassReader cls = context.getClassSource().get(methodRef.getClassName());
if (cls == null || cls.getMethod(methodRef.getDescriptor()) == null) {
continue;
}
writer.append("if (type === $rt_" + primitives[i].toLowerCase() + "cls()) {").indent().softNewLine();
writer.append("return ").appendMethodBody(methodRef).append("(item);").softNewLine();
writer.outdent().append("} else ");
}
writer.append("{").softNewLine();
writer.append("return item;").softNewLine();
writer.outdent().append("}").softNewLine();
}
private void achieveGet(final DependencyChecker checker, final MethodDependency method) {
method.getVariable(1).getArrayItem().connect(method.getResult());
method.getVariable(1).addConsumer(new DependencyConsumer() {
@Override public void consume(String type) {
if (type.equals("[I")) {
checker.linkMethod(valueOfIntMethod, method.getStack()).use();
} else if (type.equals("[C")) {
checker.linkMethod(valueOfCharMethod, method.getStack()).use();
if (type.startsWith("[")) {
type = type.substring(1);
for (int i = 0; i < primitiveTypes.length; ++i) {
if (primitiveTypes[i].toString().equals(type)) {
String wrapper = "java.lang." + primitiveWrappers[i];
MethodReference methodRef = new MethodReference(wrapper, "valueOf",
primitiveTypes[i], ValueType.object(wrapper));
checker.linkMethod(methodRef, method.getStack()).use();
method.getResult().propagate("java.lang." + primitiveWrappers[i]);
}
}
}
}
});

View File

@ -46,7 +46,7 @@ public final class TArray extends TObject {
private static native TObject newInstanceImpl(TClass<?> componentType, int length);
public static TObject get(TObject array, int index) throws TIllegalArgumentException,
TArrayIndexOutOfBoundsException {
TArrayIndexOutOfBoundsException {
if (index < 0 || index >= getLength(array)) {
throw new TArrayIndexOutOfBoundsException();
}

View File

@ -62,9 +62,7 @@ public class StringTest {
public void stringCharactersRead() {
char[] buffer = new char[4];
"123".getChars(0, 3, buffer, 0);
assertEquals('1', buffer[0]);
assertEquals('2', buffer[1]);
assertEquals('3', buffer[2]);
assertArrayEquals(new char[] { '1', '2', '3', '\0' }, buffer);
}
@Test
@ -202,9 +200,7 @@ public class StringTest {
public void convertedToCharArray() {
char[] array = "123".toCharArray();
assertEquals(3, array.length);
assertEquals('1', array[0]);
assertEquals('2', array[1]);
assertEquals('3', array[2]);
assertArrayEquals(new char[] { '1', '2', '3' }, array);
}
@Test
@ -236,25 +232,14 @@ public class StringTest {
@Test
public void getByteArray() throws UnsupportedEncodingException {
byte[] bytes = "123".getBytes("UTF-8");
assertEquals(49, bytes[0]);
assertEquals(50, bytes[1]);
assertEquals(51, bytes[2]);
assertArrayEquals(new byte[] { 49, 50, 51 }, bytes);
assertEquals(3, bytes.length);
}
@Test
public void getUTF8ByteArray() throws UnsupportedEncodingException {
byte[] bytes = "A\u00BB\u0BBB\uD8BB\uDCBB".getBytes("UTF-8");
assertEquals(65, bytes[0]);
assertEquals(-62, bytes[1]);
assertEquals(-69, bytes[2]);
assertEquals(-32, bytes[3]);
assertEquals(-82, bytes[4]);
assertEquals(-69, bytes[5]);
assertEquals(-16, bytes[6]);
assertEquals(-66, bytes[7]);
assertEquals(-78, bytes[8]);
assertEquals(-69, bytes[9]);
assertArrayEquals(new byte[] { 65, -62, -69, -32, -82, -69, -16, -66, -78, -69 }, bytes);
}
@Test