From bc68602dae4fa8ba49366807a2a59f1a671c3c37 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Fri, 31 Jan 2014 10:49:01 +0400 Subject: [PATCH] Remove .$class property and use built-in .constructor instead --- .../org/teavm/classlib/java/lang/ObjectNativeGenerator.java | 4 ++-- .../classlib/java/lang/reflect/ArrayNativeGenerator.java | 2 +- teavm-core/src/main/java/org/teavm/javascript/Renderer.java | 5 +++-- .../src/main/resources/org/teavm/javascript/runtime.js | 4 ++-- .../src/main/resources/org/teavm/maven/junit-support.js | 4 ++-- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ObjectNativeGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ObjectNativeGenerator.java index 5f813f078..83af1fe03 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ObjectNativeGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/ObjectNativeGenerator.java @@ -73,7 +73,7 @@ public class ObjectNativeGenerator implements Generator, DependencyPlugin { private void generateGetClass(GeneratorContext context, SourceWriter writer) throws IOException { String thisArg = context.getParameterName(0); - writer.append("return $rt_cls(").append(thisArg).append(".$class);").softNewLine(); + writer.append("return $rt_cls(").append(thisArg).append(".constructor);").softNewLine(); } private void achieveGetClass(DependencyChecker checker, MethodReference method) { @@ -89,7 +89,7 @@ public class ObjectNativeGenerator implements Generator, DependencyPlugin { } private void generateClone(GeneratorContext context, SourceWriter writer) throws IOException { - writer.append("var copy = new ").append(context.getParameterName(0)).append(".$class();").softNewLine(); + writer.append("var copy = new ").append(context.getParameterName(0)).append(".constructor();").softNewLine(); writer.append("for (var field in obj) {").softNewLine().indent(); writer.append("if (!obj.hasOwnProperty(field)) {").softNewLine().indent(); writer.append("continue;").softNewLine().outdent().append("}").softNewLine(); diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java index f0d6bb2e4..bade26509 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java @@ -50,7 +50,7 @@ public class ArrayNativeGenerator implements Generator, DependencyPlugin { private void generateGetLength(GeneratorContext context, SourceWriter writer) throws IOException { String array = context.getParameterName(1); - writer.append("if (" + array + " === null || " + array + " .$class.$meta.item === undefined) {") + writer.append("if (" + array + " === null || " + array + ".constructor.$meta.item === undefined) {") .softNewLine().indent(); String clsName = "java.lang.IllegalArgumentException"; MethodReference cons = new MethodReference(clsName, new MethodDescriptor("", ValueType.VOID)); 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 ec06a0f4d..52ea6cf89 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java @@ -122,7 +122,7 @@ public class Renderer implements ExprVisitor, StatementVisitor { public void render(ClassNode cls) throws RenderingException { try { - writer.appendClass(cls.getName()).ws().append("=").ws().append("function()").ws().append("{") + writer.append("function ").appendClass(cls.getName()).append("()").ws().append("{") .indent().softNewLine(); for (FieldNode field : cls.getFields()) { if (field.getModifiers().contains(NodeModifier.STATIC)) { @@ -135,7 +135,6 @@ public class Renderer implements ExprVisitor, StatementVisitor { writer.append("this.").appendField(new FieldReference(cls.getName(), field.getName())).ws() .append("=").ws().append(constantToString(value)).append(";").softNewLine(); } - writer.append("this.$class").ws().append("=").ws().appendClass(cls.getName()).append(";").softNewLine(); writer.outdent().append("}").newLine(); for (FieldNode field : cls.getFields()) { @@ -154,6 +153,8 @@ public class Renderer implements ExprVisitor, StatementVisitor { writer.appendClass(cls.getName()).append(".prototype").ws().append("=").ws().append("new ") .append(cls.getParentName() != null ? naming.getNameFor(cls.getParentName()) : "Object").append("();").softNewLine(); + writer.appendClass(cls.getName()).append(".prototype.constructor").ws().append("=").ws() + .appendClass(cls.getName()).append(';').softNewLine(); writer.appendClass(cls.getName()).append(".$meta").ws().append("=").ws().append("{").ws(); writer.append("name").ws().append(":").ws().append("\"").append(cls.getName()).append("\",").ws(); writer.append("primitive").ws().append(":").ws().append("false,").ws(); 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 6179aad94..84c2191cb 100644 --- a/teavm-core/src/main/resources/org/teavm/javascript/runtime.js +++ b/teavm-core/src/main/resources/org/teavm/javascript/runtime.js @@ -21,7 +21,7 @@ $rt_compare = function(a, b) { return a > b ? 1 : a < b ? -1 : 0; } $rt_isInstance = function(obj, cls) { - return $rt_isAssignable(obj.$class, cls); + return $rt_isAssignable(obj.constructor, cls); } $rt_isAssignable = function(from, to) { if (from === to) { @@ -100,9 +100,9 @@ $rt_arraycls = function(cls) { if (cls.$array == undefined) { var arraycls = function(data) { this.data = data; - this.$class = arraycls; }; arraycls.prototype = new ($rt_objcls())(); + arraycls.prototype.constructor = arraycls; arraycls.$meta = { item : cls, supertypes : [$rt_objcls()], primitive : false }; cls.$array = arraycls; } diff --git a/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit-support.js b/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit-support.js index 54dccc19e..fa40f9804 100644 --- a/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit-support.js +++ b/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit-support.js @@ -208,8 +208,8 @@ JUnitClient.run = function() { message.status = "ok"; } catch (e) { message.status = "exception"; - if (e.$javaException && e.$javaException.$class && e.$javaException.$class.$meta) { - message.exception = e.$javaException.$class.$meta.name; + if (e.$javaException && e.$javaException.constructor.$meta) { + message.exception = e.$javaException.constructor.$meta.name; } message.stack = e.stack; }