Fix dependency problem in JSO

This commit is contained in:
konsoletyper 2015-02-10 21:57:15 +04:00
parent 6d7d2691ae
commit b69b5b9219
6 changed files with 241 additions and 9 deletions

View File

@ -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 <konsoletyper@gmail.com>
*/
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<String, ExposedClass> 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<MethodDescriptor, String> 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();
}
}

View File

@ -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 <konsoletyper@gmail.com>
*/
class JSODependencyListener implements DependencyListener {
private Map<String, ExposedClass> 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<String, ExposedClass> getExposedClasses() {
return exposedClasses;
}
static class ExposedClass {
Map<MethodDescriptor, String> inheritedMethods = new HashMap<>();
Map<MethodDescriptor, String> methods = new HashMap<>();
Set<String> 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) {
}
}

View File

@ -22,9 +22,13 @@ import org.teavm.vm.spi.TeaVMPlugin;
* *
* @author Alexey Andreev * @author Alexey Andreev
*/ */
public class JSObjectBuilderPlugin implements TeaVMPlugin { public class JSOPlugin implements TeaVMPlugin {
@Override @Override
public void install(TeaVMHost host) { public void install(TeaVMHost host) {
host.add(new JSObjectClassTransformer()); host.add(new JSObjectClassTransformer());
JSODependencyListener dependencyListener = new JSODependencyListener();
JSOAliasRenderer aliasRenderer = new JSOAliasRenderer(dependencyListener);
host.add(dependencyListener);
host.add(aliasRenderer);
} }
} }

View File

@ -18,7 +18,6 @@ package org.teavm.jso.plugin;
import java.util.*; import java.util.*;
import org.teavm.diagnostics.Diagnostics; import org.teavm.diagnostics.Diagnostics;
import org.teavm.javascript.spi.GeneratedBy; import org.teavm.javascript.spi.GeneratedBy;
import org.teavm.javascript.spi.PreserveOriginalName;
import org.teavm.jso.*; import org.teavm.jso.*;
import org.teavm.model.*; import org.teavm.model.*;
import org.teavm.model.instructions.*; import org.teavm.model.instructions.*;
@ -51,12 +50,6 @@ class JavascriptNativeProcessor {
addPreservedMethods(iface, preservedMethods); 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<MethodDescriptor> methods) { private void addPreservedMethods(String ifaceName, Set<MethodDescriptor> methods) {

View File

@ -1 +1 @@
org.teavm.jso.plugin.JSObjectBuilderPlugin org.teavm.jso.plugin.JSOPlugin

View File

@ -18,6 +18,8 @@ package org.teavm.jso.test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
import org.teavm.jso.JS; 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() { private static Window getWindow() {
return (Window)JS.getGlobal(); 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);
} }