mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 16:14:10 -08:00
Add frequency-based naming in minified mode
This commit is contained in:
parent
97889bc217
commit
62118e2cfe
|
@ -73,7 +73,7 @@ public class ArrayNativeGenerator implements Generator, DependencyPlugin {
|
||||||
writer.append("if (" + array + " === null || " + array + ".constructor.$meta.item === undefined) {")
|
writer.append("if (" + array + " === null || " + array + ".constructor.$meta.item === undefined) {")
|
||||||
.softNewLine().indent();
|
.softNewLine().indent();
|
||||||
String clsName = "java.lang.IllegalArgumentException";
|
String clsName = "java.lang.IllegalArgumentException";
|
||||||
MethodReference cons = new MethodReference(clsName, new MethodDescriptor("<init>", ValueType.VOID));
|
MethodDescriptor cons = new MethodDescriptor("<init>", ValueType.VOID);
|
||||||
writer.append("$rt_throw(").appendClass(clsName).append(".").appendMethod(cons).append("());").softNewLine();
|
writer.append("$rt_throw(").appendClass(clsName).append(".").appendMethod(cons).append("());").softNewLine();
|
||||||
writer.outdent().append("}").softNewLine();
|
writer.outdent().append("}").softNewLine();
|
||||||
writer.append("return " + array + ".data.length;").softNewLine();
|
writer.append("return " + array + ".data.length;").softNewLine();
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
package org.teavm.codegen;
|
package org.teavm.codegen;
|
||||||
|
|
||||||
import org.teavm.model.FieldReference;
|
import org.teavm.model.FieldReference;
|
||||||
|
import org.teavm.model.MethodDescriptor;
|
||||||
import org.teavm.model.MethodReference;
|
import org.teavm.model.MethodReference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,5 +28,7 @@ public interface AliasProvider {
|
||||||
|
|
||||||
String getAlias(MethodReference method);
|
String getAlias(MethodReference method);
|
||||||
|
|
||||||
|
String getAlias(MethodDescriptor method);
|
||||||
|
|
||||||
String getAlias(String className);
|
String getAlias(String className);
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
package org.teavm.codegen;
|
package org.teavm.codegen;
|
||||||
|
|
||||||
import org.teavm.model.FieldReference;
|
import org.teavm.model.FieldReference;
|
||||||
|
import org.teavm.model.MethodDescriptor;
|
||||||
import org.teavm.model.MethodReference;
|
import org.teavm.model.MethodReference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -24,6 +25,7 @@ import org.teavm.model.MethodReference;
|
||||||
*/
|
*/
|
||||||
public class DefaultAliasProvider implements AliasProvider {
|
public class DefaultAliasProvider implements AliasProvider {
|
||||||
private int lastSuffix;
|
private int lastSuffix;
|
||||||
|
private int lastVirtualSuffix;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAlias(String cls) {
|
public String getAlias(String cls) {
|
||||||
|
@ -48,6 +50,17 @@ public class DefaultAliasProvider implements AliasProvider {
|
||||||
return alias.toString();
|
return alias.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAlias(MethodDescriptor method) {
|
||||||
|
String alias = method.getName();
|
||||||
|
if (alias.equals("<init>")) {
|
||||||
|
alias = "$init";
|
||||||
|
} else if (alias.equals("<clinit>")) {
|
||||||
|
alias = "$clinit";
|
||||||
|
}
|
||||||
|
return alias + lastVirtualSuffix++;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAlias(MethodReference method) {
|
public String getAlias(MethodReference method) {
|
||||||
String alias = method.getDescriptor().getName();
|
String alias = method.getDescriptor().getName();
|
||||||
|
|
|
@ -56,17 +56,17 @@ public class DefaultNamingStrategy implements NamingStrategy {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getNameFor(MethodReference method) {
|
public String getNameFor(MethodDescriptor method) {
|
||||||
return getNameFor(method, 'S');
|
return getNameFor(method, 'S');
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getNameForAsync(MethodReference method) throws NamingException {
|
public String getNameForAsync(MethodDescriptor method) throws NamingException {
|
||||||
return getNameFor(method, 'A');
|
return getNameFor(method, 'A');
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getNameFor(MethodReference method, char classifier) {
|
private String getNameFor(MethodDescriptor method, char classifier) {
|
||||||
String key = classifier + method.getDescriptor().toString();
|
String key = classifier + method.toString();
|
||||||
String alias = aliases.get(key);
|
String alias = aliases.get(key);
|
||||||
if (alias == null) {
|
if (alias == null) {
|
||||||
alias = aliasProvider.getAlias(method);
|
alias = aliasProvider.getAlias(method);
|
||||||
|
@ -97,7 +97,7 @@ public class DefaultNamingStrategy implements NamingStrategy {
|
||||||
throw new NamingException("Can't provide name for method as it was not found: " + originalMethod);
|
throw new NamingException("Can't provide name for method as it was not found: " + originalMethod);
|
||||||
}
|
}
|
||||||
if (!minifying) {
|
if (!minifying) {
|
||||||
return getNameFor(method.getClassName()) + "_" + getNameFor(method, classifier);
|
return getNameFor(method.getClassName()) + "_" + getNameFor(method.getDescriptor(), classifier);
|
||||||
}
|
}
|
||||||
String key = classifier + method.toString();
|
String key = classifier + method.toString();
|
||||||
String alias = privateAliases.get(key);
|
String alias = privateAliases.get(key);
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
package org.teavm.codegen;
|
package org.teavm.codegen;
|
||||||
|
|
||||||
import org.teavm.model.FieldReference;
|
import org.teavm.model.FieldReference;
|
||||||
|
import org.teavm.model.MethodDescriptor;
|
||||||
import org.teavm.model.MethodReference;
|
import org.teavm.model.MethodReference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,25 +26,31 @@ import org.teavm.model.MethodReference;
|
||||||
public class MinifyingAliasProvider implements AliasProvider {
|
public class MinifyingAliasProvider implements AliasProvider {
|
||||||
private static String startLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
private static String startLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
private static String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
private static String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||||
|
private static String startVirtualLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
private int lastSuffix;
|
private int lastSuffix;
|
||||||
|
private int lastVirtual;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAlias(FieldReference field) {
|
public String getAlias(FieldReference field) {
|
||||||
return getNewAlias();
|
return getNewAlias(lastVirtual++, startVirtualLetters);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAlias(MethodReference method) {
|
public String getAlias(MethodReference method) {
|
||||||
return getNewAlias();
|
return getNewAlias(lastSuffix++, startLetters);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAlias(MethodDescriptor method) {
|
||||||
|
return getNewAlias(lastVirtual++, startVirtualLetters);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAlias(String className) {
|
public String getAlias(String className) {
|
||||||
return getNewAlias();
|
return getNewAlias(lastSuffix++, startLetters);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getNewAlias() {
|
private String getNewAlias(int index, String startLetters) {
|
||||||
int index = lastSuffix++;
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append(startLetters.charAt(index % startLetters.length()));
|
sb.append(startLetters.charAt(index % startLetters.length()));
|
||||||
index /= startLetters.length();
|
index /= startLetters.length();
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2015 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.teavm.codegen;
|
||||||
|
|
||||||
|
import org.teavm.model.FieldReference;
|
||||||
|
import org.teavm.model.MethodDescriptor;
|
||||||
|
import org.teavm.model.MethodReference;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Alexey Andreev
|
||||||
|
*/
|
||||||
|
public interface NameFrequencyConsumer {
|
||||||
|
void consume(MethodReference method);
|
||||||
|
|
||||||
|
void consumeAsync(MethodReference method);
|
||||||
|
|
||||||
|
void consumeInit(MethodReference method);
|
||||||
|
|
||||||
|
void consume(MethodDescriptor method);
|
||||||
|
|
||||||
|
void consumeAsync(MethodDescriptor method);
|
||||||
|
|
||||||
|
void consume(String className);
|
||||||
|
|
||||||
|
void consume(FieldReference field);
|
||||||
|
}
|
162
teavm-core/src/main/java/org/teavm/codegen/NamingOrderer.java
Normal file
162
teavm-core/src/main/java/org/teavm/codegen/NamingOrderer.java
Normal file
|
@ -0,0 +1,162 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2015 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.teavm.codegen;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import org.teavm.model.FieldReference;
|
||||||
|
import org.teavm.model.MethodDescriptor;
|
||||||
|
import org.teavm.model.MethodReference;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Alexey Andreev
|
||||||
|
*/
|
||||||
|
public class NamingOrderer implements NameFrequencyConsumer {
|
||||||
|
private Map<String, Entry> entries = new HashMap<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void consume(final MethodReference method) {
|
||||||
|
String key = "R:" + method;
|
||||||
|
Entry entry = entries.get(key);
|
||||||
|
if (entry == null) {
|
||||||
|
entry = new Entry();
|
||||||
|
entry.operation = new NamingOperation() {
|
||||||
|
@Override public void perform(NamingStrategy naming) {
|
||||||
|
naming.getFullNameFor(method);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
entries.put(key, entry);
|
||||||
|
}
|
||||||
|
entry.frequency++;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void consumeAsync(final MethodReference method) {
|
||||||
|
String key = "A:" + method;
|
||||||
|
Entry entry = entries.get(key);
|
||||||
|
if (entry == null) {
|
||||||
|
entry = new Entry();
|
||||||
|
entry.operation = new NamingOperation() {
|
||||||
|
@Override public void perform(NamingStrategy naming) {
|
||||||
|
naming.getFullNameForAsync(method);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
entries.put(key, entry);
|
||||||
|
}
|
||||||
|
entry.frequency++;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void consumeInit(final MethodReference method) {
|
||||||
|
String key = "I:" + method;
|
||||||
|
Entry entry = entries.get(key);
|
||||||
|
if (entry == null) {
|
||||||
|
entry = new Entry();
|
||||||
|
entry.operation = new NamingOperation() {
|
||||||
|
@Override public void perform(NamingStrategy naming) {
|
||||||
|
naming.getNameForInit(method);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
entries.put(key, entry);
|
||||||
|
}
|
||||||
|
entry.frequency++;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void consume(final MethodDescriptor method) {
|
||||||
|
String key = "r:" + method;
|
||||||
|
Entry entry = entries.get(key);
|
||||||
|
if (entry == null) {
|
||||||
|
entry = new Entry();
|
||||||
|
entry.operation = new NamingOperation() {
|
||||||
|
@Override public void perform(NamingStrategy naming) {
|
||||||
|
naming.getNameFor(method);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
entries.put(key, entry);
|
||||||
|
}
|
||||||
|
entry.frequency++;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void consumeAsync(final MethodDescriptor method) {
|
||||||
|
String key = "a:" + method;
|
||||||
|
Entry entry = entries.get(key);
|
||||||
|
if (entry == null) {
|
||||||
|
entry = new Entry();
|
||||||
|
entry.operation = new NamingOperation() {
|
||||||
|
@Override public void perform(NamingStrategy naming) {
|
||||||
|
naming.getNameForAsync(method);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
entries.put(key, entry);
|
||||||
|
}
|
||||||
|
entry.frequency++;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void consume(final String className) {
|
||||||
|
String key = "c:" + className;
|
||||||
|
Entry entry = entries.get(key);
|
||||||
|
if (entry == null) {
|
||||||
|
entry = new Entry();
|
||||||
|
entry.operation = new NamingOperation() {
|
||||||
|
@Override public void perform(NamingStrategy naming) {
|
||||||
|
naming.getNameFor(className);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
entries.put(key, entry);
|
||||||
|
}
|
||||||
|
entry.frequency++;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void consume(final FieldReference field) {
|
||||||
|
String key = "f:" + field;
|
||||||
|
Entry entry = entries.get(key);
|
||||||
|
if (entry == null) {
|
||||||
|
entry = new Entry();
|
||||||
|
entry.operation = new NamingOperation() {
|
||||||
|
@Override public void perform(NamingStrategy naming) {
|
||||||
|
naming.getNameFor(field);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
entries.put(key, entry);
|
||||||
|
}
|
||||||
|
entry.frequency++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void apply(NamingStrategy naming) {
|
||||||
|
List<Entry> entryList = new ArrayList<>(entries.values());
|
||||||
|
Collections.sort(entryList, new Comparator<Entry>() {
|
||||||
|
@Override public int compare(Entry o1, Entry o2) {
|
||||||
|
return Integer.compare(o2.frequency, o1.frequency);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
for (Entry entry : entryList) {
|
||||||
|
entry.operation.perform(naming);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class Entry {
|
||||||
|
NamingOperation operation;
|
||||||
|
int frequency;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface NamingOperation {
|
||||||
|
void perform(NamingStrategy naming);
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,6 +16,7 @@
|
||||||
package org.teavm.codegen;
|
package org.teavm.codegen;
|
||||||
|
|
||||||
import org.teavm.model.FieldReference;
|
import org.teavm.model.FieldReference;
|
||||||
|
import org.teavm.model.MethodDescriptor;
|
||||||
import org.teavm.model.MethodReference;
|
import org.teavm.model.MethodReference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,9 +26,9 @@ import org.teavm.model.MethodReference;
|
||||||
public interface NamingStrategy {
|
public interface NamingStrategy {
|
||||||
String getNameFor(String cls) throws NamingException;
|
String getNameFor(String cls) throws NamingException;
|
||||||
|
|
||||||
String getNameFor(MethodReference method) throws NamingException;
|
String getNameFor(MethodDescriptor method) throws NamingException;
|
||||||
|
|
||||||
String getNameForAsync(MethodReference method) throws NamingException;
|
String getNameForAsync(MethodDescriptor method) throws NamingException;
|
||||||
|
|
||||||
String getNameForInit(MethodReference method) throws NamingException;
|
String getNameForInit(MethodReference method) throws NamingException;
|
||||||
|
|
||||||
|
|
|
@ -111,18 +111,16 @@ public class SourceWriter implements Appendable, LocationProvider {
|
||||||
return append(naming.getNameFor(field));
|
return append(naming.getNameFor(field));
|
||||||
}
|
}
|
||||||
|
|
||||||
public SourceWriter appendMethod(MethodReference method) throws NamingException, IOException {
|
public SourceWriter appendMethod(MethodDescriptor method) throws NamingException, IOException {
|
||||||
return append(naming.getNameFor(method));
|
return append(naming.getNameFor(method));
|
||||||
}
|
}
|
||||||
|
|
||||||
public SourceWriter appendMethod(String className, String name, ValueType... params)
|
public SourceWriter appendMethod(String name, ValueType... params) throws NamingException, IOException {
|
||||||
throws NamingException, IOException {
|
return append(naming.getNameFor(new MethodDescriptor(name, params)));
|
||||||
return append(naming.getNameFor(new MethodReference(className, name, params)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public SourceWriter appendMethod(Class<?> cls, String name, Class<?>... params)
|
public SourceWriter appendMethod(String name, Class<?>... params) throws NamingException, IOException {
|
||||||
throws NamingException, IOException {
|
return append(naming.getNameFor(new MethodDescriptor(name, params)));
|
||||||
return append(naming.getNameFor(new MethodReference(cls, name, params)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public SourceWriter appendMethodBody(MethodReference method) throws NamingException, IOException {
|
public SourceWriter appendMethodBody(MethodReference method) throws NamingException, IOException {
|
||||||
|
|
|
@ -0,0 +1,347 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2015 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.teavm.javascript;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import org.teavm.codegen.NameFrequencyConsumer;
|
||||||
|
import org.teavm.javascript.ast.*;
|
||||||
|
import org.teavm.model.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Alexey Andreev
|
||||||
|
*/
|
||||||
|
public class NameFrequencyEstimator implements StatementVisitor, ExprVisitor, MethodNodeVisitor {
|
||||||
|
private NameFrequencyConsumer consumer;
|
||||||
|
private ClassReaderSource classSource;
|
||||||
|
private boolean async;
|
||||||
|
private Set<MethodReference> injectedMethods;
|
||||||
|
private Set<MethodReference> asyncFamilyMethods;
|
||||||
|
|
||||||
|
public NameFrequencyEstimator(NameFrequencyConsumer consumer, ClassReaderSource classSource,
|
||||||
|
Set<MethodReference> injectedMethods, Set<MethodReference> asyncFamilyMethods) {
|
||||||
|
this.consumer = consumer;
|
||||||
|
this.classSource = classSource;
|
||||||
|
this.injectedMethods = injectedMethods;
|
||||||
|
this.asyncFamilyMethods = asyncFamilyMethods;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void visit(List<Statement> statements) {
|
||||||
|
for (Statement part : statements) {
|
||||||
|
part.acceptVisitor(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void estimate(ClassNode cls) {
|
||||||
|
// Declaration
|
||||||
|
consumer.consume(cls.getName());
|
||||||
|
if (cls.getParentName() != null) {
|
||||||
|
consumer.consume(cls.getParentName());
|
||||||
|
}
|
||||||
|
for (FieldNode field : cls.getFields()) {
|
||||||
|
consumer.consume(new FieldReference(cls.getName(), field.getName()));
|
||||||
|
if (field.getModifiers().contains(NodeModifier.STATIC)) {
|
||||||
|
consumer.consume(cls.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Methods
|
||||||
|
MethodReader clinit = classSource.get(cls.getName()).getMethod(
|
||||||
|
new MethodDescriptor("<clinit>", ValueType.VOID));
|
||||||
|
for (MethodNode method : cls.getMethods()) {
|
||||||
|
if (method.isAsync()) {
|
||||||
|
consumer.consumeAsync(method.getReference());
|
||||||
|
} else {
|
||||||
|
consumer.consume(method.getReference());
|
||||||
|
if (asyncFamilyMethods.contains(method.getReference())) {
|
||||||
|
consumer.consume(method.getReference());
|
||||||
|
consumer.consumeAsync(method.getReference());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (clinit != null && (method.getModifiers().contains(NodeModifier.STATIC) ||
|
||||||
|
method.getReference().getName().equals("<init>"))) {
|
||||||
|
if (!method.isAsync()) {
|
||||||
|
consumer.consume(method.getReference());
|
||||||
|
}
|
||||||
|
if (asyncFamilyMethods.contains(method.getReference())) {
|
||||||
|
consumer.consumeAsync(method.getReference());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!method.getModifiers().contains(NodeModifier.STATIC)) {
|
||||||
|
if (method.isAsync()) {
|
||||||
|
consumer.consumeAsync(method.getReference().getDescriptor());
|
||||||
|
consumer.consumeAsync(method.getReference());
|
||||||
|
} else {
|
||||||
|
consumer.consume(method.getReference().getDescriptor());
|
||||||
|
consumer.consume(method.getReference());
|
||||||
|
if (asyncFamilyMethods.contains(method.getReference())) {
|
||||||
|
consumer.consumeAsync(method.getReference().getDescriptor());
|
||||||
|
consumer.consumeAsync(method.getReference());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Metadata
|
||||||
|
consumer.consume(cls.getName());
|
||||||
|
consumer.consume(cls.getName());
|
||||||
|
if (cls.getParentName() != null) {
|
||||||
|
consumer.consume(cls.getParentName());
|
||||||
|
}
|
||||||
|
for (String iface : cls.getInterfaces()) {
|
||||||
|
consumer.consume(iface);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(RegularMethodNode methodNode) {
|
||||||
|
async = false;
|
||||||
|
methodNode.getBody().acceptVisitor(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(AsyncMethodNode methodNode) {
|
||||||
|
async = true;
|
||||||
|
for (AsyncMethodPart part : methodNode.getBody()) {
|
||||||
|
part.getStatement().acceptVisitor(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(NativeMethodNode methodNode) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(AssignmentStatement statement) {
|
||||||
|
if (statement.getLeftValue() != null) {
|
||||||
|
statement.getLeftValue().acceptVisitor(this);
|
||||||
|
}
|
||||||
|
statement.getRightValue().acceptVisitor(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(SequentialStatement statement) {
|
||||||
|
visit(statement.getSequence());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(ConditionalStatement statement) {
|
||||||
|
statement.getCondition().acceptVisitor(this);
|
||||||
|
visit(statement.getConsequent());
|
||||||
|
visit(statement.getAlternative());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(SwitchStatement statement) {
|
||||||
|
statement.getValue().acceptVisitor(this);
|
||||||
|
for (SwitchClause clause : statement.getClauses()) {
|
||||||
|
visit(clause.getBody());
|
||||||
|
}
|
||||||
|
visit(statement.getDefaultClause());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(WhileStatement statement) {
|
||||||
|
if (statement.getCondition() != null) {
|
||||||
|
statement.getCondition().acceptVisitor(this);
|
||||||
|
}
|
||||||
|
visit(statement.getBody());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(BlockStatement statement) {
|
||||||
|
visit(statement.getBody());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(BreakStatement statement) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(ContinueStatement statement) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(ReturnStatement statement) {
|
||||||
|
if (statement.getResult() != null) {
|
||||||
|
statement.getResult().acceptVisitor(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(ThrowStatement statement) {
|
||||||
|
statement.getException().acceptVisitor(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(InitClassStatement statement) {
|
||||||
|
consumer.consume(statement.getClassName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(TryCatchStatement statement) {
|
||||||
|
visit(statement.getProtectedBody());
|
||||||
|
visit(statement.getHandler());
|
||||||
|
if (statement.getExceptionType() != null) {
|
||||||
|
consumer.consume(statement.getExceptionType());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(RestoreAsyncStatement statement) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(MonitorEnterStatement statement) {
|
||||||
|
if (async) {
|
||||||
|
MethodReference monitorEnterRef = new MethodReference(
|
||||||
|
Object.class, "monitorEnter", Object.class, void.class);
|
||||||
|
consumer.consumeAsync(monitorEnterRef);
|
||||||
|
} else {
|
||||||
|
MethodReference monitorEnterRef = new MethodReference(
|
||||||
|
Object.class, "monitorEnterSync", Object.class, void.class);
|
||||||
|
consumer.consume(monitorEnterRef);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(MonitorExitStatement statement) {
|
||||||
|
if (async) {
|
||||||
|
MethodReference monitorEnterRef = new MethodReference(
|
||||||
|
Object.class, "monitorExit", Object.class, void.class);
|
||||||
|
consumer.consumeAsync(monitorEnterRef);
|
||||||
|
} else {
|
||||||
|
MethodReference monitorEnterRef = new MethodReference(
|
||||||
|
Object.class, "monitorExitSync", Object.class, void.class);
|
||||||
|
consumer.consume(monitorEnterRef);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(BinaryExpr expr) {
|
||||||
|
expr.getFirstOperand().acceptVisitor(this);
|
||||||
|
expr.getSecondOperand().acceptVisitor(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(UnaryExpr expr) {
|
||||||
|
expr.getOperand().acceptVisitor(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(ConditionalExpr expr) {
|
||||||
|
expr.getCondition().acceptVisitor(this);
|
||||||
|
expr.getConsequent().acceptVisitor(this);
|
||||||
|
expr.getAlternative().acceptVisitor(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(ConstantExpr expr) {
|
||||||
|
if (expr.getValue() instanceof ValueType) {
|
||||||
|
visitType((ValueType)expr.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void visitType(ValueType type) {
|
||||||
|
while (type instanceof ValueType.Array) {
|
||||||
|
type = ((ValueType.Array)type).getItemType();
|
||||||
|
}
|
||||||
|
if (type instanceof ValueType.Object) {
|
||||||
|
String clsName = ((ValueType.Object)type).getClassName();
|
||||||
|
consumer.consume(clsName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(VariableExpr expr) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(SubscriptExpr expr) {
|
||||||
|
expr.getArray().acceptVisitor(this);
|
||||||
|
expr.getIndex().acceptVisitor(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(UnwrapArrayExpr expr) {
|
||||||
|
expr.getArray().acceptVisitor(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(InvocationExpr expr) {
|
||||||
|
if (injectedMethods.contains(expr.getMethod())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
boolean asyncCall = expr.getAsyncTarget() != null;
|
||||||
|
switch (expr.getType()) {
|
||||||
|
case SPECIAL:
|
||||||
|
case STATIC:
|
||||||
|
if (asyncCall) {
|
||||||
|
consumer.consumeAsync(expr.getMethod());
|
||||||
|
} else {
|
||||||
|
consumer.consume(expr.getMethod());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case CONSTRUCTOR:
|
||||||
|
consumer.consumeInit(expr.getMethod());
|
||||||
|
break;
|
||||||
|
case DYNAMIC:
|
||||||
|
if (asyncCall) {
|
||||||
|
consumer.consumeAsync(expr.getMethod().getDescriptor());
|
||||||
|
} else {
|
||||||
|
consumer.consume(expr.getMethod().getDescriptor());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(QualificationExpr expr) {
|
||||||
|
expr.getQualified().acceptVisitor(this);
|
||||||
|
consumer.consume(expr.getField());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(NewExpr expr) {
|
||||||
|
consumer.consume(expr.getConstructedClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(NewArrayExpr expr) {
|
||||||
|
visitType(expr.getType());
|
||||||
|
expr.getLength().acceptVisitor(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(NewMultiArrayExpr expr) {
|
||||||
|
visitType(expr.getType());
|
||||||
|
for (Expr dimension : expr.getDimensions()) {
|
||||||
|
dimension.acceptVisitor(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(InstanceOfExpr expr) {
|
||||||
|
expr.getExpr().acceptVisitor(this);
|
||||||
|
visitType(expr.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(StaticClassExpr expr) {
|
||||||
|
visitType(expr.getType());
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,6 +20,7 @@ import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import org.teavm.codegen.NamingException;
|
import org.teavm.codegen.NamingException;
|
||||||
|
import org.teavm.codegen.NamingOrderer;
|
||||||
import org.teavm.codegen.NamingStrategy;
|
import org.teavm.codegen.NamingStrategy;
|
||||||
import org.teavm.codegen.SourceWriter;
|
import org.teavm.codegen.SourceWriter;
|
||||||
import org.teavm.common.ServiceRepository;
|
import org.teavm.common.ServiceRepository;
|
||||||
|
@ -259,6 +260,15 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
||||||
}
|
}
|
||||||
|
|
||||||
public void render(List<ClassNode> classes) throws RenderingException {
|
public void render(List<ClassNode> classes) throws RenderingException {
|
||||||
|
if (minifying) {
|
||||||
|
NamingOrderer orderer = new NamingOrderer();
|
||||||
|
NameFrequencyEstimator estimator = new NameFrequencyEstimator(orderer, classSource, asyncMethods,
|
||||||
|
asyncFamilyMethods);
|
||||||
|
for (ClassNode cls : classes) {
|
||||||
|
estimator.estimate(cls);
|
||||||
|
}
|
||||||
|
orderer.apply(naming);
|
||||||
|
}
|
||||||
for (ClassNode cls : classes) {
|
for (ClassNode cls : classes) {
|
||||||
renderDeclaration(cls);
|
renderDeclaration(cls);
|
||||||
}
|
}
|
||||||
|
@ -313,7 +323,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
||||||
.append(constantToString(value)).append(";").softNewLine();
|
.append(constantToString(value)).append(";").softNewLine();
|
||||||
}
|
}
|
||||||
} 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 cause for details", e);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RenderingException("IO error occured", e);
|
throw new RenderingException("IO error occured", e);
|
||||||
}
|
}
|
||||||
|
@ -529,7 +539,8 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
||||||
}
|
}
|
||||||
|
|
||||||
private void emitVirtualDeclaration(MethodReference ref, boolean async) throws IOException {
|
private void emitVirtualDeclaration(MethodReference ref, boolean async) throws IOException {
|
||||||
String methodName = async ? naming.getNameForAsync(ref) : naming.getNameFor(ref);
|
String methodName = async ? naming.getNameForAsync(ref.getDescriptor()) :
|
||||||
|
naming.getNameFor(ref.getDescriptor());
|
||||||
writer.append("\"").append(methodName).append("\"");
|
writer.append("\"").append(methodName).append("\"");
|
||||||
writer.append(",").ws().append("function(");
|
writer.append(",").ws().append("function(");
|
||||||
List<String> args = new ArrayList<>();
|
List<String> args = new ArrayList<>();
|
||||||
|
@ -1665,7 +1676,8 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
|
||||||
expr.getArguments().get(0).acceptVisitor(this);
|
expr.getArguments().get(0).acceptVisitor(this);
|
||||||
}
|
}
|
||||||
MethodReference method = expr.getMethod();
|
MethodReference method = expr.getMethod();
|
||||||
String name = asyncCall ? naming.getNameForAsync(method) : naming.getNameFor(method);
|
String name = asyncCall ? naming.getNameForAsync(method.getDescriptor()) :
|
||||||
|
naming.getNameFor(method.getDescriptor());
|
||||||
DeferredCallSite callSite = prevCallSite;
|
DeferredCallSite callSite = prevCallSite;
|
||||||
boolean shouldEraseCallSite = lastCallSite == null;
|
boolean shouldEraseCallSite = lastCallSite == null;
|
||||||
if (lastCallSite == null) {
|
if (lastCallSite == null) {
|
||||||
|
|
|
@ -549,12 +549,11 @@ public class TeaVM implements TeaVMHost, ServiceRepository {
|
||||||
for (MethodReference injectedMethod : methodInjectors.keySet()) {
|
for (MethodReference injectedMethod : methodInjectors.keySet()) {
|
||||||
decompiler.addMethodToPass(injectedMethod);
|
decompiler.addMethodToPass(injectedMethod);
|
||||||
}
|
}
|
||||||
List<String> classOrder = decompiler.getClassOrdering(classes.getClassNames());
|
|
||||||
List<ClassNode> classNodes = new ArrayList<>();
|
List<ClassNode> classNodes = new ArrayList<>();
|
||||||
int index = 0;
|
int index = 0;
|
||||||
try (PrintWriter bytecodeLogger = bytecodeLogging ?
|
try (PrintWriter bytecodeLogger = bytecodeLogging ?
|
||||||
new PrintWriter(new OutputStreamWriter(logStream, "UTF-8")) : null) {
|
new PrintWriter(new OutputStreamWriter(logStream, "UTF-8")) : null) {
|
||||||
for (String className : classOrder) {
|
for (String className : classes.getClassNames()) {
|
||||||
ClassHolder cls = classes.get(className);
|
ClassHolder cls = classes.get(className);
|
||||||
for (MethodHolder method : cls.getMethods()) {
|
for (MethodHolder method : cls.getMethods()) {
|
||||||
processMethod(method);
|
processMethod(method);
|
||||||
|
|
|
@ -99,7 +99,7 @@ public class JavaScriptBodyGenerator implements Generator {
|
||||||
if (ident == null) {
|
if (ident == null) {
|
||||||
sb.append(naming.getFullNameFor(reader.getReference()));
|
sb.append(naming.getFullNameFor(reader.getReference()));
|
||||||
} else {
|
} else {
|
||||||
sb.append("$this.").append(naming.getNameFor(reader.getReference()));
|
sb.append("$this.").append(naming.getNameFor(reader.getDescriptor()));
|
||||||
}
|
}
|
||||||
sb.append("(");
|
sb.append("(");
|
||||||
for (int i = 0; i < reader.parameterCount(); ++i) {
|
for (int i = 0; i < reader.parameterCount(); ++i) {
|
||||||
|
|
|
@ -21,7 +21,6 @@ import org.teavm.codegen.SourceWriter;
|
||||||
import org.teavm.javascript.RenderingContext;
|
import org.teavm.javascript.RenderingContext;
|
||||||
import org.teavm.jso.plugin.JSODependencyListener.ExposedClass;
|
import org.teavm.jso.plugin.JSODependencyListener.ExposedClass;
|
||||||
import org.teavm.model.MethodDescriptor;
|
import org.teavm.model.MethodDescriptor;
|
||||||
import org.teavm.model.MethodReference;
|
|
||||||
import org.teavm.vm.BuildTarget;
|
import org.teavm.vm.BuildTarget;
|
||||||
import org.teavm.vm.spi.RendererListener;
|
import org.teavm.vm.spi.RendererListener;
|
||||||
|
|
||||||
|
@ -57,8 +56,7 @@ class JSOAliasRenderer implements RendererListener {
|
||||||
writer.append("c").ws().append("=").ws().appendClass(entry.getKey()).append(".prototype;").softNewLine();
|
writer.append("c").ws().append("=").ws().appendClass(entry.getKey()).append(".prototype;").softNewLine();
|
||||||
for (Map.Entry<MethodDescriptor, String> aliasEntry : entry.getValue().methods.entrySet()) {
|
for (Map.Entry<MethodDescriptor, String> aliasEntry : entry.getValue().methods.entrySet()) {
|
||||||
writer.append("c.").append(aliasEntry.getValue()).ws().append("=").ws().append("c.")
|
writer.append("c.").append(aliasEntry.getValue()).ws().append("=").ws().append("c.")
|
||||||
.appendMethod(new MethodReference(entry.getKey(), aliasEntry.getKey()))
|
.appendMethod(aliasEntry.getKey()).append(";").softNewLine();
|
||||||
.append(";").softNewLine();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writer.outdent().append("})();").newLine();
|
writer.outdent().append("})();").newLine();
|
||||||
|
|
|
@ -39,13 +39,13 @@ public class AsyncMethodGenerator implements Generator, DependencyPlugin {
|
||||||
public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException {
|
public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException {
|
||||||
MethodReference asyncRef = getAsyncReference(methodRef);
|
MethodReference asyncRef = getAsyncReference(methodRef);
|
||||||
writer.append("var callback").ws().append("=").ws().append("function()").ws().append("{};").softNewLine();
|
writer.append("var callback").ws().append("=").ws().append("function()").ws().append("{};").softNewLine();
|
||||||
writer.append("callback.").appendMethod(completeMethod).ws().append("=").ws().append("function(val)").ws()
|
writer.append("callback.").appendMethod(completeMethod.getDescriptor()).ws().append("=").ws()
|
||||||
.append("{").indent().softNewLine();
|
.append("function(val)").ws().append("{").indent().softNewLine();
|
||||||
writer.append("return ").append(context.getCompleteContinuation()).append("($rt_asyncResult(val));")
|
writer.append("return ").append(context.getCompleteContinuation()).append("($rt_asyncResult(val));")
|
||||||
.softNewLine();
|
.softNewLine();
|
||||||
writer.outdent().append("};").softNewLine();
|
writer.outdent().append("};").softNewLine();
|
||||||
writer.append("callback.").appendMethod(errorMethod).ws().append("=").ws().append("function(e)").ws()
|
writer.append("callback.").appendMethod(errorMethod.getDescriptor()).ws().append("=").ws()
|
||||||
.append("{").indent().softNewLine();
|
.append("function(e)").ws().append("{").indent().softNewLine();
|
||||||
writer.append("return ").append(context.getCompleteContinuation()).append("($rt_asyncError(e));")
|
writer.append("return ").append(context.getCompleteContinuation()).append("($rt_asyncError(e));")
|
||||||
.softNewLine();
|
.softNewLine();
|
||||||
writer.outdent().append("};").softNewLine();
|
writer.outdent().append("};").softNewLine();
|
||||||
|
|
|
@ -80,7 +80,7 @@ public class ClassScopedMetadataProviderNativeGenerator implements Generator {
|
||||||
|
|
||||||
Map<String, Resource> resourceMap = generator.generateMetadata(metadataContext, methodRef);
|
Map<String, Resource> resourceMap = generator.generateMetadata(metadataContext, methodRef);
|
||||||
writer.append("var p").ws().append("=").ws().append("\"" + Renderer.escapeString("$$res_" +
|
writer.append("var p").ws().append("=").ws().append("\"" + Renderer.escapeString("$$res_" +
|
||||||
writer.getNaming().getNameFor(methodRef)) + "\"").append(";").softNewLine();
|
writer.getNaming().getFullNameFor(methodRef)) + "\"").append(";").softNewLine();
|
||||||
for (Map.Entry<String, Resource> entry : resourceMap.entrySet()) {
|
for (Map.Entry<String, Resource> entry : resourceMap.entrySet()) {
|
||||||
writer.appendClass(entry.getKey()).append("[p]").ws().append("=").ws();
|
writer.appendClass(entry.getKey()).append("[p]").ws().append("=").ws();
|
||||||
ResourceWriterHelper.write(writer, entry.getValue());
|
ResourceWriterHelper.write(writer, entry.getValue());
|
||||||
|
|
Loading…
Reference in New Issue
Block a user