diff --git a/teavm-jso/src/main/java/org/teavm/jso/plugin/JSOAliasRenderer.java b/teavm-jso/src/main/java/org/teavm/jso/plugin/JSOAliasRenderer.java new file mode 100644 index 000000000..fccfbb178 --- /dev/null +++ b/teavm-jso/src/main/java/org/teavm/jso/plugin/JSOAliasRenderer.java @@ -0,0 +1,75 @@ +/* + * 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.jso.plugin; + +import java.io.IOException; +import java.util.Map; +import org.teavm.codegen.SourceWriter; +import org.teavm.javascript.RenderingContext; +import org.teavm.jso.plugin.JSODependencyListener.ExposedClass; +import org.teavm.model.ClassReader; +import org.teavm.model.MethodDescriptor; +import org.teavm.model.MethodReference; +import org.teavm.vm.BuildTarget; +import org.teavm.vm.spi.RendererListener; + +/** + * + * @author Alexey Andreev + */ +class JSOAliasRenderer implements RendererListener { + private JSODependencyListener dependencyListener; + private SourceWriter writer; + + public JSOAliasRenderer(JSODependencyListener dependencyListener) { + this.dependencyListener = dependencyListener; + } + + @Override + public void begin(RenderingContext context, BuildTarget buildTarget) throws IOException { + writer = context.getWriter(); + } + + @Override + public void beforeClass(ClassReader cls) throws IOException { + } + + @Override + public void afterClass(ClassReader cls) throws IOException { + } + + @Override + public void complete() throws IOException { + if (!dependencyListener.isAnyAliasExists()) { + return; + } + + writer.append("(function()").ws().append("{").softNewLine().indent(); + writer.append("var c;").softNewLine(); + for (Map.Entry entry : dependencyListener.getExposedClasses().entrySet()) { + if (entry.getValue().methods.isEmpty()) { + continue; + } + writer.append("c").ws().append("=").ws().appendClass(entry.getKey()).append(".prototype").softNewLine(); + for (Map.Entry aliasEntry : entry.getValue().methods.entrySet()) { + writer.append("c.").append(aliasEntry.getValue()).ws().append("=").ws().append("c.") + .appendMethod(new MethodReference(entry.getKey(), aliasEntry.getKey())) + .append(";").softNewLine(); + } + } + writer.outdent().append("})();").softNewLine(); + } +} diff --git a/teavm-jso/src/main/java/org/teavm/jso/plugin/JSODependencyListener.java b/teavm-jso/src/main/java/org/teavm/jso/plugin/JSODependencyListener.java new file mode 100644 index 000000000..66d3d3f8a --- /dev/null +++ b/teavm-jso/src/main/java/org/teavm/jso/plugin/JSODependencyListener.java @@ -0,0 +1,141 @@ +/* + * 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.jso.plugin; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import org.teavm.dependency.DependencyAgent; +import org.teavm.dependency.DependencyListener; +import org.teavm.dependency.FieldDependency; +import org.teavm.dependency.MethodDependency; +import org.teavm.jso.JSMethod; +import org.teavm.jso.JSObject; +import org.teavm.model.*; + +/** + * + * @author Alexey Andreev + */ +class JSODependencyListener implements DependencyListener { + private Map exposedClasses = new HashMap<>(); + private ClassReaderSource classSource; + private DependencyAgent agent; + private boolean anyAliasExists; + + @Override + public void started(DependencyAgent agent) { + this.agent = agent; + classSource = agent.getClassSource(); + } + + @Override + public void classAchieved(DependencyAgent agent, String className, CallLocation location) { + getExposedClass(className); + } + + boolean isAnyAliasExists() { + return anyAliasExists; + } + + Map getExposedClasses() { + return exposedClasses; + } + + static class ExposedClass { + Map inheritedMethods = new HashMap<>(); + Map methods = new HashMap<>(); + Set implementedInterfaces = new HashSet<>(); + } + + private ExposedClass getExposedClass(String name) { + ExposedClass cls = exposedClasses.get(name); + if (cls == null) { + cls = createExposedClass(name); + exposedClasses.put(name, cls); + } + return cls; + } + + private ExposedClass createExposedClass(String name) { + ClassReader cls = classSource.get(name); + ExposedClass exposedCls = new ExposedClass(); + if (cls.getParent() != null && !cls.getParent().equals(cls.getName())) { + ExposedClass parent = getExposedClass(cls.getParent()); + exposedCls.inheritedMethods.putAll(parent.inheritedMethods); + exposedCls.inheritedMethods.putAll(parent.methods); + exposedCls.implementedInterfaces.addAll(parent.implementedInterfaces); + } + addInterfaces(exposedCls, cls); + for (MethodReader method : cls.getMethods()) { + if (exposedCls.inheritedMethods.containsKey(method.getDescriptor()) || + exposedCls.methods.containsKey(method.getDescriptor())) { + agent.linkMethod(method.getReference(), null).use(); + } + } + return exposedCls; + } + + private boolean addInterfaces(ExposedClass exposedCls, ClassReader cls) { + boolean added = false; + for (String ifaceName : cls.getInterfaces()) { + if (exposedCls.implementedInterfaces.contains(ifaceName)) { + continue; + } + ClassReader iface = classSource.get(ifaceName); + if (iface == null) { + continue; + } + if (addInterface(exposedCls, iface)) { + added = true; + for (MethodReader method : iface.getMethods()) { + if (!exposedCls.inheritedMethods.containsKey(method.getDescriptor())) { + String name = method.getName(); + AnnotationReader methodAnnot = method.getAnnotations().get(JSMethod.class.getName()); + if (methodAnnot != null) { + AnnotationValue nameVal = methodAnnot.getValue("value"); + if (nameVal != null) { + String nameStr = nameVal.getString(); + if (!nameStr.isEmpty()) { + name = nameStr; + } + } + } + exposedCls.methods.put(method.getDescriptor(), name); + anyAliasExists = true; + } + } + } + } + return added; + } + + private boolean addInterface(ExposedClass exposedCls, ClassReader cls) { + if (cls.getName().equals(JSObject.class.getName())) { + return true; + } + return addInterfaces(exposedCls, cls); + } + + @Override + public void methodAchieved(DependencyAgent agent, MethodDependency method, CallLocation location) { + } + + @Override + public void fieldAchieved(DependencyAgent agent, FieldDependency field, CallLocation location) { + } +} diff --git a/teavm-jso/src/main/java/org/teavm/jso/plugin/JSObjectBuilderPlugin.java b/teavm-jso/src/main/java/org/teavm/jso/plugin/JSOPlugin.java similarity index 75% rename from teavm-jso/src/main/java/org/teavm/jso/plugin/JSObjectBuilderPlugin.java rename to teavm-jso/src/main/java/org/teavm/jso/plugin/JSOPlugin.java index 97672e804..9bd7a16cd 100644 --- a/teavm-jso/src/main/java/org/teavm/jso/plugin/JSObjectBuilderPlugin.java +++ b/teavm-jso/src/main/java/org/teavm/jso/plugin/JSOPlugin.java @@ -22,9 +22,13 @@ import org.teavm.vm.spi.TeaVMPlugin; * * @author Alexey Andreev */ -public class JSObjectBuilderPlugin implements TeaVMPlugin { +public class JSOPlugin implements TeaVMPlugin { @Override public void install(TeaVMHost host) { host.add(new JSObjectClassTransformer()); + JSODependencyListener dependencyListener = new JSODependencyListener(); + JSOAliasRenderer aliasRenderer = new JSOAliasRenderer(dependencyListener); + host.add(dependencyListener); + host.add(aliasRenderer); } } diff --git a/teavm-jso/src/main/java/org/teavm/jso/plugin/JavascriptNativeProcessor.java b/teavm-jso/src/main/java/org/teavm/jso/plugin/JavascriptNativeProcessor.java index af8872317..8f2f1f47c 100644 --- a/teavm-jso/src/main/java/org/teavm/jso/plugin/JavascriptNativeProcessor.java +++ b/teavm-jso/src/main/java/org/teavm/jso/plugin/JavascriptNativeProcessor.java @@ -18,7 +18,6 @@ package org.teavm.jso.plugin; import java.util.*; import org.teavm.diagnostics.Diagnostics; import org.teavm.javascript.spi.GeneratedBy; -import org.teavm.javascript.spi.PreserveOriginalName; import org.teavm.jso.*; import org.teavm.model.*; import org.teavm.model.instructions.*; @@ -51,12 +50,6 @@ class JavascriptNativeProcessor { addPreservedMethods(iface, preservedMethods); } } - for (MethodHolder method : cls.getMethods()) { - if (preservedMethods.contains(method.getDescriptor()) && - method.getAnnotations().get(PreserveOriginalName.class.getName()) == null) { - method.getAnnotations().add(new AnnotationHolder(PreserveOriginalName.class.getName())); - } - } } private void addPreservedMethods(String ifaceName, Set methods) { diff --git a/teavm-jso/src/main/resources/META-INF/services/org.teavm.vm.spi.TeaVMPlugin b/teavm-jso/src/main/resources/META-INF/services/org.teavm.vm.spi.TeaVMPlugin index faefa2589..c0f1b934e 100644 --- a/teavm-jso/src/main/resources/META-INF/services/org.teavm.vm.spi.TeaVMPlugin +++ b/teavm-jso/src/main/resources/META-INF/services/org.teavm.vm.spi.TeaVMPlugin @@ -1 +1 @@ -org.teavm.jso.plugin.JSObjectBuilderPlugin \ No newline at end of file +org.teavm.jso.plugin.JSOPlugin \ No newline at end of file diff --git a/teavm-tests/src/test/java/org/teavm/jso/test/JSOTest.java b/teavm-tests/src/test/java/org/teavm/jso/test/JSOTest.java index 476788661..adc59de90 100644 --- a/teavm-tests/src/test/java/org/teavm/jso/test/JSOTest.java +++ b/teavm-tests/src/test/java/org/teavm/jso/test/JSOTest.java @@ -18,6 +18,8 @@ package org.teavm.jso.test; import static org.junit.Assert.*; import org.junit.Test; import org.teavm.jso.JS; +import org.teavm.jso.JSBody; +import org.teavm.jso.JSObject; /** * @@ -33,4 +35,21 @@ public class JSOTest { private static Window getWindow() { return (Window)JS.getGlobal(); } + + @Test + public void externalMethodsResolved() { + int r = jsMethod(new Callback() { + @Override public int call() { + return 23; + } + }); + assertEquals(23, r); + } + + interface Callback extends JSObject { + int call(); + } + + @JSBody(params = "cb", script = "return cb.call();") + private static native int jsMethod(Callback cb); }