diff --git a/classlib/src/main/java/org/teavm/classlib/impl/ServiceLoaderJSSupport.java b/classlib/src/main/java/org/teavm/classlib/impl/ServiceLoaderJSSupport.java index 4108b9e96..7d67ccef0 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/ServiceLoaderJSSupport.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/ServiceLoaderJSSupport.java @@ -15,7 +15,6 @@ */ package org.teavm.classlib.impl; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.spi.Generator; import org.teavm.backend.javascript.spi.GeneratorContext; @@ -27,7 +26,7 @@ public class ServiceLoaderJSSupport implements Generator { private static final MethodDescriptor INIT_METHOD = new MethodDescriptor("", void.class); @Override - public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { + public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { var templateFactory = new JavaScriptTemplateFactory(context.getClassLoader(), context.getClassSource()); var template = templateFactory.createFromResource("org/teavm/classlib/java/util/ServiceLoader.js"); var information = context.getService(ServiceLoaderInformation.class); diff --git a/classlib/src/main/java/org/teavm/classlib/impl/reflection/ConverterInjector.java b/classlib/src/main/java/org/teavm/classlib/impl/reflection/ConverterInjector.java index 9c59dec44..ab91fb440 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/reflection/ConverterInjector.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/reflection/ConverterInjector.java @@ -15,14 +15,13 @@ */ package org.teavm.classlib.impl.reflection; -import java.io.IOException; import org.teavm.backend.javascript.spi.Injector; import org.teavm.backend.javascript.spi.InjectorContext; import org.teavm.model.MethodReference; public class ConverterInjector implements Injector { @Override - public void generate(InjectorContext context, MethodReference methodRef) throws IOException { + public void generate(InjectorContext context, MethodReference methodRef) { switch (methodRef.getName()) { case "toJava": case "fromJava": diff --git a/classlib/src/main/java/org/teavm/classlib/impl/string/JSStringConstructorGenerator.java b/classlib/src/main/java/org/teavm/classlib/impl/string/JSStringConstructorGenerator.java index cdedfd357..23da055fc 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/string/JSStringConstructorGenerator.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/string/JSStringConstructorGenerator.java @@ -15,7 +15,6 @@ */ package org.teavm.classlib.impl.string; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.spi.Generator; import org.teavm.backend.javascript.spi.GeneratorContext; @@ -23,7 +22,7 @@ import org.teavm.model.MethodReference; public class JSStringConstructorGenerator implements Generator { @Override - public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { + public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { writer.append(context.getParameterName(0)); writer.append(".").appendField(JSStringInjector.NATIVE_FIELD).ws().append("=").ws(); writer.append(context.getParameterName(1)).append(";").softNewLine(); diff --git a/classlib/src/main/java/org/teavm/classlib/impl/string/JSStringInjector.java b/classlib/src/main/java/org/teavm/classlib/impl/string/JSStringInjector.java index ac6d3a223..67b174b00 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/string/JSStringInjector.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/string/JSStringInjector.java @@ -15,7 +15,6 @@ */ package org.teavm.classlib.impl.string; -import java.io.IOException; import java.util.function.Function; import org.teavm.backend.javascript.ProviderContext; import org.teavm.backend.javascript.spi.Injector; @@ -51,7 +50,7 @@ public class JSStringInjector implements Injector, Function accessibleFields = reflection.getAccessibleFields(className); @@ -178,8 +176,7 @@ public class ClassGenerator implements Generator, Injector, DependencyPlugin { writer.outdent().append("];").softNewLine(); } - private void generateCreateMethodsForClass(GeneratorContext context, SourceWriter writer, String className) - throws IOException { + private void generateCreateMethodsForClass(GeneratorContext context, SourceWriter writer, String className) { ReflectionDependencyListener reflection = context.getService(ReflectionDependencyListener.class); Set accessibleMethods = reflection.getAccessibleMethods(className); @@ -219,7 +216,7 @@ public class ClassGenerator implements Generator, Injector, DependencyPlugin { } private void generateCreateMembers(SourceWriter writer, Iterable members, - MemberRenderer renderer) throws IOException { + MemberRenderer renderer) { boolean first = true; for (T member : members) { if (!first) { @@ -240,7 +237,7 @@ public class ClassGenerator implements Generator, Injector, DependencyPlugin { } } - private void appendProperty(SourceWriter writer, String name, boolean first, Fragment value) throws IOException { + private void appendProperty(SourceWriter writer, String name, boolean first, Fragment value) { if (!first) { writer.append(",").softNewLine(); } @@ -248,7 +245,7 @@ public class ClassGenerator implements Generator, Injector, DependencyPlugin { value.render(); } - private void renderGetter(GeneratorContext context, SourceWriter writer, FieldReader field) throws IOException { + private void renderGetter(GeneratorContext context, SourceWriter writer, FieldReader field) { writer.append("function(obj)").ws().append("{").indent().softNewLine(); initClass(context, writer, field); writer.append("return "); @@ -257,7 +254,7 @@ public class ClassGenerator implements Generator, Injector, DependencyPlugin { writer.outdent().append("}"); } - private void renderSetter(GeneratorContext context, SourceWriter writer, FieldReader field) throws IOException { + private void renderSetter(GeneratorContext context, SourceWriter writer, FieldReader field) { writer.append("function(obj,").ws().append("val)").ws().append("{").indent().softNewLine(); initClass(context, writer, field); fieldAccess(writer, field); @@ -267,7 +264,7 @@ public class ClassGenerator implements Generator, Injector, DependencyPlugin { writer.outdent().append("}"); } - private void renderCallable(GeneratorContext context, SourceWriter writer, MethodReader method) throws IOException { + private void renderCallable(GeneratorContext context, SourceWriter writer, MethodReader method) { writer.append("function(obj,").ws().append("args)").ws().append("{").indent().softNewLine(); initClass(context, writer, method); @@ -299,13 +296,13 @@ public class ClassGenerator implements Generator, Injector, DependencyPlugin { writer.outdent().append("}"); } - private void initClass(GeneratorContext context, SourceWriter writer, MemberReader member) throws IOException { + private void initClass(GeneratorContext context, SourceWriter writer, MemberReader member) { if (member.hasModifier(ElementModifier.STATIC) && context.isDynamicInitializer(member.getOwnerName())) { writer.appendClassInit(member.getOwnerName()).append("();").softNewLine(); } } - private void fieldAccess(SourceWriter writer, FieldReader field) throws IOException { + private void fieldAccess(SourceWriter writer, FieldReader field) { if (field.hasModifier(ElementModifier.STATIC)) { writer.appendStaticField(field.getReference()); } else { @@ -313,7 +310,7 @@ public class ClassGenerator implements Generator, Injector, DependencyPlugin { } } - private void boxIfNecessary(SourceWriter writer, ValueType type, Fragment fragment) throws IOException { + private void boxIfNecessary(SourceWriter writer, ValueType type, Fragment fragment) { boolean boxed = false; if (type instanceof ValueType.Primitive) { switch (((ValueType.Primitive) type).getKind()) { @@ -353,7 +350,7 @@ public class ClassGenerator implements Generator, Injector, DependencyPlugin { } } - private void unboxIfNecessary(SourceWriter writer, ValueType type, Fragment fragment) throws IOException { + private void unboxIfNecessary(SourceWriter writer, ValueType type, Fragment fragment) { boolean boxed = false; if (type instanceof ValueType.Primitive) { switch (((ValueType.Primitive) type).getKind()) { @@ -392,10 +389,10 @@ public class ClassGenerator implements Generator, Injector, DependencyPlugin { } private interface Fragment { - void render() throws IOException; + void render(); } private interface MemberRenderer { - void render(T member) throws IOException; + void render(T member); } } diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/ClassLoaderNativeGenerator.java b/classlib/src/main/java/org/teavm/classlib/java/lang/ClassLoaderNativeGenerator.java index 818fa0533..a5cb2d5a1 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/ClassLoaderNativeGenerator.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/ClassLoaderNativeGenerator.java @@ -23,7 +23,6 @@ import java.util.HashSet; import java.util.Properties; import java.util.ServiceLoader; import java.util.Set; -import org.apache.commons.io.IOUtils; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.rendering.RenderingUtil; import org.teavm.backend.javascript.spi.Injector; @@ -36,7 +35,7 @@ import org.teavm.model.MethodReference; public class ClassLoaderNativeGenerator implements Injector { @Override - public void generate(InjectorContext context, MethodReference methodRef) throws IOException { + public void generate(InjectorContext context, MethodReference methodRef) { switch (methodRef.getName()) { case "supplyResources": generateSupplyResources(context); @@ -44,7 +43,7 @@ public class ClassLoaderNativeGenerator implements Injector { } } - private void generateSupplyResources(InjectorContext context) throws IOException { + private void generateSupplyResources(InjectorContext context) { SourceWriter writer = context.getWriter(); writer.append("{").indent(); @@ -69,7 +68,7 @@ public class ClassLoaderNativeGenerator implements Injector { } first = false; writer.newLine(); - byte[] dataBytes = Base64Impl.encode(IOUtils.toByteArray(new BufferedInputStream(input)), true); + byte[] dataBytes = Base64Impl.encode(new BufferedInputStream(input).readAllBytes(), true); char[] dataChars = new char[dataBytes.length]; for (int i = 0; i < dataBytes.length; ++i) { dataChars[i] = (char) dataBytes[i]; @@ -77,6 +76,8 @@ public class ClassLoaderNativeGenerator implements Injector { RenderingUtil.writeString(writer, resource); writer.append(':').ws(); RenderingUtil.writeString(writer, new String(dataChars)); + } catch (IOException e) { + throw new RuntimeException(e); } } diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/DoubleGenerator.java b/classlib/src/main/java/org/teavm/classlib/java/lang/DoubleGenerator.java index a3e5cb238..6f0b994cc 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/DoubleGenerator.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/DoubleGenerator.java @@ -15,14 +15,13 @@ */ package org.teavm.classlib.java.lang; -import java.io.IOException; import org.teavm.backend.javascript.spi.Injector; import org.teavm.backend.javascript.spi.InjectorContext; import org.teavm.model.MethodReference; public class DoubleGenerator implements Injector { @Override - public void generate(InjectorContext context, MethodReference methodRef) throws IOException { + public void generate(InjectorContext context, MethodReference methodRef) { if (methodRef.getName().equals("doubleEqualsJs")) { context.getWriter().appendFunction("$rt_equalDoubles").append("("); context.writeExpr(context.getArgument(0)); diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/IntegerNativeGenerator.java b/classlib/src/main/java/org/teavm/classlib/java/lang/IntegerNativeGenerator.java index 6b46faa0c..c292701da 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/IntegerNativeGenerator.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/IntegerNativeGenerator.java @@ -15,14 +15,13 @@ */ package org.teavm.classlib.java.lang; -import java.io.IOException; import org.teavm.backend.javascript.spi.Injector; import org.teavm.backend.javascript.spi.InjectorContext; import org.teavm.model.MethodReference; public class IntegerNativeGenerator implements Injector { @Override - public void generate(InjectorContext context, MethodReference methodRef) throws IOException { + public void generate(InjectorContext context, MethodReference methodRef) { switch (methodRef.getName()) { case "divideUnsigned": generateRuntimeCall(context, "$rt_udiv"); @@ -37,7 +36,7 @@ public class IntegerNativeGenerator implements Injector { } - private void generateRuntimeCall(InjectorContext context, String name) throws IOException { + private void generateRuntimeCall(InjectorContext context, String name) { context.getWriter().appendFunction(name).append("("); context.writeExpr(context.getArgument(0)); context.getWriter().append(",").ws(); diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/LongNativeGenerator.java b/classlib/src/main/java/org/teavm/classlib/java/lang/LongNativeGenerator.java index 3655eb87f..5e6ed5b71 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/LongNativeGenerator.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/LongNativeGenerator.java @@ -15,7 +15,6 @@ */ package org.teavm.classlib.java.lang; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.spi.Generator; import org.teavm.backend.javascript.spi.GeneratorContext; @@ -23,7 +22,7 @@ import org.teavm.model.MethodReference; public class LongNativeGenerator implements Generator { @Override - public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { + public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { switch (methodRef.getName()) { case "compare": generateRuntimeCall(context, writer, "Long_compare"); @@ -40,7 +39,7 @@ public class LongNativeGenerator implements Generator { } } - private void generateRuntimeCall(GeneratorContext context, SourceWriter writer, String name) throws IOException { + private void generateRuntimeCall(GeneratorContext context, SourceWriter writer, String name) { writer.append("return ").appendFunction(name).append("(").append(context.getParameterName(1)) .append(",").ws() .append(context.getParameterName(2)).append(");").softNewLine(); diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/MathNativeGenerator.java b/classlib/src/main/java/org/teavm/classlib/java/lang/MathNativeGenerator.java index e98f195e4..cd9b1c137 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/MathNativeGenerator.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/MathNativeGenerator.java @@ -15,7 +15,6 @@ */ package org.teavm.classlib.java.lang; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.spi.Generator; import org.teavm.backend.javascript.spi.GeneratorContext; @@ -23,7 +22,7 @@ import org.teavm.model.MethodReference; public class MathNativeGenerator implements Generator { @Override - public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { + public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { String name = methodRef.getName(); if (name.endsWith("Impl")) { name = name.substring(0, name.length() - 4); @@ -34,8 +33,7 @@ public class MathNativeGenerator implements Generator { function(context, writer, name, methodRef.parameterCount()); } - private void function(GeneratorContext context, SourceWriter writer, String name, int paramCount) - throws IOException { + private void function(GeneratorContext context, SourceWriter writer, String name, int paramCount) { writer.append("return ").append("$rt_globals.Math").append(".").append(name).append("("); for (int i = 0; i < paramCount; ++i) { if (i > 0) { diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java b/classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java index 2c9e8f56f..cebe35f7c 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java @@ -15,7 +15,6 @@ */ package org.teavm.classlib.java.lang; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.spi.Generator; import org.teavm.backend.javascript.spi.GeneratorContext; @@ -26,12 +25,12 @@ import org.teavm.model.MethodReference; public class SystemNativeGenerator implements Generator { private JavaScriptTemplate template; - public SystemNativeGenerator(JavaScriptTemplateFactory templateFactory) throws IOException { + public SystemNativeGenerator(JavaScriptTemplateFactory templateFactory) { template = templateFactory.createFromResource("org/teavm/classlib/java/lang/System.js"); } @Override - public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { + public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { var fragment = template.builder(methodRef.getName()).withContext(context).build(); fragment.write(writer, 0); } diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java b/classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java index 1712cef98..a06e9f329 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java @@ -15,7 +15,6 @@ */ package org.teavm.classlib.java.lang.reflect; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.spi.Generator; import org.teavm.backend.javascript.spi.GeneratorContext; @@ -26,12 +25,12 @@ import org.teavm.model.MethodReference; public class ArrayNativeGenerator implements Generator { private JavaScriptTemplate template; - public ArrayNativeGenerator(JavaScriptTemplateFactory templateFactory) throws IOException { + public ArrayNativeGenerator(JavaScriptTemplateFactory templateFactory) { template = templateFactory.createFromResource("org/teavm/classlib/java/lang/reflect/Array.js"); } @Override - public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { + public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { template.builder(methodRef.getName()).withContext(context).build().write(writer, 0); } } diff --git a/core/src/main/java/org/teavm/backend/javascript/JavaScriptTarget.java b/core/src/main/java/org/teavm/backend/javascript/JavaScriptTarget.java index 33fccaac5..acb976669 100644 --- a/core/src/main/java/org/teavm/backend/javascript/JavaScriptTarget.java +++ b/core/src/main/java/org/teavm/backend/javascript/JavaScriptTarget.java @@ -458,58 +458,55 @@ public class JavaScriptTarget implements TeaVMTarget, TeaVMJavaScriptHost { for (var entry : methodInjectors.entrySet()) { renderingContext.addInjector(entry.getKey(), entry.getValue()); } - try { - printWrapperStart(sourceWriter); - for (RendererListener listener : rendererListeners) { - listener.begin(renderer, target); - } - int start = sourceWriter.getOffset(); + printWrapperStart(sourceWriter); - renderer.prepare(clsNodes); - runtimeRenderer.renderRuntime(); - sourceWriter.append("var ").append(renderer.getNaming().getScopeName()).ws().append("=").ws() - .append("Object.create(null);").newLine(); - if (!renderer.render(clsNodes)) { - return; - } - runtimeRenderer.renderHandWrittenRuntime("array.js"); - renderer.renderStringPool(); - renderer.renderStringConstants(); - renderer.renderCompatibilityStubs(); - - if (renderer.isLongLibraryUsed()) { - runtimeRenderer.renderHandWrittenRuntime("long.js"); - } - if (renderer.isThreadLibraryUsed()) { - runtimeRenderer.renderHandWrittenRuntime("thread.js"); - } else { - runtimeRenderer.renderHandWrittenRuntime("simpleThread.js"); - } - - for (var entry : controller.getEntryPoints().entrySet()) { - sourceWriter.append("$rt_exports.").append(entry.getKey()).ws().append("=").ws(); - var ref = entry.getValue().getMethod(); - sourceWriter.appendFunction("$rt_mainStarter").append("(").appendMethodBody(ref); - sourceWriter.append(");").newLine(); - sourceWriter.append("$rt_exports.").append(entry.getKey()).append(".").append("javaException") - .ws().append("=").ws().appendFunction("$rt_javaException").append(";").newLine(); - } - - for (var listener : rendererListeners) { - listener.complete(); - } - - printWrapperEnd(sourceWriter); - - int totalSize = sourceWriter.getOffset() - start; - printStats(renderer, totalSize); - } catch (IOException e) { - throw new RenderingException("IO Error occurred", e); + for (RendererListener listener : rendererListeners) { + listener.begin(renderer, target); } + int start = sourceWriter.getOffset(); + + renderer.prepare(clsNodes); + runtimeRenderer.renderRuntime(); + sourceWriter.append("var ").append(renderer.getNaming().getScopeName()).ws().append("=").ws() + .append("Object.create(null);").newLine(); + if (!renderer.render(clsNodes)) { + return; + } + runtimeRenderer.renderHandWrittenRuntime("array.js"); + renderer.renderStringPool(); + renderer.renderStringConstants(); + renderer.renderCompatibilityStubs(); + + if (renderer.isLongLibraryUsed()) { + runtimeRenderer.renderHandWrittenRuntime("long.js"); + } + if (renderer.isThreadLibraryUsed()) { + runtimeRenderer.renderHandWrittenRuntime("thread.js"); + } else { + runtimeRenderer.renderHandWrittenRuntime("simpleThread.js"); + } + + for (var entry : controller.getEntryPoints().entrySet()) { + sourceWriter.append("$rt_exports.").append(entry.getKey()).ws().append("=").ws(); + var ref = entry.getValue().getMethod(); + sourceWriter.appendFunction("$rt_mainStarter").append("(").appendMethodBody(ref); + sourceWriter.append(");").newLine(); + sourceWriter.append("$rt_exports.").append(entry.getKey()).append(".").append("javaException") + .ws().append("=").ws().appendFunction("$rt_javaException").append(";").newLine(); + } + + for (var listener : rendererListeners) { + listener.complete(); + } + + printWrapperEnd(sourceWriter); + + int totalSize = sourceWriter.getOffset() - start; + printStats(renderer, totalSize); } - private void printWrapperStart(SourceWriter writer) throws IOException { + private void printWrapperStart(SourceWriter writer) { writer.append("\"use strict\";").newLine(); printUmdStart(writer); writer.append("function($rt_globals,").ws().append("$rt_exports"); @@ -523,7 +520,7 @@ public class JavaScriptTarget implements TeaVMTarget, TeaVMJavaScriptHost { return importedModules.get(name); } - private void printUmdStart(SourceWriter writer) throws IOException { + private void printUmdStart(SourceWriter writer) { writer.append("(function(root,").ws().append("module)").appendBlockStart(); writer.appendIf().append("typeof define").ws().append("===").ws().append("'function'") .ws().append("&&").ws().append("define.amd)").appendBlockStart(); @@ -562,7 +559,7 @@ public class JavaScriptTarget implements TeaVMTarget, TeaVMJavaScriptHost { .ws(); } - private void printWrapperEnd(SourceWriter writer) throws IOException { + private void printWrapperEnd(SourceWriter writer) { writer.outdent().append("}));").newLine(); } diff --git a/core/src/main/java/org/teavm/backend/javascript/ast/AstUtil.java b/core/src/main/java/org/teavm/backend/javascript/ast/AstUtil.java index 93f072bb5..041c82e6d 100644 --- a/core/src/main/java/org/teavm/backend/javascript/ast/AstUtil.java +++ b/core/src/main/java/org/teavm/backend/javascript/ast/AstUtil.java @@ -36,9 +36,11 @@ public final class AstUtil { return factory.parse(string, null, 0); } - public static AstNode parseFromResources(ClassLoader classLoader, String path) throws IOException { + public static AstNode parseFromResources(ClassLoader classLoader, String path) { try (var input = classLoader.getResourceAsStream(path)) { return parse(new String(input.readAllBytes(), StandardCharsets.UTF_8)); + } catch (IOException e) { + throw new RuntimeException(e); } } } diff --git a/core/src/main/java/org/teavm/backend/javascript/codegen/SourceWriter.java b/core/src/main/java/org/teavm/backend/javascript/codegen/SourceWriter.java index 5e1591732..f72c5e74b 100644 --- a/core/src/main/java/org/teavm/backend/javascript/codegen/SourceWriter.java +++ b/core/src/main/java/org/teavm/backend/javascript/codegen/SourceWriter.java @@ -42,39 +42,43 @@ public class SourceWriter implements Appendable, LocationProvider { this.minified = minified; } - public SourceWriter append(String value) throws IOException { + public SourceWriter append(String value) { append((CharSequence) value); return this; } - public SourceWriter appendBlockStart() throws IOException { + public SourceWriter appendBlockStart() { return ws().append("{").indent().softNewLine(); } - public SourceWriter appendBlockEnd() throws IOException { + public SourceWriter appendBlockEnd() { return outdent().append("}").softNewLine(); } - public SourceWriter appendIf() throws IOException { + public SourceWriter appendIf() { return append("if").ws().append("("); } - public SourceWriter appendElseIf() throws IOException { + public SourceWriter appendElseIf() { return outdent().append("}").ws().append("else ").appendIf(); } - public SourceWriter appendElse() throws IOException { + public SourceWriter appendElse() { return outdent().append("}").ws().append("else").appendBlockStart(); } - public SourceWriter append(int value) throws IOException { + public SourceWriter append(int value) { return append(String.valueOf(value)); } @Override - public SourceWriter append(char value) throws IOException { + public SourceWriter append(char value) { appendIndent(); - innerWriter.append(value); + try { + innerWriter.append(value); + } catch (IOException e) { + throw new RuntimeException(e); + } if (value == '\n') { newLine(); } else { @@ -85,13 +89,13 @@ public class SourceWriter implements Appendable, LocationProvider { } @Override - public SourceWriter append(CharSequence csq) throws IOException { + public SourceWriter append(CharSequence csq) { append(csq, 0, csq.length()); return this; } @Override - public SourceWriter append(CharSequence csq, int start, int end) throws IOException { + public SourceWriter append(CharSequence csq, int start, int end) { int last = start; for (int i = start; i < end; ++i) { if (csq.charAt(i) == '\n') { @@ -104,65 +108,69 @@ public class SourceWriter implements Appendable, LocationProvider { return this; } - private void appendSingleLine(CharSequence csq, int start, int end) throws IOException { + private void appendSingleLine(CharSequence csq, int start, int end) { if (start == end) { return; } appendIndent(); column += end - start; offset += end - start; - innerWriter.append(csq, start, end); + try { + innerWriter.append(csq, start, end); + } catch (IOException e) { + throw new RuntimeException(e); + } } - public SourceWriter appendClass(String cls) throws IOException { + public SourceWriter appendClass(String cls) { return appendName(naming.getNameFor(cls)); } - public SourceWriter appendClass(Class cls) throws IOException { + public SourceWriter appendClass(Class cls) { return appendClass(cls.getName()); } - public SourceWriter appendField(FieldReference field) throws IOException { + public SourceWriter appendField(FieldReference field) { return append(naming.getNameFor(field)); } - public SourceWriter appendStaticField(FieldReference field) throws IOException { + public SourceWriter appendStaticField(FieldReference field) { return appendName(naming.getFullNameFor(field)); } - public SourceWriter appendMethod(MethodDescriptor method) throws IOException { + public SourceWriter appendMethod(MethodDescriptor method) { return append(naming.getNameFor(method)); } - public SourceWriter appendMethod(String name, Class... params) throws IOException { + public SourceWriter appendMethod(String name, Class... params) { return append(naming.getNameFor(new MethodDescriptor(name, params))); } - public SourceWriter appendMethodBody(MethodReference method) throws IOException { + public SourceWriter appendMethodBody(MethodReference method) { return appendName(naming.getFullNameFor(method)); } - public SourceWriter appendMethodBody(String className, String name, ValueType... params) throws IOException { + public SourceWriter appendMethodBody(String className, String name, ValueType... params) { return appendMethodBody(new MethodReference(className, new MethodDescriptor(name, params))); } - public SourceWriter appendMethodBody(Class cls, String name, Class... params) throws IOException { + public SourceWriter appendMethodBody(Class cls, String name, Class... params) { return appendMethodBody(new MethodReference(cls, name, params)); } - public SourceWriter appendFunction(String name) throws IOException { + public SourceWriter appendFunction(String name) { return append(naming.getNameForFunction(name)); } - public SourceWriter appendInit(MethodReference method) throws IOException { + public SourceWriter appendInit(MethodReference method) { return appendName(naming.getNameForInit(method)); } - public SourceWriter appendClassInit(String className) throws IOException { + public SourceWriter appendClassInit(String className) { return appendName(naming.getNameForClassInit(className)); } - private SourceWriter appendName(ScopedName name) throws IOException { + private SourceWriter appendName(ScopedName name) { if (name.scoped) { append(naming.getScopeName()).append("."); } @@ -170,22 +178,30 @@ public class SourceWriter implements Appendable, LocationProvider { return this; } - private void appendIndent() throws IOException { + private void appendIndent() { if (minified) { return; } if (lineStart) { - for (int i = 0; i < indentSize; ++i) { - innerWriter.append(" "); - column += 4; - offset += 4; + try { + for (int i = 0; i < indentSize; ++i) { + innerWriter.append(" "); + column += 4; + offset += 4; + } + lineStart = false; + } catch (IOException e) { + throw new RuntimeException(e); } - lineStart = false; } } - public SourceWriter newLine() throws IOException { - innerWriter.append('\n'); + public SourceWriter newLine() { + try { + innerWriter.append('\n'); + } catch (IOException e) { + throw new RuntimeException(e); + } column = 0; ++line; ++offset; @@ -193,12 +209,16 @@ public class SourceWriter implements Appendable, LocationProvider { return this; } - public SourceWriter ws() throws IOException { + public SourceWriter ws() { if (column >= lineWidth) { newLine(); } else { if (!minified) { - innerWriter.append(' '); + try { + innerWriter.append(' '); + } catch (IOException e) { + throw new RuntimeException(e); + } column++; offset++; } @@ -206,16 +226,20 @@ public class SourceWriter implements Appendable, LocationProvider { return this; } - public SourceWriter tokenBoundary() throws IOException { + public SourceWriter tokenBoundary() { if (column >= lineWidth) { newLine(); } return this; } - public SourceWriter softNewLine() throws IOException { + public SourceWriter softNewLine() { if (!minified) { - innerWriter.append('\n'); + try { + innerWriter.append('\n'); + } catch (IOException e) { + throw new RuntimeException(e); + } column = 0; ++offset; ++line; diff --git a/core/src/main/java/org/teavm/backend/javascript/intrinsics/ref/ReferenceQueueGenerator.java b/core/src/main/java/org/teavm/backend/javascript/intrinsics/ref/ReferenceQueueGenerator.java index 6866f55c7..25cb3c669 100644 --- a/core/src/main/java/org/teavm/backend/javascript/intrinsics/ref/ReferenceQueueGenerator.java +++ b/core/src/main/java/org/teavm/backend/javascript/intrinsics/ref/ReferenceQueueGenerator.java @@ -15,7 +15,6 @@ */ package org.teavm.backend.javascript.intrinsics.ref; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.spi.Generator; import org.teavm.backend.javascript.spi.GeneratorContext; @@ -27,7 +26,7 @@ public class ReferenceQueueGenerator implements Generator { private JavaScriptTemplate template; @Override - public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { + public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { ensureTemplate(context); switch (methodRef.getName()) { case "": @@ -39,7 +38,7 @@ public class ReferenceQueueGenerator implements Generator { } } - private void ensureTemplate(GeneratorContext context) throws IOException { + private void ensureTemplate(GeneratorContext context) { if (template == null) { template = new JavaScriptTemplateFactory(context.getClassLoader(), context.getClassSource()) .createFromResource("org/teavm/classlib/java/lang/ref/ReferenceQueue.js"); diff --git a/core/src/main/java/org/teavm/backend/javascript/intrinsics/ref/WeakReferenceGenerator.java b/core/src/main/java/org/teavm/backend/javascript/intrinsics/ref/WeakReferenceGenerator.java index f4eece43d..74bdeeedf 100644 --- a/core/src/main/java/org/teavm/backend/javascript/intrinsics/ref/WeakReferenceGenerator.java +++ b/core/src/main/java/org/teavm/backend/javascript/intrinsics/ref/WeakReferenceGenerator.java @@ -15,7 +15,6 @@ */ package org.teavm.backend.javascript.intrinsics.ref; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.spi.Generator; import org.teavm.backend.javascript.spi.GeneratorContext; @@ -27,15 +26,11 @@ public class WeakReferenceGenerator implements Generator { private JavaScriptTemplate template; public WeakReferenceGenerator(JavaScriptTemplateFactory templateFactory) { - try { - template = templateFactory.createFromResource("org/teavm/classlib/java/lang/ref/WeakReference.js"); - } catch (IOException e) { - throw new RuntimeException(e); - } + template = templateFactory.createFromResource("org/teavm/classlib/java/lang/ref/WeakReference.js"); } @Override - public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { + public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { switch (methodRef.getName()) { case "": template.builder("init").withContext(context).build().write(writer, 0); diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/AstWriter.java b/core/src/main/java/org/teavm/backend/javascript/rendering/AstWriter.java index 90427ff2c..de80d2a3d 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/AstWriter.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/AstWriter.java @@ -15,7 +15,6 @@ */ package org.teavm.backend.javascript.rendering; -import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -155,19 +154,19 @@ public class AstWriter { }); } - public void print(Object node) throws IOException { + public void print(Object node) { print((AstNode) node); } - public void print(Object node, int precedence) throws IOException { + public void print(Object node, int precedence) { print((AstNode) node, precedence); } - public void print(AstNode node) throws IOException { + public void print(AstNode node) { print(node, PRECEDENCE_COMMA); } - public void print(AstNode node, int precedence) throws IOException { + public void print(AstNode node, int precedence) { switch (node.getType()) { case Token.SCRIPT: print((AstRoot) node); @@ -305,14 +304,14 @@ public class AstWriter { } } - private void print(AstRoot node) throws IOException { + private void print(AstRoot node) { for (Node child : node) { print((AstNode) child); writer.softNewLine(); } } - private void print(Block node) throws IOException { + private void print(Block node) { writer.append('{').softNewLine().indent(); for (Node child = node.getFirstChild(); child != null; child = child.getNext()) { print((AstNode) child); @@ -321,7 +320,7 @@ public class AstWriter { writer.outdent().append('}'); } - private void print(Scope node) throws IOException { + private void print(Scope node) { var scope = enterScope(node); writer.append('{').softNewLine().indent(); for (Node child = node.getFirstChild(); child != null; child = child.getNext()) { @@ -332,14 +331,14 @@ public class AstWriter { leaveScope(scope); } - private void print(LabeledStatement node) throws IOException { + private void print(LabeledStatement node) { for (Label label : node.getLabels()) { writer.append(label.getName()).append(':').ws(); } print(node.getStatement()); } - private void print(BreakStatement node) throws IOException { + private void print(BreakStatement node) { writer.append("break"); if (node.getBreakLabel() != null) { writer.append(' ').append(node.getBreakLabel().getString()); @@ -347,7 +346,7 @@ public class AstWriter { writer.append(';'); } - private void print(ContinueStatement node) throws IOException { + private void print(ContinueStatement node) { writer.append("continue"); if (node.getLabel() != null) { writer.append(' ').append(node.getLabel().getString()); @@ -355,7 +354,7 @@ public class AstWriter { writer.append(';'); } - private void print(ReturnStatement node) throws IOException { + private void print(ReturnStatement node) { writer.append("return"); if (node.getReturnValue() != null) { writer.append(' '); @@ -364,13 +363,13 @@ public class AstWriter { writer.append(';'); } - private void print(ThrowStatement node) throws IOException { + private void print(ThrowStatement node) { writer.append("throw "); print(node.getExpression()); writer.append(';'); } - private void print(DoLoop node) throws IOException { + private void print(DoLoop node) { var scope = enterScope(node); writer.append("do ").ws(); print(node.getBody()); @@ -380,7 +379,7 @@ public class AstWriter { leaveScope(scope); } - private void print(ForInLoop node) throws IOException { + private void print(ForInLoop node) { var scope = enterScope(node); writer.append("for"); if (node.isForEach()) { @@ -395,7 +394,7 @@ public class AstWriter { leaveScope(scope); } - private void print(ForLoop node) throws IOException { + private void print(ForLoop node) { var scope = enterScope(node); writer.append("for").ws().append('('); print(node.getInitializer()); @@ -408,7 +407,7 @@ public class AstWriter { leaveScope(scope); } - private void print(WhileLoop node) throws IOException { + private void print(WhileLoop node) { var scope = enterScope(node); writer.append("while").ws().append('('); print(node.getCondition()); @@ -417,7 +416,7 @@ public class AstWriter { leaveScope(scope); } - private void print(IfStatement node) throws IOException { + private void print(IfStatement node) { writer.append("if").ws().append('('); print(node.getCondition()); writer.append(')').ws(); @@ -428,7 +427,7 @@ public class AstWriter { } } - private void print(SwitchStatement node) throws IOException { + private void print(SwitchStatement node) { writer.append("switch").ws().append('('); print(node.getExpression()); writer.append(')').ws().append('{').indent().softNewLine(); @@ -452,7 +451,7 @@ public class AstWriter { writer.outdent().append('}'); } - private void print(TryStatement node) throws IOException { + private void print(TryStatement node) { writer.append("try "); print(node.getTryBlock()); for (CatchClause cc : node.getCatchClauses()) { @@ -471,7 +470,7 @@ public class AstWriter { } } - private void print(VariableDeclaration node) throws IOException { + private void print(VariableDeclaration node) { switch (node.getType()) { case Token.VAR: writer.append("var "); @@ -495,7 +494,7 @@ public class AstWriter { } } - private void print(VariableInitializer node) throws IOException { + private void print(VariableInitializer node) { print(node.getTarget()); if (node.getInitializer() != null) { writer.ws().append('=').ws(); @@ -503,19 +502,19 @@ public class AstWriter { } } - private void print(ExpressionStatement node) throws IOException { + private void print(ExpressionStatement node) { print(node.getExpression()); writer.append(';'); } - protected void print(ElementGet node) throws IOException { + protected void print(ElementGet node) { print(node.getTarget(), PRECEDENCE_MEMBER); writer.append('['); print(node.getElement()); writer.append(']'); } - public void print(PropertyGet node) throws IOException { + public void print(PropertyGet node) { print(node.getLeft(), PRECEDENCE_MEMBER); writer.append('.'); var oldRootScope = rootScope; @@ -524,7 +523,7 @@ public class AstWriter { rootScope = oldRootScope; } - private void print(FunctionCall node, int precedence) throws IOException { + private void print(FunctionCall node, int precedence) { if (intrinsic(node, precedence)) { return; } @@ -556,11 +555,11 @@ public class AstWriter { } } - protected boolean intrinsic(FunctionCall node, int precedence) throws IOException { + protected boolean intrinsic(FunctionCall node, int precedence) { return false; } - private boolean tryJavaInvocation(FunctionCall node) throws IOException { + private boolean tryJavaInvocation(FunctionCall node) { if (!(node.getTarget() instanceof PropertyGet)) { return false; } @@ -593,7 +592,7 @@ public class AstWriter { return str.substring("$$JSO$$_".length()); } - private void print(ConditionalExpression node, int precedence) throws IOException { + private void print(ConditionalExpression node, int precedence) { if (precedence < PRECEDENCE_COND) { writer.append('('); } @@ -607,7 +606,7 @@ public class AstWriter { } } - private void printList(List nodes) throws IOException { + private void printList(List nodes) { if (nodes == null || nodes.isEmpty()) { return; } @@ -618,7 +617,7 @@ public class AstWriter { } } - private void print(ArrayComprehension node) throws IOException { + private void print(ArrayComprehension node) { var scope = enterScope(node); writer.append("["); for (ArrayComprehensionLoop loop : node.getLoops()) { @@ -638,7 +637,7 @@ public class AstWriter { leaveScope(scope); } - private void print(GeneratorExpression node) throws IOException { + private void print(GeneratorExpression node) { var scope = enterScope(node); writer.append("("); for (GeneratorExpressionLoop loop : node.getLoops()) { @@ -658,17 +657,17 @@ public class AstWriter { leaveScope(scope); } - private void print(NumberLiteral node) throws IOException { + private void print(NumberLiteral node) { writer.append(node.getValue()); } - private void print(StringLiteral node) throws IOException { + private void print(StringLiteral node) { writer.append(node.getQuoteCharacter()); writer.append(ScriptRuntime.escapeString(node.getValue(), node.getQuoteCharacter())); writer.append(node.getQuoteCharacter()); } - public void print(Name node, int precedence) throws IOException { + public void print(Name node, int precedence) { var definingScope = scopeOfId(node.getIdentifier()); if (rootScope && definingScope == null) { var alias = nameMap.get(node.getIdentifier()); @@ -685,17 +684,17 @@ public class AstWriter { } } - private void print(RegExpLiteral node) throws IOException { + private void print(RegExpLiteral node) { writer.append('/').append(node.getValue()).append('/').append(node.getFlags()); } - private void print(ArrayLiteral node) throws IOException { + private void print(ArrayLiteral node) { writer.append('['); printList(node.getElements()); writer.append(']'); } - private void print(ObjectLiteral node) throws IOException { + private void print(ObjectLiteral node) { writer.append('{').ws(); if (node.getElements() != null && !node.getElements().isEmpty()) { print(node.getElements().get(0)); @@ -707,7 +706,7 @@ public class AstWriter { writer.ws().append('}'); } - private void print(ObjectProperty node) throws IOException { + private void print(ObjectProperty node) { if (node.isGetterMethod()) { writer.append("get "); } else if (node.isSetterMethod()) { @@ -723,7 +722,7 @@ public class AstWriter { print(node.getRight()); } - private void print(FunctionNode node) throws IOException { + private void print(FunctionNode node) { var scope = enterScope(node); var isArrow = node.getFunctionType() == FunctionNode.ARROW_FUNCTION; if (!isArrow) { @@ -761,7 +760,7 @@ public class AstWriter { leaveScope(scope); } - private void print(LetNode node) throws IOException { + private void print(LetNode node) { var scope = enterScope(node); writer.append("let").ws().append('('); printList(node.getVariables().getVariables()); @@ -770,11 +769,11 @@ public class AstWriter { leaveScope(scope); } - private void print(ParenthesizedExpression node, int precedence) throws IOException { + private void print(ParenthesizedExpression node, int precedence) { print(node.getExpression(), precedence); } - private void printUnary(UnaryExpression node, int precedence) throws IOException { + private void printUnary(UnaryExpression node, int precedence) { int innerPrecedence = PRECEDENCE_PREFIX; if (innerPrecedence > precedence) { @@ -797,7 +796,7 @@ public class AstWriter { } } - private void printUnary(UpdateExpression node, int precedence) throws IOException { + private void printUnary(UpdateExpression node, int precedence) { int innerPrecedence = node.isPostfix() ? PRECEDENCE_POSTFIX : PRECEDENCE_PREFIX; if (innerPrecedence > precedence) { @@ -826,7 +825,7 @@ public class AstWriter { } } - private void printInfix(InfixExpression node, int precedence) throws IOException { + private void printInfix(InfixExpression node, int precedence) { int innerPrecedence = getPrecedence(node.getType()); if (innerPrecedence > precedence) { diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/NameEmitter.java b/core/src/main/java/org/teavm/backend/javascript/rendering/NameEmitter.java index 4b811b7be..162978cc6 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/NameEmitter.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/NameEmitter.java @@ -15,8 +15,6 @@ */ package org.teavm.backend.javascript.rendering; -import java.io.IOException; - public interface NameEmitter { - void emit(int precedence) throws IOException; + void emit(int precedence); } diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/Renderer.java b/core/src/main/java/org/teavm/backend/javascript/rendering/Renderer.java index 3c3a68d1f..2936ce304 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/Renderer.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/Renderer.java @@ -17,7 +17,6 @@ package org.teavm.backend.javascript.rendering; import com.carrotsearch.hppc.ObjectIntHashMap; import com.carrotsearch.hppc.ObjectIntMap; -import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -181,48 +180,36 @@ public class Renderer implements RenderingManager { if (context.getStringPool().isEmpty()) { return; } - try { - int start = writer.getOffset(); - writer.appendFunction("$rt_stringPool").append("(["); - for (int i = 0; i < context.getStringPool().size(); ++i) { - if (i > 0) { - writer.append(',').ws(); - } - RenderingUtil.writeString(writer, context.getStringPool().get(i)); + int start = writer.getOffset(); + writer.appendFunction("$rt_stringPool").append("(["); + for (int i = 0; i < context.getStringPool().size(); ++i) { + if (i > 0) { + writer.append(',').ws(); } - writer.append("]);").newLine(); - stringPoolSize = writer.getOffset() - start; - } catch (IOException e) { - throw new RenderingException("IO error", e); + RenderingUtil.writeString(writer, context.getStringPool().get(i)); } + writer.append("]);").newLine(); + stringPoolSize = writer.getOffset() - start; } public void renderStringConstants() throws RenderingException { - try { - for (PostponedFieldInitializer initializer : postponedFieldInitializers) { - int start = writer.getOffset(); - writer.appendStaticField(initializer.field).ws().append("=").ws(); - context.constantToString(writer, initializer.value); - writer.append(";").softNewLine(); - int sz = writer.getOffset() - start; - appendClassSize(initializer.field.getClassName(), sz); - } - } catch (IOException e) { - throw new RenderingException("IO error", e); + for (PostponedFieldInitializer initializer : postponedFieldInitializers) { + int start = writer.getOffset(); + writer.appendStaticField(initializer.field).ws().append("=").ws(); + context.constantToString(writer, initializer.value); + writer.append(";").softNewLine(); + int sz = writer.getOffset() - start; + appendClassSize(initializer.field.getClassName(), sz); } } public void renderCompatibilityStubs() throws RenderingException { - try { - renderJavaStringToString(); - renderJavaObjectToString(); - renderTeaVMClass(); - } catch (IOException e) { - throw new RenderingException("IO error", e); - } + renderJavaStringToString(); + renderJavaObjectToString(); + renderTeaVMClass(); } - private void renderJavaStringToString() throws IOException { + private void renderJavaStringToString() { writer.appendClass("java.lang.String").append(".prototype.toString").ws().append("=").ws().append("()") .ws().append("=>").ws().append("{").indent().softNewLine(); writer.append("return ").appendFunction("$rt_ustr").append("(this);").softNewLine(); @@ -231,7 +218,7 @@ public class Renderer implements RenderingManager { .appendClass("java.lang.String").append(".prototype.toString;").softNewLine(); } - private void renderJavaObjectToString() throws IOException { + private void renderJavaObjectToString() { writer.appendClass("java.lang.Object").append(".prototype.toString").ws().append("=").ws() .append("()").ws().append("=>").ws().append("{").indent().softNewLine(); writer.append("return ").appendFunction("$rt_ustr").append("(") @@ -240,7 +227,7 @@ public class Renderer implements RenderingManager { writer.outdent().append("};").newLine(); } - private void renderTeaVMClass() throws IOException { + private void renderTeaVMClass() { writer.appendClass("java.lang.Object").append(".prototype.__teavm_class__").ws().append("=").ws() .append("function()").ws().append("{").indent().softNewLine(); writer.append("return ").appendFunction("$dbg_class").append("(this);").softNewLine(); @@ -282,52 +269,48 @@ public class Renderer implements RenderingManager { private void renderDeclaration(PreparedClass cls) throws RenderingException { ScopedName jsName = naming.getNameFor(cls.getName()); debugEmitter.addClass(jsName.value, cls.getName(), cls.getParentName()); - try { - List nonStaticFields = new ArrayList<>(); - List staticFields = new ArrayList<>(); - for (FieldHolder field : cls.getClassHolder().getFields()) { - if (field.getModifiers().contains(ElementModifier.STATIC)) { - staticFields.add(field); - } else { - nonStaticFields.add(field); - } - } - - if (nonStaticFields.isEmpty() && !cls.getClassHolder().getName().equals("java.lang.Object")) { - renderShortClassFunctionDeclaration(cls, jsName); + List nonStaticFields = new ArrayList<>(); + List staticFields = new ArrayList<>(); + for (FieldHolder field : cls.getClassHolder().getFields()) { + if (field.getModifiers().contains(ElementModifier.STATIC)) { + staticFields.add(field); } else { - renderFullClassFunctionDeclaration(cls, jsName, nonStaticFields); + nonStaticFields.add(field); + } + } + + if (nonStaticFields.isEmpty() && !cls.getClassHolder().getName().equals("java.lang.Object")) { + renderShortClassFunctionDeclaration(cls, jsName); + } else { + renderFullClassFunctionDeclaration(cls, jsName, nonStaticFields); + } + + for (FieldHolder field : staticFields) { + Object value = field.getInitialValue(); + if (value == null) { + value = getDefaultValue(field.getType()); + } + FieldReference fieldRef = new FieldReference(cls.getName(), field.getName()); + if (value instanceof String) { + context.lookupString((String) value); + postponedFieldInitializers.add(new PostponedFieldInitializer(fieldRef, (String) value)); + value = null; } - for (FieldHolder field : staticFields) { - Object value = field.getInitialValue(); - if (value == null) { - value = getDefaultValue(field.getType()); - } - FieldReference fieldRef = new FieldReference(cls.getName(), field.getName()); - if (value instanceof String) { - context.lookupString((String) value); - postponedFieldInitializers.add(new PostponedFieldInitializer(fieldRef, (String) value)); - value = null; - } - - ScopedName fieldName = naming.getFullNameFor(fieldRef); - if (fieldName.scoped) { - writer.append(naming.getScopeName()).append("."); - } else { - writer.append("var "); - } - writer.append(fieldName.value).ws().append("=").ws(); - context.constantToString(writer, value); - writer.append(";").softNewLine(); + ScopedName fieldName = naming.getFullNameFor(fieldRef); + if (fieldName.scoped) { + writer.append(naming.getScopeName()).append("."); + } else { + writer.append("var "); } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + writer.append(fieldName.value).ws().append("=").ws(); + context.constantToString(writer, value); + writer.append(";").softNewLine(); } } private void renderFullClassFunctionDeclaration(PreparedClass cls, ScopedName jsName, - List nonStaticFields) throws IOException { + List nonStaticFields) { boolean thisAliased = false; renderFunctionDeclaration(jsName); writer.append("()").ws().append("{").indent().softNewLine(); @@ -364,7 +347,7 @@ public class Renderer implements RenderingManager { writer.newLine(); } - private void renderShortClassFunctionDeclaration(PreparedClass cls, ScopedName jsName) throws IOException { + private void renderShortClassFunctionDeclaration(PreparedClass cls, ScopedName jsName) { if (jsName.scoped) { writer.append(naming.getScopeName()).append("."); } else { @@ -381,34 +364,31 @@ public class Renderer implements RenderingManager { private void renderMethodBodies(PreparedClass cls) throws RenderingException { debugEmitter.emitClass(cls.getName()); - try { - MethodReader clinit = classSource.get(cls.getName()).getMethod(CLINIT_METHOD); - if (clinit != null && context.isDynamicInitializer(cls.getName())) { - renderCallClinit(clinit, cls); - } - if (!cls.getClassHolder().hasModifier(ElementModifier.INTERFACE) - && !cls.getClassHolder().hasModifier(ElementModifier.ABSTRACT)) { - for (PreparedMethod method : cls.getMethods()) { - if (!method.methodHolder.getModifiers().contains(ElementModifier.STATIC)) { - if (method.reference.getName().equals("")) { - renderInitializer(method); - } + MethodReader clinit = classSource.get(cls.getName()).getMethod(CLINIT_METHOD); + + if (clinit != null && context.isDynamicInitializer(cls.getName())) { + renderCallClinit(clinit, cls); + } + if (!cls.getClassHolder().hasModifier(ElementModifier.INTERFACE) + && !cls.getClassHolder().hasModifier(ElementModifier.ABSTRACT)) { + for (PreparedMethod method : cls.getMethods()) { + if (!method.methodHolder.getModifiers().contains(ElementModifier.STATIC)) { + if (method.reference.getName().equals("")) { + renderInitializer(method); } } } - - for (PreparedMethod method : cls.getMethods()) { - renderBody(method); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); } + + for (PreparedMethod method : cls.getMethods()) { + renderBody(method); + } + debugEmitter.emitClass(null); } - private void renderCallClinit(MethodReader clinit, PreparedClass cls) - throws IOException { + private void renderCallClinit(MethodReader clinit, PreparedClass cls) { boolean isAsync = asyncMethods.contains(clinit.getReference()); ScopedName className = naming.getNameFor(cls.getName()); @@ -470,7 +450,7 @@ public class Renderer implements RenderingManager { writer.newLine(); } - private void renderEraseClinit(PreparedClass cls) throws IOException { + private void renderEraseClinit(PreparedClass cls) { writer.appendClassInit(cls.getName()).ws().append("=").ws() .appendFunction("$rt_eraseClinit").append("(") .appendClass(cls.getName()).append(");").softNewLine(); @@ -484,25 +464,21 @@ public class Renderer implements RenderingManager { ClassMetadataRequirements metadataRequirements = new ClassMetadataRequirements(context.getDependencyInfo()); int start = writer.getOffset(); - try { - writer.appendFunction("$rt_packages").append("(["); - ObjectIntMap packageIndexes = generatePackageMetadata(classes, metadataRequirements); - writer.append("]);").newLine(); - for (int i = 0; i < classes.size(); i += 50) { - int j = Math.min(i + 50, classes.size()); - renderClassMetadataPortion(classes.subList(i, j), packageIndexes, metadataRequirements); - } + writer.appendFunction("$rt_packages").append("(["); + ObjectIntMap packageIndexes = generatePackageMetadata(classes, metadataRequirements); + writer.append("]);").newLine(); - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + for (int i = 0; i < classes.size(); i += 50) { + int j = Math.min(i + 50, classes.size()); + renderClassMetadataPortion(classes.subList(i, j), packageIndexes, metadataRequirements); } metadataSize = writer.getOffset() - start; } private void renderClassMetadataPortion(List classes, ObjectIntMap packageIndexes, - ClassMetadataRequirements metadataRequirements) throws IOException { + ClassMetadataRequirements metadataRequirements) { writer.appendFunction("$rt_metadata").append("(["); boolean first = true; for (PreparedClass cls : classes) { @@ -597,7 +573,7 @@ public class Renderer implements RenderingManager { } private ObjectIntMap generatePackageMetadata(List classes, - ClassMetadataRequirements metadataRequirements) throws IOException { + ClassMetadataRequirements metadataRequirements) { PackageNode root = new PackageNode(null); for (PreparedClass classNode : classes) { @@ -621,8 +597,7 @@ public class Renderer implements RenderingManager { return indexes; } - private int writePackageStructure(PackageNode node, int startIndex, String prefix, ObjectIntMap indexes) - throws IOException { + private int writePackageStructure(PackageNode node, int startIndex, String prefix, ObjectIntMap indexes) { int index = startIndex; for (PackageNode child : node.children.values()) { if (index >= 0) { @@ -738,7 +713,7 @@ public class Renderer implements RenderingManager { return null; } - private void renderInitializer(PreparedMethod method) throws IOException { + private void renderInitializer(PreparedMethod method) { MethodReference ref = method.reference; debugEmitter.emitMethod(ref.getDescriptor()); ScopedName name = naming.getNameForInit(ref); @@ -774,7 +749,7 @@ public class Renderer implements RenderingManager { return minifying ? RenderingUtil.indexToId(index) : "var_" + index; } - private void renderVirtualDeclarations(Collection methods) throws IOException { + private void renderVirtualDeclarations(Collection methods) { if (methods.stream().noneMatch(this::isVirtual)) { writer.append('0'); return; @@ -797,14 +772,14 @@ public class Renderer implements RenderingManager { writer.append("]"); } - private void emitVirtualDeclaration(MethodReference ref) throws IOException { + private void emitVirtualDeclaration(MethodReference ref) { String methodName = naming.getNameFor(ref.getDescriptor()); writer.append("\"").append(methodName).append("\""); writer.append(",").ws(); emitVirtualFunctionWrapper(ref); } - private void emitVirtualFunctionWrapper(MethodReference method) throws IOException { + private void emitVirtualFunctionWrapper(MethodReference method) { if (method.parameterCount() <= 4) { writer.appendFunction("$rt_wrapFunction" + method.parameterCount()); writer.append("(").appendMethodBody(method).append(")"); @@ -834,7 +809,7 @@ public class Renderer implements RenderingManager { writer.append(");").ws().append("}"); } - private void renderBody(PreparedMethod method) throws IOException { + private void renderBody(PreparedMethod method) { StatementRenderer statementRenderer = new StatementRenderer(context, writer); statementRenderer.setCurrentMethod(method.node); @@ -886,7 +861,7 @@ public class Renderer implements RenderingManager { return body instanceof ReturnStatement && ((ReturnStatement) body).getResult() == null; } - private void renderFunctionDeclaration(ScopedName name) throws IOException { + private void renderFunctionDeclaration(ScopedName name) { if (name.scoped) { writer.append(naming.getScopeName()).append(".").append(name.value).ws().append("=").ws(); } @@ -896,14 +871,14 @@ public class Renderer implements RenderingManager { } } - private void renderAsyncPrologue() throws IOException { + private void renderAsyncPrologue() { writer.append(context.mainLoopName()).append(":").ws().append("while").ws().append("(true)") .ws().append("{").ws(); writer.append("switch").ws().append("(").append(context.pointerName()).append(")").ws() .append('{').softNewLine(); } - private void renderAsyncEpilogue() throws IOException { + private void renderAsyncEpilogue() { writer.append("default:").ws().appendFunction("$rt_invalidPointer").append("();").softNewLine(); writer.append("}}").softNewLine(); } @@ -922,112 +897,36 @@ public class Renderer implements RenderingManager { } public void renderNative(PreparedMethod method) { - try { - this.async = method.async; - statementRenderer.setAsync(method.async); - method.generator.generate(this, writer, method.reference); - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); - } + this.async = method.async; + statementRenderer.setAsync(method.async); + method.generator.generate(this, writer, method.reference); } @Override public void visit(RegularMethodNode method) { - try { - statementRenderer.setAsync(false); - this.async = false; - MethodReference ref = method.getReference(); - for (int i = 0; i < method.getVariables().size(); ++i) { - debugEmitter.emitVariable(new String[] { method.getVariables().get(i).getName() }, - statementRenderer.variableName(i)); - } - - int variableCount = 0; - for (VariableNode var : method.getVariables()) { - variableCount = Math.max(variableCount, var.getIndex() + 1); - } - TryCatchFinder tryCatchFinder = new TryCatchFinder(); - method.getBody().acceptVisitor(tryCatchFinder); - boolean hasTryCatch = tryCatchFinder.tryCatchFound; - List variableNames = new ArrayList<>(); - for (int i = ref.parameterCount() + 1; i < variableCount; ++i) { - variableNames.add(statementRenderer.variableName(i)); - } - if (hasTryCatch) { - variableNames.add("$$je"); - } - if (!variableNames.isEmpty()) { - writer.append("var "); - for (int i = 0; i < variableNames.size(); ++i) { - if (i > 0) { - writer.append(",").ws(); - } - writer.append(variableNames.get(i)); - } - writer.append(";").softNewLine(); - } - - statementRenderer.setEnd(true); - statementRenderer.setCurrentPart(0); - - if (method.getModifiers().contains(ElementModifier.SYNCHRONIZED)) { - writer.appendMethodBody(NameFrequencyEstimator.MONITOR_ENTER_SYNC_METHOD); - writer.append("("); - appendMonitor(statementRenderer, method); - writer.append(");").softNewLine(); - - writer.append("try").ws().append("{").softNewLine().indent(); - } - - method.getBody().acceptVisitor(statementRenderer); - - if (method.getModifiers().contains(ElementModifier.SYNCHRONIZED)) { - writer.outdent().append("}").ws().append("finally").ws().append("{").indent().softNewLine(); - - writer.appendMethodBody(NameFrequencyEstimator.MONITOR_EXIT_SYNC_METHOD); - writer.append("("); - appendMonitor(statementRenderer, method); - writer.append(");").softNewLine(); - - writer.outdent().append("}").softNewLine(); - } - - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + statementRenderer.setAsync(false); + this.async = false; + MethodReference ref = method.getReference(); + for (int i = 0; i < method.getVariables().size(); ++i) { + debugEmitter.emitVariable(new String[] { method.getVariables().get(i).getName() }, + statementRenderer.variableName(i)); } - } - @Override - public void visit(AsyncMethodNode methodNode) { - threadLibraryUsed = true; - try { - statementRenderer.setAsync(true); - this.async = true; - MethodReference ref = methodNode.getReference(); - for (int i = 0; i < methodNode.getVariables().size(); ++i) { - debugEmitter.emitVariable(new String[] { methodNode.getVariables().get(i).getName() }, - statementRenderer.variableName(i)); - } - int variableCount = 0; - for (VariableNode var : methodNode.getVariables()) { - variableCount = Math.max(variableCount, var.getIndex() + 1); - } - List variableNames = new ArrayList<>(); - for (int i = ref.parameterCount() + 1; i < variableCount; ++i) { - variableNames.add(statementRenderer.variableName(i)); - } - TryCatchFinder tryCatchFinder = new TryCatchFinder(); - for (AsyncMethodPart part : methodNode.getBody()) { - if (!tryCatchFinder.tryCatchFound) { - part.getStatement().acceptVisitor(tryCatchFinder); - } - } - boolean hasTryCatch = tryCatchFinder.tryCatchFound; - if (hasTryCatch) { - variableNames.add("$$je"); - } - variableNames.add(context.pointerName()); - variableNames.add(context.tempVarName()); + int variableCount = 0; + for (VariableNode var : method.getVariables()) { + variableCount = Math.max(variableCount, var.getIndex() + 1); + } + TryCatchFinder tryCatchFinder = new TryCatchFinder(); + method.getBody().acceptVisitor(tryCatchFinder); + boolean hasTryCatch = tryCatchFinder.tryCatchFound; + List variableNames = new ArrayList<>(); + for (int i = ref.parameterCount() + 1; i < variableCount; ++i) { + variableNames.add(statementRenderer.variableName(i)); + } + if (hasTryCatch) { + variableNames.add("$$je"); + } + if (!variableNames.isEmpty()) { writer.append("var "); for (int i = 0; i < variableNames.size(); ++i) { if (i > 0) { @@ -1036,71 +935,134 @@ public class Renderer implements RenderingManager { writer.append(variableNames.get(i)); } writer.append(";").softNewLine(); + } - int firstToSave = 0; - if (methodNode.getModifiers().contains(ElementModifier.STATIC)) { - firstToSave = 1; - } + statementRenderer.setEnd(true); + statementRenderer.setCurrentPart(0); + + if (method.getModifiers().contains(ElementModifier.SYNCHRONIZED)) { + writer.appendMethodBody(NameFrequencyEstimator.MONITOR_ENTER_SYNC_METHOD); + writer.append("("); + appendMonitor(statementRenderer, method); + writer.append(");").softNewLine(); + + writer.append("try").ws().append("{").softNewLine().indent(); + } + + method.getBody().acceptVisitor(statementRenderer); + + if (method.getModifiers().contains(ElementModifier.SYNCHRONIZED)) { + writer.outdent().append("}").ws().append("finally").ws().append("{").indent().softNewLine(); + + writer.appendMethodBody(NameFrequencyEstimator.MONITOR_EXIT_SYNC_METHOD); + writer.append("("); + appendMonitor(statementRenderer, method); + writer.append(");").softNewLine(); - String popName = minifying ? "l" : "pop"; - String pushName = minifying ? "s" : "push"; - writer.append(context.pointerName()).ws().append('=').ws().append("0;").softNewLine(); - writer.append("if").ws().append("(").appendFunction("$rt_resuming").append("())").ws() - .append("{").indent().softNewLine(); - writer.append("var ").append(context.threadName()).ws().append('=').ws() - .appendFunction("$rt_nativeThread").append("();").softNewLine(); - writer.append(context.pointerName()).ws().append('=').ws().append(context.threadName()).append(".") - .append(popName).append("();"); - for (int i = variableCount - 1; i >= firstToSave; --i) { - writer.append(statementRenderer.variableName(i)).ws().append('=').ws().append(context.threadName()) - .append(".").append(popName).append("();"); - } - writer.softNewLine(); writer.outdent().append("}").softNewLine(); + } + } - if (methodNode.getModifiers().contains(ElementModifier.SYNCHRONIZED)) { - writer.append("try").ws().append('{').indent().softNewLine(); + @Override + public void visit(AsyncMethodNode methodNode) { + threadLibraryUsed = true; + statementRenderer.setAsync(true); + this.async = true; + MethodReference ref = methodNode.getReference(); + for (int i = 0; i < methodNode.getVariables().size(); ++i) { + debugEmitter.emitVariable(new String[] { methodNode.getVariables().get(i).getName() }, + statementRenderer.variableName(i)); + } + int variableCount = 0; + for (VariableNode var : methodNode.getVariables()) { + variableCount = Math.max(variableCount, var.getIndex() + 1); + } + List variableNames = new ArrayList<>(); + for (int i = ref.parameterCount() + 1; i < variableCount; ++i) { + variableNames.add(statementRenderer.variableName(i)); + } + TryCatchFinder tryCatchFinder = new TryCatchFinder(); + for (AsyncMethodPart part : methodNode.getBody()) { + if (!tryCatchFinder.tryCatchFound) { + part.getStatement().acceptVisitor(tryCatchFinder); } - - renderAsyncPrologue(); - for (int i = 0; i < methodNode.getBody().size(); ++i) { - writer.append("case ").append(i).append(":").indent().softNewLine(); - if (i == 0 && methodNode.getModifiers().contains(ElementModifier.SYNCHRONIZED)) { - writer.appendMethodBody(NameFrequencyEstimator.MONITOR_ENTER_METHOD); - writer.append("("); - appendMonitor(statementRenderer, methodNode); - writer.append(");").softNewLine(); - statementRenderer.emitSuspendChecker(); - } - AsyncMethodPart part = methodNode.getBody().get(i); - statementRenderer.setEnd(true); - statementRenderer.setCurrentPart(i); - part.getStatement().acceptVisitor(statementRenderer); - writer.outdent(); + } + boolean hasTryCatch = tryCatchFinder.tryCatchFound; + if (hasTryCatch) { + variableNames.add("$$je"); + } + variableNames.add(context.pointerName()); + variableNames.add(context.tempVarName()); + writer.append("var "); + for (int i = 0; i < variableNames.size(); ++i) { + if (i > 0) { + writer.append(",").ws(); } - renderAsyncEpilogue(); + writer.append(variableNames.get(i)); + } + writer.append(";").softNewLine(); - if (methodNode.getModifiers().contains(ElementModifier.SYNCHRONIZED)) { - writer.outdent().append("}").ws().append("finally").ws().append('{').indent().softNewLine(); - writer.append("if").ws().append("(!").appendFunction("$rt_suspending").append("())") - .ws().append("{").indent().softNewLine(); - writer.appendMethodBody(NameFrequencyEstimator.MONITOR_EXIT_METHOD); + int firstToSave = 0; + if (methodNode.getModifiers().contains(ElementModifier.STATIC)) { + firstToSave = 1; + } + + String popName = minifying ? "l" : "pop"; + String pushName = minifying ? "s" : "push"; + writer.append(context.pointerName()).ws().append('=').ws().append("0;").softNewLine(); + writer.append("if").ws().append("(").appendFunction("$rt_resuming").append("())").ws() + .append("{").indent().softNewLine(); + writer.append("var ").append(context.threadName()).ws().append('=').ws() + .appendFunction("$rt_nativeThread").append("();").softNewLine(); + writer.append(context.pointerName()).ws().append('=').ws().append(context.threadName()).append(".") + .append(popName).append("();"); + for (int i = variableCount - 1; i >= firstToSave; --i) { + writer.append(statementRenderer.variableName(i)).ws().append('=').ws().append(context.threadName()) + .append(".").append(popName).append("();"); + } + writer.softNewLine(); + writer.outdent().append("}").softNewLine(); + + if (methodNode.getModifiers().contains(ElementModifier.SYNCHRONIZED)) { + writer.append("try").ws().append('{').indent().softNewLine(); + } + + renderAsyncPrologue(); + for (int i = 0; i < methodNode.getBody().size(); ++i) { + writer.append("case ").append(i).append(":").indent().softNewLine(); + if (i == 0 && methodNode.getModifiers().contains(ElementModifier.SYNCHRONIZED)) { + writer.appendMethodBody(NameFrequencyEstimator.MONITOR_ENTER_METHOD); writer.append("("); appendMonitor(statementRenderer, methodNode); writer.append(");").softNewLine(); - writer.outdent().append('}').softNewLine(); - writer.outdent().append('}').softNewLine(); + statementRenderer.emitSuspendChecker(); } - - writer.appendFunction("$rt_nativeThread").append("().").append(pushName).append("("); - for (int i = firstToSave; i < variableCount; ++i) { - writer.append(statementRenderer.variableName(i)).append(',').ws(); - } - writer.append(context.pointerName()).append(");"); - writer.softNewLine(); - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + AsyncMethodPart part = methodNode.getBody().get(i); + statementRenderer.setEnd(true); + statementRenderer.setCurrentPart(i); + part.getStatement().acceptVisitor(statementRenderer); + writer.outdent(); } + renderAsyncEpilogue(); + + if (methodNode.getModifiers().contains(ElementModifier.SYNCHRONIZED)) { + writer.outdent().append("}").ws().append("finally").ws().append('{').indent().softNewLine(); + writer.append("if").ws().append("(!").appendFunction("$rt_suspending").append("())") + .ws().append("{").indent().softNewLine(); + writer.appendMethodBody(NameFrequencyEstimator.MONITOR_EXIT_METHOD); + writer.append("("); + appendMonitor(statementRenderer, methodNode); + writer.append(");").softNewLine(); + writer.outdent().append('}').softNewLine(); + writer.outdent().append('}').softNewLine(); + } + + writer.appendFunction("$rt_nativeThread").append("().").append(pushName).append("("); + for (int i = firstToSave; i < variableCount; ++i) { + writer.append(statementRenderer.variableName(i)).append(',').ws(); + } + writer.append(context.pointerName()).append(");"); + writer.softNewLine(); } @Override @@ -1155,11 +1117,7 @@ public class Renderer implements RenderingManager { @Override public void typeToClassString(SourceWriter writer, ValueType type) { - try { - context.typeToClsString(writer, type); - } catch (IOException e) { - throw new RuntimeException(e); - } + context.typeToClsString(writer, type); } @Override @@ -1178,7 +1136,7 @@ public class Renderer implements RenderingManager { } } - private void appendMonitor(StatementRenderer statementRenderer, MethodNode methodNode) throws IOException { + private void appendMonitor(StatementRenderer statementRenderer, MethodNode methodNode) { if (methodNode.getModifiers().contains(ElementModifier.STATIC)) { writer.appendFunction("$rt_cls").append("(") .appendClass(methodNode.getReference().getClassName()).append(")"); diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/RenderingContext.java b/core/src/main/java/org/teavm/backend/javascript/rendering/RenderingContext.java index f32af0897..e405ffd66 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/RenderingContext.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/RenderingContext.java @@ -15,7 +15,6 @@ */ package org.teavm.backend.javascript.rendering; -import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayDeque; @@ -222,7 +221,7 @@ public abstract class RenderingContext { return readonlyStringPool; } - public void constantToString(SourceWriter writer, Object cst) throws IOException { + public void constantToString(SourceWriter writer, Object cst) { if (cst == null) { writer.append("null"); } @@ -297,7 +296,7 @@ public abstract class RenderingContext { } } - public void typeToClsString(SourceWriter writer, ValueType type) throws IOException { + public void typeToClsString(SourceWriter writer, ValueType type) { int arrayCount = 0; while (type instanceof ValueType.Array) { arrayCount++; diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/RenderingUtil.java b/core/src/main/java/org/teavm/backend/javascript/rendering/RenderingUtil.java index 667ba3a97..5bf3f68cc 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/RenderingUtil.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/RenderingUtil.java @@ -15,7 +15,6 @@ */ package org.teavm.backend.javascript.rendering; -import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; @@ -45,7 +44,7 @@ public final class RenderingUtil { return sb.toString(); } - public static void writeString(SourceWriter writer, String s) throws IOException { + public static void writeString(SourceWriter writer, String s) { if (s.isEmpty()) { writer.append("\"\""); return; diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/RuntimeRenderer.java b/core/src/main/java/org/teavm/backend/javascript/rendering/RuntimeRenderer.java index 2197d4615..16fd1359f 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/RuntimeRenderer.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/RuntimeRenderer.java @@ -39,15 +39,11 @@ public class RuntimeRenderer { } public void renderRuntime() throws RenderingException { - try { - renderHandWrittenRuntime("runtime.js"); - renderHandWrittenRuntime("intern.js"); - } catch (IOException e) { - throw new RenderingException("IO error", e); - } + renderHandWrittenRuntime("runtime.js"); + renderHandWrittenRuntime("intern.js"); } - public void renderHandWrittenRuntime(String name) throws IOException { + public void renderHandWrittenRuntime(String name) { AstRoot ast = parseRuntime(name); ast.visit(new StringConstantElimination()); new TemplatingAstTransformer(classSource).visit(ast); @@ -56,7 +52,7 @@ public class RuntimeRenderer { astWriter.print(ast); } - private AstRoot parseRuntime(String name) throws IOException { + private AstRoot parseRuntime(String name) { CompilerEnvirons env = new CompilerEnvirons(); env.setRecoverFromErrors(true); env.setLanguageVersion(Context.VERSION_1_8); @@ -66,6 +62,8 @@ public class RuntimeRenderer { try (InputStream input = loader.getResourceAsStream("org/teavm/backend/javascript/" + name); Reader reader = new InputStreamReader(input, StandardCharsets.UTF_8)) { return factory.parse(reader, null, 0); + } catch (IOException e) { + throw new RenderingException(e); } } } diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java b/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java index 41739ad6b..35f537fc8 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java @@ -17,7 +17,6 @@ package org.teavm.backend.javascript.rendering; import com.carrotsearch.hppc.IntArrayList; import com.carrotsearch.hppc.IntIndexedContainer; -import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -146,38 +145,34 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { @Override public void visit(AssignmentStatement statement) throws RenderingException { - try { - debugEmitter.emitStatementStart(); - if (statement.getLocation() != null) { - pushLocation(statement.getLocation()); - } - prevCallSite = debugEmitter.emitCallSite(); - if (statement.getLeftValue() != null) { - if (statement.isAsync()) { - writer.append(context.tempVarName()); - } else { - precedence = Precedence.COMMA; - statement.getLeftValue().acceptVisitor(this); - } - writer.ws().append("=").ws(); - } - precedence = Precedence.COMMA; - statement.getRightValue().acceptVisitor(this); - debugEmitter.emitCallSite(); - writer.append(";").softNewLine(); + debugEmitter.emitStatementStart(); + if (statement.getLocation() != null) { + pushLocation(statement.getLocation()); + } + prevCallSite = debugEmitter.emitCallSite(); + if (statement.getLeftValue() != null) { if (statement.isAsync()) { - emitSuspendChecker(); - if (statement.getLeftValue() != null) { - precedence = Precedence.COMMA; - statement.getLeftValue().acceptVisitor(this); - writer.ws().append("=").ws().append(context.tempVarName()).append(";").softNewLine(); - } + writer.append(context.tempVarName()); + } else { + precedence = Precedence.COMMA; + statement.getLeftValue().acceptVisitor(this); } - if (statement.getLocation() != null) { - popLocation(); + writer.ws().append("=").ws(); + } + precedence = Precedence.COMMA; + statement.getRightValue().acceptVisitor(this); + debugEmitter.emitCallSite(); + writer.append(";").softNewLine(); + if (statement.isAsync()) { + emitSuspendChecker(); + if (statement.getLeftValue() != null) { + precedence = Precedence.COMMA; + statement.getLeftValue().acceptVisitor(this); + writer.ws().append("=").ws().append(context.tempVarName()).append(";").softNewLine(); } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + } + if (statement.getLocation() != null) { + popLocation(); } } @@ -188,63 +183,59 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { @Override public void visit(ConditionalStatement statement) { - try { - boolean needClosingBracket; - while (true) { - debugEmitter.emitStatementStart(); - if (statement.getCondition().getLocation() != null) { - pushLocation(statement.getCondition().getLocation()); + boolean needClosingBracket; + while (true) { + debugEmitter.emitStatementStart(); + if (statement.getCondition().getLocation() != null) { + pushLocation(statement.getCondition().getLocation()); + } + prevCallSite = debugEmitter.emitCallSite(); + writer.append("if").ws().append("("); + precedence = Precedence.COMMA; + statement.getCondition().acceptVisitor(this); + if (statement.getCondition().getLocation() != null) { + popLocation(); + } + debugEmitter.emitCallSite(); + writer.append(")"); + if (isSimpleIfContent(statement.getConsequent())) { + needClosingBracket = false; + } else { + writer.ws().append("{"); + needClosingBracket = true; + } + writer.softNewLine().indent(); + visitStatements(statement.getConsequent()); + + if (!statement.getAlternative().isEmpty()) { + writer.outdent(); + if (needClosingBracket) { + writer.append("}").ws(); } - prevCallSite = debugEmitter.emitCallSite(); - writer.append("if").ws().append("("); - precedence = Precedence.COMMA; - statement.getCondition().acceptVisitor(this); - if (statement.getCondition().getLocation() != null) { - popLocation(); + if (statement.getAlternative().size() == 1 + && statement.getAlternative().get(0) instanceof ConditionalStatement) { + statement = (ConditionalStatement) statement.getAlternative().get(0); + writer.append("else "); + continue; } - debugEmitter.emitCallSite(); - writer.append(")"); - if (isSimpleIfContent(statement.getConsequent())) { + writer.append("else"); + if (isSimpleIfContent(statement.getAlternative())) { + if (minifying) { + writer.append(" "); + } needClosingBracket = false; } else { writer.ws().append("{"); needClosingBracket = true; } - writer.softNewLine().indent(); - visitStatements(statement.getConsequent()); - - if (!statement.getAlternative().isEmpty()) { - writer.outdent(); - if (needClosingBracket) { - writer.append("}").ws(); - } - if (statement.getAlternative().size() == 1 - && statement.getAlternative().get(0) instanceof ConditionalStatement) { - statement = (ConditionalStatement) statement.getAlternative().get(0); - writer.append("else "); - continue; - } - writer.append("else"); - if (isSimpleIfContent(statement.getAlternative())) { - if (minifying) { - writer.append(" "); - } - needClosingBracket = false; - } else { - writer.ws().append("{"); - needClosingBracket = true; - } - writer.indent().softNewLine(); - visitStatements(statement.getAlternative()); - } - break; + writer.indent().softNewLine(); + visitStatements(statement.getAlternative()); } - writer.outdent(); - if (needClosingBracket) { - writer.append("}").softNewLine(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + break; + } + writer.outdent(); + if (needClosingBracket) { + writer.append("}").softNewLine(); } } @@ -259,80 +250,72 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { @Override public void visit(SwitchStatement statement) { - try { - debugEmitter.emitStatementStart(); - if (statement.getValue().getLocation() != null) { - pushLocation(statement.getValue().getLocation()); - } - if (statement.getId() != null) { - writer.append(mapBlockId(statement.getId())).append(":").ws(); - } - prevCallSite = debugEmitter.emitCallSite(); - writer.append("switch").ws().append("("); - precedence = Precedence.min(); - statement.getValue().acceptVisitor(this); - if (statement.getValue().getLocation() != null) { - popLocation(); - } - debugEmitter.emitCallSite(); - writer.append(")").ws().append("{").softNewLine().indent(); - for (SwitchClause clause : statement.getClauses()) { - for (int condition : clause.getConditions()) { - writer.append("case ").append(condition).append(":").softNewLine(); - } - writer.indent(); - boolean oldEnd = end; - for (Statement part : clause.getBody()) { - end = false; - part.acceptVisitor(this); - } - end = oldEnd; - writer.outdent(); - } - if (statement.getDefaultClause() != null) { - writer.append("default:").softNewLine().indent(); - boolean oldEnd = end; - for (Statement part : statement.getDefaultClause()) { - end = false; - part.acceptVisitor(this); - } - end = oldEnd; - writer.outdent(); - } - writer.outdent().append("}").softNewLine(); - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + debugEmitter.emitStatementStart(); + if (statement.getValue().getLocation() != null) { + pushLocation(statement.getValue().getLocation()); } - } - - @Override - public void visit(WhileStatement statement) { - try { - debugEmitter.emitStatementStart(); - if (statement.getId() != null) { - writer.append(mapBlockId(statement.getId())).append(":").ws(); + if (statement.getId() != null) { + writer.append(mapBlockId(statement.getId())).append(":").ws(); + } + prevCallSite = debugEmitter.emitCallSite(); + writer.append("switch").ws().append("("); + precedence = Precedence.min(); + statement.getValue().acceptVisitor(this); + if (statement.getValue().getLocation() != null) { + popLocation(); + } + debugEmitter.emitCallSite(); + writer.append(")").ws().append("{").softNewLine().indent(); + for (SwitchClause clause : statement.getClauses()) { + for (int condition : clause.getConditions()) { + writer.append("case ").append(condition).append(":").softNewLine(); } - writer.append("while"); - writer.ws().append("("); - if (statement.getCondition() != null) { - prevCallSite = debugEmitter.emitCallSite(); - precedence = Precedence.min(); - statement.getCondition().acceptVisitor(this); - debugEmitter.emitCallSite(); - } else { - writer.append("true"); - } - writer.append(")").ws().append("{").softNewLine().indent(); + writer.indent(); boolean oldEnd = end; - for (Statement part : statement.getBody()) { + for (Statement part : clause.getBody()) { end = false; part.acceptVisitor(this); } end = oldEnd; - writer.outdent().append("}").softNewLine(); - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + writer.outdent(); } + if (statement.getDefaultClause() != null) { + writer.append("default:").softNewLine().indent(); + boolean oldEnd = end; + for (Statement part : statement.getDefaultClause()) { + end = false; + part.acceptVisitor(this); + } + end = oldEnd; + writer.outdent(); + } + writer.outdent().append("}").softNewLine(); + } + + @Override + public void visit(WhileStatement statement) { + debugEmitter.emitStatementStart(); + if (statement.getId() != null) { + writer.append(mapBlockId(statement.getId())).append(":").ws(); + } + writer.append("while"); + writer.ws().append("("); + if (statement.getCondition() != null) { + prevCallSite = debugEmitter.emitCallSite(); + precedence = Precedence.min(); + statement.getCondition().acceptVisitor(this); + debugEmitter.emitCallSite(); + } else { + writer.append("true"); + } + writer.append(")").ws().append("{").softNewLine().indent(); + boolean oldEnd = end; + for (Statement part : statement.getBody()) { + end = false; + part.acceptVisitor(this); + } + end = oldEnd; + writer.outdent().append("}").softNewLine(); } private String mapBlockId(String id) { @@ -361,97 +344,77 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { @Override public void visit(BlockStatement statement) { - try { - writer.append(mapBlockId(statement.getId())).append(":").ws().append("{").softNewLine().indent(); - visitStatements(statement.getBody()); - writer.outdent().append("}").softNewLine(); - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); - } + writer.append(mapBlockId(statement.getId())).append(":").ws().append("{").softNewLine().indent(); + visitStatements(statement.getBody()); + writer.outdent().append("}").softNewLine(); } @Override public void visit(BreakStatement statement) { - try { - debugEmitter.emitStatementStart(); - if (statement.getLocation() != null) { - pushLocation(statement.getLocation()); - } - writer.append("break"); - if (statement.getTarget() != null) { - writer.append(' ').append(mapBlockId(statement.getTarget().getId())); - } - writer.append(";").softNewLine(); - if (statement.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + debugEmitter.emitStatementStart(); + if (statement.getLocation() != null) { + pushLocation(statement.getLocation()); + } + writer.append("break"); + if (statement.getTarget() != null) { + writer.append(' ').append(mapBlockId(statement.getTarget().getId())); + } + writer.append(";").softNewLine(); + if (statement.getLocation() != null) { + popLocation(); } } @Override public void visit(ContinueStatement statement) { - try { - debugEmitter.emitStatementStart(); - if (statement.getLocation() != null) { - pushLocation(statement.getLocation()); - } - writer.append("continue"); - if (statement.getTarget() != null) { - writer.append(' ').append(mapBlockId(statement.getTarget().getId())); - } - writer.append(";").softNewLine(); - if (statement.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + debugEmitter.emitStatementStart(); + if (statement.getLocation() != null) { + pushLocation(statement.getLocation()); + } + writer.append("continue"); + if (statement.getTarget() != null) { + writer.append(' ').append(mapBlockId(statement.getTarget().getId())); + } + writer.append(";").softNewLine(); + if (statement.getLocation() != null) { + popLocation(); } } @Override public void visit(ReturnStatement statement) { - try { - debugEmitter.emitStatementStart(); - if (statement.getLocation() != null) { - pushLocation(statement.getLocation()); - } - writer.append("return"); - if (statement.getResult() != null) { - writer.append(' '); - prevCallSite = debugEmitter.emitCallSite(); - precedence = Precedence.min(); - statement.getResult().acceptVisitor(this); - debugEmitter.emitCallSite(); - } - writer.append(";").softNewLine(); - if (statement.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + debugEmitter.emitStatementStart(); + if (statement.getLocation() != null) { + pushLocation(statement.getLocation()); + } + writer.append("return"); + if (statement.getResult() != null) { + writer.append(' '); + prevCallSite = debugEmitter.emitCallSite(); + precedence = Precedence.min(); + statement.getResult().acceptVisitor(this); + debugEmitter.emitCallSite(); + } + writer.append(";").softNewLine(); + if (statement.getLocation() != null) { + popLocation(); } } @Override public void visit(ThrowStatement statement) { - try { - debugEmitter.emitStatementStart(); - if (statement.getLocation() != null) { - pushLocation(statement.getLocation()); - } - writer.appendFunction("$rt_throw").append("("); - prevCallSite = debugEmitter.emitCallSite(); - precedence = Precedence.min(); - statement.getException().acceptVisitor(this); - writer.append(");").softNewLine(); - debugEmitter.emitCallSite(); - if (statement.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + debugEmitter.emitStatementStart(); + if (statement.getLocation() != null) { + pushLocation(statement.getLocation()); + } + writer.appendFunction("$rt_throw").append("("); + prevCallSite = debugEmitter.emitCallSite(); + precedence = Precedence.min(); + statement.getException().acceptVisitor(this); + writer.append(");").softNewLine(); + debugEmitter.emitCallSite(); + if (statement.getLocation() != null) { + popLocation(); } } @@ -465,20 +428,16 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { if (method == null) { return; } - try { - debugEmitter.emitStatementStart(); - if (statement.getLocation() != null) { - pushLocation(statement.getLocation()); - } - writer.appendClassInit(statement.getClassName()).append("();").softNewLine(); - if (statement.isAsync()) { - emitSuspendChecker(); - } - if (statement.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + debugEmitter.emitStatementStart(); + if (statement.getLocation() != null) { + pushLocation(statement.getLocation()); + } + writer.appendClassInit(statement.getClassName()).append("();").softNewLine(); + if (statement.isAsync()) { + emitSuspendChecker(); + } + if (statement.getLocation() != null) { + popLocation(); } } @@ -492,13 +451,7 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { } if (guarded) { visitBinary(BinaryOperation.OR, "|", () -> visitBinary(expr, op, false), - () -> { - try { - writer.append("0"); - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); - } - }); + () -> writer.append("0")); } else { visitBinary(expr.getOperation(), op, () -> expr.getFirstOperand().acceptVisitor(this), () -> expr.getSecondOperand().acceptVisitor(this)); @@ -509,56 +462,52 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { } private void visitBinary(BinaryOperation operation, String infixText, Runnable a, Runnable b) { - try { - Precedence outerPrecedence = precedence; - Precedence innerPrecedence = getPrecedence(operation); - if (innerPrecedence.ordinal() < outerPrecedence.ordinal()) { - writer.append('('); - } + Precedence outerPrecedence = precedence; + Precedence innerPrecedence = getPrecedence(operation); + if (innerPrecedence.ordinal() < outerPrecedence.ordinal()) { + writer.append('('); + } - switch (operation) { - case ADD: - case SUBTRACT: - case MULTIPLY: - case DIVIDE: - case AND: - case OR: - case BITWISE_AND: - case BITWISE_OR: - case BITWISE_XOR: - case LEFT_SHIFT: - case RIGHT_SHIFT: - case UNSIGNED_RIGHT_SHIFT: - precedence = innerPrecedence; - break; - default: - precedence = innerPrecedence.next(); - } - a.run(); + switch (operation) { + case ADD: + case SUBTRACT: + case MULTIPLY: + case DIVIDE: + case AND: + case OR: + case BITWISE_AND: + case BITWISE_OR: + case BITWISE_XOR: + case LEFT_SHIFT: + case RIGHT_SHIFT: + case UNSIGNED_RIGHT_SHIFT: + precedence = innerPrecedence; + break; + default: + precedence = innerPrecedence.next(); + } + a.run(); - writer.ws().append(infixText).ws(); + writer.ws().append(infixText).ws(); - switch (operation) { - case ADD: - case MULTIPLY: - case AND: - case OR: - case BITWISE_AND: - case BITWISE_OR: - case BITWISE_XOR: - precedence = innerPrecedence; - break; - default: - precedence = innerPrecedence.next(); - break; - } - b.run(); + switch (operation) { + case ADD: + case MULTIPLY: + case AND: + case OR: + case BITWISE_AND: + case BITWISE_OR: + case BITWISE_XOR: + precedence = innerPrecedence; + break; + default: + precedence = innerPrecedence.next(); + break; + } + b.run(); - if (innerPrecedence.ordinal() < outerPrecedence.ordinal()) { - writer.append(')'); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + if (innerPrecedence.ordinal() < outerPrecedence.ordinal()) { + writer.append(')'); } } @@ -600,23 +549,19 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { } private void visitBinaryFunction(BinaryExpr expr, String function) { - try { - if (expr.getLocation() != null) { - pushLocation(expr.getLocation()); - } - writer.appendFunction(function); - writer.append('('); - precedence = Precedence.min(); - expr.getFirstOperand().acceptVisitor(this); - writer.append(",").ws(); - precedence = Precedence.min(); - expr.getSecondOperand().acceptVisitor(this); - writer.append(')'); - if (expr.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + if (expr.getLocation() != null) { + pushLocation(expr.getLocation()); + } + writer.appendFunction(function); + writer.append('('); + precedence = Precedence.min(); + expr.getFirstOperand().acceptVisitor(this); + writer.append(",").ws(); + precedence = Precedence.min(); + expr.getSecondOperand().acceptVisitor(this); + writer.append(')'); + if (expr.getLocation() != null) { + popLocation(); } } @@ -764,140 +709,132 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { @Override public void visit(UnaryExpr expr) { - try { - if (expr.getLocation() != null) { - pushLocation(expr.getLocation()); - } - Precedence outerPrecedence = precedence; - switch (expr.getOperation()) { - case NOT: { - if (expr.getType() == OperationType.LONG) { - longLibraryUsed = true; - writer.appendFunction("Long_not").append("("); - precedence = Precedence.min(); - expr.getOperand().acceptVisitor(this); - writer.append(')'); - } else { - if (outerPrecedence.ordinal() > Precedence.UNARY.ordinal()) { - writer.append('('); - } - writer.append(expr.getType() == null ? "!" : "~"); - precedence = Precedence.UNARY; - expr.getOperand().acceptVisitor(this); - if (outerPrecedence.ordinal() > Precedence.UNARY.ordinal()) { - writer.append(')'); - } - } - break; - } - case NEGATE: - if (expr.getType() == OperationType.LONG) { - longLibraryUsed = true; - writer.appendFunction("Long_neg").append("("); - precedence = Precedence.min(); - expr.getOperand().acceptVisitor(this); - writer.append(')'); - } else if (expr.getType() == OperationType.INT) { - if (outerPrecedence.ordinal() > Precedence.BITWISE_OR.ordinal()) { - writer.append('('); - } - writer.append(" -"); - precedence = Precedence.UNARY; - expr.getOperand().acceptVisitor(this); - writer.ws().append("|").ws(); - writer.append("0"); - if (outerPrecedence.ordinal() > Precedence.BITWISE_OR.ordinal()) { - writer.append(')'); - } - } else { - if (outerPrecedence.ordinal() > Precedence.UNARY.ordinal()) { - writer.append('('); - } - writer.append(" -"); - precedence = Precedence.UNARY; - expr.getOperand().acceptVisitor(this); - if (outerPrecedence.ordinal() > Precedence.UNARY.ordinal()) { - writer.append(')'); - } - } - break; - case LENGTH: - precedence = Precedence.MEMBER_ACCESS; - expr.getOperand().acceptVisitor(this); - writer.append(".length"); - break; - case INT_TO_BYTE: - if (outerPrecedence.ordinal() > Precedence.BITWISE_SHIFT.ordinal()) { - writer.append('('); - } - precedence = Precedence.BITWISE_SHIFT; - expr.getOperand().acceptVisitor(this); - writer.ws().append("<<").ws().append("24").ws().append(">>").ws().append("24"); - if (outerPrecedence.ordinal() > Precedence.BITWISE_SHIFT.ordinal()) { - writer.append(')'); - } - break; - case INT_TO_SHORT: - if (outerPrecedence.ordinal() > Precedence.BITWISE_SHIFT.ordinal()) { - writer.append('('); - } - precedence = Precedence.BITWISE_SHIFT; - expr.getOperand().acceptVisitor(this); - writer.ws().append("<<").ws().append("16").ws().append(">>").ws().append("16"); - if (outerPrecedence.ordinal() > Precedence.BITWISE_SHIFT.ordinal()) { - writer.append(')'); - } - break; - case INT_TO_CHAR: - if (outerPrecedence.ordinal() > Precedence.BITWISE_AND.ordinal()) { - writer.append('('); - } - precedence = Precedence.BITWISE_AND; - expr.getOperand().acceptVisitor(this); - writer.ws().append("&").ws().append("65535"); - if (outerPrecedence.ordinal() > Precedence.BITWISE_AND.ordinal()) { - writer.append(')'); - } - break; - case NULL_CHECK: - writer.appendFunction("$rt_nullCheck").append("("); + if (expr.getLocation() != null) { + pushLocation(expr.getLocation()); + } + Precedence outerPrecedence = precedence; + switch (expr.getOperation()) { + case NOT: { + if (expr.getType() == OperationType.LONG) { + longLibraryUsed = true; + writer.appendFunction("Long_not").append("("); precedence = Precedence.min(); expr.getOperand().acceptVisitor(this); writer.append(')'); - break; + } else { + if (outerPrecedence.ordinal() > Precedence.UNARY.ordinal()) { + writer.append('('); + } + writer.append(expr.getType() == null ? "!" : "~"); + precedence = Precedence.UNARY; + expr.getOperand().acceptVisitor(this); + if (outerPrecedence.ordinal() > Precedence.UNARY.ordinal()) { + writer.append(')'); + } + } + break; } - if (expr.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + case NEGATE: + if (expr.getType() == OperationType.LONG) { + longLibraryUsed = true; + writer.appendFunction("Long_neg").append("("); + precedence = Precedence.min(); + expr.getOperand().acceptVisitor(this); + writer.append(')'); + } else if (expr.getType() == OperationType.INT) { + if (outerPrecedence.ordinal() > Precedence.BITWISE_OR.ordinal()) { + writer.append('('); + } + writer.append(" -"); + precedence = Precedence.UNARY; + expr.getOperand().acceptVisitor(this); + writer.ws().append("|").ws(); + writer.append("0"); + if (outerPrecedence.ordinal() > Precedence.BITWISE_OR.ordinal()) { + writer.append(')'); + } + } else { + if (outerPrecedence.ordinal() > Precedence.UNARY.ordinal()) { + writer.append('('); + } + writer.append(" -"); + precedence = Precedence.UNARY; + expr.getOperand().acceptVisitor(this); + if (outerPrecedence.ordinal() > Precedence.UNARY.ordinal()) { + writer.append(')'); + } + } + break; + case LENGTH: + precedence = Precedence.MEMBER_ACCESS; + expr.getOperand().acceptVisitor(this); + writer.append(".length"); + break; + case INT_TO_BYTE: + if (outerPrecedence.ordinal() > Precedence.BITWISE_SHIFT.ordinal()) { + writer.append('('); + } + precedence = Precedence.BITWISE_SHIFT; + expr.getOperand().acceptVisitor(this); + writer.ws().append("<<").ws().append("24").ws().append(">>").ws().append("24"); + if (outerPrecedence.ordinal() > Precedence.BITWISE_SHIFT.ordinal()) { + writer.append(')'); + } + break; + case INT_TO_SHORT: + if (outerPrecedence.ordinal() > Precedence.BITWISE_SHIFT.ordinal()) { + writer.append('('); + } + precedence = Precedence.BITWISE_SHIFT; + expr.getOperand().acceptVisitor(this); + writer.ws().append("<<").ws().append("16").ws().append(">>").ws().append("16"); + if (outerPrecedence.ordinal() > Precedence.BITWISE_SHIFT.ordinal()) { + writer.append(')'); + } + break; + case INT_TO_CHAR: + if (outerPrecedence.ordinal() > Precedence.BITWISE_AND.ordinal()) { + writer.append('('); + } + precedence = Precedence.BITWISE_AND; + expr.getOperand().acceptVisitor(this); + writer.ws().append("&").ws().append("65535"); + if (outerPrecedence.ordinal() > Precedence.BITWISE_AND.ordinal()) { + writer.append(')'); + } + break; + case NULL_CHECK: + writer.appendFunction("$rt_nullCheck").append("("); + precedence = Precedence.min(); + expr.getOperand().acceptVisitor(this); + writer.append(')'); + break; + } + if (expr.getLocation() != null) { + popLocation(); } } @Override public void visit(CastExpr expr) { if (context.isStrict()) { - try { - if (expr.getLocation() != null) { - pushLocation(expr.getLocation()); - } + if (expr.getLocation() != null) { + pushLocation(expr.getLocation()); + } - if (isClass(expr.getTarget(), context.getClassSource())) { - writer.appendFunction("$rt_castToClass"); - } else { - writer.appendFunction("$rt_castToInterface"); - } - writer.append("("); - precedence = Precedence.min(); - expr.getValue().acceptVisitor(this); - writer.append(",").ws(); - context.typeToClsString(writer, expr.getTarget()); - writer.append(")"); - if (expr.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + if (isClass(expr.getTarget(), context.getClassSource())) { + writer.appendFunction("$rt_castToClass"); + } else { + writer.appendFunction("$rt_castToInterface"); + } + writer.append("("); + precedence = Precedence.min(); + expr.getValue().acceptVisitor(this); + writer.append(",").ws(); + context.typeToClsString(writer, expr.getTarget()); + writer.append(")"); + if (expr.getLocation() != null) { + popLocation(); } } else { expr.getValue().acceptVisitor(this); @@ -915,76 +852,66 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { @Override public void visit(PrimitiveCastExpr expr) { - try { - if (expr.getLocation() != null) { - pushLocation(expr.getLocation()); - } - switch (expr.getSource()) { - case INT: - if (expr.getTarget() == OperationType.LONG) { - writer.appendFunction("Long_fromInt").append("("); + if (expr.getLocation() != null) { + pushLocation(expr.getLocation()); + } + switch (expr.getSource()) { + case INT: + if (expr.getTarget() == OperationType.LONG) { + writer.appendFunction("Long_fromInt").append("("); + precedence = Precedence.min(); + expr.getValue().acceptVisitor(this); + writer.append(')'); + } else { + expr.getValue().acceptVisitor(this); + } + break; + case LONG: + switch (expr.getTarget()) { + case INT: + precedence = Precedence.MEMBER_ACCESS; + Expr longShifted = extractLongRightShiftedBy32(expr.getValue()); + if (longShifted != null) { + writer.appendFunction("Long_hi").append("("); + longShifted.acceptVisitor(this); + writer.append(")"); + } else { + writer.appendFunction("Long_lo").append("("); + expr.getValue().acceptVisitor(this); + writer.append(")"); + } + break; + case FLOAT: + case DOUBLE: + writer.appendFunction("Long_toNumber").append("("); precedence = Precedence.min(); expr.getValue().acceptVisitor(this); writer.append(')'); - } else { + break; + default: expr.getValue().acceptVisitor(this); - } - break; - case LONG: - switch (expr.getTarget()) { - case INT: - precedence = Precedence.MEMBER_ACCESS; - Expr longShifted = extractLongRightShiftedBy32(expr.getValue()); - if (longShifted != null) { - writer.appendFunction("Long_hi").append("("); - longShifted.acceptVisitor(this); - writer.append(")"); - } else { - writer.appendFunction("Long_lo").append("("); - expr.getValue().acceptVisitor(this); - writer.append(")"); - } - break; - case FLOAT: - case DOUBLE: - writer.appendFunction("Long_toNumber").append("("); - precedence = Precedence.min(); - expr.getValue().acceptVisitor(this); - writer.append(')'); - break; - default: - expr.getValue().acceptVisitor(this); - } - break; - case FLOAT: - case DOUBLE: - switch (expr.getTarget()) { - case LONG: - writer.appendFunction("Long_fromNumber").append("("); - precedence = Precedence.min(); - expr.getValue().acceptVisitor(this); - writer.append(')'); - break; - case INT: - visitBinary(BinaryOperation.BITWISE_OR, "|", () -> expr.getValue().acceptVisitor(this), - () -> { - try { - writer.append("0"); - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); - } - }); - break; - default: - expr.getValue().acceptVisitor(this); - } - break; - } - if (expr.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + } + break; + case FLOAT: + case DOUBLE: + switch (expr.getTarget()) { + case LONG: + writer.appendFunction("Long_fromNumber").append("("); + precedence = Precedence.min(); + expr.getValue().acceptVisitor(this); + writer.append(')'); + break; + case INT: + visitBinary(BinaryOperation.BITWISE_OR, "|", () -> expr.getValue().acceptVisitor(this), + () -> writer.append("0")); + break; + default: + expr.getValue().acceptVisitor(this); + } + break; + } + if (expr.getLocation() != null) { + popLocation(); } } @@ -1015,381 +942,341 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { @Override public void visit(ConditionalExpr expr) { - try { - if (expr.getLocation() != null) { - pushLocation(expr.getLocation()); - } + if (expr.getLocation() != null) { + pushLocation(expr.getLocation()); + } - Precedence outerPrecedence = precedence; - if (outerPrecedence.ordinal() > Precedence.CONDITIONAL.ordinal()) { - writer.append('('); - } + Precedence outerPrecedence = precedence; + if (outerPrecedence.ordinal() > Precedence.CONDITIONAL.ordinal()) { + writer.append('('); + } - precedence = Precedence.CONDITIONAL.next(); - expr.getCondition().acceptVisitor(this); - writer.ws().append("?").ws(); - precedence = Precedence.CONDITIONAL.next(); - expr.getConsequent().acceptVisitor(this); - writer.ws().append(":").ws(); - precedence = Precedence.CONDITIONAL; - expr.getAlternative().acceptVisitor(this); + precedence = Precedence.CONDITIONAL.next(); + expr.getCondition().acceptVisitor(this); + writer.ws().append("?").ws(); + precedence = Precedence.CONDITIONAL.next(); + expr.getConsequent().acceptVisitor(this); + writer.ws().append(":").ws(); + precedence = Precedence.CONDITIONAL; + expr.getAlternative().acceptVisitor(this); - if (outerPrecedence.ordinal() > Precedence.CONDITIONAL.ordinal()) { - writer.append(')'); - } + if (outerPrecedence.ordinal() > Precedence.CONDITIONAL.ordinal()) { + writer.append(')'); + } - if (expr.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + if (expr.getLocation() != null) { + popLocation(); } } @Override public void visit(ConstantExpr expr) { - try { - if (expr.getLocation() != null) { - pushLocation(expr.getLocation()); - } - context.constantToString(writer, expr.getValue()); - if (expr.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + if (expr.getLocation() != null) { + pushLocation(expr.getLocation()); + } + context.constantToString(writer, expr.getValue()); + if (expr.getLocation() != null) { + popLocation(); } } @Override public void visit(VariableExpr expr) { - try { - if (expr.getLocation() != null) { - pushLocation(expr.getLocation()); - } - writer.append(variableName(expr.getIndex())); - if (expr.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + if (expr.getLocation() != null) { + pushLocation(expr.getLocation()); + } + writer.append(variableName(expr.getIndex())); + if (expr.getLocation() != null) { + popLocation(); } } @Override public void visit(SubscriptExpr expr) { - try { - if (expr.getLocation() != null) { - pushLocation(expr.getLocation()); - } - precedence = Precedence.MEMBER_ACCESS; - expr.getArray().acceptVisitor(this); - writer.append('['); - precedence = Precedence.min(); - expr.getIndex().acceptVisitor(this); - writer.append(']'); - if (expr.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + if (expr.getLocation() != null) { + pushLocation(expr.getLocation()); + } + precedence = Precedence.MEMBER_ACCESS; + expr.getArray().acceptVisitor(this); + writer.append('['); + precedence = Precedence.min(); + expr.getIndex().acceptVisitor(this); + writer.append(']'); + if (expr.getLocation() != null) { + popLocation(); } } @Override public void visit(UnwrapArrayExpr expr) { - try { - if (expr.getLocation() != null) { - pushLocation(expr.getLocation()); - } - precedence = Precedence.MEMBER_ACCESS; - expr.getArray().acceptVisitor(this); - writer.append(".data"); - if (expr.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + if (expr.getLocation() != null) { + pushLocation(expr.getLocation()); + } + precedence = Precedence.MEMBER_ACCESS; + expr.getArray().acceptVisitor(this); + writer.append(".data"); + if (expr.getLocation() != null) { + popLocation(); } } @Override public void visit(InvocationExpr expr) { - try { - if (expr.getLocation() != null) { - pushLocation(expr.getLocation()); + if (expr.getLocation() != null) { + pushLocation(expr.getLocation()); + } + Injector injector = context.getInjector(expr.getMethod()); + if (injector != null) { + injector.generate(new InjectorContextImpl(expr.getArguments()), expr.getMethod()); + } else { + Precedence outerPrecedence = precedence; + if (outerPrecedence.ordinal() > Precedence.FUNCTION_CALL.ordinal()) { + writer.append('('); } - Injector injector = context.getInjector(expr.getMethod()); - if (injector != null) { - injector.generate(new InjectorContextImpl(expr.getArguments()), expr.getMethod()); - } else { - Precedence outerPrecedence = precedence; - if (outerPrecedence.ordinal() > Precedence.FUNCTION_CALL.ordinal()) { - writer.append('('); - } - if (expr.getType() == InvocationType.DYNAMIC) { - precedence = Precedence.MEMBER_ACCESS; - expr.getArguments().get(0).acceptVisitor(this); - } - MethodReference method = expr.getMethod(); - String name = naming.getNameFor(method.getDescriptor()); - DeferredCallSite callSite = prevCallSite; - boolean shouldEraseCallSite = lastCallSite == null; - if (lastCallSite == null) { - lastCallSite = callSite; - } - boolean virtual = false; - switch (expr.getType()) { - case STATIC: - writer.appendMethodBody(method).append("("); - prevCallSite = debugEmitter.emitCallSite(); - for (int i = 0; i < expr.getArguments().size(); ++i) { - if (i > 0) { - writer.append(",").ws(); - } - precedence = Precedence.min(); - expr.getArguments().get(i).acceptVisitor(this); - } - break; - case SPECIAL: - writer.appendMethodBody(method).append("("); - prevCallSite = debugEmitter.emitCallSite(); - precedence = Precedence.min(); - expr.getArguments().get(0).acceptVisitor(this); - for (int i = 1; i < expr.getArguments().size(); ++i) { + if (expr.getType() == InvocationType.DYNAMIC) { + precedence = Precedence.MEMBER_ACCESS; + expr.getArguments().get(0).acceptVisitor(this); + } + MethodReference method = expr.getMethod(); + String name = naming.getNameFor(method.getDescriptor()); + DeferredCallSite callSite = prevCallSite; + boolean shouldEraseCallSite = lastCallSite == null; + if (lastCallSite == null) { + lastCallSite = callSite; + } + boolean virtual = false; + switch (expr.getType()) { + case STATIC: + writer.appendMethodBody(method).append("("); + prevCallSite = debugEmitter.emitCallSite(); + for (int i = 0; i < expr.getArguments().size(); ++i) { + if (i > 0) { writer.append(",").ws(); - precedence = Precedence.min(); - expr.getArguments().get(i).acceptVisitor(this); } - break; - case DYNAMIC: - writer.append(".").append(name).append("("); - prevCallSite = debugEmitter.emitCallSite(); - for (int i = 1; i < expr.getArguments().size(); ++i) { - if (i > 1) { - writer.append(",").ws(); - } - precedence = Precedence.min(); - expr.getArguments().get(i).acceptVisitor(this); - } - virtual = true; - break; - case CONSTRUCTOR: - writer.appendInit(expr.getMethod()).append("("); - prevCallSite = debugEmitter.emitCallSite(); - for (int i = 0; i < expr.getArguments().size(); ++i) { - if (i > 0) { - writer.append(",").ws(); - } - precedence = Precedence.min(); - expr.getArguments().get(i).acceptVisitor(this); - } - break; - } - writer.append(')'); - if (lastCallSite != null) { - if (virtual) { - lastCallSite.setVirtualMethod(expr.getMethod()); - } else { - lastCallSite.setStaticMethod(expr.getMethod()); + precedence = Precedence.min(); + expr.getArguments().get(i).acceptVisitor(this); } - lastCallSite = callSite; - } - if (shouldEraseCallSite) { - lastCallSite = null; + break; + case SPECIAL: + writer.appendMethodBody(method).append("("); + prevCallSite = debugEmitter.emitCallSite(); + precedence = Precedence.min(); + expr.getArguments().get(0).acceptVisitor(this); + for (int i = 1; i < expr.getArguments().size(); ++i) { + writer.append(",").ws(); + precedence = Precedence.min(); + expr.getArguments().get(i).acceptVisitor(this); + } + break; + case DYNAMIC: + writer.append(".").append(name).append("("); + prevCallSite = debugEmitter.emitCallSite(); + for (int i = 1; i < expr.getArguments().size(); ++i) { + if (i > 1) { + writer.append(",").ws(); + } + precedence = Precedence.min(); + expr.getArguments().get(i).acceptVisitor(this); + } + virtual = true; + break; + case CONSTRUCTOR: + writer.appendInit(expr.getMethod()).append("("); + prevCallSite = debugEmitter.emitCallSite(); + for (int i = 0; i < expr.getArguments().size(); ++i) { + if (i > 0) { + writer.append(",").ws(); + } + precedence = Precedence.min(); + expr.getArguments().get(i).acceptVisitor(this); + } + break; + } + writer.append(')'); + if (lastCallSite != null) { + if (virtual) { + lastCallSite.setVirtualMethod(expr.getMethod()); + } else { + lastCallSite.setStaticMethod(expr.getMethod()); } + lastCallSite = callSite; + } + if (shouldEraseCallSite) { + lastCallSite = null; + } - if (outerPrecedence.ordinal() > Precedence.FUNCTION_CALL.ordinal()) { - writer.append(')'); - } + if (outerPrecedence.ordinal() > Precedence.FUNCTION_CALL.ordinal()) { + writer.append(')'); } - if (expr.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + } + if (expr.getLocation() != null) { + popLocation(); } } @Override public void visit(QualificationExpr expr) { - try { - if (expr.getLocation() != null) { - pushLocation(expr.getLocation()); - } - precedence = Precedence.MEMBER_ACCESS; + if (expr.getLocation() != null) { + pushLocation(expr.getLocation()); + } + precedence = Precedence.MEMBER_ACCESS; - if (expr.getQualified() != null) { - expr.getQualified().acceptVisitor(this); - writer.append('.').appendField(expr.getField()); - } else { - writer.appendStaticField(expr.getField()); - } + if (expr.getQualified() != null) { + expr.getQualified().acceptVisitor(this); + writer.append('.').appendField(expr.getField()); + } else { + writer.appendStaticField(expr.getField()); + } - if (expr.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + if (expr.getLocation() != null) { + popLocation(); } } @Override public void visit(NewExpr expr) { - try { - if (expr.getLocation() != null) { - pushLocation(expr.getLocation()); - } + if (expr.getLocation() != null) { + pushLocation(expr.getLocation()); + } - Precedence outerPrecedence = precedence; - if (outerPrecedence.ordinal() > Precedence.NEW.ordinal()) { - writer.append('('); - } + Precedence outerPrecedence = precedence; + if (outerPrecedence.ordinal() > Precedence.NEW.ordinal()) { + writer.append('('); + } - precedence = Precedence.NEW; + precedence = Precedence.NEW; - writer.append("new ").appendClass(expr.getConstructedClass()); - if (outerPrecedence.ordinal() > Precedence.NEW.ordinal()) { - writer.append(')'); - } + writer.append("new ").appendClass(expr.getConstructedClass()); + if (outerPrecedence.ordinal() > Precedence.NEW.ordinal()) { + writer.append(')'); + } - if (expr.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + if (expr.getLocation() != null) { + popLocation(); } } @Override public void visit(NewArrayExpr expr) { - try { - if (expr.getLocation() != null) { - pushLocation(expr.getLocation()); + if (expr.getLocation() != null) { + pushLocation(expr.getLocation()); + } + ValueType type = expr.getType(); + if (type instanceof ValueType.Primitive) { + switch (((ValueType.Primitive) type).getKind()) { + case BOOLEAN: + writer.appendFunction("$rt_createBooleanArray").append("("); + precedence = Precedence.min(); + expr.getLength().acceptVisitor(this); + writer.append(")"); + break; + case BYTE: + writer.appendFunction("$rt_createByteArray").append("("); + precedence = Precedence.min(); + expr.getLength().acceptVisitor(this); + writer.append(")"); + break; + case SHORT: + writer.appendFunction("$rt_createShortArray").append("("); + precedence = Precedence.min(); + expr.getLength().acceptVisitor(this); + writer.append(")"); + break; + case INTEGER: + writer.appendFunction("$rt_createIntArray").append("("); + precedence = Precedence.min(); + expr.getLength().acceptVisitor(this); + writer.append(")"); + break; + case LONG: + writer.appendFunction("$rt_createLongArray").append("("); + precedence = Precedence.min(); + expr.getLength().acceptVisitor(this); + writer.append(")"); + break; + case FLOAT: + writer.appendFunction("$rt_createFloatArray").append("("); + precedence = Precedence.min(); + expr.getLength().acceptVisitor(this); + writer.append(")"); + break; + case DOUBLE: + writer.appendFunction("$rt_createDoubleArray").append("("); + precedence = Precedence.min(); + expr.getLength().acceptVisitor(this); + writer.append(")"); + break; + case CHARACTER: + writer.appendFunction("$rt_createCharArray").append("("); + precedence = Precedence.min(); + expr.getLength().acceptVisitor(this); + writer.append(")"); + break; } - ValueType type = expr.getType(); - if (type instanceof ValueType.Primitive) { - switch (((ValueType.Primitive) type).getKind()) { - case BOOLEAN: - writer.appendFunction("$rt_createBooleanArray").append("("); - precedence = Precedence.min(); - expr.getLength().acceptVisitor(this); - writer.append(")"); - break; - case BYTE: - writer.appendFunction("$rt_createByteArray").append("("); - precedence = Precedence.min(); - expr.getLength().acceptVisitor(this); - writer.append(")"); - break; - case SHORT: - writer.appendFunction("$rt_createShortArray").append("("); - precedence = Precedence.min(); - expr.getLength().acceptVisitor(this); - writer.append(")"); - break; - case INTEGER: - writer.appendFunction("$rt_createIntArray").append("("); - precedence = Precedence.min(); - expr.getLength().acceptVisitor(this); - writer.append(")"); - break; - case LONG: - writer.appendFunction("$rt_createLongArray").append("("); - precedence = Precedence.min(); - expr.getLength().acceptVisitor(this); - writer.append(")"); - break; - case FLOAT: - writer.appendFunction("$rt_createFloatArray").append("("); - precedence = Precedence.min(); - expr.getLength().acceptVisitor(this); - writer.append(")"); - break; - case DOUBLE: - writer.appendFunction("$rt_createDoubleArray").append("("); - precedence = Precedence.min(); - expr.getLength().acceptVisitor(this); - writer.append(")"); - break; - case CHARACTER: - writer.appendFunction("$rt_createCharArray").append("("); - precedence = Precedence.min(); - expr.getLength().acceptVisitor(this); - writer.append(")"); - break; - } - } else { - writer.appendFunction("$rt_createArray").append("("); - context.typeToClsString(writer, expr.getType()); - writer.append(",").ws(); - precedence = Precedence.min(); - expr.getLength().acceptVisitor(this); - writer.append(")"); - } - if (expr.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + } else { + writer.appendFunction("$rt_createArray").append("("); + context.typeToClsString(writer, expr.getType()); + writer.append(",").ws(); + precedence = Precedence.min(); + expr.getLength().acceptVisitor(this); + writer.append(")"); + } + if (expr.getLocation() != null) { + popLocation(); } } @Override public void visit(ArrayFromDataExpr expr) { - try { - if (expr.getLocation() != null) { - pushLocation(expr.getLocation()); - } - ValueType type = expr.getType(); - if (type instanceof ValueType.Primitive) { - switch (((ValueType.Primitive) type).getKind()) { - case BOOLEAN: - writer.appendFunction("$rt_createBooleanArrayFromData"); - break; - case BYTE: - writer.appendFunction("$rt_createByteArrayFromData"); - break; - case SHORT: - writer.appendFunction("$rt_createShortArrayFromData"); - break; - case INTEGER: - writer.appendFunction("$rt_createIntArrayFromData"); - break; - case LONG: - writer.appendFunction("$rt_createLongArrayFromData"); - break; - case FLOAT: - writer.appendFunction("$rt_createFloatArrayFromData"); - break; - case DOUBLE: - writer.appendFunction("$rt_createDoubleArrayFromData"); - break; - case CHARACTER: - writer.appendFunction("$rt_createCharArrayFromData"); - break; - } - writer.append("("); - } else { - writer.appendFunction("$rt_createArrayFromData").append("("); - context.typeToClsString(writer, expr.getType()); - writer.append(",").ws(); + if (expr.getLocation() != null) { + pushLocation(expr.getLocation()); + } + ValueType type = expr.getType(); + if (type instanceof ValueType.Primitive) { + switch (((ValueType.Primitive) type).getKind()) { + case BOOLEAN: + writer.appendFunction("$rt_createBooleanArrayFromData"); + break; + case BYTE: + writer.appendFunction("$rt_createByteArrayFromData"); + break; + case SHORT: + writer.appendFunction("$rt_createShortArrayFromData"); + break; + case INTEGER: + writer.appendFunction("$rt_createIntArrayFromData"); + break; + case LONG: + writer.appendFunction("$rt_createLongArrayFromData"); + break; + case FLOAT: + writer.appendFunction("$rt_createFloatArrayFromData"); + break; + case DOUBLE: + writer.appendFunction("$rt_createDoubleArrayFromData"); + break; + case CHARACTER: + writer.appendFunction("$rt_createCharArrayFromData"); + break; } + writer.append("("); + } else { + writer.appendFunction("$rt_createArrayFromData").append("("); + context.typeToClsString(writer, expr.getType()); + writer.append(",").ws(); + } - writer.append("["); - writeCommaSeparated(expr.getData()); - writer.append("])"); + writer.append("["); + writeCommaSeparated(expr.getData()); + writer.append("])"); - if (expr.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + if (expr.getLocation() != null) { + popLocation(); } } - private void writeCommaSeparated(List expressions) throws IOException { + private void writeCommaSeparated(List expressions) { boolean first = true; for (Expr element : expressions) { if (!first) { @@ -1403,98 +1290,90 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { @Override public void visit(NewMultiArrayExpr expr) { - try { - if (expr.getLocation() != null) { - pushLocation(expr.getLocation()); + if (expr.getLocation() != null) { + pushLocation(expr.getLocation()); + } + ValueType type = expr.getType(); + for (int i = 0; i < expr.getDimensions().size(); ++i) { + type = ((ValueType.Array) type).getItemType(); + } + if (type instanceof ValueType.Primitive) { + switch (((ValueType.Primitive) type).getKind()) { + case BOOLEAN: + writer.appendFunction("$rt_createBooleanMultiArray").append("("); + break; + case BYTE: + writer.appendFunction("$rt_createByteMultiArray").append("("); + break; + case SHORT: + writer.appendFunction("$rt_createShortMultiArray").append("("); + break; + case INTEGER: + writer.appendFunction("$rt_createIntMultiArray").append("("); + break; + case LONG: + writer.appendFunction("$rt_createLongMultiArray").append("("); + break; + case FLOAT: + writer.appendFunction("$rt_createFloatMultiArray").append("("); + break; + case DOUBLE: + writer.appendFunction("$rt_createDoubleMultiArray").append("("); + break; + case CHARACTER: + writer.appendFunction("$rt_createCharMultiArray").append("("); + break; } - ValueType type = expr.getType(); - for (int i = 0; i < expr.getDimensions().size(); ++i) { - type = ((ValueType.Array) type).getItemType(); - } - if (type instanceof ValueType.Primitive) { - switch (((ValueType.Primitive) type).getKind()) { - case BOOLEAN: - writer.appendFunction("$rt_createBooleanMultiArray").append("("); - break; - case BYTE: - writer.appendFunction("$rt_createByteMultiArray").append("("); - break; - case SHORT: - writer.appendFunction("$rt_createShortMultiArray").append("("); - break; - case INTEGER: - writer.appendFunction("$rt_createIntMultiArray").append("("); - break; - case LONG: - writer.appendFunction("$rt_createLongMultiArray").append("("); - break; - case FLOAT: - writer.appendFunction("$rt_createFloatMultiArray").append("("); - break; - case DOUBLE: - writer.appendFunction("$rt_createDoubleMultiArray").append("("); - break; - case CHARACTER: - writer.appendFunction("$rt_createCharMultiArray").append("("); - break; - } - } else { - writer.appendFunction("$rt_createMultiArray").append("("); - context.typeToClsString(writer, type); + } else { + writer.appendFunction("$rt_createMultiArray").append("("); + context.typeToClsString(writer, type); + writer.append(",").ws(); + } + writer.append("["); + boolean first = true; + List dimensions = new ArrayList<>(expr.getDimensions()); + Collections.reverse(dimensions); + for (Expr dimension : dimensions) { + if (!first) { writer.append(",").ws(); } - writer.append("["); - boolean first = true; - List dimensions = new ArrayList<>(expr.getDimensions()); - Collections.reverse(dimensions); - for (Expr dimension : dimensions) { - if (!first) { - writer.append(",").ws(); - } - first = false; - precedence = Precedence.min(); - dimension.acceptVisitor(this); - } - writer.append("])"); - if (expr.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + first = false; + precedence = Precedence.min(); + dimension.acceptVisitor(this); + } + writer.append("])"); + if (expr.getLocation() != null) { + popLocation(); } } @Override public void visit(InstanceOfExpr expr) { - try { - if (expr.getLocation() != null) { - pushLocation(expr.getLocation()); + if (expr.getLocation() != null) { + pushLocation(expr.getLocation()); + } + if (isClass(expr.getType(), context.getClassSource())) { + boolean needsParentheses = Precedence.COMPARISON.ordinal() < precedence.ordinal(); + if (needsParentheses) { + writer.append('('); } - if (isClass(expr.getType(), context.getClassSource())) { - boolean needsParentheses = Precedence.COMPARISON.ordinal() < precedence.ordinal(); - if (needsParentheses) { - writer.append('('); - } - precedence = Precedence.CONDITIONAL.next(); - expr.getExpr().acceptVisitor(this); - writer.append(" instanceof "); - context.typeToClsString(writer, expr.getType()); - if (needsParentheses) { - writer.append(')'); - } - } else { - writer.appendFunction("$rt_isInstance").append("("); - precedence = Precedence.min(); - expr.getExpr().acceptVisitor(this); - writer.append(",").ws(); - context.typeToClsString(writer, expr.getType()); - writer.append(")"); + precedence = Precedence.CONDITIONAL.next(); + expr.getExpr().acceptVisitor(this); + writer.append(" instanceof "); + context.typeToClsString(writer, expr.getType()); + if (needsParentheses) { + writer.append(')'); } - if (expr.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + } else { + writer.appendFunction("$rt_isInstance").append("("); + precedence = Precedence.min(); + expr.getExpr().acceptVisitor(this); + writer.append(",").ws(); + context.typeToClsString(writer, expr.getType()); + writer.append(")"); + } + if (expr.getLocation() != null) { + popLocation(); } } @@ -1514,107 +1393,95 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { @Override public void visit(TryCatchStatement statement) { - try { - writer.append("try").ws().append("{").softNewLine().indent(); - List sequence = new ArrayList<>(); - sequence.add(statement); - List protectedBody = statement.getProtectedBody(); - while (protectedBody.size() == 1 && protectedBody.get(0) instanceof TryCatchStatement) { - TryCatchStatement nextStatement = (TryCatchStatement) protectedBody.get(0); - sequence.add(nextStatement); - protectedBody = nextStatement.getProtectedBody(); - } - visitStatements(protectedBody); - writer.outdent().append("}").ws().append("catch").ws().append("($$e)") - .ws().append("{").indent().softNewLine(); - writer.append("$$je").ws().append("=").ws().appendFunction("$rt_wrapException").append("($$e);") - .softNewLine(); - boolean first = true; - boolean defaultHandlerOccurred = false; - for (int i = sequence.size() - 1; i >= 0; --i) { - TryCatchStatement catchClause = sequence.get(i); - if (!first) { - writer.ws().append("else"); - } - if (catchClause.getExceptionType() != null) { - if (!first) { - writer.append(" "); - } - writer.append("if").ws().append("($$je instanceof ").appendClass(catchClause.getExceptionType()); - writer.append(")").ws(); - } else { - defaultHandlerOccurred = true; - } - - if (catchClause.getExceptionType() != null || !first) { - writer.append("{").indent().softNewLine(); - } - - if (catchClause.getExceptionVariable() != null) { - writer.append(variableName(catchClause.getExceptionVariable())).ws().append("=").ws() - .append("$$je;").softNewLine(); - } - visitStatements(catchClause.getHandler()); - - if (catchClause.getExceptionType() != null || !first) { - writer.outdent().append("}"); - } - - first = false; - - if (defaultHandlerOccurred) { - break; - } - } - if (!defaultHandlerOccurred) { - writer.ws().append("else").ws().append("{").indent().softNewLine(); - writer.append("throw $$e;").softNewLine(); - writer.outdent().append("}").softNewLine(); - } else { - writer.softNewLine(); - } - writer.outdent().append("}").softNewLine(); - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + writer.append("try").ws().append("{").softNewLine().indent(); + List sequence = new ArrayList<>(); + sequence.add(statement); + List protectedBody = statement.getProtectedBody(); + while (protectedBody.size() == 1 && protectedBody.get(0) instanceof TryCatchStatement) { + TryCatchStatement nextStatement = (TryCatchStatement) protectedBody.get(0); + sequence.add(nextStatement); + protectedBody = nextStatement.getProtectedBody(); } + visitStatements(protectedBody); + writer.outdent().append("}").ws().append("catch").ws().append("($$e)") + .ws().append("{").indent().softNewLine(); + writer.append("$$je").ws().append("=").ws().appendFunction("$rt_wrapException").append("($$e);") + .softNewLine(); + boolean first = true; + boolean defaultHandlerOccurred = false; + for (int i = sequence.size() - 1; i >= 0; --i) { + TryCatchStatement catchClause = sequence.get(i); + if (!first) { + writer.ws().append("else"); + } + if (catchClause.getExceptionType() != null) { + if (!first) { + writer.append(" "); + } + writer.append("if").ws().append("($$je instanceof ").appendClass(catchClause.getExceptionType()); + writer.append(")").ws(); + } else { + defaultHandlerOccurred = true; + } + + if (catchClause.getExceptionType() != null || !first) { + writer.append("{").indent().softNewLine(); + } + + if (catchClause.getExceptionVariable() != null) { + writer.append(variableName(catchClause.getExceptionVariable())).ws().append("=").ws() + .append("$$je;").softNewLine(); + } + visitStatements(catchClause.getHandler()); + + if (catchClause.getExceptionType() != null || !first) { + writer.outdent().append("}"); + } + + first = false; + + if (defaultHandlerOccurred) { + break; + } + } + if (!defaultHandlerOccurred) { + writer.ws().append("else").ws().append("{").indent().softNewLine(); + writer.append("throw $$e;").softNewLine(); + writer.outdent().append("}").softNewLine(); + } else { + writer.softNewLine(); + } + writer.outdent().append("}").softNewLine(); } @Override public void visit(GotoPartStatement statement) { - try { - if (statement.getPart() != currentPart) { - writer.append(context.pointerName()).ws().append("=").ws().append(statement.getPart()).append(";") - .softNewLine(); - } - if (!end || statement.getPart() != currentPart + 1) { - writer.append("continue ").append(context.mainLoopName()).append(";").softNewLine(); - } - } catch (IOException ex) { - throw new RenderingException("IO error occurred", ex); + if (statement.getPart() != currentPart) { + writer.append(context.pointerName()).ws().append("=").ws().append(statement.getPart()).append(";") + .softNewLine(); + } + if (!end || statement.getPart() != currentPart + 1) { + writer.append("continue ").append(context.mainLoopName()).append(";").softNewLine(); } } @Override public void visit(MonitorEnterStatement statement) { - try { - if (async) { - writer.appendMethodBody(NameFrequencyEstimator.MONITOR_ENTER_METHOD).append("("); - precedence = Precedence.min(); - statement.getObjectRef().acceptVisitor(this); - writer.append(");").softNewLine(); - emitSuspendChecker(); - } else { - writer.appendMethodBody(NameFrequencyEstimator.MONITOR_ENTER_SYNC_METHOD).append('('); - precedence = Precedence.min(); - statement.getObjectRef().acceptVisitor(this); - writer.append(");").softNewLine(); - } - } catch (IOException ex) { - throw new RenderingException("IO error occurred", ex); + if (async) { + writer.appendMethodBody(NameFrequencyEstimator.MONITOR_ENTER_METHOD).append("("); + precedence = Precedence.min(); + statement.getObjectRef().acceptVisitor(this); + writer.append(");").softNewLine(); + emitSuspendChecker(); + } else { + writer.appendMethodBody(NameFrequencyEstimator.MONITOR_ENTER_SYNC_METHOD).append('('); + precedence = Precedence.min(); + statement.getObjectRef().acceptVisitor(this); + writer.append(");").softNewLine(); } } - public void emitSuspendChecker() throws IOException { + public void emitSuspendChecker() { writer.append("if").ws().append("(").appendFunction("$rt_suspending").append("())").ws() .append("{").indent().softNewLine(); writer.append("break ").append(context.mainLoopName()).append(";").softNewLine(); @@ -1623,54 +1490,46 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { @Override public void visit(MonitorExitStatement statement) { - try { - if (async) { - writer.appendMethodBody(NameFrequencyEstimator.MONITOR_EXIT_METHOD).append("("); - precedence = Precedence.min(); - statement.getObjectRef().acceptVisitor(this); - writer.append(");").softNewLine(); - } else { - writer.appendMethodBody(NameFrequencyEstimator.MONITOR_EXIT_SYNC_METHOD).append('('); - precedence = Precedence.min(); - statement.getObjectRef().acceptVisitor(this); - writer.append(");").softNewLine(); - } - } catch (IOException ex) { - throw new RenderingException("IO error occurred", ex); + if (async) { + writer.appendMethodBody(NameFrequencyEstimator.MONITOR_EXIT_METHOD).append("("); + precedence = Precedence.min(); + statement.getObjectRef().acceptVisitor(this); + writer.append(");").softNewLine(); + } else { + writer.appendMethodBody(NameFrequencyEstimator.MONITOR_EXIT_SYNC_METHOD).append('('); + precedence = Precedence.min(); + statement.getObjectRef().acceptVisitor(this); + writer.append(");").softNewLine(); } } @Override public void visit(BoundCheckExpr expr) { - try { - if (expr.getLocation() != null) { - pushLocation(expr.getLocation()); - } + if (expr.getLocation() != null) { + pushLocation(expr.getLocation()); + } - if (expr.getArray() != null && expr.isLower()) { - writer.appendFunction("$rt_checkBounds").append("("); - } else if (expr.getArray() != null) { - writer.appendFunction("$rt_checkUpperBound").append("("); - } else if (expr.isLower()) { - writer.appendFunction("$rt_checkLowerBound").append("("); - } + if (expr.getArray() != null && expr.isLower()) { + writer.appendFunction("$rt_checkBounds").append("("); + } else if (expr.getArray() != null) { + writer.appendFunction("$rt_checkUpperBound").append("("); + } else if (expr.isLower()) { + writer.appendFunction("$rt_checkLowerBound").append("("); + } - expr.getIndex().acceptVisitor(this); + expr.getIndex().acceptVisitor(this); - if (expr.getArray() != null) { - writer.append(",").ws(); - expr.getArray().acceptVisitor(this); - } + if (expr.getArray() != null) { + writer.append(",").ws(); + expr.getArray().acceptVisitor(this); + } - if (expr.getArray() != null || expr.isLower()) { - writer.append(")"); - } + if (expr.getArray() != null || expr.isLower()) { + writer.append(")"); + } - if (expr.getLocation() != null) { - popLocation(); - } - } catch (IOException e) { - throw new RenderingException("IO error occurred", e); + if (expr.getLocation() != null) { + popLocation(); } } @@ -1698,12 +1557,12 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { } @Override - public void writeEscaped(String str) throws IOException { + public void writeEscaped(String str) { writer.append(RenderingUtil.escapeString(str)); } @Override - public void writeType(ValueType type) throws IOException { + public void writeType(ValueType type) { context.typeToClsString(writer, type); } diff --git a/core/src/main/java/org/teavm/backend/javascript/spi/Generator.java b/core/src/main/java/org/teavm/backend/javascript/spi/Generator.java index 138d3797a..235040260 100644 --- a/core/src/main/java/org/teavm/backend/javascript/spi/Generator.java +++ b/core/src/main/java/org/teavm/backend/javascript/spi/Generator.java @@ -15,10 +15,9 @@ */ package org.teavm.backend.javascript.spi; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.model.MethodReference; public interface Generator { - void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException; + void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef); } diff --git a/core/src/main/java/org/teavm/backend/javascript/spi/Injector.java b/core/src/main/java/org/teavm/backend/javascript/spi/Injector.java index 05609efae..e49bf5ce7 100644 --- a/core/src/main/java/org/teavm/backend/javascript/spi/Injector.java +++ b/core/src/main/java/org/teavm/backend/javascript/spi/Injector.java @@ -15,9 +15,8 @@ */ package org.teavm.backend.javascript.spi; -import java.io.IOException; import org.teavm.model.MethodReference; public interface Injector { - void generate(InjectorContext context, MethodReference methodRef) throws IOException; + void generate(InjectorContext context, MethodReference methodRef); } diff --git a/core/src/main/java/org/teavm/backend/javascript/spi/InjectorContext.java b/core/src/main/java/org/teavm/backend/javascript/spi/InjectorContext.java index 8facb3ec9..d3dd88b8c 100644 --- a/core/src/main/java/org/teavm/backend/javascript/spi/InjectorContext.java +++ b/core/src/main/java/org/teavm/backend/javascript/spi/InjectorContext.java @@ -15,7 +15,6 @@ */ package org.teavm.backend.javascript.spi; -import java.io.IOException; import java.util.Properties; import org.teavm.ast.Expr; import org.teavm.backend.javascript.codegen.SourceWriter; @@ -37,9 +36,9 @@ public interface InjectorContext extends ServiceRepository { Properties getProperties(); - void writeEscaped(String str) throws IOException; + void writeEscaped(String str); - void writeType(ValueType type) throws IOException; + void writeType(ValueType type); void writeExpr(Expr expr); diff --git a/core/src/main/java/org/teavm/backend/javascript/templating/JavaScriptTemplateFactory.java b/core/src/main/java/org/teavm/backend/javascript/templating/JavaScriptTemplateFactory.java index a941c310a..0fa3843b1 100644 --- a/core/src/main/java/org/teavm/backend/javascript/templating/JavaScriptTemplateFactory.java +++ b/core/src/main/java/org/teavm/backend/javascript/templating/JavaScriptTemplateFactory.java @@ -15,7 +15,6 @@ */ package org.teavm.backend.javascript.templating; -import java.io.IOException; import org.teavm.backend.javascript.ast.AstUtil; import org.teavm.model.ClassReaderSource; @@ -28,7 +27,7 @@ public class JavaScriptTemplateFactory { this.classSource = classSource; } - public JavaScriptTemplate createFromResource(String path) throws IOException { + public JavaScriptTemplate createFromResource(String path) { return new JavaScriptTemplate(AstUtil.parseFromResources(classLoader, path), classSource); } } diff --git a/core/src/main/java/org/teavm/backend/javascript/templating/SourceFragment.java b/core/src/main/java/org/teavm/backend/javascript/templating/SourceFragment.java index e405f862e..c954930ce 100644 --- a/core/src/main/java/org/teavm/backend/javascript/templating/SourceFragment.java +++ b/core/src/main/java/org/teavm/backend/javascript/templating/SourceFragment.java @@ -15,9 +15,8 @@ */ package org.teavm.backend.javascript.templating; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; public interface SourceFragment { - void write(SourceWriter writer, int precedence) throws IOException; + void write(SourceWriter writer, int precedence); } diff --git a/core/src/main/java/org/teavm/backend/javascript/templating/TemplatingAstWriter.java b/core/src/main/java/org/teavm/backend/javascript/templating/TemplatingAstWriter.java index b109e3cf8..7e8504bb8 100644 --- a/core/src/main/java/org/teavm/backend/javascript/templating/TemplatingAstWriter.java +++ b/core/src/main/java/org/teavm/backend/javascript/templating/TemplatingAstWriter.java @@ -15,7 +15,6 @@ */ package org.teavm.backend.javascript.templating; -import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.mozilla.javascript.ast.ElementGet; @@ -56,7 +55,7 @@ public class TemplatingAstWriter extends AstWriter { } @Override - protected boolean intrinsic(FunctionCall node, int precedence) throws IOException { + protected boolean intrinsic(FunctionCall node, int precedence) { if (node.getTarget() instanceof Name) { var name = (Name) node.getTarget(); if (scopeOfId(name.getIdentifier()) == null) { @@ -66,7 +65,7 @@ public class TemplatingAstWriter extends AstWriter { return super.intrinsic(node, precedence); } - private boolean tryIntrinsicName(FunctionCall node, String name) throws IOException { + private boolean tryIntrinsicName(FunctionCall node, String name) { switch (name) { case "teavm_javaClass": return writeJavaClass(node); @@ -83,7 +82,7 @@ public class TemplatingAstWriter extends AstWriter { } } - private boolean writeJavaClass(FunctionCall node) throws IOException { + private boolean writeJavaClass(FunctionCall node) { if (node.getArguments().size() != 1) { return false; } @@ -95,7 +94,7 @@ public class TemplatingAstWriter extends AstWriter { return true; } - private boolean writeJavaMethod(FunctionCall node) throws IOException { + private boolean writeJavaMethod(FunctionCall node) { if (node.getArguments().size() != 2) { return false; } @@ -110,7 +109,7 @@ public class TemplatingAstWriter extends AstWriter { return true; } - private boolean writeJavaConstructor(FunctionCall node) throws IOException { + private boolean writeJavaConstructor(FunctionCall node) { if (node.getArguments().size() != 2) { return false; } @@ -125,7 +124,7 @@ public class TemplatingAstWriter extends AstWriter { return true; } - private boolean writeJavaClassInit(FunctionCall node) throws IOException { + private boolean writeJavaClassInit(FunctionCall node) { if (node.getArguments().size() != 1) { return false; } @@ -137,7 +136,7 @@ public class TemplatingAstWriter extends AstWriter { return true; } - private boolean writeFragment(FunctionCall node) throws IOException { + private boolean writeFragment(FunctionCall node) { if (node.getArguments().size() != 1) { return false; } @@ -151,7 +150,7 @@ public class TemplatingAstWriter extends AstWriter { } @Override - protected void print(ElementGet node) throws IOException { + protected void print(ElementGet node) { if (node.getElement() instanceof FunctionCall) { var call = (FunctionCall) node.getElement(); if (call.getTarget() instanceof Name) { @@ -176,7 +175,7 @@ public class TemplatingAstWriter extends AstWriter { } @Override - public void print(PropertyGet node) throws IOException { + public void print(PropertyGet node) { if (node.getTarget() instanceof Name) { var name = (Name) node.getTarget(); var scope = scopeOfId(name.getIdentifier()); @@ -192,7 +191,7 @@ public class TemplatingAstWriter extends AstWriter { super.print(node); } - private boolean writeJavaVirtualMethod(ElementGet get, FunctionCall call) throws IOException { + private boolean writeJavaVirtualMethod(ElementGet get, FunctionCall call) { var arg = call.getArguments().get(0); if (!(arg instanceof StringLiteral)) { return false; @@ -203,7 +202,7 @@ public class TemplatingAstWriter extends AstWriter { return true; } - private boolean writeJavaField(ElementGet get, FunctionCall call) throws IOException { + private boolean writeJavaField(ElementGet get, FunctionCall call) { if (call.getArguments().size() != 2) { return false; } @@ -220,7 +219,7 @@ public class TemplatingAstWriter extends AstWriter { } @Override - public void print(Name node, int precedence) throws IOException { + public void print(Name node, int precedence) { var definingScope = scopeOfId(node.getIdentifier()); if (rootScope) { if (names != null && definingScope == scope) { diff --git a/core/src/main/java/org/teavm/vm/spi/AbstractRendererListener.java b/core/src/main/java/org/teavm/vm/spi/AbstractRendererListener.java index 3eef3ffdd..61e6fef60 100644 --- a/core/src/main/java/org/teavm/vm/spi/AbstractRendererListener.java +++ b/core/src/main/java/org/teavm/vm/spi/AbstractRendererListener.java @@ -15,16 +15,15 @@ */ package org.teavm.vm.spi; -import java.io.IOException; import org.teavm.backend.javascript.rendering.RenderingManager; import org.teavm.vm.BuildTarget; public abstract class AbstractRendererListener implements RendererListener { @Override - public void begin(RenderingManager manager, BuildTarget buildTarget) throws IOException { + public void begin(RenderingManager manager, BuildTarget buildTarget) { } @Override - public void complete() throws IOException { + public void complete() { } } diff --git a/core/src/main/java/org/teavm/vm/spi/RendererListener.java b/core/src/main/java/org/teavm/vm/spi/RendererListener.java index 1535b5b8f..50e082534 100644 --- a/core/src/main/java/org/teavm/vm/spi/RendererListener.java +++ b/core/src/main/java/org/teavm/vm/spi/RendererListener.java @@ -15,12 +15,11 @@ */ package org.teavm.vm.spi; -import java.io.IOException; import org.teavm.backend.javascript.rendering.RenderingManager; import org.teavm.vm.BuildTarget; public interface RendererListener { - void begin(RenderingManager context, BuildTarget buildTarget) throws IOException; + void begin(RenderingManager context, BuildTarget buildTarget); - void complete() throws IOException; + void complete(); } diff --git a/jso/impl/src/main/java/org/teavm/jso/impl/JSAliasRenderer.java b/jso/impl/src/main/java/org/teavm/jso/impl/JSAliasRenderer.java index 1824fa399..27b5902bc 100644 --- a/jso/impl/src/main/java/org/teavm/jso/impl/JSAliasRenderer.java +++ b/jso/impl/src/main/java/org/teavm/jso/impl/JSAliasRenderer.java @@ -15,7 +15,6 @@ */ package org.teavm.jso.impl; -import java.io.IOException; import java.util.HashMap; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.rendering.RenderingManager; @@ -44,7 +43,7 @@ class JSAliasRenderer implements RendererListener, VirtualMethodContributor { } @Override - public void complete() throws IOException { + public void complete() { if (!hasClassesToExpose()) { return; } @@ -178,7 +177,7 @@ class JSAliasRenderer implements RendererListener, VirtualMethodContributor { } } - private void writeFunctor(ClassReader cls, FieldReference functorField) throws IOException { + private void writeFunctor(ClassReader cls, FieldReference functorField) { AnnotationReader implAnnot = cls.getAnnotations().get(FunctorImpl.class.getName()); MethodDescriptor functorMethod = MethodDescriptor.parse(implAnnot.getValue("value").getString()); String alias = cls.getMethod(functorMethod).getAnnotations() @@ -206,7 +205,7 @@ class JSAliasRenderer implements RendererListener, VirtualMethodContributor { writer.outdent().append("};").softNewLine(); } - private void appendArguments(int count) throws IOException { + private void appendArguments(int count) { for (int i = 0; i < count; ++i) { if (i > 0) { writer.append(',').ws(); diff --git a/jso/impl/src/main/java/org/teavm/jso/impl/JSBodyAstEmitter.java b/jso/impl/src/main/java/org/teavm/jso/impl/JSBodyAstEmitter.java index 497164c3e..2d3c5e704 100644 --- a/jso/impl/src/main/java/org/teavm/jso/impl/JSBodyAstEmitter.java +++ b/jso/impl/src/main/java/org/teavm/jso/impl/JSBodyAstEmitter.java @@ -15,7 +15,6 @@ */ package org.teavm.jso.impl; -import java.io.IOException; import org.mozilla.javascript.Node; import org.mozilla.javascript.ast.AstNode; import org.mozilla.javascript.ast.Block; @@ -44,7 +43,7 @@ class JSBodyAstEmitter implements JSBodyEmitter { } @Override - public void emit(InjectorContext context) throws IOException { + public void emit(InjectorContext context) { var astWriter = new AstWriter(context.getWriter(), new DefaultGlobalNameWriter(context.getWriter())); int paramIndex = 0; if (!isStatic) { @@ -148,7 +147,7 @@ class JSBodyAstEmitter implements JSBodyEmitter { } @Override - public void emit(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { + public void emit(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { var astWriter = new AstWriter(writer, new DefaultGlobalNameWriter(writer)); int paramIndex = 1; if (!isStatic) { diff --git a/jso/impl/src/main/java/org/teavm/jso/impl/JSBodyBloatedEmitter.java b/jso/impl/src/main/java/org/teavm/jso/impl/JSBodyBloatedEmitter.java index 287bdc382..586c4b206 100644 --- a/jso/impl/src/main/java/org/teavm/jso/impl/JSBodyBloatedEmitter.java +++ b/jso/impl/src/main/java/org/teavm/jso/impl/JSBodyBloatedEmitter.java @@ -15,7 +15,6 @@ */ package org.teavm.jso.impl; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.spi.GeneratorContext; import org.teavm.backend.javascript.spi.InjectorContext; @@ -38,7 +37,7 @@ class JSBodyBloatedEmitter implements JSBodyEmitter { } @Override - public void emit(InjectorContext context) throws IOException { + public void emit(InjectorContext context) { emit(context.getWriter(), new EmissionStrategy() { @Override public void emitArgument(int argument) { @@ -46,28 +45,28 @@ class JSBodyBloatedEmitter implements JSBodyEmitter { } @Override - public void emitModule(String name) throws IOException { + public void emitModule(String name) { context.getWriter().append(context.importModule(name)); } }); } @Override - public void emit(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { + public void emit(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { emit(writer, new EmissionStrategy() { @Override - public void emitArgument(int argument) throws IOException { + public void emitArgument(int argument) { writer.append(context.getParameterName(argument + 1)); } @Override - public void emitModule(String name) throws IOException { + public void emitModule(String name) { writer.append(context.importModule(name)); } }); } - private void emit(SourceWriter writer, EmissionStrategy strategy) throws IOException { + private void emit(SourceWriter writer, EmissionStrategy strategy) { int bodyParamCount = isStatic ? method.parameterCount() : method.parameterCount() - 1; writer.append("if (!").appendMethodBody(method).append(".$native)").ws().append('{').indent().newLine(); @@ -151,8 +150,8 @@ class JSBodyBloatedEmitter implements JSBodyEmitter { } interface EmissionStrategy { - void emitArgument(int argument) throws IOException; + void emitArgument(int argument); - void emitModule(String name) throws IOException; + void emitModule(String name); } } diff --git a/jso/impl/src/main/java/org/teavm/jso/impl/JSBodyEmitter.java b/jso/impl/src/main/java/org/teavm/jso/impl/JSBodyEmitter.java index ec1f4a763..4cb5fc7e5 100644 --- a/jso/impl/src/main/java/org/teavm/jso/impl/JSBodyEmitter.java +++ b/jso/impl/src/main/java/org/teavm/jso/impl/JSBodyEmitter.java @@ -15,14 +15,13 @@ */ package org.teavm.jso.impl; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.spi.GeneratorContext; import org.teavm.backend.javascript.spi.InjectorContext; import org.teavm.model.MethodReference; interface JSBodyEmitter { - void emit(InjectorContext context) throws IOException; + void emit(InjectorContext context); - void emit(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException; + void emit(GeneratorContext context, SourceWriter writer, MethodReference methodRef); } diff --git a/jso/impl/src/main/java/org/teavm/jso/impl/JSBodyGenerator.java b/jso/impl/src/main/java/org/teavm/jso/impl/JSBodyGenerator.java index ac6597f9f..8109863e1 100644 --- a/jso/impl/src/main/java/org/teavm/jso/impl/JSBodyGenerator.java +++ b/jso/impl/src/main/java/org/teavm/jso/impl/JSBodyGenerator.java @@ -15,7 +15,6 @@ */ package org.teavm.jso.impl; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.spi.Generator; import org.teavm.backend.javascript.spi.GeneratorContext; @@ -25,14 +24,14 @@ import org.teavm.model.MethodReference; public class JSBodyGenerator implements Injector, Generator { @Override - public void generate(InjectorContext context, MethodReference methodRef) throws IOException { + public void generate(InjectorContext context, MethodReference methodRef) { JSBodyRepository emitterRepository = context.getService(JSBodyRepository.class); JSBodyEmitter emitter = emitterRepository.emitters.get(methodRef); emitter.emit(context); } @Override - public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { + public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { JSBodyRepository emitterRepository = context.getService(JSBodyRepository.class); JSBodyEmitter emitter = emitterRepository.emitters.get(methodRef); emitter.emit(context, writer, methodRef); diff --git a/jso/impl/src/main/java/org/teavm/jso/impl/JSExceptionsGenerator.java b/jso/impl/src/main/java/org/teavm/jso/impl/JSExceptionsGenerator.java index b4ee6f805..b0d52c031 100644 --- a/jso/impl/src/main/java/org/teavm/jso/impl/JSExceptionsGenerator.java +++ b/jso/impl/src/main/java/org/teavm/jso/impl/JSExceptionsGenerator.java @@ -15,14 +15,13 @@ */ package org.teavm.jso.impl; -import java.io.IOException; import org.teavm.backend.javascript.spi.Injector; import org.teavm.backend.javascript.spi.InjectorContext; import org.teavm.model.MethodReference; public class JSExceptionsGenerator implements Injector { @Override - public void generate(InjectorContext context, MethodReference methodRef) throws IOException { + public void generate(InjectorContext context, MethodReference methodRef) { switch (methodRef.getName()) { case "getJavaException": context.getWriter().appendFunction("$rt_javaException").append("("); diff --git a/jso/impl/src/main/java/org/teavm/jso/impl/JSNativeGenerator.java b/jso/impl/src/main/java/org/teavm/jso/impl/JSNativeGenerator.java index 2e79462eb..f90ef9cd7 100644 --- a/jso/impl/src/main/java/org/teavm/jso/impl/JSNativeGenerator.java +++ b/jso/impl/src/main/java/org/teavm/jso/impl/JSNativeGenerator.java @@ -15,7 +15,6 @@ */ package org.teavm.jso.impl; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.spi.Generator; import org.teavm.backend.javascript.spi.GeneratorContext; @@ -26,13 +25,12 @@ import org.teavm.model.MethodReference; public class JSNativeGenerator implements Generator { private JavaScriptTemplate template; - public JSNativeGenerator(JavaScriptTemplateFactory templateFactory) throws IOException { + public JSNativeGenerator(JavaScriptTemplateFactory templateFactory) { template = templateFactory.createFromResource("org/teavm/jso/impl/JS.js"); } @Override - public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) - throws IOException { + public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { switch (methodRef.getName()) { case "function": template.builder("jsFunction").withContext(context).build().write(writer, 0); diff --git a/jso/impl/src/main/java/org/teavm/jso/impl/JSNativeInjector.java b/jso/impl/src/main/java/org/teavm/jso/impl/JSNativeInjector.java index 23d8e5645..cf000048f 100644 --- a/jso/impl/src/main/java/org/teavm/jso/impl/JSNativeInjector.java +++ b/jso/impl/src/main/java/org/teavm/jso/impl/JSNativeInjector.java @@ -16,7 +16,6 @@ package org.teavm.jso.impl; import static org.teavm.backend.javascript.rendering.RenderingUtil.escapeString; -import java.io.IOException; import java.util.HashSet; import java.util.Set; import org.teavm.ast.ConstantExpr; @@ -41,7 +40,7 @@ public class JSNativeInjector implements Injector, DependencyPlugin { private Set functorParamNodes = new HashSet<>(); @Override - public void generate(InjectorContext context, MethodReference methodRef) throws IOException { + public void generate(InjectorContext context, MethodReference methodRef) { SourceWriter writer = context.getWriter(); switch (methodRef.getName()) { case "arrayData": @@ -162,7 +161,7 @@ public class JSNativeInjector implements Injector, DependencyPlugin { } } - private void dataToArray(InjectorContext context, String className) throws IOException { + private void dataToArray(InjectorContext context, String className) { var writer = context.getWriter(); writer.appendFunction("$rt_wrapArray").append("(").appendFunction(className).append(",").ws(); context.writeExpr(context.getArgument(0), Precedence.min()); @@ -229,7 +228,7 @@ public class JSNativeInjector implements Injector, DependencyPlugin { } - private void renderProperty(Expr property, InjectorContext context) throws IOException { + private void renderProperty(Expr property, InjectorContext context) { SourceWriter writer = context.getWriter(); String name = extractPropertyName(property); if (name == null) { diff --git a/jso/impl/src/main/java/org/teavm/jso/impl/JSWrapperGenerator.java b/jso/impl/src/main/java/org/teavm/jso/impl/JSWrapperGenerator.java index 09f277faa..f1c4ad2eb 100644 --- a/jso/impl/src/main/java/org/teavm/jso/impl/JSWrapperGenerator.java +++ b/jso/impl/src/main/java/org/teavm/jso/impl/JSWrapperGenerator.java @@ -15,7 +15,6 @@ */ package org.teavm.jso.impl; -import java.io.IOException; import org.teavm.backend.javascript.rendering.Precedence; import org.teavm.backend.javascript.spi.Injector; import org.teavm.backend.javascript.spi.InjectorContext; @@ -29,7 +28,7 @@ public class JSWrapperGenerator implements Injector, DependencyPlugin { private DependencyNode externalClassesNode; @Override - public void generate(InjectorContext context, MethodReference methodRef) throws IOException { + public void generate(InjectorContext context, MethodReference methodRef) { switch (methodRef.getName()) { case "directJavaToJs": case "directJsToJava": diff --git a/platform/src/main/java/org/teavm/platform/plugin/AsyncMethodGenerator.java b/platform/src/main/java/org/teavm/platform/plugin/AsyncMethodGenerator.java index f49414346..2d3e6bd84 100644 --- a/platform/src/main/java/org/teavm/platform/plugin/AsyncMethodGenerator.java +++ b/platform/src/main/java/org/teavm/platform/plugin/AsyncMethodGenerator.java @@ -15,7 +15,6 @@ */ package org.teavm.platform.plugin; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.spi.Generator; import org.teavm.backend.javascript.spi.GeneratorContext; @@ -41,7 +40,7 @@ public class AsyncMethodGenerator implements Generator, DependencyPlugin, Virtua private JavaScriptTemplate template; @Override - public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { + public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { if (template == null) { var templateFactory = new JavaScriptTemplateFactory(context.getClassLoader(), context.getClassSource()); template = templateFactory.createFromResource("org/teavm/platform/plugin/Async.js"); diff --git a/platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceArray.java b/platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceArray.java index 451a79618..194ee101c 100644 --- a/platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceArray.java +++ b/platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceArray.java @@ -15,7 +15,6 @@ */ package org.teavm.platform.plugin; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.teavm.backend.javascript.codegen.SourceWriter; @@ -41,7 +40,7 @@ class BuildTimeResourceArray implements ResourceArray, Re } @Override - public void write(SourceWriter writer) throws IOException { + public void write(SourceWriter writer) { writer.append('[').tokenBoundary(); for (int i = 0; i < data.size(); ++i) { if (i > 0) { diff --git a/platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceMap.java b/platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceMap.java index 91317c92f..7e1a77b43 100644 --- a/platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceMap.java +++ b/platform/src/main/java/org/teavm/platform/plugin/BuildTimeResourceMap.java @@ -15,7 +15,6 @@ */ package org.teavm.platform.plugin; -import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.teavm.backend.javascript.codegen.SourceWriter; @@ -42,7 +41,7 @@ class BuildTimeResourceMap implements ResourceMap, Resour } @Override - public void write(SourceWriter writer) throws IOException { + public void write(SourceWriter writer) { writer.append('{'); boolean first = true; for (Map.Entry entry : data.entrySet()) { diff --git a/platform/src/main/java/org/teavm/platform/plugin/BuildTimeStaticFieldResource.java b/platform/src/main/java/org/teavm/platform/plugin/BuildTimeStaticFieldResource.java index bfa67491c..b535bea79 100644 --- a/platform/src/main/java/org/teavm/platform/plugin/BuildTimeStaticFieldResource.java +++ b/platform/src/main/java/org/teavm/platform/plugin/BuildTimeStaticFieldResource.java @@ -15,7 +15,6 @@ */ package org.teavm.platform.plugin; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.model.FieldReference; import org.teavm.platform.metadata.StaticFieldResource; @@ -32,7 +31,7 @@ class BuildTimeStaticFieldResource implements StaticFieldResource, ResourceWrite } @Override - public void write(SourceWriter writer) throws IOException { + public void write(SourceWriter writer) { writer.appendField(field); } } diff --git a/platform/src/main/java/org/teavm/platform/plugin/MetadataProviderNativeGenerator.java b/platform/src/main/java/org/teavm/platform/plugin/MetadataProviderNativeGenerator.java index 21c22dae1..32d5bff67 100644 --- a/platform/src/main/java/org/teavm/platform/plugin/MetadataProviderNativeGenerator.java +++ b/platform/src/main/java/org/teavm/platform/plugin/MetadataProviderNativeGenerator.java @@ -15,7 +15,6 @@ */ package org.teavm.platform.plugin; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.spi.Generator; import org.teavm.backend.javascript.spi.GeneratorContext; @@ -33,7 +32,7 @@ class MetadataProviderNativeGenerator implements Generator { } @Override - public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { + public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { DefaultMetadataGeneratorContext metadataContext = new DefaultMetadataGeneratorContext(context.getClassSource(), context.getClassLoader(), context.getProperties(), context); diff --git a/platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java b/platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java index 8b37339fe..ee0c358e9 100644 --- a/platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java +++ b/platform/src/main/java/org/teavm/platform/plugin/PlatformGenerator.java @@ -15,7 +15,6 @@ */ package org.teavm.platform.plugin; -import java.io.IOException; import java.lang.annotation.Annotation; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.spi.Generator; @@ -75,7 +74,7 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin } @Override - public void generate(InjectorContext context, MethodReference methodRef) throws IOException { + public void generate(InjectorContext context, MethodReference methodRef) { switch (methodRef.getName()) { case "asJavaClass": case "classFromResource": @@ -92,7 +91,7 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin } @Override - public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { + public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { switch (methodRef.getName()) { case "prepareNewInstance": generatePrepareNewInstance(context, writer); @@ -112,8 +111,7 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin } } - private void generateWithTemplate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) - throws IOException { + private void generateWithTemplate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { if (template == null) { template = new JavaScriptTemplateFactory(context.getClassLoader(), context.getClassSource()) .createFromResource("org/teavm/platform/plugin/Platform.js"); @@ -121,7 +119,7 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin template.builder(methodRef.getName()).withContext(context).build().write(writer, 0); } - private void generatePrepareNewInstance(GeneratorContext context, SourceWriter writer) throws IOException { + private void generatePrepareNewInstance(GeneratorContext context, SourceWriter writer) { MethodDependencyInfo newInstanceMethod = context.getDependency().getMethod( new MethodReference(Platform.class, "newInstanceImpl", PlatformClass.class, Object.class)); writer.append("let c").ws().append("=").ws().append("'$$constructor$$';").softNewLine(); @@ -140,7 +138,7 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin } } - private void generateLookup(GeneratorContext context, SourceWriter writer) throws IOException { + private void generateLookup(GeneratorContext context, SourceWriter writer) { String param = context.getParameterName(1); writer.append("switch").ws().append("(").appendFunction("$rt_ustr").append("(" + param + "))") .ws().append("{").softNewLine().indent(); @@ -153,7 +151,7 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin } - private void generateEnumConstants(GeneratorContext context, SourceWriter writer) throws IOException { + private void generateEnumConstants(GeneratorContext context, SourceWriter writer) { writer.append("let c").ws().append("=").ws().append("'$$enumConstants$$';").softNewLine(); for (String clsName : context.getClassSource().getClassNames()) { ClassReader cls = context.getClassSource().get(clsName); @@ -184,7 +182,7 @@ public class PlatformGenerator implements Generator, Injector, DependencyPlugin .append(");").softNewLine(); } - private void generateAnnotations(GeneratorContext context, SourceWriter writer) throws IOException { + private void generateAnnotations(GeneratorContext context, SourceWriter writer) { writer.append("let c").ws().append("=").ws().append("'$$annotations$$';").softNewLine(); for (String clsName : context.getClassSource().getClassNames()) { ClassReader annotCls = context.getClassSource().get(clsName + "$$__annotations__$$"); diff --git a/platform/src/main/java/org/teavm/platform/plugin/PlatformQueueGenerator.java b/platform/src/main/java/org/teavm/platform/plugin/PlatformQueueGenerator.java index 8541c36ae..8546fa5e5 100644 --- a/platform/src/main/java/org/teavm/platform/plugin/PlatformQueueGenerator.java +++ b/platform/src/main/java/org/teavm/platform/plugin/PlatformQueueGenerator.java @@ -15,7 +15,6 @@ */ package org.teavm.platform.plugin; -import java.io.IOException; import org.teavm.backend.javascript.spi.Injector; import org.teavm.backend.javascript.spi.InjectorContext; import org.teavm.dependency.DependencyAgent; @@ -34,7 +33,7 @@ public class PlatformQueueGenerator implements Injector, DependencyPlugin { } @Override - public void generate(InjectorContext context, MethodReference methodRef) throws IOException { + public void generate(InjectorContext context, MethodReference methodRef) { context.writeExpr(context.getArgument(0)); } } diff --git a/platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorGenerator.java b/platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorGenerator.java index 4d0a1802e..7413c7da1 100644 --- a/platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorGenerator.java +++ b/platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorGenerator.java @@ -15,7 +15,6 @@ */ package org.teavm.platform.plugin; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.spi.Generator; import org.teavm.backend.javascript.spi.GeneratorContext; @@ -23,7 +22,7 @@ import org.teavm.model.MethodReference; class ResourceAccessorGenerator implements Generator { @Override - public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { + public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) { writer.append("let result").ws().append("=").ws().append("[];").softNewLine(); writer.append("for").ws().append("(let key in ").append(context.getParameterName(1)).append(")").ws() .append("{").indent().softNewLine(); diff --git a/platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorInjector.java b/platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorInjector.java index 955748751..b7d0d1598 100644 --- a/platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorInjector.java +++ b/platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorInjector.java @@ -15,7 +15,6 @@ */ package org.teavm.platform.plugin; -import java.io.IOException; import org.teavm.ast.ConstantExpr; import org.teavm.ast.Expr; import org.teavm.backend.javascript.spi.Injector; @@ -25,7 +24,7 @@ import org.teavm.model.ValueType; class ResourceAccessorInjector implements Injector { @Override - public void generate(InjectorContext context, MethodReference methodRef) throws IOException { + public void generate(InjectorContext context, MethodReference methodRef) { switch (methodRef.getName()) { case "get": case "getProperty": @@ -102,7 +101,7 @@ class ResourceAccessorInjector implements Injector { } } - private void writePropertyAccessor(InjectorContext context, Expr property) throws IOException { + private void writePropertyAccessor(InjectorContext context, Expr property) { if (property instanceof ConstantExpr) { String str = (String) ((ConstantExpr) property).getValue(); if (str.isEmpty()) { @@ -119,7 +118,7 @@ class ResourceAccessorInjector implements Injector { context.getWriter().append(")]"); } - private void writeStringExpr(InjectorContext context, Expr expr) throws IOException { + private void writeStringExpr(InjectorContext context, Expr expr) { if (expr instanceof ConstantExpr) { String str = (String) ((ConstantExpr) expr).getValue(); context.getWriter().append('"'); diff --git a/platform/src/main/java/org/teavm/platform/plugin/ResourceWriter.java b/platform/src/main/java/org/teavm/platform/plugin/ResourceWriter.java index 5247b185c..c5447ecd5 100644 --- a/platform/src/main/java/org/teavm/platform/plugin/ResourceWriter.java +++ b/platform/src/main/java/org/teavm/platform/plugin/ResourceWriter.java @@ -15,9 +15,8 @@ */ package org.teavm.platform.plugin; -import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; public interface ResourceWriter { - void write(SourceWriter writer) throws IOException; + void write(SourceWriter writer); } diff --git a/platform/src/main/java/org/teavm/platform/plugin/ResourceWriterHelper.java b/platform/src/main/java/org/teavm/platform/plugin/ResourceWriterHelper.java index 09da2309a..9727414b1 100644 --- a/platform/src/main/java/org/teavm/platform/plugin/ResourceWriterHelper.java +++ b/platform/src/main/java/org/teavm/platform/plugin/ResourceWriterHelper.java @@ -23,7 +23,7 @@ final class ResourceWriterHelper { private ResourceWriterHelper() { } - public static void write(SourceWriter writer, Object resource) throws IOException { + public static void write(SourceWriter writer, Object resource) { if (resource == null) { writer.append("null"); } else { diff --git a/tests/src/test/java/org/teavm/incremental/EntryPointGenerator.java b/tests/src/test/java/org/teavm/incremental/EntryPointGenerator.java index 96c6c200e..1dbca3292 100644 --- a/tests/src/test/java/org/teavm/incremental/EntryPointGenerator.java +++ b/tests/src/test/java/org/teavm/incremental/EntryPointGenerator.java @@ -15,7 +15,6 @@ */ package org.teavm.incremental; -import java.io.IOException; import org.teavm.backend.javascript.spi.Injector; import org.teavm.backend.javascript.spi.InjectorContext; import org.teavm.model.FieldReference; @@ -23,7 +22,7 @@ import org.teavm.model.MethodReference; public class EntryPointGenerator implements Injector { @Override - public void generate(InjectorContext context, MethodReference methodRef) throws IOException { + public void generate(InjectorContext context, MethodReference methodRef) { context.getWriter().append("main.result = ("); context.writeExpr(context.getArgument(0)); context.getWriter().append(").").appendField(new FieldReference("java.lang.String", "nativeString")); diff --git a/tools/junit/src/main/java/org/teavm/junit/TestExceptionPlugin.java b/tools/junit/src/main/java/org/teavm/junit/TestExceptionPlugin.java index c3807249b..b8a04fc99 100644 --- a/tools/junit/src/main/java/org/teavm/junit/TestExceptionPlugin.java +++ b/tools/junit/src/main/java/org/teavm/junit/TestExceptionPlugin.java @@ -15,7 +15,6 @@ */ package org.teavm.junit; -import java.io.IOException; import org.teavm.backend.javascript.TeaVMJavaScriptHost; import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.rendering.RenderingManager; @@ -51,18 +50,18 @@ class TestExceptionPlugin implements TeaVMPlugin { RenderingManager manager; @Override - public void begin(RenderingManager manager, BuildTarget buildTarget) throws IOException { + public void begin(RenderingManager manager, BuildTarget buildTarget) { this.manager = manager; } @Override - public void complete() throws IOException { + public void complete() { renderExceptionMessage(manager.getWriter()); } }); } - private void renderExceptionMessage(SourceWriter writer) throws IOException { + private void renderExceptionMessage(SourceWriter writer) { writer.appendClass("java.lang.Throwable").append(".prototype.getMessage").ws().append("=").ws() .append("function()").ws().append("{").indent().softNewLine(); writer.append("return ").appendFunction("$rt_ustr").append("(this.")