diff --git a/teavm-classlib/pom.xml b/teavm-classlib/pom.xml index 4a9a8f104..6dac0cd24 100644 --- a/teavm-classlib/pom.xml +++ b/teavm-classlib/pom.xml @@ -29,6 +29,10 @@ TeaVM Java class library TeaVM Java class library emulation + + false + + junit @@ -93,6 +97,7 @@ en, en_US, en_GB, ru, ru_RU + ${teavm.classlib.test.incremental} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java index bf2c6ba35..655a9478e 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java @@ -25,6 +25,7 @@ import org.teavm.javascript.ni.InjectedBy; */ public class TClass extends TObject { TString name; + TString binaryName; boolean primitive; boolean array; boolean isEnum; @@ -42,7 +43,7 @@ public class TClass extends TObject { public native boolean isAssignableFrom(TClass obj); public TString getName() { - return new TString(name); + return name; } public boolean isPrimitive() { diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java index 4904efa9b..a74052040 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObject.java @@ -51,7 +51,7 @@ public class TObject { @Rename("toString") public TString toString0() { - return TString.wrap(getClass().getName() + "@" + identity()); + return TString.wrap(getClass().getName() + "@" + TInteger.toHexString(identity())); } @GeneratedBy(ObjectNativeGenerator.class) diff --git a/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/ClassTest.java b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/ClassTest.java index 2cc4cf501..e03c02e90 100644 --- a/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/ClassTest.java +++ b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/ClassTest.java @@ -26,6 +26,9 @@ public class ClassTest { @Test public void classNameEvaluated() { assertEquals("java.lang.Object", Object.class.getName()); + assertEquals("[Ljava.lang.Object;", Object[].class.getName()); + assertEquals("int", int.class.getName()); + assertEquals("[I", int[].class.getName()); } @Test diff --git a/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/ObjectTest.java b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/ObjectTest.java index ac9530a69..6db988184 100644 --- a/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/ObjectTest.java +++ b/teavm-classlib/src/test/java/org/teavm/classlib/java/lang/ObjectTest.java @@ -60,4 +60,11 @@ public class ObjectTest { public void properInstanceDetected() { assertTrue(Object.class.isInstance(new Object())); } + + @Test + public void toStringWorks() { + assertTrue(new Object().toString().startsWith("java.lang.Object@")); + assertTrue(new Object[2].toString().startsWith("[Ljava.lang.Object;@")); + assertTrue(new byte[3].toString().startsWith("[B@")); + } } diff --git a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java index a7c2f917b..b13f3a929 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java @@ -178,6 +178,12 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext .append("clsProto.$meta.name").ws().append("!==").ws().append("undefined").ws().append("?").ws() .append("$rt_str(clsProto.$meta.name)").ws().append(":").ws().append("null;").softNewLine(); } + if (classSource.get(classClass).getField("name") != null) { + writer.append("cls.").appendField(new FieldReference(classClass, "binaryName")).ws().append("=").ws() + .append("clsProto.$meta.binaryName").ws().append("!==").ws().append("undefined").ws() + .append("?").ws() + .append("$rt_str(clsProto.$meta.binaryName)").ws().append(":").ws().append("null;").softNewLine(); + } if (classSource.get(classClass).getField("primitive") != null) { writer.append("cls.").appendField(new FieldReference(classClass, "primitive")) .append(" = clsProto.$meta.primitive ? 1 : 0;").newLine(); diff --git a/teavm-core/src/main/resources/org/teavm/javascript/runtime.js b/teavm-core/src/main/resources/org/teavm/javascript/runtime.js index 9b5300490..5845769ec 100644 --- a/teavm-core/src/main/resources/org/teavm/javascript/runtime.js +++ b/teavm-core/src/main/resources/org/teavm/javascript/runtime.js @@ -128,7 +128,9 @@ function $rt_arraycls(cls) { str += "]"; return str; } - arraycls.$meta = { item : cls, supertypes : [$rt_objcls()], primitive : false, superclass : $rt_objcls() }; + var name = "[" + cls.$meta.binaryName; + arraycls.$meta = { item : cls, supertypes : [$rt_objcls()], primitive : false, superclass : $rt_objcls(), + name : name, binaryName : name }; cls.$array = arraycls; } return cls.$array; @@ -140,84 +142,73 @@ function $rt_createcls() { } }; } +function $rt_createPrimitiveCls(name, binaryName) { + var cls = $rt_createcls(); + cls.$meta.primitive = true; + cls.$meta.name = name; + cls.$meta.binaryName = binaryName; + return cls; +} var $rt_booleanclsCache = null; function $rt_booleancls() { if ($rt_booleanclsCache === null) { - $rt_booleanclsCache = $rt_createcls(); - $rt_booleanclsCache.primitive = true; - $rt_booleanclsCache.name = "boolean"; + $rt_booleanclsCache = $rt_createPrimitiveCls("boolean", "Z"); } return $rt_booleanclsCache; } var $rt_charclsCache = null; function $rt_charcls() { if ($rt_charclsCache === null) { - $rt_charclsCache = $rt_createcls(); - $rt_charclsCache.primitive = true; - $rt_charclsCache.name = "char"; + $rt_charclsCache = $rt_createPrimitiveCls("char", "C"); } return $rt_charclsCache; } var $rt_byteclsCache = null; function $rt_bytecls() { if ($rt_byteclsCache === null) { - $rt_byteclsCache = $rt_createcls(); - $rt_byteclsCache.primitive = true; - $rt_byteclsCache.name = "byte"; + $rt_byteclsCache = $rt_createPrimitiveCls("byte", "B"); } return $rt_byteclsCache; } var $rt_shortclsCache = null; function $rt_shortcls() { if ($rt_shortclsCache === null) { - $rt_shortclsCache = $rt_createcls(); - $rt_shortclsCache.primitive = true; - $rt_shortclsCache.name = "short"; + $rt_shortclsCache = $rt_createPrimitiveCls("short", "S"); } return $rt_shortclsCache; } var $rt_intclsCache = null; function $rt_intcls() { if ($rt_intclsCache === null) { - $rt_intclsCache = $rt_createcls(); - $rt_intclsCache.primitive = true; - $rt_intclsCache.name = "int"; + $rt_intclsCache = $rt_createPrimitiveCls("int", "I"); } return $rt_intclsCache; } var $rt_longclsCache = null; function $rt_longcls() { if ($rt_longclsCache === null) { - $rt_longclsCache = $rt_createcls(); - $rt_longclsCache.primitive = true; - $rt_longclsCache.name = "long"; + $rt_longclsCache = $rt_createPrimitiveCls("long", "J"); } return $rt_longclsCache; } var $rt_floatclsCache = null; function $rt_floatcls() { if ($rt_floatclsCache === null) { - $rt_floatclsCache = $rt_createcls(); - $rt_floatclsCache.primitive = true; - $rt_floatclsCache.name = "float"; + $rt_floatclsCache = $rt_createPrimitiveCls("float", "F"); } return $rt_floatclsCache; } var $rt_doubleclsCache = null; function $rt_doublecls() { if ($rt_doubleclsCache === null) { - $rt_doubleclsCache = $rt_createcls(); - $rt_doubleclsCache.primitive = true; - $rt_doubleclsCache.name = "double"; + $rt_doubleclsCache = $rt_createPrimitiveCls("double", "D"); } return $rt_doubleclsCache; } var $rt_voidclsCache = null; function $rt_voidcls() { if ($rt_voidclsCache === null) { - $rt_voidclsCache = $rt_createcls(); - $rt_voidclsCache.primitive = true; - $rt_voidclsCache.name = "void"; + $rt_voidclsCache = $rt_createPrimitiveCls("void", "V"); } return $rt_voidclsCache; } @@ -383,6 +374,7 @@ function $rt_declClass(cls, data) { cls.prototype = new Object(); } cls.$meta.name = data.name; + cls.$meta.binaryName = "L" + data.name + ";"; cls.$meta.enum = data.enum; cls.prototype.constructor = cls; cls.$clinit = data.clinit; diff --git a/teavm-eclipse/teavm-eclipse-core-plugin/META-INF/MANIFEST.MF b/teavm-eclipse/teavm-eclipse-core-plugin/META-INF/MANIFEST.MF index 01d49b2b0..ae109742e 100644 --- a/teavm-eclipse/teavm-eclipse-core-plugin/META-INF/MANIFEST.MF +++ b/teavm-eclipse/teavm-eclipse-core-plugin/META-INF/MANIFEST.MF @@ -6,7 +6,7 @@ Bundle-Version: 0.3.0.qualifer Bundle-Vendor: Alexey Andreev Bundle-RequiredExecutionEnvironment: JavaSE-1.7 Bundle-ClassPath: ., - lib/asm-debug-all-4.0.3.jar, + lib/asm-debug-all-5.0.3.jar, lib/cdi-api-1.2.jar, lib/commons-io-2.4.jar, lib/jackson-core-asl-1.9.13.jar,