Start implementing diagnostics API

This commit is contained in:
konsoletyper 2014-11-05 22:14:41 +03:00
parent 19c18a41c5
commit e736cf09d7
22 changed files with 297 additions and 69 deletions

View File

@ -24,7 +24,7 @@ import org.teavm.model.*;
*/ */
public class EnumTransformer implements ClassHolderTransformer { public class EnumTransformer implements ClassHolderTransformer {
@Override @Override
public void transformClass(ClassHolder cls, ClassReaderSource innerSource) { public void transformClass(ClassHolder cls, ClassReaderSource innerSource, Diagnostics diagnostics) {
if (cls.getParent() != null && !cls.getParent().equals("java.lang.Enum")) { if (cls.getParent() != null && !cls.getParent().equals("java.lang.Enum")) {
return; return;
} }

View File

@ -27,7 +27,7 @@ import org.teavm.model.instructions.InvokeInstruction;
*/ */
public class JavacSupport implements ClassHolderTransformer { public class JavacSupport implements ClassHolderTransformer {
@Override @Override
public void transformClass(ClassHolder cls, ClassReaderSource innerSource) { public void transformClass(ClassHolder cls, ClassReaderSource innerSource, Diagnostics diagnostics) {
if (cls.getName().equals("javax.tools.ToolProvider")) { if (cls.getName().equals("javax.tools.ToolProvider")) {
MethodHolder method = cls.getMethod(new MethodDescriptor("getSystemJavaCompiler", MethodHolder method = cls.getMethod(new MethodDescriptor("getSystemJavaCompiler",
ValueType.object("javax.tools.JavaCompiler"))); ValueType.object("javax.tools.JavaCompiler")));

View File

@ -17,6 +17,7 @@ package org.teavm.dependency;
import org.teavm.common.ServiceRepository; import org.teavm.common.ServiceRepository;
import org.teavm.model.ClassHolder; import org.teavm.model.ClassHolder;
import org.teavm.model.Diagnostics;
import org.teavm.model.FieldReference; import org.teavm.model.FieldReference;
import org.teavm.model.MethodReference; import org.teavm.model.MethodReference;
@ -38,4 +39,6 @@ public interface DependencyAgent extends DependencyInfo, ServiceRepository {
ClassDependency linkClass(String className, final DependencyStack stack); ClassDependency linkClass(String className, final DependencyStack stack);
FieldDependency linkField(FieldReference fieldRef, DependencyStack stack); FieldDependency linkField(FieldReference fieldRef, DependencyStack stack);
Diagnostics getDiagnostics();
} }

View File

@ -50,9 +50,12 @@ public class DependencyChecker implements DependencyInfo, DependencyAgent {
private DependencyViolations dependencyViolations; private DependencyViolations dependencyViolations;
private DependencyCheckerInterruptor interruptor; private DependencyCheckerInterruptor interruptor;
private boolean interrupted; private boolean interrupted;
private Diagnostics diagnostics;
public DependencyChecker(ClassReaderSource classSource, ClassLoader classLoader, ServiceRepository services) { public DependencyChecker(ClassReaderSource classSource, ClassLoader classLoader, ServiceRepository services,
this.classSource = new DependencyClassSource(classSource); Diagnostics diagnostics) {
this.diagnostics = diagnostics;
this.classSource = new DependencyClassSource(classSource, diagnostics);
this.classLoader = classLoader; this.classLoader = classLoader;
this.services = services; this.services = services;
methodReaderCache = new CachedMapper<>(new Mapper<MethodReference, MethodReader>() { methodReaderCache = new CachedMapper<>(new Mapper<MethodReference, MethodReader>() {
@ -431,23 +434,23 @@ public class DependencyChecker implements DependencyInfo, DependencyAgent {
return methodCache.getKnown(methodRef); return methodCache.getKnown(methodRef);
} }
public DependencyViolations getDependencyViolations() { public DependencyViolations getViolations() {
if (dependencyViolations == null) { if (dependencyViolations == null) {
dependencyViolations = new DependencyViolations(missingMethods, missingClasses, missingFields); dependencyViolations = new DependencyViolations(missingMethods, missingClasses, missingFields);
} }
return dependencyViolations; return dependencyViolations;
} }
public void checkForMissingItems() { public void checkForViolations() {
getDependencyViolations().checkForMissingItems(); getViolations().checkForViolations();
} }
public boolean hasMissingItems() { public boolean hasViolations() {
return getDependencyViolations().hasMissingItems(); return getViolations().hasSevereViolations();
} }
public void showMissingItems(Appendable sb) throws IOException { public void showViolations(Appendable sb) throws IOException {
getDependencyViolations().showMissingItems(sb); getViolations().showViolations(sb);
} }
public void processDependencies() { public void processDependencies() {
@ -469,4 +472,9 @@ public class DependencyChecker implements DependencyInfo, DependencyAgent {
public <T> T getService(Class<T> type) { public <T> T getService(Class<T> type) {
return services.getService(type); return services.getService(type);
} }
@Override
public Diagnostics getDiagnostics() {
return diagnostics;
}
} }

View File

@ -22,10 +22,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import org.teavm.common.CachedMapper; import org.teavm.common.CachedMapper;
import org.teavm.common.Mapper; import org.teavm.common.Mapper;
import org.teavm.model.ClassHolder; import org.teavm.model.*;
import org.teavm.model.ClassHolderTransformer;
import org.teavm.model.ClassReader;
import org.teavm.model.ClassReaderSource;
import org.teavm.model.util.ModelUtils; import org.teavm.model.util.ModelUtils;
/** /**
@ -34,6 +31,7 @@ import org.teavm.model.util.ModelUtils;
*/ */
class DependencyClassSource implements ClassReaderSource { class DependencyClassSource implements ClassReaderSource {
private ClassReaderSource innerSource; private ClassReaderSource innerSource;
private Diagnostics diagnostics;
private ConcurrentMap<String, ClassHolder> generatedClasses = new ConcurrentHashMap<>(); private ConcurrentMap<String, ClassHolder> generatedClasses = new ConcurrentHashMap<>();
private List<ClassHolderTransformer> transformers = new ArrayList<>(); private List<ClassHolderTransformer> transformers = new ArrayList<>();
private CachedMapper<String, ClassReader> cache = new CachedMapper<>( private CachedMapper<String, ClassReader> cache = new CachedMapper<>(
@ -43,8 +41,9 @@ class DependencyClassSource implements ClassReaderSource {
} }
}); });
public DependencyClassSource(ClassReaderSource innerSource) { public DependencyClassSource(ClassReaderSource innerSource, Diagnostics diagnostics) {
this.innerSource = innerSource; this.innerSource = innerSource;
this.diagnostics = diagnostics;
} }
@Override @Override
@ -65,7 +64,7 @@ class DependencyClassSource implements ClassReaderSource {
ClassHolder cls = findClass(name); ClassHolder cls = findClass(name);
if (cls != null && !transformers.isEmpty()) { if (cls != null && !transformers.isEmpty()) {
for (ClassHolderTransformer transformer : transformers) { for (ClassHolderTransformer transformer : transformers) {
transformer.transformClass(cls, innerSource); transformer.transformClass(cls, innerSource, diagnostics);
} }
cls = ModelUtils.copyClass(cls); cls = ModelUtils.copyClass(cls);
} }

View File

@ -0,0 +1,63 @@
/*
* 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.dependency;
import java.util.ArrayList;
import java.util.List;
import org.teavm.model.Diagnostics;
import org.teavm.model.InstructionLocation;
import org.teavm.vm.DiagnosticsProblem;
import org.teavm.vm.DiagnosticsProblemSeverity;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
class DependencyDiagnostics implements Diagnostics {
private List<DiagnosticsProblem> problems = new ArrayList<>();
private List<DiagnosticsProblem> severeProblems = new ArrayList<>();
@Override
public void error(InstructionLocation location, String error) {
DiagnosticsProblem violation = new DiagnosticsProblem(DiagnosticsProblemSeverity.ERROR, location, error);
problems.add(violation);
severeProblems.add(violation);
}
@Override
public void error(String error) {
error(null, error);
}
@Override
public void warning(InstructionLocation location, String error) {
DiagnosticsProblem violation = new DiagnosticsProblem(DiagnosticsProblemSeverity.WARNING, location, error);
problems.add(violation);
}
@Override
public void warning(String error) {
warning(null, error);
}
public List<DiagnosticsProblem> getProblems() {
return problems;
}
public List<DiagnosticsProblem> getSevereProblems() {
return severeProblems;
}
}

View File

@ -47,24 +47,24 @@ public class DependencyViolations {
return missingFields; return missingFields;
} }
public boolean hasMissingItems() { public boolean hasSevereViolations() {
return !missingMethods.isEmpty() || !missingClasses.isEmpty() || !missingFields.isEmpty(); return !missingMethods.isEmpty() || !missingClasses.isEmpty() || !missingFields.isEmpty();
} }
public void checkForMissingItems() { public void checkForViolations() {
if (!hasMissingItems()) { if (!hasSevereViolations()) {
return; return;
} }
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
try { try {
showMissingItems(sb); showViolations(sb);
} catch (IOException e) { } catch (IOException e) {
throw new AssertionError("StringBuilder should not throw IOException"); throw new AssertionError("StringBuilder should not throw IOException");
} }
throw new IllegalStateException(sb.toString()); throw new IllegalStateException(sb.toString());
} }
public void showMissingItems(Appendable sb) throws IOException { public void showViolations(Appendable sb) throws IOException {
List<String> items = new ArrayList<>(); List<String> items = new ArrayList<>();
Map<String, DependencyStack> stackMap = new HashMap<>(); Map<String, DependencyStack> stackMap = new HashMap<>();
for (ClassDependencyInfo cls : missingClasses) { for (ClassDependencyInfo cls : missingClasses) {
@ -80,9 +80,9 @@ public class DependencyViolations {
items.add(field.getReference().toString()); items.add(field.getReference().toString());
} }
Collections.sort(items); Collections.sort(items);
sb.append("Can't compile due to the following items missing:\n"); sb.append("Can't compile due to the following violations:\n");
for (String item : items) { for (String item : items) {
sb.append(" ").append(item).append("\n"); sb.append("Missing ").append(item).append("\n");
DependencyStack stack = stackMap.get(item); DependencyStack stack = stackMap.get(item);
if (stack == null) { if (stack == null) {
sb.append(" at unknown location\n"); sb.append(" at unknown location\n");

View File

@ -26,7 +26,7 @@ import org.teavm.model.instructions.NullCheckInstruction;
*/ */
public class NullPointerExceptionTransformer implements ClassHolderTransformer { public class NullPointerExceptionTransformer implements ClassHolderTransformer {
@Override @Override
public void transformClass(ClassHolder cls, ClassReaderSource innerSource) { public void transformClass(ClassHolder cls, ClassReaderSource innerSource, Diagnostics diagnostics) {
for (MethodHolder method : cls.getMethods()) { for (MethodHolder method : cls.getMethods()) {
Program program = method.getProgram(); Program program = method.getProgram();
if (program == null) { if (program == null) {

View File

@ -15,10 +15,11 @@
*/ */
package org.teavm.model; package org.teavm.model;
/** /**
* *
* @author Alexey Andreev * @author Alexey Andreev
*/ */
public interface ClassHolderTransformer { public interface ClassHolderTransformer {
void transformClass(ClassHolder cls, ClassReaderSource innerSource); void transformClass(ClassHolder cls, ClassReaderSource innerSource, Diagnostics diagnostics);
} }

View File

@ -0,0 +1,31 @@
/*
* 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.model;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public interface Diagnostics {
void error(InstructionLocation location, String error);
void error(String error);
void warning(InstructionLocation location, String error);
void warning(String error);
}

View File

@ -345,12 +345,12 @@ public class TeaVMTool {
} }
} }
public DependencyViolations getDependencyViolations() { public DependencyViolations getViolations() {
return vm.getDependencyViolations(); return vm.getViolations();
} }
public void checkForMissingItems() { public void checkForMissingItems() {
vm.checkForMissingItems(); vm.checkForViolations();
} }
private void copySourceFiles() { private void copySourceFiles() {

View File

@ -0,0 +1,46 @@
/*
* 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.vm;
import org.teavm.model.InstructionLocation;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public class DiagnosticsProblem {
private DiagnosticsProblemSeverity severity;
private InstructionLocation location;
private String text;
public DiagnosticsProblem(DiagnosticsProblemSeverity severity, InstructionLocation location, String text) {
this.severity = severity;
this.location = location;
this.text = text;
}
public DiagnosticsProblemSeverity getSeverity() {
return severity;
}
public InstructionLocation getLocation() {
return location;
}
public String getText() {
return text;
}
}

View File

@ -0,0 +1,25 @@
/*
* 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.vm;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public enum DiagnosticsProblemSeverity {
ERROR,
WARNING
}

View File

@ -291,7 +291,7 @@ public class TeaVM implements TeaVMHost, ServiceRepository {
* Java class library methods. The behavior of this method before building is not specified.</p> * Java class library methods. The behavior of this method before building is not specified.</p>
*/ */
public boolean hasMissingItems() { public boolean hasMissingItems() {
return dependencyChecker.hasMissingItems(); return dependencyChecker.hasViolations();
} }
/** /**
@ -302,11 +302,11 @@ public class TeaVM implements TeaVMHost, ServiceRepository {
* @param target where to append all dependency diagnostics errors. * @param target where to append all dependency diagnostics errors.
*/ */
public void showMissingItems(Appendable target) throws IOException { public void showMissingItems(Appendable target) throws IOException {
dependencyChecker.showMissingItems(target); dependencyChecker.showViolations(target);
} }
public DependencyViolations getDependencyViolations() { public DependencyViolations getViolations() {
return dependencyChecker.getDependencyViolations(); return dependencyChecker.getViolations();
} }
public Collection<String> getClasses() { public Collection<String> getClasses() {
@ -327,8 +327,8 @@ public class TeaVM implements TeaVMHost, ServiceRepository {
* This can happen when you forgot some items in class path or when your code uses unimplemented * This can happen when you forgot some items in class path or when your code uses unimplemented
* Java class library methods. The behavior of this method before building is not specified.</p> * Java class library methods. The behavior of this method before building is not specified.</p>
*/ */
public void checkForMissingItems() { public void checkForViolations() {
dependencyChecker.checkForMissingItems(); dependencyChecker.checkForViolations();
} }
public DebugInformationEmitter getDebugEmitter() { public DebugInformationEmitter getDebugEmitter() {
@ -344,7 +344,7 @@ public class TeaVM implements TeaVMHost, ServiceRepository {
* are specified. This method may fail if there are items (classes, methods and fields) * are specified. This method may fail if there are items (classes, methods and fields)
* that are required by entry points, but weren't found in classpath. In this case no * that are required by entry points, but weren't found in classpath. In this case no
* actual generation happens and no exceptions thrown, but you can further call * actual generation happens and no exceptions thrown, but you can further call
* {@link #checkForMissingItems()} or {@link #hasMissingItems()} to learn the build state.</p> * {@link #checkForViolations()} or {@link #hasMissingItems()} to learn the build state.</p>
* *
* @param writer where to generate JavaScript. Should not be null. * @param writer where to generate JavaScript. Should not be null.
* @param target where to generate additional resources. Can be null, but if there are * @param target where to generate additional resources. Can be null, but if there are

View File

@ -0,0 +1,42 @@
/*
* 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.vm;
import java.util.List;
import java.util.Set;
import org.teavm.dependency.ClassDependencyInfo;
import org.teavm.dependency.FieldDependencyInfo;
import org.teavm.dependency.MethodDependencyInfo;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public interface Violations {
Set<MethodDependencyInfo> getMissingMethods();
Set<ClassDependencyInfo> getMissingClasses();
Set<FieldDependencyInfo> getMissingFields();
List<DiagnosticsProblem> getDiagnosticsProblems();
List<DiagnosticsProblem> getSevereDiagnosticsProblems();
boolean hasSevereViolations();
void checkForViolations();
}

View File

@ -24,7 +24,7 @@ import org.teavm.model.instructions.*;
*/ */
public class JCLHacks implements ClassHolderTransformer { public class JCLHacks implements ClassHolderTransformer {
@Override @Override
public void transformClass(ClassHolder cls, ClassReaderSource innerSource) { public void transformClass(ClassHolder cls, ClassReaderSource innerSource, Diagnostics diagnostics) {
if (cls.getName().equals("java.lang.Thread")) { if (cls.getName().equals("java.lang.Thread")) {
installThreadMethods(cls); installThreadMethods(cls);
} }

View File

@ -25,7 +25,7 @@ import org.teavm.model.*;
*/ */
public class JavaScriptBodyTransformer implements ClassHolderTransformer { public class JavaScriptBodyTransformer implements ClassHolderTransformer {
@Override @Override
public void transformClass(ClassHolder cls, ClassReaderSource innerSource) { public void transformClass(ClassHolder cls, ClassReaderSource innerSource, Diagnostics diagnostics) {
for (MethodHolder method : cls.getMethods()) { for (MethodHolder method : cls.getMethods()) {
if (method.getAnnotations().get(JavaScriptBody.class.getName()) != null) { if (method.getAnnotations().get(JavaScriptBody.class.getName()) != null) {
AnnotationHolder genAnnot = new AnnotationHolder(GeneratedBy.class.getName()); AnnotationHolder genAnnot = new AnnotationHolder(GeneratedBy.class.getName());

View File

@ -15,10 +15,7 @@
*/ */
package org.teavm.jso.plugin; package org.teavm.jso.plugin;
import org.teavm.model.ClassHolder; import org.teavm.model.*;
import org.teavm.model.ClassHolderTransformer;
import org.teavm.model.ClassReaderSource;
import org.teavm.model.MethodHolder;
/** /**
* *
@ -28,8 +25,9 @@ class JSObjectClassTransformer implements ClassHolderTransformer {
private ThreadLocal<JavascriptNativeProcessor> processor = new ThreadLocal<>(); private ThreadLocal<JavascriptNativeProcessor> processor = new ThreadLocal<>();
@Override @Override
public void transformClass(ClassHolder cls, ClassReaderSource innerSource) { public void transformClass(ClassHolder cls, ClassReaderSource innerSource, Diagnostics diagnostics) {
JavascriptNativeProcessor processor = getProcessor(innerSource); JavascriptNativeProcessor processor = getProcessor(innerSource);
processor.setDiagnostics(diagnostics);
processor.processClass(cls); processor.processClass(cls);
for (MethodHolder method : cls.getMethods()) { for (MethodHolder method : cls.getMethods()) {
if (method.getProgram() != null) { if (method.getProgram() != null) {

View File

@ -30,12 +30,17 @@ class JavascriptNativeProcessor {
private Program program; private Program program;
private List<Instruction> replacement = new ArrayList<>(); private List<Instruction> replacement = new ArrayList<>();
private NativeJavascriptClassRepository nativeRepos; private NativeJavascriptClassRepository nativeRepos;
private Diagnostics diagnostics;
public JavascriptNativeProcessor(ClassReaderSource classSource) { public JavascriptNativeProcessor(ClassReaderSource classSource) {
this.classSource = classSource; this.classSource = classSource;
nativeRepos = new NativeJavascriptClassRepository(classSource); nativeRepos = new NativeJavascriptClassRepository(classSource);
} }
public void setDiagnostics(Diagnostics diagnostics) {
this.diagnostics = diagnostics;
}
public void processClass(ClassHolder cls) { public void processClass(ClassHolder cls) {
Set<MethodDescriptor> preservedMethods = new HashSet<>(); Set<MethodDescriptor> preservedMethods = new HashSet<>();
for (String iface : cls.getInterfaces()) { for (String iface : cls.getInterfaces()) {
@ -90,7 +95,7 @@ class JavascriptNativeProcessor {
Variable result = invoke.getReceiver() != null ? program.createVariable() : null; Variable result = invoke.getReceiver() != null ? program.createVariable() : null;
addPropertyGet(propertyName, invoke.getInstance(), result); addPropertyGet(propertyName, invoke.getInstance(), result);
if (result != null) { if (result != null) {
result = unwrap(result, method.getResultType()); result = unwrap(insn.getLocation(), result, method.getResultType());
copyVar(result, invoke.getReceiver()); copyVar(result, invoke.getReceiver());
} }
} else if (isProperSetter(method.getDescriptor())) { } else if (isProperSetter(method.getDescriptor())) {
@ -101,11 +106,13 @@ class JavascriptNativeProcessor {
} else { } else {
propertyName = cutPrefix(method.getName(), 3); propertyName = cutPrefix(method.getName(), 3);
} }
Variable wrapped = wrapArgument(invoke.getArguments().get(0), method.parameterType(0)); Variable wrapped = wrapArgument(insn.getLocation(), invoke.getArguments().get(0),
method.parameterType(0));
addPropertySet(propertyName, invoke.getInstance(), wrapped); addPropertySet(propertyName, invoke.getInstance(), wrapped);
} else { } else {
throw new RuntimeException("Method " + invoke.getMethod() + " is not " + diagnostics.error(insn.getLocation(), "Method " + invoke.getMethod() + " is not " +
"a proper native JavaScript property declaration"); "a proper native JavaScript property declaration");
continue;
} }
} else if (method.getAnnotations().get(JSIndexer.class.getName()) != null) { } else if (method.getAnnotations().get(JSIndexer.class.getName()) != null) {
if (isProperGetIndexer(method.getDescriptor())) { if (isProperGetIndexer(method.getDescriptor())) {
@ -113,7 +120,7 @@ class JavascriptNativeProcessor {
addIndexerGet(invoke.getInstance(), wrap(invoke.getArguments().get(0), addIndexerGet(invoke.getInstance(), wrap(invoke.getArguments().get(0),
method.parameterType(0)), result); method.parameterType(0)), result);
if (result != null) { if (result != null) {
result = unwrap(result, method.getResultType()); result = unwrap(insn.getLocation(), result, method.getResultType());
copyVar(result, invoke.getReceiver()); copyVar(result, invoke.getReceiver());
} }
} else if (isProperSetIndexer(method.getDescriptor())) { } else if (isProperSetIndexer(method.getDescriptor())) {
@ -121,8 +128,9 @@ class JavascriptNativeProcessor {
Variable value = wrap(invoke.getArguments().get(1), method.parameterType(1)); Variable value = wrap(invoke.getArguments().get(1), method.parameterType(1));
addIndexerSet(invoke.getInstance(), index, value); addIndexerSet(invoke.getInstance(), index, value);
} else { } else {
throw new RuntimeException("Method " + invoke.getMethod() + " is not " + diagnostics.error(insn.getLocation(), "Method " + invoke.getMethod() + " is not " +
"a proper native JavaScript indexer declaration"); "a proper native JavaScript indexer declaration");
continue;
} }
} else { } else {
String name = method.getName(); String name = method.getName();
@ -130,16 +138,18 @@ class JavascriptNativeProcessor {
boolean isConstructor = false; boolean isConstructor = false;
if (constructorAnnot != null) { if (constructorAnnot != null) {
if (!isSupportedType(method.getResultType())) { if (!isSupportedType(method.getResultType())) {
throw new RuntimeException("Method " + invoke.getMethod() + " is not " + diagnostics.error(insn.getLocation(), "Method " + invoke.getMethod() + " is not " +
"a proper native JavaScript constructor declaration"); "a proper native JavaScript constructor declaration");
continue;
} }
AnnotationValue nameVal = constructorAnnot.getValue("value"); AnnotationValue nameVal = constructorAnnot.getValue("value");
name = nameVal != null ? constructorAnnot.getValue("value").getString() : ""; name = nameVal != null ? constructorAnnot.getValue("value").getString() : "";
if (name.isEmpty()) { if (name.isEmpty()) {
if (!method.getName().startsWith("new") || method.getName().length() == 3) { if (!method.getName().startsWith("new") || method.getName().length() == 3) {
throw new RuntimeException("Method " + invoke.getMethod() + " is not " + diagnostics.error(insn.getLocation(), "Method " + invoke.getMethod() + " is not " +
"declared as a native JavaScript constructor, but its name does " + "declared as a native JavaScript constructor, but its name does " +
"not satisfy conventions"); "not satisfy conventions");
continue;
} }
name = method.getName().substring(3); name = method.getName().substring(3);
} }
@ -153,14 +163,16 @@ class JavascriptNativeProcessor {
} }
} }
if (method.getResultType() != ValueType.VOID && !isSupportedType(method.getResultType())) { if (method.getResultType() != ValueType.VOID && !isSupportedType(method.getResultType())) {
throw new RuntimeException("Method " + invoke.getMethod() + " is not " + diagnostics.error(insn.getLocation(), "Method " + invoke.getMethod() + " is not " +
"a proper native JavaScript method declaration"); "a proper native JavaScript method declaration");
continue;
} }
} }
for (ValueType arg : method.getParameterTypes()) { for (ValueType arg : method.getParameterTypes()) {
if (!isSupportedType(arg)) { if (!isSupportedType(arg)) {
throw new RuntimeException("Method " + invoke.getMethod() + " is not " + diagnostics.error(insn.getLocation(), "Method " + invoke.getMethod() + " is not " +
"a proper native JavaScript method or constructor declaration"); "a proper native JavaScript method or constructor declaration");
continue;
} }
} }
Variable result = invoke.getReceiver() != null ? program.createVariable() : null; Variable result = invoke.getReceiver() != null ? program.createVariable() : null;
@ -174,12 +186,13 @@ class JavascriptNativeProcessor {
newInvoke.getArguments().add(invoke.getInstance()); newInvoke.getArguments().add(invoke.getInstance());
newInvoke.getArguments().add(addStringWrap(addString(name))); newInvoke.getArguments().add(addStringWrap(addString(name)));
for (int k = 0; k < invoke.getArguments().size(); ++k) { for (int k = 0; k < invoke.getArguments().size(); ++k) {
Variable arg = wrapArgument(invoke.getArguments().get(k), method.parameterType(k)); Variable arg = wrapArgument(insn.getLocation(), invoke.getArguments().get(k),
method.parameterType(k));
newInvoke.getArguments().add(arg); newInvoke.getArguments().add(arg);
} }
replacement.add(newInvoke); replacement.add(newInvoke);
if (result != null) { if (result != null) {
result = unwrap(result, method.getResultType()); result = unwrap(insn.getLocation(), result, method.getResultType());
copyVar(result, invoke.getReceiver()); copyVar(result, invoke.getReceiver());
} }
} }
@ -254,7 +267,7 @@ class JavascriptNativeProcessor {
return var; return var;
} }
private Variable unwrap(Variable var, ValueType type) { private Variable unwrap(InstructionLocation location, Variable var, ValueType type) {
if (type instanceof ValueType.Primitive) { if (type instanceof ValueType.Primitive) {
switch (((ValueType.Primitive)type).getKind()) { switch (((ValueType.Primitive)type).getKind()) {
case BOOLEAN: case BOOLEAN:
@ -290,7 +303,8 @@ class JavascriptNativeProcessor {
return result; return result;
} }
} }
throw new IllegalArgumentException("Unsupported type: " + type); diagnostics.error(location, "Unsupported type: " + type);
return var;
} }
private Variable unwrap(Variable var, String methodName, ValueType resultType) { private Variable unwrap(Variable var, String methodName, ValueType resultType) {
@ -305,20 +319,21 @@ class JavascriptNativeProcessor {
return result; return result;
} }
private Variable wrapArgument(Variable var, ValueType type) { private Variable wrapArgument(InstructionLocation location, Variable var, ValueType type) {
if (type instanceof ValueType.Object) { if (type instanceof ValueType.Object) {
String className = ((ValueType.Object)type).getClassName(); String className = ((ValueType.Object)type).getClassName();
ClassReader cls = classSource.get(className); ClassReader cls = classSource.get(className);
if (cls.getAnnotations().get(JSFunctor.class.getName()) != null) { if (cls.getAnnotations().get(JSFunctor.class.getName()) != null) {
return wrapFunctor(var, cls); return wrapFunctor(location, var, cls);
} }
} }
return wrap(var, type); return wrap(var, type);
} }
private Variable wrapFunctor(Variable var, ClassReader type) { private Variable wrapFunctor(InstructionLocation location, Variable var, ClassReader type) {
if (!type.hasModifier(ElementModifier.INTERFACE) || type.getMethods().size() != 1) { if (!type.hasModifier(ElementModifier.INTERFACE) || type.getMethods().size() != 1) {
throw new RuntimeException("Wrong functor: " + type.getName()); diagnostics.error(location, "Wrong functor: " + type.getName());
return var;
} }
String name = type.getMethods().iterator().next().getName(); String name = type.getMethods().iterator().next().getName();
Variable functor = program.createVariable(); Variable functor = program.createVariable();

View File

@ -25,7 +25,7 @@ import org.teavm.platform.metadata.MetadataProvider;
*/ */
class MetadataProviderTransformer implements ClassHolderTransformer { class MetadataProviderTransformer implements ClassHolderTransformer {
@Override @Override
public void transformClass(ClassHolder cls, ClassReaderSource innerSource) { public void transformClass(ClassHolder cls, ClassReaderSource innerSource, Diagnostics diagnostics) {
for (MethodHolder method : cls.getMethods()) { for (MethodHolder method : cls.getMethods()) {
AnnotationReader providerAnnot = method.getAnnotations().get(MetadataProvider.class.getName()); AnnotationReader providerAnnot = method.getAnnotations().get(MetadataProvider.class.getName());
if (providerAnnot == null) { if (providerAnnot == null) {

View File

@ -15,10 +15,7 @@
*/ */
package org.teavm.platform.plugin; package org.teavm.platform.plugin;
import org.teavm.model.ClassHolder; import org.teavm.model.*;
import org.teavm.model.ClassHolderTransformer;
import org.teavm.model.ClassReaderSource;
import org.teavm.model.MethodHolder;
import org.teavm.vm.spi.TeaVMHost; import org.teavm.vm.spi.TeaVMHost;
/** /**
@ -33,7 +30,7 @@ class ResourceAccessorTransformer implements ClassHolderTransformer {
} }
@Override @Override
public void transformClass(ClassHolder cls, ClassReaderSource innerSource) { public void transformClass(ClassHolder cls, ClassReaderSource innerSource, Diagnostics diagnostics) {
if (cls.getName().equals(ResourceAccessor.class.getName())) { if (cls.getName().equals(ResourceAccessor.class.getName())) {
ResourceAccessorGenerator generator = new ResourceAccessorGenerator(); ResourceAccessorGenerator generator = new ResourceAccessorGenerator();
for (MethodHolder method : cls.getMethods()) { for (MethodHolder method : cls.getMethods()) {

View File

@ -23,7 +23,7 @@ import org.teavm.model.*;
*/ */
class ResourceTransformer implements ClassHolderTransformer { class ResourceTransformer implements ClassHolderTransformer {
@Override @Override
public void transformClass(ClassHolder cls, ClassReaderSource innerSource) { public void transformClass(ClassHolder cls, ClassReaderSource innerSource, Diagnostics diagnostics) {
for (MethodHolder method : cls.getMethods()) { for (MethodHolder method : cls.getMethods()) {
Program program = method.getProgram(); Program program = method.getProgram();
if (program != null) { if (program != null) {