mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
JS: get rid of IOException in SourceWriter and related classes
This commit is contained in:
parent
32ee8943c1
commit
23ad999bbd
|
@ -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("<init>", 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);
|
||||
|
|
|
@ -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":
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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<ProviderContext, Inj
|
|||
}
|
||||
|
||||
@Override
|
||||
public void generate(InjectorContext context, MethodReference methodRef) throws IOException {
|
||||
public void generate(InjectorContext context, MethodReference methodRef) {
|
||||
switch (methodRef.getName()) {
|
||||
case "initWithEmptyChars":
|
||||
initWithEmptyChars(context);
|
||||
|
@ -104,13 +103,13 @@ public class JSStringInjector implements Injector, Function<ProviderContext, Inj
|
|||
}
|
||||
}
|
||||
|
||||
private void initWithEmptyChars(InjectorContext context) throws IOException {
|
||||
private void initWithEmptyChars(InjectorContext context) {
|
||||
var writer = context.getWriter();
|
||||
context.writeExpr(context.getArgument(0));
|
||||
writer.append(".").appendField(NATIVE_FIELD).ws().append("=").ws().append("\"\"");
|
||||
}
|
||||
|
||||
private void borrowChars(InjectorContext context) throws IOException {
|
||||
private void borrowChars(InjectorContext context) {
|
||||
var writer = context.getWriter();
|
||||
context.writeExpr(context.getArgument(0));
|
||||
writer.append(".").appendField(NATIVE_FIELD).ws().append("=").ws();
|
||||
|
@ -118,7 +117,7 @@ public class JSStringInjector implements Injector, Function<ProviderContext, Inj
|
|||
writer.append(".").appendField(NATIVE_FIELD);
|
||||
}
|
||||
|
||||
private void initWithCharArray(InjectorContext context) throws IOException {
|
||||
private void initWithCharArray(InjectorContext context) {
|
||||
var writer = context.getWriter();
|
||||
context.writeExpr(context.getArgument(0));
|
||||
writer.append(".").appendField(NATIVE_FIELD).ws().append("=").ws();
|
||||
|
@ -131,7 +130,7 @@ public class JSStringInjector implements Injector, Function<ProviderContext, Inj
|
|||
writer.append(")");
|
||||
}
|
||||
|
||||
private void takeCharArray(InjectorContext context) throws IOException {
|
||||
private void takeCharArray(InjectorContext context) {
|
||||
var writer = context.getWriter();
|
||||
context.writeExpr(context.getArgument(0));
|
||||
writer.append(".").appendField(NATIVE_FIELD).ws().append("=").ws();
|
||||
|
@ -140,13 +139,13 @@ public class JSStringInjector implements Injector, Function<ProviderContext, Inj
|
|||
writer.append(".data)");
|
||||
}
|
||||
|
||||
private void charactersLength(InjectorContext context) throws IOException {
|
||||
private void charactersLength(InjectorContext context) {
|
||||
var writer = context.getWriter();
|
||||
context.writeExpr(context.getArgument(0));
|
||||
writer.append(".").appendField(NATIVE_FIELD).append(".length");
|
||||
}
|
||||
|
||||
private void charactersGet(InjectorContext context) throws IOException {
|
||||
private void charactersGet(InjectorContext context) {
|
||||
var writer = context.getWriter();
|
||||
context.writeExpr(context.getArgument(0));
|
||||
writer.append(".").appendField(NATIVE_FIELD).append(".charCodeAt(");
|
||||
|
@ -154,7 +153,7 @@ public class JSStringInjector implements Injector, Function<ProviderContext, Inj
|
|||
writer.append(")");
|
||||
}
|
||||
|
||||
private void copyCharsToArray(InjectorContext context) throws IOException {
|
||||
private void copyCharsToArray(InjectorContext context) {
|
||||
var writer = context.getWriter();
|
||||
writer.appendFunction("$rt_stringToCharArray").append("(");
|
||||
context.writeExpr(context.getArgument(0));
|
||||
|
@ -171,7 +170,7 @@ public class JSStringInjector implements Injector, Function<ProviderContext, Inj
|
|||
writer.append(")");
|
||||
}
|
||||
|
||||
private void substringJS(InjectorContext context) throws IOException {
|
||||
private void substringJS(InjectorContext context) {
|
||||
var writer = context.getWriter();
|
||||
context.writeExpr(context.getArgument(0));
|
||||
writer.append(".substring(");
|
||||
|
@ -181,7 +180,7 @@ public class JSStringInjector implements Injector, Function<ProviderContext, Inj
|
|||
writer.append(")");
|
||||
}
|
||||
|
||||
private void fastCharArray(InjectorContext context) throws IOException {
|
||||
private void fastCharArray(InjectorContext context) {
|
||||
var writer = context.getWriter();
|
||||
writer.appendFunction("$rt_fastStringToCharArray").append("(");
|
||||
context.writeExpr(context.getArgument(0));
|
||||
|
@ -189,44 +188,44 @@ public class JSStringInjector implements Injector, Function<ProviderContext, Inj
|
|||
writer.append(")");
|
||||
}
|
||||
|
||||
private void nativeString(InjectorContext context) throws IOException {
|
||||
private void nativeString(InjectorContext context) {
|
||||
var writer = context.getWriter();
|
||||
context.writeExpr(context.getArgument(0));
|
||||
writer.append(".").appendField(NATIVE_FIELD);
|
||||
}
|
||||
|
||||
private void toLowerCaseJS(InjectorContext context) throws IOException {
|
||||
private void toLowerCaseJS(InjectorContext context) {
|
||||
var writer = context.getWriter();
|
||||
context.writeExpr(context.getArgument(0));
|
||||
writer.append(".toLowerCase()");
|
||||
}
|
||||
|
||||
private void toUpperCaseJS(InjectorContext context) throws IOException {
|
||||
private void toUpperCaseJS(InjectorContext context) {
|
||||
var writer = context.getWriter();
|
||||
context.writeExpr(context.getArgument(0));
|
||||
writer.append(".toUpperCase()");
|
||||
}
|
||||
|
||||
private void intern(InjectorContext context) throws IOException {
|
||||
private void intern(InjectorContext context) {
|
||||
var writer = context.getWriter();
|
||||
writer.appendFunction("$rt_intern").append("(");
|
||||
context.writeExpr(context.getArgument(0));
|
||||
writer.append(")");
|
||||
}
|
||||
|
||||
private void stripJS(InjectorContext context) throws IOException {
|
||||
private void stripJS(InjectorContext context) {
|
||||
var writer = context.getWriter();
|
||||
context.writeExpr(context.getArgument(0));
|
||||
writer.append(".trim()");
|
||||
}
|
||||
|
||||
private void stripLeadingJS(InjectorContext context) throws IOException {
|
||||
private void stripLeadingJS(InjectorContext context) {
|
||||
var writer = context.getWriter();
|
||||
context.writeExpr(context.getArgument(0));
|
||||
writer.append(".trimStart()");
|
||||
}
|
||||
|
||||
private void stripTrailingJS(InjectorContext context) throws IOException {
|
||||
private void stripTrailingJS(InjectorContext context) {
|
||||
var writer = context.getWriter();
|
||||
context.writeExpr(context.getArgument(0));
|
||||
writer.append(".trimEnd()");
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
*/
|
||||
package org.teavm.classlib.java.lang;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
import org.teavm.backend.javascript.codegen.SourceWriter;
|
||||
import org.teavm.backend.javascript.rendering.Precedence;
|
||||
|
@ -43,7 +42,7 @@ public class ClassGenerator implements Generator, Injector, DependencyPlugin {
|
|||
new FieldReference(Class.class.getName(), "platformClass");
|
||||
|
||||
@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 "createMetadata":
|
||||
generateCreateMetadata(context, writer);
|
||||
|
@ -52,7 +51,7 @@ public class ClassGenerator 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 "newEmptyInstance":
|
||||
context.getWriter().append("new").ws().append("(");
|
||||
|
@ -133,7 +132,7 @@ public class ClassGenerator implements Generator, Injector, DependencyPlugin {
|
|||
});
|
||||
}
|
||||
|
||||
private void generateCreateMetadata(GeneratorContext context, SourceWriter writer) throws IOException {
|
||||
private void generateCreateMetadata(GeneratorContext context, SourceWriter writer) {
|
||||
ReflectionDependencyListener reflection = context.getService(ReflectionDependencyListener.class);
|
||||
for (String className : reflection.getClassesWithReflectableFields()) {
|
||||
generateCreateFieldsForClass(context, writer, className);
|
||||
|
@ -143,8 +142,7 @@ public class ClassGenerator implements Generator, Injector, DependencyPlugin {
|
|||
}
|
||||
}
|
||||
|
||||
private void generateCreateFieldsForClass(GeneratorContext context, SourceWriter writer, String className)
|
||||
throws IOException {
|
||||
private void generateCreateFieldsForClass(GeneratorContext context, SourceWriter writer, String className) {
|
||||
ReflectionDependencyListener reflection = context.getService(ReflectionDependencyListener.class);
|
||||
Set<String> 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<MethodDescriptor> accessibleMethods = reflection.getAccessibleMethods(className);
|
||||
|
||||
|
@ -219,7 +216,7 @@ public class ClassGenerator implements Generator, Injector, DependencyPlugin {
|
|||
}
|
||||
|
||||
private <T extends MemberReader> void generateCreateMembers(SourceWriter writer, Iterable<T> members,
|
||||
MemberRenderer<T> renderer) throws IOException {
|
||||
MemberRenderer<T> 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<T extends MemberReader> {
|
||||
void render(T member) throws IOException;
|
||||
void render(T member);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -458,7 +458,7 @@ public class JavaScriptTarget implements TeaVMTarget, TeaVMJavaScriptHost {
|
|||
for (var entry : methodInjectors.entrySet()) {
|
||||
renderingContext.addInjector(entry.getKey(), entry.getValue());
|
||||
}
|
||||
try {
|
||||
|
||||
printWrapperStart(sourceWriter);
|
||||
|
||||
for (RendererListener listener : rendererListeners) {
|
||||
|
@ -504,12 +504,9 @@ public class JavaScriptTarget implements TeaVMTarget, TeaVMJavaScriptHost {
|
|||
|
||||
int totalSize = sourceWriter.getOffset() - start;
|
||||
printStats(renderer, totalSize);
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO Error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
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;
|
||||
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) {
|
||||
try {
|
||||
for (int i = 0; i < indentSize; ++i) {
|
||||
innerWriter.append(" ");
|
||||
column += 4;
|
||||
offset += 4;
|
||||
}
|
||||
lineStart = false;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public SourceWriter newLine() throws IOException {
|
||||
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) {
|
||||
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) {
|
||||
try {
|
||||
innerWriter.append('\n');
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
column = 0;
|
||||
++offset;
|
||||
++line;
|
||||
|
|
|
@ -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 "<init>":
|
||||
|
@ -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");
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@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 "<init>":
|
||||
template.builder("init").withContext(context).build().write(writer, 0);
|
||||
|
|
|
@ -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<? extends AstNode> nodes) throws IOException {
|
||||
private void printList(List<? extends AstNode> 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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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,7 +180,6 @@ 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) {
|
||||
|
@ -192,13 +190,9 @@ public class Renderer implements RenderingManager {
|
|||
}
|
||||
writer.append("]);").newLine();
|
||||
stringPoolSize = writer.getOffset() - start;
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void renderStringConstants() throws RenderingException {
|
||||
try {
|
||||
for (PostponedFieldInitializer initializer : postponedFieldInitializers) {
|
||||
int start = writer.getOffset();
|
||||
writer.appendStaticField(initializer.field).ws().append("=").ws();
|
||||
|
@ -207,22 +201,15 @@ public class Renderer implements RenderingManager {
|
|||
int sz = writer.getOffset() - start;
|
||||
appendClassSize(initializer.field.getClassName(), sz);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void renderCompatibilityStubs() throws RenderingException {
|
||||
try {
|
||||
renderJavaStringToString();
|
||||
renderJavaObjectToString();
|
||||
renderTeaVMClass();
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error", e);
|
||||
}
|
||||
}
|
||||
|
||||
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,7 +269,6 @@ 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<FieldHolder> nonStaticFields = new ArrayList<>();
|
||||
List<FieldHolder> staticFields = new ArrayList<>();
|
||||
for (FieldHolder field : cls.getClassHolder().getFields()) {
|
||||
|
@ -321,13 +307,10 @@ public class Renderer implements RenderingManager {
|
|||
context.constantToString(writer, value);
|
||||
writer.append(";").softNewLine();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void renderFullClassFunctionDeclaration(PreparedClass cls, ScopedName jsName,
|
||||
List<FieldHolder> nonStaticFields) throws IOException {
|
||||
List<FieldHolder> 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,7 +364,7 @@ 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())) {
|
||||
|
@ -401,14 +384,11 @@ public class Renderer implements RenderingManager {
|
|||
for (PreparedMethod method : cls.getMethods()) {
|
||||
renderBody(method);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
|
||||
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,7 +464,7 @@ public class Renderer implements RenderingManager {
|
|||
ClassMetadataRequirements metadataRequirements = new ClassMetadataRequirements(context.getDependencyInfo());
|
||||
|
||||
int start = writer.getOffset();
|
||||
try {
|
||||
|
||||
writer.appendFunction("$rt_packages").append("([");
|
||||
ObjectIntMap<String> packageIndexes = generatePackageMetadata(classes, metadataRequirements);
|
||||
writer.append("]);").newLine();
|
||||
|
@ -494,15 +474,11 @@ public class Renderer implements RenderingManager {
|
|||
renderClassMetadataPortion(classes.subList(i, j), packageIndexes, metadataRequirements);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
|
||||
metadataSize = writer.getOffset() - start;
|
||||
}
|
||||
|
||||
private void renderClassMetadataPortion(List<PreparedClass> classes, ObjectIntMap<String> 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<String> generatePackageMetadata(List<PreparedClass> 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<String> indexes)
|
||||
throws IOException {
|
||||
private int writePackageStructure(PackageNode node, int startIndex, String prefix, ObjectIntMap<String> 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<MethodReference> methods) throws IOException {
|
||||
private void renderVirtualDeclarations(Collection<MethodReference> 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,18 +897,13 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(RegularMethodNode method) {
|
||||
try {
|
||||
statementRenderer.setAsync(false);
|
||||
this.async = false;
|
||||
MethodReference ref = method.getReference();
|
||||
|
@ -991,16 +961,11 @@ public class Renderer implements RenderingManager {
|
|||
|
||||
writer.outdent().append("}").softNewLine();
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(AsyncMethodNode methodNode) {
|
||||
threadLibraryUsed = true;
|
||||
try {
|
||||
statementRenderer.setAsync(true);
|
||||
this.async = true;
|
||||
MethodReference ref = methodNode.getReference();
|
||||
|
@ -1098,9 +1063,6 @@ public class Renderer implements RenderingManager {
|
|||
}
|
||||
writer.append(context.pointerName()).append(");");
|
||||
writer.softNewLine();
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
@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(")");
|
||||
|
|
|
@ -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++;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,7 +145,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
|
||||
@Override
|
||||
public void visit(AssignmentStatement statement) throws RenderingException {
|
||||
try {
|
||||
debugEmitter.emitStatementStart();
|
||||
if (statement.getLocation() != null) {
|
||||
pushLocation(statement.getLocation());
|
||||
|
@ -176,9 +174,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (statement.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -188,7 +183,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
|
||||
@Override
|
||||
public void visit(ConditionalStatement statement) {
|
||||
try {
|
||||
boolean needClosingBracket;
|
||||
while (true) {
|
||||
debugEmitter.emitStatementStart();
|
||||
|
@ -243,9 +237,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (needClosingBracket) {
|
||||
writer.append("}").softNewLine();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isSimpleIfContent(List<Statement> statements) {
|
||||
|
@ -259,7 +250,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
|
||||
@Override
|
||||
public void visit(SwitchStatement statement) {
|
||||
try {
|
||||
debugEmitter.emitStatementStart();
|
||||
if (statement.getValue().getLocation() != null) {
|
||||
pushLocation(statement.getValue().getLocation());
|
||||
|
@ -300,14 +290,10 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
writer.outdent();
|
||||
}
|
||||
writer.outdent().append("}").softNewLine();
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(WhileStatement statement) {
|
||||
try {
|
||||
debugEmitter.emitStatementStart();
|
||||
if (statement.getId() != null) {
|
||||
writer.append(mapBlockId(statement.getId())).append(":").ws();
|
||||
|
@ -330,9 +316,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
}
|
||||
end = oldEnd;
|
||||
writer.outdent().append("}").softNewLine();
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String mapBlockId(String id) {
|
||||
|
@ -361,18 +344,13 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(BreakStatement statement) {
|
||||
try {
|
||||
debugEmitter.emitStatementStart();
|
||||
if (statement.getLocation() != null) {
|
||||
pushLocation(statement.getLocation());
|
||||
|
@ -385,14 +363,10 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (statement.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ContinueStatement statement) {
|
||||
try {
|
||||
debugEmitter.emitStatementStart();
|
||||
if (statement.getLocation() != null) {
|
||||
pushLocation(statement.getLocation());
|
||||
|
@ -405,14 +379,10 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (statement.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ReturnStatement statement) {
|
||||
try {
|
||||
debugEmitter.emitStatementStart();
|
||||
if (statement.getLocation() != null) {
|
||||
pushLocation(statement.getLocation());
|
||||
|
@ -429,14 +399,10 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (statement.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ThrowStatement statement) {
|
||||
try {
|
||||
debugEmitter.emitStatementStart();
|
||||
if (statement.getLocation() != null) {
|
||||
pushLocation(statement.getLocation());
|
||||
|
@ -450,9 +416,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (statement.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -465,7 +428,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (method == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
debugEmitter.emitStatementStart();
|
||||
if (statement.getLocation() != null) {
|
||||
pushLocation(statement.getLocation());
|
||||
|
@ -477,9 +439,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (statement.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
public String variableName(int index) {
|
||||
|
@ -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,7 +462,6 @@ 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()) {
|
||||
|
@ -557,9 +509,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (innerPrecedence.ordinal() < outerPrecedence.ordinal()) {
|
||||
writer.append(')');
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Precedence getPrecedence(BinaryOperation op) {
|
||||
|
@ -600,7 +549,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
}
|
||||
|
||||
private void visitBinaryFunction(BinaryExpr expr, String function) {
|
||||
try {
|
||||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
|
@ -615,9 +563,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -764,7 +709,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
|
||||
@Override
|
||||
public void visit(UnaryExpr expr) {
|
||||
try {
|
||||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
|
@ -869,15 +813,11 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(CastExpr expr) {
|
||||
if (context.isStrict()) {
|
||||
try {
|
||||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
|
@ -896,9 +836,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
} else {
|
||||
expr.getValue().acceptVisitor(this);
|
||||
}
|
||||
|
@ -915,7 +852,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
|
||||
@Override
|
||||
public void visit(PrimitiveCastExpr expr) {
|
||||
try {
|
||||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
|
@ -967,13 +903,7 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
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);
|
||||
}
|
||||
});
|
||||
() -> writer.append("0"));
|
||||
break;
|
||||
default:
|
||||
expr.getValue().acceptVisitor(this);
|
||||
|
@ -983,9 +913,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
private Expr extractLongRightShiftedBy32(Expr expr) {
|
||||
|
@ -1015,7 +942,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
|
||||
@Override
|
||||
public void visit(ConditionalExpr expr) {
|
||||
try {
|
||||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
|
@ -1041,14 +967,10 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ConstantExpr expr) {
|
||||
try {
|
||||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
|
@ -1056,14 +978,10 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(VariableExpr expr) {
|
||||
try {
|
||||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
|
@ -1071,14 +989,10 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(SubscriptExpr expr) {
|
||||
try {
|
||||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
|
@ -1091,14 +1005,10 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(UnwrapArrayExpr expr) {
|
||||
try {
|
||||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
|
@ -1108,14 +1018,10 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(InvocationExpr expr) {
|
||||
try {
|
||||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
|
@ -1207,14 +1113,10 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(QualificationExpr expr) {
|
||||
try {
|
||||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
|
@ -1230,14 +1132,10 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(NewExpr expr) {
|
||||
try {
|
||||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
|
@ -1257,14 +1155,10 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(NewArrayExpr expr) {
|
||||
try {
|
||||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
|
@ -1331,14 +1225,10 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ArrayFromDataExpr expr) {
|
||||
try {
|
||||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
|
@ -1384,12 +1274,9 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void writeCommaSeparated(List<Expr> expressions) throws IOException {
|
||||
private void writeCommaSeparated(List<Expr> expressions) {
|
||||
boolean first = true;
|
||||
for (Expr element : expressions) {
|
||||
if (!first) {
|
||||
|
@ -1403,7 +1290,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
|
||||
@Override
|
||||
public void visit(NewMultiArrayExpr expr) {
|
||||
try {
|
||||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
|
@ -1459,14 +1345,10 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(InstanceOfExpr expr) {
|
||||
try {
|
||||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
|
@ -1493,9 +1375,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void visitStatements(List<Statement> statements) {
|
||||
|
@ -1514,7 +1393,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
|
||||
@Override
|
||||
public void visit(TryCatchStatement statement) {
|
||||
try {
|
||||
writer.append("try").ws().append("{").softNewLine().indent();
|
||||
List<TryCatchStatement> sequence = new ArrayList<>();
|
||||
sequence.add(statement);
|
||||
|
@ -1574,14 +1452,10 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
writer.softNewLine();
|
||||
}
|
||||
writer.outdent().append("}").softNewLine();
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(GotoPartStatement statement) {
|
||||
try {
|
||||
if (statement.getPart() != currentPart) {
|
||||
writer.append(context.pointerName()).ws().append("=").ws().append(statement.getPart()).append(";")
|
||||
.softNewLine();
|
||||
|
@ -1589,14 +1463,10 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (!end || statement.getPart() != currentPart + 1) {
|
||||
writer.append("continue ").append(context.mainLoopName()).append(";").softNewLine();
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
throw new RenderingException("IO error occurred", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(MonitorEnterStatement statement) {
|
||||
try {
|
||||
if (async) {
|
||||
writer.appendMethodBody(NameFrequencyEstimator.MONITOR_ENTER_METHOD).append("(");
|
||||
precedence = Precedence.min();
|
||||
|
@ -1609,12 +1479,9 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
statement.getObjectRef().acceptVisitor(this);
|
||||
writer.append(");").softNewLine();
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
throw new RenderingException("IO error occurred", ex);
|
||||
}
|
||||
}
|
||||
|
||||
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,7 +1490,6 @@ 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();
|
||||
|
@ -1635,14 +1501,10 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
statement.getObjectRef().acceptVisitor(this);
|
||||
writer.append(");").softNewLine();
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
throw new RenderingException("IO error occurred", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(BoundCheckExpr expr) {
|
||||
try {
|
||||
if (expr.getLocation() != null) {
|
||||
pushLocation(expr.getLocation());
|
||||
}
|
||||
|
@ -1669,9 +1531,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
|
|||
if (expr.getLocation() != null) {
|
||||
popLocation();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RenderingException("IO error occurred", e);
|
||||
}
|
||||
}
|
||||
|
||||
private class InjectorContextImpl implements InjectorContext {
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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() {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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("(");
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<DependencyNode> 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) {
|
||||
|
|
|
@ -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":
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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<T extends Resource> implements ResourceArray<T>, 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) {
|
||||
|
|
|
@ -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<T extends Resource> implements ResourceMap<T>, Resour
|
|||
}
|
||||
|
||||
@Override
|
||||
public void write(SourceWriter writer) throws IOException {
|
||||
public void write(SourceWriter writer) {
|
||||
writer.append('{');
|
||||
boolean first = true;
|
||||
for (Map.Entry<String, T> entry : data.entrySet()) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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__$$");
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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('"');
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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"));
|
||||
|
|
|
@ -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.")
|
||||
|
|
Loading…
Reference in New Issue
Block a user