Reduce amount of generated JSO aliases

This commit is contained in:
Alexey Andreev 2015-12-25 22:39:25 +03:00
parent 03538071c4
commit 33d61412fb
2 changed files with 18 additions and 4 deletions

View File

@ -20,6 +20,8 @@ import java.util.Map;
import org.teavm.codegen.SourceWriter; import org.teavm.codegen.SourceWriter;
import org.teavm.javascript.RenderingContext; import org.teavm.javascript.RenderingContext;
import org.teavm.jso.impl.JSDependencyListener.ExposedClass; import org.teavm.jso.impl.JSDependencyListener.ExposedClass;
import org.teavm.model.ClassReader;
import org.teavm.model.ClassReaderSource;
import org.teavm.model.MethodDescriptor; import org.teavm.model.MethodDescriptor;
import org.teavm.vm.BuildTarget; import org.teavm.vm.BuildTarget;
import org.teavm.vm.spi.RendererListener; import org.teavm.vm.spi.RendererListener;
@ -32,6 +34,7 @@ class JSAliasRenderer implements RendererListener {
private static String variableChars = "abcdefghijklmnopqrstuvwxyz"; private static String variableChars = "abcdefghijklmnopqrstuvwxyz";
private JSDependencyListener dependencyListener; private JSDependencyListener dependencyListener;
private SourceWriter writer; private SourceWriter writer;
private ClassReaderSource classSource;
public JSAliasRenderer(JSDependencyListener dependencyListener) { public JSAliasRenderer(JSDependencyListener dependencyListener) {
this.dependencyListener = dependencyListener; this.dependencyListener = dependencyListener;
@ -40,6 +43,7 @@ class JSAliasRenderer implements RendererListener {
@Override @Override
public void begin(RenderingContext context, BuildTarget buildTarget) throws IOException { public void begin(RenderingContext context, BuildTarget buildTarget) throws IOException {
writer = context.getWriter(); writer = context.getWriter();
classSource = context.getClassSource();
} }
@Override @Override
@ -52,11 +56,20 @@ class JSAliasRenderer implements RendererListener {
writer.append("var c;").softNewLine(); writer.append("var c;").softNewLine();
for (Map.Entry<String, ExposedClass> entry : dependencyListener.getExposedClasses().entrySet()) { for (Map.Entry<String, ExposedClass> entry : dependencyListener.getExposedClasses().entrySet()) {
ExposedClass cls = entry.getValue(); ExposedClass cls = entry.getValue();
if (cls.methods.isEmpty()) { ClassReader classReader = classSource.get(entry.getKey());
if (classReader == null || cls.methods.isEmpty()) {
continue; continue;
} }
writer.append("c").ws().append("=").ws().appendClass(entry.getKey()).append(".prototype;").softNewLine(); boolean first = true;
for (Map.Entry<MethodDescriptor, String> aliasEntry : cls.methods.entrySet()) { for (Map.Entry<MethodDescriptor, String> aliasEntry : cls.methods.entrySet()) {
if (classReader.getMethod(aliasEntry.getKey()) == null) {
continue;
}
if (first) {
writer.append("c").ws().append("=").ws().appendClass(entry.getKey()).append(".prototype;")
.softNewLine();
first = false;
}
if (isKeyword(aliasEntry.getValue())) { if (isKeyword(aliasEntry.getValue())) {
writer.append("c[\"").append(aliasEntry.getValue()).append("\"]"); writer.append("c[\"").append(aliasEntry.getValue()).append("\"]");
} else { } else {

View File

@ -101,7 +101,7 @@ class JSDependencyListener extends AbstractDependencyListener {
private ExposedClass createExposedClass(String name) { private ExposedClass createExposedClass(String name) {
ClassReader cls = classSource.get(name); ClassReader cls = classSource.get(name);
ExposedClass exposedCls = new ExposedClass(); ExposedClass exposedCls = new ExposedClass();
if (cls == null) { if (cls == null || cls.hasModifier(ElementModifier.INTERFACE)) {
return exposedCls; return exposedCls;
} }
if (cls.getParent() != null && !cls.getParent().equals(cls.getName())) { if (cls.getParent() != null && !cls.getParent().equals(cls.getName())) {
@ -146,7 +146,8 @@ class JSDependencyListener extends AbstractDependencyListener {
if (addInterface(exposedCls, iface)) { if (addInterface(exposedCls, iface)) {
added = true; added = true;
for (MethodReader method : iface.getMethods()) { for (MethodReader method : iface.getMethods()) {
if (method.hasModifier(ElementModifier.STATIC)) { if (method.hasModifier(ElementModifier.STATIC)
|| (method.getProgram() != null && method.getProgram().basicBlockCount() > 0)) {
continue; continue;
} }
if (!exposedCls.inheritedMethods.containsKey(method.getDescriptor())) { if (!exposedCls.inheritedMethods.containsKey(method.getDescriptor())) {