Search in sources :

Example 11 with AstNode

use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.

the class IRFactory method decompileFunctionHeader.

Node decompileFunctionHeader(FunctionNode fn) {
    Node mexpr = null;
    if (fn.getFunctionName() != null) {
        decompiler.addName(fn.getName());
    } else if (fn.getMemberExprNode() != null) {
        mexpr = transform(fn.getMemberExprNode());
    }
    boolean isArrow = fn.getFunctionType() == FunctionNode.ARROW_FUNCTION;
    boolean noParen = isArrow && fn.getLp() == -1;
    if (!noParen) {
        decompiler.addToken(Token.LP);
    }
    List<AstNode> params = fn.getParams();
    for (int i = 0; i < params.size(); i++) {
        decompile(params.get(i));
        if (i < params.size() - 1) {
            decompiler.addToken(Token.COMMA);
        }
    }
    if (!noParen) {
        decompiler.addToken(Token.RP);
    }
    if (isArrow) {
        decompiler.addToken(Token.ARROW);
    }
    if (!fn.isExpressionClosure()) {
        decompiler.addEOL(Token.LC);
    }
    return mexpr;
}
Also used : ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) AstNode(org.mozilla.javascript.ast.AstNode)

Example 12 with AstNode

use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.

the class IRFactory method transformScript.

private Node transformScript(ScriptNode node) {
    decompiler.addToken(Token.SCRIPT);
    if (currentScope != null)
        Kit.codeBug();
    currentScope = node;
    Node body = new Node(Token.BLOCK);
    for (Node kid : node) {
        body.addChildToBack(transform((AstNode) kid));
    }
    node.removeChildren();
    Node children = body.getFirstChild();
    if (children != null) {
        node.addChildrenToBack(children);
    }
    return node;
}
Also used : ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) AstNode(org.mozilla.javascript.ast.AstNode)

Example 13 with AstNode

use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.

the class IRFactory method genExprTransformHelper.

private Node genExprTransformHelper(GeneratorExpression node) {
    decompiler.addToken(Token.LP);
    int lineno = node.getLineno();
    Node expr = transform(node.getResult());
    List<GeneratorExpressionLoop> loops = node.getLoops();
    int numLoops = loops.size();
    // Walk through loops, collecting and defining their iterator symbols.
    Node[] iterators = new Node[numLoops];
    Node[] iteratedObjs = new Node[numLoops];
    for (int i = 0; i < numLoops; i++) {
        GeneratorExpressionLoop acl = loops.get(i);
        decompiler.addName(" ");
        decompiler.addToken(Token.FOR);
        decompiler.addToken(Token.LP);
        AstNode iter = acl.getIterator();
        String name = null;
        if (iter.getType() == Token.NAME) {
            name = iter.getString();
            decompiler.addName(name);
        } else {
            // destructuring assignment
            decompile(iter);
            name = currentScriptOrFn.getNextTempName();
            defineSymbol(Token.LP, name, false);
            expr = createBinary(Token.COMMA, createAssignment(Token.ASSIGN, iter, createName(name)), expr);
        }
        Node init = createName(name);
        // Define as a let since we want the scope of the variable to
        // be restricted to the array comprehension
        defineSymbol(Token.LET, name, false);
        iterators[i] = init;
        if (acl.isForOf()) {
            decompiler.addName("of ");
        } else {
            decompiler.addToken(Token.IN);
        }
        iteratedObjs[i] = transform(acl.getIteratedObject());
        decompiler.addToken(Token.RP);
    }
    // generate code for tmpArray.push(body)
    Node yield = new Node(Token.YIELD, expr, node.getLineno());
    Node body = new Node(Token.EXPR_VOID, yield, lineno);
    if (node.getFilter() != null) {
        decompiler.addName(" ");
        decompiler.addToken(Token.IF);
        decompiler.addToken(Token.LP);
        body = createIf(transform(node.getFilter()), body, null, lineno);
        decompiler.addToken(Token.RP);
    }
    // Now walk loops in reverse to build up the body statement.
    int pushed = 0;
    try {
        for (int i = numLoops - 1; i >= 0; i--) {
            GeneratorExpressionLoop acl = loops.get(i);
            Scope loop = createLoopNode(// no label
            null, acl.getLineno());
            pushScope(loop);
            pushed++;
            body = createForIn(Token.LET, loop, iterators[i], iteratedObjs[i], body, acl.isForEach(), acl.isForOf());
        }
    } finally {
        for (int i = 0; i < pushed; i++) {
            popScope();
        }
    }
    decompiler.addToken(Token.RP);
    return body;
}
Also used : Scope(org.mozilla.javascript.ast.Scope) ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) GeneratorExpressionLoop(org.mozilla.javascript.ast.GeneratorExpressionLoop) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode)

Example 14 with AstNode

use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.

the class IRFactory method transformNewExpr.

private Node transformNewExpr(NewExpression node) {
    decompiler.addToken(Token.NEW);
    Node nx = createCallOrNew(Token.NEW, transform(node.getTarget()));
    nx.setLineno(node.getLineno());
    List<AstNode> args = node.getArguments();
    decompiler.addToken(Token.LP);
    for (int i = 0; i < args.size(); i++) {
        AstNode arg = args.get(i);
        nx.addChildToBack(transform(arg));
        if (i < args.size() - 1) {
            decompiler.addToken(Token.COMMA);
        }
    }
    decompiler.addToken(Token.RP);
    if (node.getInitializer() != null) {
        nx.addChildToBack(transformObjectLiteral(node.getInitializer()));
    }
    return nx;
}
Also used : ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) AstNode(org.mozilla.javascript.ast.AstNode)

Example 15 with AstNode

use of org.mozilla.javascript.ast.AstNode in project HL4A by HL4A.

the class IRFactory method transformAssignment.

private Node transformAssignment(Assignment node) {
    AstNode left = removeParens(node.getLeft());
    Node target = null;
    if (isDestructuring(left)) {
        decompile(left);
        target = left;
    } else {
        target = transform(left);
    }
    decompiler.addToken(node.getType());
    return createAssignment(node.getType(), target, transform(node.getRight()));
}
Also used : ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) AstNode(org.mozilla.javascript.ast.AstNode)

Aggregations

AstNode (org.mozilla.javascript.ast.AstNode)80 FunctionNode (org.mozilla.javascript.ast.FunctionNode)22 LetNode (org.mozilla.javascript.ast.LetNode)22 ScriptNode (org.mozilla.javascript.ast.ScriptNode)19 XmlString (org.mozilla.javascript.ast.XmlString)15 InfixExpression (org.mozilla.javascript.ast.InfixExpression)13 EmptyExpression (org.mozilla.javascript.ast.EmptyExpression)9 Comment (org.mozilla.javascript.ast.Comment)8 Name (org.mozilla.javascript.ast.Name)8 ArrayList (java.util.ArrayList)6 Block (org.mozilla.javascript.ast.Block)5 ExpressionStatement (org.mozilla.javascript.ast.ExpressionStatement)5 VariableDeclaration (org.mozilla.javascript.ast.VariableDeclaration)5 ArrayComprehensionLoop (org.mozilla.javascript.ast.ArrayComprehensionLoop)4 ErrorNode (org.mozilla.javascript.ast.ErrorNode)4 GeneratorExpressionLoop (org.mozilla.javascript.ast.GeneratorExpressionLoop)4 DoLoop (org.mozilla.javascript.ast.DoLoop)3 Scope (org.mozilla.javascript.ast.Scope)3 VariableInitializer (org.mozilla.javascript.ast.VariableInitializer)3 IOException (java.io.IOException)2