}
}
@Override
public void visitForInStatement(final ForInStatement forInStatement) throws IOException {
final Ide exprAuxIde = forInStatement.getExprAuxIde();
IdeDeclaration exprType = forInStatement.getExpr().getType();
String exprTypeName = exprType != null ? exprType.getQualifiedNameStr() : "";
boolean iterateArrayMode = "Array".equals(exprTypeName) || "Vector$object".equals(exprTypeName);
if (exprAuxIde != null && !iterateArrayMode) {
new SemicolonTerminatedStatement(new VariableDeclaration(SYM_VAR, exprAuxIde, null, null), SYM_SEMICOLON).visit(this);
}
out.writeSymbol(forInStatement.getSymKeyword());
final boolean isForEach = forInStatement.getSymEach() != null;
if (isForEach) {
out.beginComment();
out.writeSymbol(forInStatement.getSymEach());
out.endComment();
}
out.writeSymbol(forInStatement.getLParen());
if (isForEach || iterateArrayMode) {
new VariableDeclaration(SYM_VAR, forInStatement.getAuxIde(), null, null).visit(this);
} else {
if (forInStatement.getDecl() != null) {
forInStatement.getDecl().visit(this);
} else {
forInStatement.getLValue().visit(this);
}
}
if (iterateArrayMode) {
String indexVarName = forInStatement.getAuxIde().getName();
out.write("=0");
if (exprAuxIde != null) {
out.write(",");
out.writeToken(exprAuxIde.getName());
out.writeToken("=");
out.beginComment();
out.writeSymbol(forInStatement.getSymIn());
out.endComment();
forInStatement.getExpr().visit(this);
}
out.write(";");
out.write(indexVarName);
out.write("<");
if (exprAuxIde != null) {
out.writeToken(exprAuxIde.getName());
} else {
out.beginComment();
out.writeSymbol(forInStatement.getSymIn());
out.endComment();
forInStatement.getExpr().visit(this);
}
out.write(".length;");
out.write("++" + indexVarName);
} else {
out.writeSymbol(forInStatement.getSymIn());
if (exprAuxIde != null) {
// assign the expression value to the auxiliary expression value variable once:
out.writeToken(exprAuxIde.getName());
out.writeToken("=");
}
forInStatement.getExpr().visit(this);
}
out.writeSymbol(forInStatement.getRParen());
if (isForEach || iterateArrayMode) {
// inject synthesized statement into loop body:
if (!(forInStatement.getBody() instanceof BlockStatement)) {
forInStatement.setBody(new BlockStatement(SYM_LBRACE, Arrays.<Directive>asList(forInStatement.getBody()), SYM_RBRACE));
}
((BlockStatement) forInStatement.getBody()).addBlockStartCodeGenerator(new CodeGenerator() {
@Override
public void generate(JsWriter out, boolean first) throws IOException {
// synthesize assigning the correct index to the variable given in the original for each statement:
if (forInStatement.getDecl() != null) {
forInStatement.getDecl().visit(JsCodeGenerator.this);
} else {
forInStatement.getLValue().visit(JsCodeGenerator.this);
}
out.writeToken("=");
if (!isForEach) {
out.write("String(" + forInStatement.getAuxIde().getName() + ")");
} else {
if (exprAuxIde == null) {
forInStatement.getExpr().visit(JsCodeGenerator.this);
} else {
out.write(exprAuxIde.getName());
}
out.write("[" + forInStatement.getAuxIde().getName() + "]");
}
out.write(";");
}