From 4c36ab9db13c619e9728f071dfef43b9f84d72a1 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Sat, 1 Mar 2014 18:05:04 +0400 Subject: [PATCH] Reduces amount of generated code --- .../java/org/teavm/javascript/Renderer.java | 67 ++++++++++--------- .../org/teavm/parsing/ClassRefsRenamer.java | 5 ++ .../resources/org/teavm/javascript/runtime.js | 16 +++++ 3 files changed, 58 insertions(+), 30 deletions(-) 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 6d5f73bb5..a9da00e6c 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java @@ -190,49 +190,49 @@ public class Renderer implements ExprVisitor, StatementVisitor { .append(constantToString(value)).append(";").softNewLine(); } - if (!cls.getModifiers().contains(NodeModifier.INTERFACE)) { - 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("$rt_declClass(").appendClass(cls.getName()).append(",").ws().append("{") + .indent().softNewLine(); + writer.append("name").ws().append(":").ws().append("\"").append(escapeString(cls.getName())) + .append("\""); if (cls.getModifiers().contains(NodeModifier.ENUM)) { - writer.append("enum").ws().append(":").ws().append("true,").ws(); + writer.append(",").softNewLine().append("enum").ws().append(":").ws().append("true"); } - writer.append("supertypes").ws().append(":").ws().append("["); - boolean first = true; - if (cls.getParentName() != null) { - writer.appendClass(cls.getParentName()); - first = false; - } - for (String iface : cls.getInterfaces()) { - if (!first) { - writer.append(",").ws(); + if (!cls.getInterfaces().isEmpty()) { + writer.append(",").softNewLine().append("interfaces").ws().append(":").ws().append("["); + for (int i = 0; i < cls.getInterfaces().size(); ++i) { + String iface = cls.getInterfaces().get(i); + if (i > 0) { + writer.append(",").ws(); + } + writer.appendClass(iface); } - first = false; - writer.appendClass(iface); + writer.append("]"); } - writer.append("]"); if (cls.getParentName() != null) { - writer.append(",").ws(); + writer.append(",").softNewLine(); writer.append("superclass").ws().append(":").ws().appendClass(cls.getParentName()); } - writer.ws().append("};").softNewLine(); if (!cls.getModifiers().contains(NodeModifier.INTERFACE)) { - writer.appendClass(cls.getName()).append(".$clinit").ws().append("=").ws() + writer.append(",").softNewLine().append("clinit").ws().append(":").ws() .append("function()").ws().append("{").ws() - .appendClass(cls.getName()).append("_$clinit();").ws().append("}").newLine(); + .appendClass(cls.getName()).append("_$clinit();").ws().append("}"); + } + writer.ws().append("});").softNewLine().outdent(); + List nonInitMethods = new ArrayList<>(); + if (!cls.getModifiers().contains(NodeModifier.INTERFACE)) { writer.append("function ").appendClass(cls.getName()).append("_$clinit()").ws() .append("{").softNewLine().indent(); writer.appendClass(cls.getName()).append("_$clinit").ws().append("=").ws() .append("function(){};").newLine(); List stubNames = new ArrayList<>(); for (MethodNode method : cls.getMethods()) { - renderBody(method); - stubNames.add(naming.getFullNameFor(method.getReference())); + if (!method.getModifiers().contains(NodeModifier.STATIC) && + !method.getReference().getName().equals("")) { + nonInitMethods.add(method); + } else { + renderBody(method, true); + stubNames.add(naming.getFullNameFor(method.getReference())); + } } MethodHolder methodHolder = classSource.get(cls.getName()).getMethod( new MethodDescriptor("", ValueType.VOID)); @@ -259,6 +259,9 @@ public class Renderer implements ExprVisitor, StatementVisitor { writer.append("]);").newLine(); } } + for (MethodNode method : nonInitMethods) { + renderBody(method, false); + } } catch (NamingException e) { throw new RenderingException("Error rendering class " + cls.getName() + ". See a cause for details", e); } catch (IOException e) { @@ -381,9 +384,13 @@ public class Renderer implements ExprVisitor, StatementVisitor { } } - public void renderBody(MethodNode method) throws IOException { + public void renderBody(MethodNode method, boolean inner) throws IOException { MethodReference ref = method.getReference(); - writer.appendMethodBody(ref).ws().append("=").ws().append("function("); + if (inner) { + writer.appendMethodBody(ref).ws().append("=").ws().append("function("); + } else { + writer.append("function ").appendMethodBody(ref).append("("); + } int startParam = 0; if (method.getModifiers().contains(NodeModifier.STATIC)) { startParam = 1; diff --git a/teavm-core/src/main/java/org/teavm/parsing/ClassRefsRenamer.java b/teavm-core/src/main/java/org/teavm/parsing/ClassRefsRenamer.java index 340310c4c..8256f764d 100644 --- a/teavm-core/src/main/java/org/teavm/parsing/ClassRefsRenamer.java +++ b/teavm-core/src/main/java/org/teavm/parsing/ClassRefsRenamer.java @@ -121,6 +121,11 @@ class ClassRefsRenamer implements InstructionVisitor { for (Instruction insn : basicBlock.getInstructions()) { insn.acceptVisitor(this); } + for (TryCatchBlock tryCatch : basicBlock.getTryCatchBlocks()) { + if (tryCatch.getExceptionType() != null) { + tryCatch.setExceptionType(classNameMapper.map(tryCatch.getExceptionType())); + } + } } } 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 d598cda72..6282ff0bc 100644 --- a/teavm-core/src/main/resources/org/teavm/javascript/runtime.js +++ b/teavm-core/src/main/resources/org/teavm/javascript/runtime.js @@ -371,6 +371,22 @@ $rt_putStderr = function(ch) { $rt_stderrBuffer += String.fromCharCode(ch); } } +function $rt_declClass(cls, data) { + cls.name = data.name; + cls.$meta = {}; + cls.$meta.superclass = data.superclass; + cls.$meta.supertypes = data.interfaces ? data.interfaces.slice() : []; + if (data.superclass) { + cls.$meta.supertypes.push(data.superclass); + cls.prototype = new data.superclass(); + } else { + cls.prototype = new Object(); + } + cls.$meta.name = data.name; + cls.$meta.enum = data.enum; + cls.prototype.constructor = cls; + cls.$clinit = data.clinit; +} Long = function(lo, hi) { this.lo = lo | 0;