diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/AsyncMethodGenerator.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/AsyncMethodGenerator.java index f3a417540..7addb5337 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/AsyncMethodGenerator.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/AsyncMethodGenerator.java @@ -19,6 +19,8 @@ import java.io.IOException; import org.teavm.codegen.SourceWriter; import org.teavm.dependency.DependencyAgent; import org.teavm.dependency.DependencyPlugin; +import org.teavm.dependency.DependencyType; +import org.teavm.dependency.DependencyTypeFilter; import org.teavm.dependency.MethodDependency; import org.teavm.javascript.spi.Generator; import org.teavm.javascript.spi.GeneratorContext; @@ -88,13 +90,61 @@ public class AsyncMethodGenerator implements Generator, DependencyPlugin { } @Override - public void methodAchieved(DependencyAgent checker, MethodDependency method, CallLocation location) { + public void methodAchieved(final DependencyAgent checker, final MethodDependency method, CallLocation location) { MethodReference asyncRef = getAsyncReference(method.getReference()); MethodDependency asyncMethod = checker.linkMethod(asyncRef, location); int paramCount = method.getReference().parameterCount(); for (int i = 0; i <= paramCount; ++i) { method.getVariable(i).connect(asyncMethod.getVariable(i)); } + asyncMethod.getVariable(paramCount + 1).propagate(checker.getType(FakeAsyncCallback.class.getName())); + + if (method.getResult() != null) { + MethodDependency completeMethod = checker.linkMethod( + new MethodReference(FakeAsyncCallback.class, "complete", Object.class, void.class), null); + completeMethod.getVariable(1).connect(method.getResult(), new DependencyTypeFilter() { + @Override + public boolean match(DependencyType type) { + return isSubtype(checker.getClassSource(), type.getName(), method.getReference().getReturnType()); + } + }); + } + + MethodDependency errorMethod = checker.linkMethod(new MethodReference(FakeAsyncCallback.class, "error", + Throwable.class, void.class), null); + errorMethod.getVariable(1).connect(method.getThrown()); + asyncMethod.use(); } + + private boolean isSubtype(ClassReaderSource classSource, String className, ValueType returnType) { + if (returnType instanceof ValueType.Primitive) { + return false; + } else if (returnType instanceof ValueType.Array) { + return className.startsWith("["); + } else { + return isSubclass(classSource, className, ((ValueType.Object)returnType).getClassName()); + } + } + + private boolean isSubclass(ClassReaderSource classSource, String className, String baseClass) { + if (className.equals(baseClass)) { + return true; + } + ClassReader cls = classSource.get(className); + if (cls == null) { + return false; + } + if (cls.getParent() != null && !cls.getParent().equals(cls.getName())) { + if (isSubclass(classSource, cls.getParent(), baseClass)) { + return true; + } + } + for (String iface : cls.getInterfaces()) { + if (isSubclass(classSource, iface, baseClass)) { + return true; + } + } + return false; + } } diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/EnumDependencySupport.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/EnumDependencySupport.java index d194142ad..92d7c7408 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/EnumDependencySupport.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/EnumDependencySupport.java @@ -38,7 +38,6 @@ public class EnumDependencySupport implements DependencyListener { return; } allEnums.propagate(agent.getType(className)); - } @Override diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/FakeAsyncCallback.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/FakeAsyncCallback.java new file mode 100644 index 000000000..14d1cbf5e --- /dev/null +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/FakeAsyncCallback.java @@ -0,0 +1,32 @@ +/* + * 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.platform.async.AsyncCallback; + +/** + * + * @author Alexey Andreev + */ +class FakeAsyncCallback implements AsyncCallback { + @Override + public void complete(Object result) { + } + + @Override + public void error(Throwable e) { + } +}