Fixes bugs in minifier

This commit is contained in:
konsoletyper 2013-12-10 21:28:11 +04:00
parent 62df1018cc
commit 18fd80be56
11 changed files with 72 additions and 52 deletions

View File

@ -57,9 +57,6 @@ public class DefaultNamingStrategy implements NamingStrategy {
@Override @Override
public String getNameFor(MethodReference method) { public String getNameFor(MethodReference method) {
if (method.getDescriptor().getName().equals("<clinit>")) {
return "$clinit";
}
method = getRealMethod(method); method = getRealMethod(method);
if (method == null) { if (method == null) {
throw new NamingException("Can't provide name for method as it was not found: " + method); throw new NamingException("Can't provide name for method as it was not found: " + method);
@ -89,7 +86,20 @@ public class DefaultNamingStrategy implements NamingStrategy {
@Override @Override
public String getFullNameFor(MethodReference method) throws NamingException { public String getFullNameFor(MethodReference method) throws NamingException {
return minifying ? getNameFor(method) : getNameFor(method.getClassName()) + "_" + getNameFor(method); if (!minifying) {
return getNameFor(method.getClassName()) + "_" + getNameFor(method);
}
method = getRealMethod(method);
if (method == null) {
throw new NamingException("Can't provide name for method as it was not found: " + method);
}
String key = method.toString();
String alias = privateAliases.get(key);
if (alias == null) {
alias = aliasProvider.getAlias(method);
privateAliases.put(key, alias);
}
return alias;
} }
@Override @Override

View File

@ -1,4 +1,4 @@
package org.teavm.codegen; package org.teavm.common;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;

View File

@ -1,4 +1,4 @@
package org.teavm.codegen; package org.teavm.common;
/** /**
* *

View File

@ -19,9 +19,9 @@ import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import org.teavm.codegen.ConcurrentCachedMapper; import org.teavm.common.ConcurrentCachedMapper;
import org.teavm.codegen.ConcurrentCachedMapper.KeyListener; import org.teavm.common.Mapper;
import org.teavm.codegen.Mapper; import org.teavm.common.ConcurrentCachedMapper.KeyListener;
import org.teavm.model.*; import org.teavm.model.*;
/** /**

View File

@ -15,6 +15,8 @@
*/ */
package org.teavm.javascript; package org.teavm.javascript;
import java.util.ArrayList;
import java.util.List;
import org.teavm.codegen.NamingException; import org.teavm.codegen.NamingException;
import org.teavm.codegen.NamingStrategy; import org.teavm.codegen.NamingStrategy;
import org.teavm.codegen.SourceWriter; import org.teavm.codegen.SourceWriter;
@ -31,6 +33,7 @@ public class Renderer implements ExprVisitor, StatementVisitor {
private NamingStrategy naming; private NamingStrategy naming;
private SourceWriter writer; private SourceWriter writer;
private ClassHolderSource classSource; private ClassHolderSource classSource;
private boolean minifying;
public Renderer(SourceWriter writer, ClassHolderSource classSource) { public Renderer(SourceWriter writer, ClassHolderSource classSource) {
this.naming = writer.getNaming(); this.naming = writer.getNaming();
@ -46,6 +49,14 @@ public class Renderer implements ExprVisitor, StatementVisitor {
return naming; return naming;
} }
public boolean isMinifying() {
return minifying;
}
public void setMinifying(boolean minifying) {
this.minifying = minifying;
}
public void renderRuntime() throws RenderingException { public void renderRuntime() throws RenderingException {
try { try {
renderRuntimeCls(); renderRuntimeCls();
@ -61,7 +72,7 @@ public class Renderer implements ExprVisitor, StatementVisitor {
.indent().softNewLine(); .indent().softNewLine();
String classClass = "java.lang.Class"; String classClass = "java.lang.Class";
writer.append("var cls").ws().append("=").ws().append("clsProto.classObject;").softNewLine(); writer.append("var cls").ws().append("=").ws().append("clsProto.classObject;").softNewLine();
writer.append("if").softNewLine().append("(cls").ws().append("===").ws().append("undefined)").ws() writer.append("if").ws().append("(cls").ws().append("===").ws().append("undefined)").ws()
.append("{").softNewLine().indent(); .append("{").softNewLine().indent();
MethodReference createMethodRef = new MethodReference(classClass, new MethodDescriptor("createNew", MethodReference createMethodRef = new MethodReference(classClass, new MethodDescriptor("createNew",
ValueType.object(classClass))); ValueType.object(classClass)));
@ -137,10 +148,10 @@ public class Renderer implements ExprVisitor, StatementVisitor {
.append(constantToString(value)).append(";").softNewLine(); .append(constantToString(value)).append(";").softNewLine();
} }
writer.appendClass(cls.getName()).append(".prototype").ws().append("=").append("new ") writer.appendClass(cls.getName()).append(".prototype").ws().append("=").ws().append("new ")
.append(cls.getParentName() != null ? naming.getNameFor(cls.getParentName()) : .append(cls.getParentName() != null ? naming.getNameFor(cls.getParentName()) :
"Object").append("();").softNewLine(); "Object").append("();").softNewLine();
writer.appendClass(cls.getName()).append(".$meta").ws().append("=").append("{").ws(); 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("name").ws().append(":").ws().append("\"").append(cls.getName()).append("\",").ws();
writer.append("primitive").ws().append(":").ws().append("false,").ws(); writer.append("primitive").ws().append(":").ws().append("false,").ws();
writer.append("supertypes").ws().append(":").ws().append("["); writer.append("supertypes").ws().append(":").ws().append("[");
@ -151,18 +162,20 @@ public class Renderer implements ExprVisitor, StatementVisitor {
} }
for (String iface : cls.getInterfaces()) { for (String iface : cls.getInterfaces()) {
if (!first) { if (!first) {
writer.append(", "); writer.append(",").ws();
} }
first = false; first = false;
writer.appendClass(iface); writer.appendClass(iface);
} }
writer.append("]"); writer.append("]");
writer.ws().append("};").softNewLine(); writer.ws().append("};").softNewLine();
writer.appendClass(cls.getName()).append(".clinit").ws().append("=").ws().append("function()").ws() writer.appendClass(cls.getName()).append("_$clinit").ws().append("=").ws().append("function()").ws()
.append("{").softNewLine().indent(); .append("{").softNewLine().indent();
writer.appendClass(cls.getName()).append(".clinit").ws().append("=").ws().append("null;").softNewLine(); writer.appendClass(cls.getName()).append("_$clinit").ws().append("=").ws().append("null;").newLine();
List<String> stubNames = new ArrayList<>();
for (MethodNode method : cls.getMethods()) { for (MethodNode method : cls.getMethods()) {
renderBody(method); renderBody(method);
stubNames.add(naming.getFullNameFor(method.getReference()));
} }
MethodHolder methodHolder = classSource.getClassHolder(cls.getName()).getMethod( MethodHolder methodHolder = classSource.getClassHolder(cls.getName()).getMethod(
new MethodDescriptor("<clinit>", ValueType.VOID)); new MethodDescriptor("<clinit>", ValueType.VOID));
@ -172,8 +185,20 @@ public class Renderer implements ExprVisitor, StatementVisitor {
} }
writer.outdent().append("}").newLine(); writer.outdent().append("}").newLine();
for (MethodNode method : cls.getMethods()) { for (MethodNode method : cls.getMethods()) {
if (!method.getModifiers().contains(NodeModifier.STATIC)) {
renderDeclaration(method); renderDeclaration(method);
renderStub(method); }
}
if (stubNames.size() > 0) {
writer.append("$rt_methodStubs(").appendClass(cls.getName()).append("_$clinit")
.append(",").ws().append("[");
for (int i = 0; i < stubNames.size(); ++i) {
if (i > 0) {
writer.append(",").ws();
}
writer.append("'").append(stubNames.get(i)).append("'");
}
writer.append("]);").newLine();
} }
} catch (NamingException e) { } catch (NamingException e) {
throw new RenderingException("Error rendering class " + cls.getName() + ". See a cause for details", e); throw new RenderingException("Error rendering class " + cls.getName() + ". See a cause for details", e);
@ -269,31 +294,6 @@ public class Renderer implements ExprVisitor, StatementVisitor {
} }
} }
public void renderStub(MethodNode method) {
MethodReference ref = method.getReference();
writer.appendMethodBody(ref).ws().append("=").ws().append("function(");
int startParam = 0;
if (method.getModifiers().contains(NodeModifier.STATIC)) {
startParam = 1;
}
for (int i = startParam; i <= ref.parameterCount(); ++i) {
if (i > startParam) {
writer.append(",").ws();
}
writer.append(variableName(i));
}
String owner = ref.getClassName();
writer.append(")").ws().append("{").ws().appendClass(owner).append(".clinit();")
.ws().append("return ").appendMethodBody(ref).append("(");
for (int i = startParam; i <= ref.parameterCount(); ++i) {
if (i > startParam) {
writer.append(", ");
}
writer.append(variableName(i));
}
writer.append(");").ws().append("};").newLine();
}
public void renderBody(MethodNode method) { public void renderBody(MethodNode method) {
MethodReference ref = method.getReference(); MethodReference ref = method.getReference();
writer.appendMethodBody(ref).ws().append("=").ws().append("function("); writer.appendMethodBody(ref).ws().append("=").ws().append("function(");
@ -303,7 +303,7 @@ public class Renderer implements ExprVisitor, StatementVisitor {
} }
for (int i = startParam; i <= ref.parameterCount(); ++i) { for (int i = startParam; i <= ref.parameterCount(); ++i) {
if (i > startParam) { if (i > startParam) {
writer.append(", "); writer.append(",").ws();
} }
writer.append(variableName(i)); writer.append(variableName(i));
} }
@ -328,7 +328,7 @@ public class Renderer implements ExprVisitor, StatementVisitor {
boolean first = true; boolean first = true;
for (int i = ref.parameterCount() + 1; i < variableCount; ++i) { for (int i = ref.parameterCount() + 1; i < variableCount; ++i) {
if (!first) { if (!first) {
writer.append(", "); writer.append(",").ws();
} }
first = false; first = false;
writer.append(variableName(i)); writer.append(variableName(i));
@ -478,7 +478,7 @@ public class Renderer implements ExprVisitor, StatementVisitor {
public String variableName(int index) { public String variableName(int index) {
if (index == 0) { if (index == 0) {
return "$this"; return minifying ? "$t" : "$this";
} }
--index; --index;
if (index < variableNames.length()) { if (index < variableNames.length()) {

View File

@ -1,7 +1,7 @@
package org.teavm.model.resource; package org.teavm.model.resource;
import java.util.Map; import java.util.Map;
import org.teavm.codegen.Mapper; import org.teavm.common.Mapper;
import org.teavm.javascript.ni.Rename; import org.teavm.javascript.ni.Rename;
import org.teavm.javascript.ni.Superclass; import org.teavm.javascript.ni.Superclass;
import org.teavm.model.*; import org.teavm.model.*;

View File

@ -4,8 +4,8 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.*; import java.util.*;
import org.teavm.codegen.ConcurrentCachedMapper; import org.teavm.common.ConcurrentCachedMapper;
import org.teavm.codegen.Mapper; import org.teavm.common.Mapper;
import org.teavm.model.ClassHolder; import org.teavm.model.ClassHolder;
/** /**

View File

@ -1,7 +1,7 @@
package org.teavm.model.resource; package org.teavm.model.resource;
import org.teavm.codegen.ConcurrentCachedMapper; import org.teavm.common.ConcurrentCachedMapper;
import org.teavm.codegen.Mapper; import org.teavm.common.Mapper;
import org.teavm.model.ClassHolder; import org.teavm.model.ClassHolder;
import org.teavm.model.ClassHolderSource; import org.teavm.model.ClassHolderSource;

View File

@ -4,7 +4,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.ClassNode;
import org.teavm.codegen.Mapper; import org.teavm.common.Mapper;
import org.teavm.model.ClassHolder; import org.teavm.model.ClassHolder;
import org.teavm.parsing.Parser; import org.teavm.parsing.Parser;

View File

@ -4,7 +4,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.ClassNode;
import org.teavm.codegen.Mapper; import org.teavm.common.Mapper;
import org.teavm.model.ClassHolder; import org.teavm.model.ClassHolder;
import org.teavm.parsing.Parser; import org.teavm.parsing.Parser;

View File

@ -238,6 +238,16 @@ $rt_assertNotNaN = function(value) {
} }
return value; return value;
} }
$rt_methodStubs = function(clinit, names) {
for (var i = 0; i < names.length; i = (i + 1) | 0) {
window[names[i]] = (function(name) {
return function() {
clinit();
return window[name].apply(window, arguments);
}
})(names[i]);
}
}
Long = function(lo, hi) { Long = function(lo, hi) {
this.lo = lo | 0; this.lo = lo | 0;