This commit is contained in:
konsoletyper 2015-02-13 22:30:41 +04:00
parent 54ecf6e00f
commit a4d948f26b
11 changed files with 9 additions and 238 deletions

View File

@ -29,7 +29,6 @@ import org.teavm.vm.spi.TeaVMPlugin;
public class JCLPlugin implements TeaVMPlugin { public class JCLPlugin implements TeaVMPlugin {
@Override @Override
public void install(TeaVMHost host) { public void install(TeaVMHost host) {
host.add(new ObjectEnrichRenderer());
ServiceLoaderSupport serviceLoaderSupp = new ServiceLoaderSupport(host.getClassLoader()); ServiceLoaderSupport serviceLoaderSupp = new ServiceLoaderSupport(host.getClassLoader());
host.add(serviceLoaderSupp); host.add(serviceLoaderSupp);
MethodReference loadServicesMethod = new MethodReference(ServiceLoader.class, "loadServices", MethodReference loadServicesMethod = new MethodReference(ServiceLoader.class, "loadServices",

View File

@ -1,53 +0,0 @@
/*
* Copyright 2014 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.classlib.impl;
import java.io.IOException;
import org.teavm.javascript.RenderingContext;
import org.teavm.model.ClassReader;
import org.teavm.model.MethodDescriptor;
import org.teavm.model.MethodReader;
import org.teavm.vm.BuildTarget;
import org.teavm.vm.spi.RendererListener;
/**
*
* @author Alexey Andreev
*/
public class ObjectEnrichRenderer implements RendererListener {
private RenderingContext context;
@Override
public void begin(RenderingContext context, BuildTarget buildTarget) throws IOException {
this.context = context;
}
@Override
public void complete() throws IOException {
ClassReader cls = context.getClassSource().get("java.lang.Object");
MethodReader toString = cls.getMethod(new MethodDescriptor("toString", String.class));
if (toString != null) {
String clsName = context.getNaming().getNameFor(cls.getName());
String toStringName = context.getNaming().getNameFor(toString.getReference());
context.getWriter().append(clsName).append(".prototype.toString").ws().append('=').ws()
.append("function()").ws().append('{').indent().softNewLine();
context.getWriter().append("return this.").append(toStringName).ws().append('?').ws()
.append("$rt_ustr(this.").append(toStringName).append("())").ws().append(':')
.append("Object.prototype.toString.call(this);").softNewLine();
context.getWriter().outdent().append("}").newLine();
}
}
}

View File

@ -59,7 +59,6 @@ public class AstIO {
} }
} }
} }
output.writeBoolean(method.isOriginalNamePreserved());
try { try {
method.getBody().acceptVisitor(new NodeWriter(output)); method.getBody().acceptVisitor(new NodeWriter(output));
} catch (IOExceptionWrapper e) { } catch (IOExceptionWrapper e) {
@ -83,7 +82,6 @@ public class AstIO {
} }
node.getParameterDebugNames().add(debugNames); node.getParameterDebugNames().add(debugNames);
} }
node.setOriginalNamePreserved(input.readBoolean());
node.setBody(readStatement(input)); node.setBody(readStatement(input));
return node; return node;
} }

View File

@ -21,7 +21,6 @@ import org.teavm.javascript.ast.*;
import org.teavm.javascript.spi.GeneratedBy; import org.teavm.javascript.spi.GeneratedBy;
import org.teavm.javascript.spi.Generator; import org.teavm.javascript.spi.Generator;
import org.teavm.javascript.spi.InjectedBy; import org.teavm.javascript.spi.InjectedBy;
import org.teavm.javascript.spi.PreserveOriginalName;
import org.teavm.model.*; import org.teavm.model.*;
import org.teavm.model.util.AsyncProgramSplitter; import org.teavm.model.util.AsyncProgramSplitter;
import org.teavm.model.util.ProgramUtils; import org.teavm.model.util.ProgramUtils;
@ -146,9 +145,6 @@ public class Decompiler {
} }
MethodNode methodNode = decompile(method); MethodNode methodNode = decompile(method);
clsNode.getMethods().add(methodNode); clsNode.getMethods().add(methodNode);
if (method.getAnnotations().get(PreserveOriginalName.class.getName()) != null) {
methodNode.setOriginalNamePreserved(true);
}
} }
clsNode.getInterfaces().addAll(cls.getInterfaces()); clsNode.getInterfaces().addAll(cls.getInterfaces());
clsNode.getModifiers().addAll(mapModifiers(cls.getModifiers())); clsNode.getModifiers().addAll(mapModifiers(cls.getModifiers()));

View File

