@Override public void emit(ListableClassHolderSource classes, BuildTarget target, String outputName) { try (OutputStream output = target.createResource(outputName); Writer writer = new OutputStreamWriter(output, StandardCharsets.UTF_8)) { emit(classes, writer, target); } catch (IOException e) { throw new RenderingException(e); } }
public void renderCompatibilityStubs() throws RenderingException { try { renderJavaStringToString(); renderJavaObjectToString(); renderTeaVMClass(); } catch (IOException e) { throw new RenderingException("IO error", e); } }
private void renderClassMetadata(List<ClassNode> classes) { if (classes.isEmpty()) { return; } Set<String> classesRequiringName = findClassesRequiringName(); int start = writer.getOffset(); try { writer.append("$rt_packages(["); ObjectIntMap<String> packageIndexes = generatePackageMetadata(classes, classesRequiringName); writer.append("]);").newLine(); for (int i = 0; i < classes.size(); i += 50) { int j = Math.min(i + 50, classes.size()); renderClassMetadataPortion(classes.subList(i, j), packageIndexes, classesRequiringName); } } catch (IOException e) { throw new RenderingException("IO error occurred", e); } metadataSize = writer.getOffset() - start; }
@Override public void visit(NativeMethodNode methodNode) { try { this.async = methodNode.isAsync(); statementRenderer.setAsync(methodNode.isAsync()); methodNode.getGenerator().generate(this, writer, methodNode.getReference()); } catch (IOException e) { throw new RenderingException("IO error occurred", e); } }
@Override public void begin(RenderingManager manager, BuildTarget buildTarget) throws IOException { boolean hasOneResource = false; for (String className : manager.getClassSource().getClassNames()) { ClassReader cls = manager.getClassSource().get(className); AnnotationReader annot = cls.getAnnotations().get(JavaScriptResource.class.getName()); if (annot == null) { continue; } String path = annot.getValue("value").getString(); String packageName = className.substring(0, className.lastIndexOf('.')); String resourceName = packageName.replace('.', '/') + "/" + path; try (InputStream input = manager.getClassLoader().getResourceAsStream(resourceName)) { if (input == null) { throw new RenderingException("Error processing JavaScriptResource annotation on class " + className + ". Resource not found: " + resourceName); } StringWriter writer = new StringWriter(); IOUtils.copy(input, writer); writer.close(); manager.getWriter().append("// Resource " + path + " included by " + className).newLine(); manager.getWriter().append(writer.toString()).newLine().newLine(); } hasOneResource = true; } if (hasOneResource) { manager.getWriter().append("// TeaVM generated classes").newLine(); } } }
public boolean render(List<ClassNode> classes) throws RenderingException { if (minifying) { try { renderRuntimeAliases(); } catch (IOException e) { throw new RenderingException(e); } } int index = 0; for (ClassNode cls : classes) { int start = writer.getOffset(); renderDeclaration(cls); renderMethodBodies(cls); appendClassSize(cls.getName(), writer.getOffset() - start); if (progressConsumer.apply(1000 * ++index / classes.size()) == TeaVMProgressFeedback.CANCEL) { return false; } } renderClassMetadata(classes); return true; }
public void renderStringPool() throws RenderingException { if (context.getStringPool().isEmpty()) { return; } try { int start = writer.getOffset(); writer.append("$rt_stringPool(["); for (int i = 0; i < context.getStringPool().size(); ++i) { if (i > 0) { writer.append(',').ws(); } RenderingUtil.writeString(writer, context.getStringPool().get(i)); } writer.append("]);").newLine(); stringPoolSize = writer.getOffset() - start; } catch (IOException e) { throw new RenderingException("IO error", e); } }
@Override public void visit(ConstantExpr expr) { try { if (expr.getLocation() != null) { pushLocation(expr.getLocation()); } String str = context.constantToString(expr.getValue()); if (str.startsWith("-")) { writer.append(' '); } writer.append(str); if (expr.getLocation() != null) { popLocation(); } } catch (IOException e) { throw new RenderingException("IO error occured", e); } }
@Override public void visit(VariableExpr expr) { try { if (expr.getLocation() != null) { pushLocation(expr.getLocation()); } writer.append(variableName(expr.getIndex())); if (expr.getLocation() != null) { popLocation(); } } catch (IOException e) { throw new RenderingException("IO error occured", e); } }
@Override public void visit(UnwrapArrayExpr expr) { try { if (expr.getLocation() != null) { pushLocation(expr.getLocation()); } precedence = Precedence.MEMBER_ACCESS; expr.getArray().acceptVisitor(this); writer.append(".data"); if (expr.getLocation() != null) { popLocation(); } } catch (IOException e) { throw new RenderingException("IO error occurred", e); } }
private void renderMethodBodies(ClassNode cls) throws RenderingException { debugEmitter.emitClass(cls.getName()); try { MethodReader clinit = classSource.get(cls.getName()).getMethod( new MethodDescriptor("<clinit>", ValueType.VOID)); if (clinit != null) { renderCallClinit(clinit, cls); } if (!cls.getModifiers().contains(ElementModifier.INTERFACE)) { for (MethodNode method : cls.getMethods()) { if (!method.getModifiers().contains(ElementModifier.STATIC)) { if (method.getReference().getName().equals("<init>")) { renderInitializer(cls.getName(), method); } } } } for (MethodNode method : cls.getMethods()) { renderBody(cls.getName(), method); } } catch (IOException e) { throw new RenderingException("IO error occurred", e); } debugEmitter.emitClass(null); }
throw new RenderingException("IO error occurred", e);
public void renderStringConstants() throws RenderingException { try { for (PostponedFieldInitializer initializer : postponedFieldInitializers) { int start = writer.getOffset(); writer.appendStaticField(initializer.field).ws().append("=").ws() .append(context.constantToString(initializer.value)).append(";").softNewLine(); int sz = writer.getOffset() - start; appendClassSize(initializer.field.getClassName(), sz); } } catch (IOException e) { throw new RenderingException("IO error", e); } }
@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(SubscriptExpr expr) { try { if (expr.getLocation() != null) { pushLocation(expr.getLocation()); } precedence = Precedence.MEMBER_ACCESS; expr.getArray().acceptVisitor(this); writer.append('['); precedence = Precedence.min(); expr.getIndex().acceptVisitor(this); writer.append(']'); if (expr.getLocation() != null) { popLocation(); } } catch (IOException e) { throw new RenderingException("IO error occurred", e); } }
public void renderRuntime() throws RenderingException { try { renderHandWrittenRuntime("runtime.js"); renderSetCloneMethod(); renderRuntimeCls(); renderRuntimeString(); renderRuntimeUnwrapString(); renderRuntimeObjcls(); renderRuntimeNullCheck(); renderRuntimeIntern(); renderRuntimeThreads(); renderRuntimeCreateException(); renderCreateStackTraceElement(); renderSetStackTrace(); } catch (IOException e) { throw new RenderingException("IO error", 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(); } 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(QualificationExpr expr) { try { if (expr.getLocation() != null) { pushLocation(expr.getLocation()); } precedence = Precedence.MEMBER_ACCESS; if (expr.getQualified() != null) { expr.getQualified().acceptVisitor(this); writer.append('.').appendField(expr.getField()); } else { writer.appendStaticField(expr.getField()); } if (expr.getLocation() != null) { popLocation(); } } catch (IOException e) { throw new RenderingException("IO error occurred", e); } }
@Override public void visit(MonitorExitStatement statement) { try { if (async) { writer.appendMethodBody(NameFrequencyEstimator.MONITOR_EXIT_METHOD).append("("); precedence = Precedence.min(); statement.getObjectRef().acceptVisitor(this); writer.append(");").softNewLine(); } else { writer.appendMethodBody(NameFrequencyEstimator.MONITOR_EXIT_SYNC_METHOD).append('('); precedence = Precedence.min(); statement.getObjectRef().acceptVisitor(this); writer.append(");").softNewLine(); } } catch (IOException ex) { throw new RenderingException("IO error occurred", ex); } }