mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 16:14:10 -08:00
Fix dependency problem in JSO
This commit is contained in:
parent
6d7d2691ae
commit
b69b5b9219
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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) {
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<MethodDescriptor> methods) {
|
||||
|
|
|
@ -1 +1 @@
|
|||
org.teavm.jso.plugin.JSObjectBuilderPlugin
|
||||
org.teavm.jso.plugin.JSOPlugin
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user