@ -278,7 +278,6 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
renderMethodBodies(cls); renderMethodBodies(cls);
} }
renderClassMetadata(classes); renderClassMetadata(classes);
renderMethodStubs(classes);
} }
private void renderDeclaration(ClassNode cls) throws RenderingException { private void renderDeclaration(ClassNode cls) throws RenderingException {
@ -364,8 +363,6 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} else { } else {
virtualMethods.add(method); virtualMethods.add(method);
} }
} else if (method.isOriginalNamePreserved()) {
renderStaticDeclaration(method);
} }
} }
} }
@ -381,56 +378,9 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
debugEmitter.emitClass(null); debugEmitter.emitClass(null);
} }
private void renderMethodStubs(List<ClassNode> classes) {
try {
boolean first = true;
writer.append("$rt_methodStubs([");
for (int i = 0; i < classes.size(); ++i) {
ClassNode cls = classes.get(i);
MethodHolder clinit = classSource.get(cls.getName()).getMethod(
new MethodDescriptor("<clinit>", ValueType.VOID));
if (clinit == null) {
continue;
}
List<String> stubNames = new ArrayList<>();
for (MethodNode method : cls.getMethods()) {
if (method.getModifiers().contains(NodeModifier.STATIC) ||
method.getReference().getName().equals("<init>")) {
stubNames.add(naming.getFullNameFor(method.getReference()));
}
}
if (stubNames.isEmpty()) {
continue;
}
if (!first) {
writer.append(',').softNewLine();
}
first = false;
writer.appendClass(cls.getName()).append("_$clinit").append(",").ws();
if (stubNames.size() == 1) {
writer.append("'").append(stubNames.get(0)).append("'");
} else {
writer.append('[');
for (int j = 0; j < stubNames.size(); ++j) {
if (j > 0) {
writer.append(",").ws();
}
writer.append("'").append(stubNames.get(j)).append("'");
}
writer.append(']');
}
}
writer.append("]);").newLine();
} catch (NamingException e) {
throw new RenderingException("Error rendering method stubs. See a cause for details", e);
} catch (IOException e) {
throw new RenderingException("IO error occured", e);
}
}
private void renderClassMetadata(List<ClassNode> classes) { private void renderClassMetadata(List<ClassNode> classes) {
try { try {
writer.append("$rt_declClasses(["); writer.append("$rt_metadata([");
boolean first = true; boolean first = true;
for (ClassNode cls : classes) { for (ClassNode cls : classes) {
if (!first) { if (!first) {
@ -538,7 +488,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
writer.append(variableName(i)); writer.append(variableName(i));
} }
writer.append(")").ws().append("{").newLine().indent(); writer.append(")").ws().append("{").softNewLine().indent();
writer.append("var result").ws().append("=").ws().append("new ").appendClass( writer.append("var result").ws().append("=").ws().append("new ").appendClass(
ref.getClassName()).append("();").softNewLine(); ref.getClassName()).append("();").softNewLine();
writer.append(naming.getFullNameFor(ref)).append("(result"); writer.append(naming.getFullNameFor(ref)).append("(result");
@ -563,12 +513,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
first = false; first = false;
String methodName = method.isAsync() ? naming.getNameForAsync(ref) : naming.getNameFor(ref); String methodName = method.isAsync() ? naming.getNameForAsync(ref) : naming.getNameFor(ref);
if (method.isOriginalNamePreserved()) { writer.append("\"").append(methodName).append("\"");
writer.append("[\"").append(methodName).append("\",").ws().append("\"").append(ref.getName())
.append("\"]");
} else {
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<>();
for (int i = 1; i <= ref.parameterCount(); ++i) { for (int i = 1; i <= ref.parameterCount(); ++i) {
@ -604,33 +549,6 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append("]"); writer.append("]");
} }
private void renderStaticDeclaration(MethodNode method) throws NamingException, IOException {
MethodReference ref = method.getReference();
debugEmitter.emitMethod(ref.getDescriptor());
if (ref.getDescriptor().getName().equals("<init>")) {
renderInitializer(method);
}
writer.appendClass(ref.getClassName()).append(".").appendMethod(ref).ws().append("=").ws().append("function(");
for (int i = 0; i < ref.parameterCount(); ++i) {
if (i > 0) {
writer.append(", ");
}
writer.append(variableName(i + 1));
}
writer.append(")").ws().append("{").softNewLine().indent();
writer.append("return ").appendMethodBody(ref).append("(");
for (int i = 0; i < ref.parameterCount(); ++i) {
writer.append(",").ws().append(variableName(i + 1));
}
writer.append(");").softNewLine();
writer.outdent().append("}").newLine();
if (method.isOriginalNamePreserved()) {
writer.appendClass(ref.getClassName()).append(".").append(ref.getName()).ws().append("=")
.ws().appendClass(ref.getClassName()).append(".").appendMethod(ref).append(';').newLine();
}
debugEmitter.emitMethod(null);
}
public void renderBody(MethodNode method, boolean inner) throws IOException { public void renderBody(MethodNode method, boolean inner) throws IOException {
MethodReference ref = method.getReference(); MethodReference ref = method.getReference();
debugEmitter.emitMethod(ref.getDescriptor()); debugEmitter.emitMethod(ref.getDescriptor());

View File

@ -26,7 +26,6 @@ import org.teavm.model.MethodReference;
public abstract class MethodNode { public abstract class MethodNode {
private MethodReference reference; private MethodReference reference;
private Set<NodeModifier> modifiers = EnumSet.noneOf(NodeModifier.class); private Set<NodeModifier> modifiers = EnumSet.noneOf(NodeModifier.class);
private boolean originalNamePreserved;
public MethodNode(MethodReference reference) { public MethodNode(MethodReference reference) {
this.reference = reference; this.reference = reference;
@ -41,14 +40,6 @@ public abstract class MethodNode {
return modifiers; return modifiers;
} }
public boolean isOriginalNamePreserved() {
return originalNamePreserved;
}
public void setOriginalNamePreserved(boolean originalNamePreserved) {
this.originalNamePreserved = originalNamePreserved;
}
public abstract void acceptVisitor(MethodNodeVisitor visitor); public abstract void acceptVisitor(MethodNodeVisitor visitor);
public abstract boolean isAsync(); public abstract boolean isAsync();

View File

@ -1,30 +0,0 @@
/*
* Copyright 2014 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.spi;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author Alexey Andreev
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PreserveOriginalName {
}

View File

@ -466,11 +466,11 @@ public class TeaVM implements TeaVMHost, ServiceRepository {
if (wrapAsync) { if (wrapAsync) {
sourceWriter.append(")"); sourceWriter.append(")");
} }
sourceWriter.append(";").softNewLine(); sourceWriter.append(";").newLine();
} }
for (Map.Entry<String, String> entry : exportedClasses.entrySet()) { for (Map.Entry<String, String> entry : exportedClasses.entrySet()) {
sourceWriter.append("var ").append(entry.getKey()).ws().append("=").ws() sourceWriter.append("var ").append(entry.getKey()).ws().append("=").ws()
.appendClass(entry.getValue()).append(";").softNewLine(); .appendClass(entry.getValue()).append(";").newLine();
} }
for (RendererListener listener : rendererListeners) { for (RendererListener listener : rendererListeners) {
listener.complete(); listener.complete();

View File

@ -217,14 +217,6 @@ function $rt_voidcls() {
} }
return $rt_voidclsCache; return $rt_voidclsCache;
} }
function $rt_clinit(cls) {
if (cls.$clinit) {
var f = cls.$clinit;
delete cls.$clinit;
f();
}
return cls;
}
function $rt_init(cls, constructor, args) { function $rt_init(cls, constructor, args) {
var obj = new cls(); var obj = new cls();
cls.prototype[constructor].apply(obj, args); cls.prototype[constructor].apply(obj, args);
@ -409,7 +401,7 @@ function $rt_metadata(data) {
return function() { return function() {
var clinit = cls.$clinit; var clinit = cls.$clinit;
cls.$clinit = function() {}; cls.$clinit = function() {};
cls.$clinit(); clinit();
return window[name].apply(window, arguments); return window[name].apply(window, arguments);
} }
})(cls, names[j]); })(cls, names[j]);
@ -417,7 +409,7 @@ function $rt_metadata(data) {
var virtualMethods = data[i + 7]; var virtualMethods = data[i + 7];
for (var j = 0; j < virtualMethods.length; j += 2) { for (var j = 0; j < virtualMethods.length; j += 2) {
name = virtualMethods[j + 0]; var name = virtualMethods[j + 0];
var func = virtualMethods[j + 1]; var func = virtualMethods[j + 1];
if (typeof name === 'string') { if (typeof name === 'string') {
name = [name]; name = [name];

View File

@ -1,39 +0,0 @@
/*
* Copyright 2014 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.platform.plugin;
import org.teavm.diagnostics.Diagnostics;
import org.teavm.javascript.spi.PreserveOriginalName;
import org.teavm.model.*;
/**
*
* @author Alexey Andreev
*/
public class EnumTransformer implements ClassHolderTransformer {
@Override
public void transformClass(ClassHolder cls, ClassReaderSource innerSource, Diagnostics diagnostics) {
if (cls.getParent() != null && !cls.getParent().equals("java.lang.Enum")) {
return;
}
MethodHolder method = cls.getMethod(new MethodDescriptor("values",
ValueType.arrayOf(ValueType.object(cls.getName()))));
if (method == null) {
return;
}
method.getAnnotations().add(new AnnotationHolder(PreserveOriginalName.class.getName()));
}
}

View File

@ -33,6 +33,5 @@ public class PlatformPlugin implements TeaVMPlugin {
host.add(new NewInstanceDependencySupport()); host.add(new NewInstanceDependencySupport());
host.add(new ClassLookupDependencySupport()); host.add(new ClassLookupDependencySupport());
host.add(new EnumDependencySupport()); host.add(new EnumDependencySupport());
host.add(new EnumTransformer());
} }
} }