Improvements

This commit is contained in:
Alexey Andreev 2013-12-06 17:16:56 +04:00
parent d05b9434d9
commit bb7053cfa4
6 changed files with 150 additions and 65 deletions

View File

@ -373,10 +373,6 @@ public class TString extends TObject implements TSerializable, TComparable<TStri
return substring(lower, upper + 1); return substring(lower, upper + 1);
} }
public static TString valueOf(int index) {
return new TStringBuilder().append(index).toString0();
}
@Override @Override
@Rename("toString") @Rename("toString")
public TString toString0() { public TString toString0() {
@ -391,6 +387,38 @@ public class TString extends TObject implements TSerializable, TComparable<TStri
return array; return array;
} }
public static TString valueOf(TObject obj) {
return obj != null ? obj.toString0() : TString.wrap("null");
}
public static TString valueOf(char[] data) {
return new TString(data);
}
public static TString valueOf(char[] data, int offset, int count) {
return new TString(data, offset, count);
}
public static TString copyValueOf(char[] data) {
return valueOf(data);
}
public static TString copyValueOf(char[] data, int offset, int count) {
return valueOf(data, offset, count);
}
public static TString valueOf(boolean b) {
return b ? TString.wrap("true") : TString.wrap("false");
}
public static TString valueOf(char c) {
return new TString(new char[] { c });
}
public static TString valueOf(int index) {
return new TStringBuilder().append(index).toString0();
}
@Override @Override
public boolean equals(TObject other) { public boolean equals(TObject other) {
if (this == other) { if (this == other) {

View File

@ -0,0 +1,39 @@
package org.teavm.classlib.java.lang;
import static org.junit.Assert.*;
import org.junit.Test;
/**
*
* @author Alexey Andreev
*/
public class VMTests {
@Test
public void multiArrayCreated() {
int[][] array = new int[2][3];
assertEquals(2, array.length);
assertEquals(3, array[0].length);
assertEquals(int[][].class, array.getClass());
assertEquals(int[].class, array[0].getClass());
}
@Test
public void longIntegersMultipied() {
long a = 1199747L;
long b = 1062911L;
assertEquals(1275224283517L, a * b);
assertEquals(-1275224283517L, a * -b);
a = 229767376164L;
b = 907271478890L;
assertEquals(-5267604004427634456L, a * b);
assertEquals(5267604004427634456L, a * -b);
}
@Test
public void longIntegersDivided() {
long a = 12752242835177213L;
long b = 1062912L;
assertEquals(11997458712L, a / b);
assertEquals(-11997458712L, a / -b);
}
}

View File

@ -32,7 +32,8 @@ public class ClasslibTestGenerator {
private static List<MethodReference> testMethods = new ArrayList<>(); private static List<MethodReference> testMethods = new ArrayList<>();
private static Map<String, List<MethodReference>> groupedMethods = new HashMap<>(); private static Map<String, List<MethodReference>> groupedMethods = new HashMap<>();
private static String[] testClasses = { "java.lang.ObjectTests", "java.lang.SystemTests", private static String[] testClasses = { "java.lang.ObjectTests", "java.lang.SystemTests",
"java.lang.StringBuilderTests", "java.lang.ClassTests", "java.lang.StringTests" }; "java.lang.StringBuilderTests", "java.lang.ClassTests", "java.lang.StringTests",
"java.lang.VMTests" };
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
out = System.out; out = System.out;

View File

@ -89,7 +89,7 @@ public class Renderer implements ExprVisitor, StatementVisitor {
MethodReference stringCons = new MethodReference(stringClass, new MethodDescriptor("<init>", MethodReference stringCons = new MethodReference(stringClass, new MethodDescriptor("<init>",
ValueType.arrayOf(ValueType.CHARACTER), ValueType.VOID)); ValueType.arrayOf(ValueType.CHARACTER), ValueType.VOID));
writer.append("$rt_str = function(str) {").indent().newLine(); writer.append("$rt_str = function(str) {").indent().newLine();
writer.append("var characters = $rt_createNumericArray($rt_charcls(), str.length);").newLine(); writer.append("var characters = $rt_createCharArray(str.length);").newLine();
writer.append("var charsBuffer = characters.data;").newLine(); writer.append("var charsBuffer = characters.data;").newLine();
writer.append("for (var i = 0; i < str.length; i = (i + 1) | 0) {").indent().newLine(); writer.append("for (var i = 0; i < str.length; i = (i + 1) | 0) {").indent().newLine();
writer.append("charsBuffer[i] = str.charCodeAt(i) & 0xFFFF;").newLine(); writer.append("charsBuffer[i] = str.charCodeAt(i) & 0xFFFF;").newLine();
@ -840,22 +840,22 @@ public class Renderer implements ExprVisitor, StatementVisitor {
if (type instanceof ValueType.Primitive) { if (type instanceof ValueType.Primitive) {
switch (((ValueType.Primitive)type).getKind()) { switch (((ValueType.Primitive)type).getKind()) {
case BOOLEAN: case BOOLEAN:
writer.append("$rt_createBooleanArray($rt_booleancls(), "); writer.append("$rt_createBooleanArray(");
expr.getLength().acceptVisitor(this); expr.getLength().acceptVisitor(this);
writer.append(")"); writer.append(")");
break; break;
case BYTE: case BYTE:
writer.append("$rt_createNumericArray($rt_bytecls(), "); writer.append("$rt_createByteArray(");
expr.getLength().acceptVisitor(this); expr.getLength().acceptVisitor(this);
writer.append(")"); writer.append(")");
break; break;
case SHORT: case SHORT:
writer.append("$rt_createNumericArray($rt_shortcls(), "); writer.append("$rt_createShortArray(");
expr.getLength().acceptVisitor(this); expr.getLength().acceptVisitor(this);
writer.append(")"); writer.append(")");
break; break;
case INTEGER: case INTEGER:
writer.append("$rt_createNumericArray($rt_intcls(), "); writer.append("$rt_createIntArray(");
expr.getLength().acceptVisitor(this); expr.getLength().acceptVisitor(this);
writer.append(")"); writer.append(")");
break; break;
@ -865,17 +865,17 @@ public class Renderer implements ExprVisitor, StatementVisitor {
writer.append(")"); writer.append(")");
break; break;
case FLOAT: case FLOAT:
writer.append("$rt_createNumericArray($rt_floatcls(), "); writer.append("$rt_createFloatArray(");
expr.getLength().acceptVisitor(this); expr.getLength().acceptVisitor(this);
writer.append(")"); writer.append(")");
break; break;
case DOUBLE: case DOUBLE:
writer.append("$rt_createNumericArray($rt_doublecls(), "); writer.append("$rt_createDoubleArray(");
expr.getLength().acceptVisitor(this); expr.getLength().acceptVisitor(this);
writer.append(")"); writer.append(")");
break; break;
case CHARACTER: case CHARACTER:
writer.append("$rt_createNumericArray($rt_charcls(), "); writer.append("$rt_createCharArray(");
expr.getLength().acceptVisitor(this); expr.getLength().acceptVisitor(this);
writer.append(")"); writer.append(")");
break; break;
@ -889,8 +889,7 @@ public class Renderer implements ExprVisitor, StatementVisitor {
@Override @Override
public void visit(NewMultiArrayExpr expr) { public void visit(NewMultiArrayExpr expr) {
writer.append("$rt_createMultiArray(").append(typeToClsString(naming, expr.getType())) writer.append("$rt_createMultiArray(").append(typeToClsString(naming, expr.getType())).append(", [");
.append(", [");
boolean first = true; boolean first = true;
for (Expr dimension : expr.getDimensions()) { for (Expr dimension : expr.getDimensions()) {
if (!first) { if (!first) {

View File

@ -331,7 +331,6 @@ public class ProgramParser {
Variable[] dimensions = new Variable[dims]; Variable[] dimensions = new Variable[dims];
for (int i = dims - 1; i >= 0; --i) { for (int i = dims - 1; i >= 0; --i) {
dimensions[i] = getVariable(--currentDepth); dimensions[i] = getVariable(--currentDepth);
arrayType = ValueType.arrayOf(arrayType);
} }
int var = currentDepth++; int var = currentDepth++;
ConstructMultiArrayInstruction insn = new ConstructMultiArrayInstruction(); ConstructMultiArrayInstruction insn = new ConstructMultiArrayInstruction();

View File

@ -25,24 +25,62 @@ $rt_createArray = function(cls, sz) {
var arr = new ($rt_arraycls(cls))(data); var arr = new ($rt_arraycls(cls))(data);
arr.$id = $rt_nextId(); arr.$id = $rt_nextId();
for (var i = 0; i < sz; i = (i + 1) | 0) { for (var i = 0; i < sz; i = (i + 1) | 0) {
arr.data[i] = null; data[i] = null;
}
return arr;
}
$rt_createNumericArray = function(cls, sz) {
var arr = $rt_createArray(cls, sz);
for (var i = 0; i < sz; i = (i + 1) | 0) {
arr.data[i] = 0;
} }
return arr; return arr;
} }
$rt_createLongArray = function(sz) { $rt_createLongArray = function(sz) {
var arr = $rt.createArray($rt_longcls(), sz); var data = new Array(sz);
var arr = new ($rt_arraycls($rt_longcls()))(data);
arr.$id = $rt_nextId();
for (var i = 0; i < sz; i = (i + 1) | 0) { for (var i = 0; i < sz; i = (i + 1) | 0) {
arr.data[i] = Long.ZERO; data[i] = Long.ZERO;
} }
return arr; return arr;
}, }
if (ArrayBuffer) {
$rt_createNumericArray = function(cls, nativeArray) {
return new ($rt_arraycls(cls))(nativeArray);
}
$rt_createByteArray = function(sz) {
return $rt_createNumericArray($rt_bytecls(), new Int8Array(new ArrayBuffer(sz)), 0);
};
$rt_createShortArray = function(sz) {
return $rt_createNumericArray($rt_shortcls(), new Int16Array(new ArrayBuffer(sz << 1)), 0);
};
$rt_createIntArray = function(sz) {
return $rt_createNumericArray($rt_intcls(), new Int32Array(new ArrayBuffer(sz << 2)), 0);
};
$rt_createBooleanArray = function(sz) {
return $rt_createNumericArray($rt_booleancls(), new Int8Array(new ArrayBuffer(sz)), 0);
};
$rt_createFloatArray = function(sz) {
return $rt_createNumericArray($rt_floatcls(), new Float32Array(new ArrayBuffer(sz << 2)), 0);
};
$rt_createDoubleArray = function(sz) {
return $rt_createNumericArray($rt_doublecls(), new Float64Array(new ArrayBuffer(sz << 3)), 0);
};
$rt_createCharArray = function(sz) {
return $rt_createNumericArray($rt_charcls(), new Uint16Array(new ArrayBuffer(sz << 1)), 0);
};
} else {
$rt_createNumericArray = function(cls, sz) {
var data = new Array(sz);
var arr = new ($rt_arraycls(cls))(data);
arr.$id = $rt_nextId();
for (var i = 0; i < sz; i = (i + 1) | 0) {
data[i] = 0;
}
return arr;
}
$rt_createByteArray = function(sz) { return $rt_createNumericArray($rt_bytecls(), sz); }
$rt_createShortArray = function(sz) { return $rt_createNumericArray($rt_shortcls(), sz); }
$rt_createIntArray = function(sz) { return $rt_createNumericArray($rt_intcls(), sz); }
$rt_createBooleanArray = function(sz) { return $rt_createNumericArray($rt_booleancls(), sz); }
$rt_createFloatArray = function(sz) { return $rt_createNumericArray($rt_floatcls(), sz); }
$rt_createDoubleArray = function(sz) { return $rt_createNumericArray($rt_doublecls(), sz); }
$rt_createCharArray = function(sz) { return $rt_createNumericArray($rt_charcls(), sz); }
}
$rt_arraycls = function(cls) { $rt_arraycls = function(cls) {
if (cls.$array == undefined) { if (cls.$array == undefined) {
var arraycls = function(data) { var arraycls = function(data) {
@ -180,45 +218,26 @@ $rt_byteToInt = function(value) {
$rt_shortToInt = function(value) { $rt_shortToInt = function(value) {
return value > 0xFFFF ? value | 0xFFFF0000 : value; return value > 0xFFFF ? value | 0xFFFF0000 : value;
} }
$rt_createMultiArray = function(cls, dimensions) {
$rt = { return $rt_createMultiArrayImpl(cls, dimensions, 0);
createBooleanArray : function(cls, sz) { }
var arr = $rt.createArray(cls, sz); $rt_createMultiArrayImpl = function(cls, dimensions, offset) {
for (var i = 0; i < sz; i = (i + 1) | 0) { cls = cls.$meta.item;
arr[i] = false; var result = $rt_createArray(cls, dimensions[offset]);
}
return arr;
},
createMultiArray : function(cls, dimensions) {
for (var i = 1; i < dimensions.length; i = (i + 1) | 0) {
cls = $rt.arraycls(cls);
}
return $rt.createMultiArrayImpl(cls, dimensions, 0);
},
createMultiArrayImpl : function(cls, dimensions, offset) {
var result = $rt.createArray(cls, dimensions[offset]);
offset = (offset + 1) | 0; offset = (offset + 1) | 0;
if (offset < dimensions.length) { if (offset < dimensions.length) {
cls = cls.$meta.item; for (var i = 0; i < result.data.length; i = (i + 1) | 0) {
for (var i = 0; i < result.length; i = (i + 1) | 0) { result.data[i] = $rt_createMultiArrayImpl(cls, dimensions, offset);
result[i] = $rt.createMultiArrayImpl(cls, dimensions, offset);
} }
} }
return result; return result;
}, }
initializeArray : function(cls, initial) { $rt_assertNotNaN = function(value) {
var arr = initial.slice();
arr.$class = $rt.arraycls(cls);
$rt.setId(arr, $rt.lastObjectId++);
return arr;
},
assertNotNaN : function(value) {
if (typeof value == 'number' && isNaN(value)) { if (typeof value == 'number' && isNaN(value)) {
throw "NaN"; throw "NaN";
} }
return value; return value;
} }
};
Long = function(lo, hi) { Long = function(lo, hi) {
this.lo = lo | 0; this.lo = lo | 0;
@ -268,7 +287,7 @@ Long_dec = function(a) {
return new Long(lo, hi); return new Long(lo, hi);
} }
Long_neg = function(a) { Long_neg = function(a) {
return Long.inc(new Long(a.lo ^ 0xFFFFFFFF, a.hi ^ 0xFFFFFFFF)); return Long_inc(new Long(a.lo ^ 0xFFFFFFFF, a.hi ^ 0xFFFFFFFF));
} }
Long_sub = function(a, b) { Long_sub = function(a, b) {
var a_lolo = a.lo & 0xFFFF; var a_lolo = a.lo & 0xFFFF;