Search in sources :

Example 41 with AstNode

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

the class IRFactory method transformTry.

private Node transformTry(TryStatement node) {
    decompiler.addToken(Token.TRY);
    decompiler.addEOL(Token.LC);
    Node tryBlock = transform(node.getTryBlock());
    decompiler.addEOL(Token.RC);
    Node catchBlocks = new Block();
    for (CatchClause cc : node.getCatchClauses()) {
        decompiler.addToken(Token.CATCH);
        decompiler.addToken(Token.LP);
        String varName = cc.getVarName().getIdentifier();
        decompiler.addName(varName);
        Node catchCond = null;
        AstNode ccc = cc.getCatchCondition();
        if (ccc != null) {
            decompiler.addName(" ");
            decompiler.addToken(Token.IF);
            catchCond = transform(ccc);
        } else {
            catchCond = new EmptyExpression();
        }
        decompiler.addToken(Token.RP);
        decompiler.addEOL(Token.LC);
        Node body = transform(cc.getBody());
        decompiler.addEOL(Token.RC);
        catchBlocks.addChildToBack(createCatch(varName, catchCond, body, cc.getLineno()));
    }
    Node finallyBlock = null;
    if (node.getFinallyBlock() != null) {
        decompiler.addToken(Token.FINALLY);
        decompiler.addEOL(Token.LC);
        finallyBlock = transform(node.getFinallyBlock());
        decompiler.addEOL(Token.RC);
    }
    return createTryCatchFinally(tryBlock, catchBlocks, finallyBlock, node.getLineno());
}
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) Block(org.mozilla.javascript.ast.Block) CatchClause(org.mozilla.javascript.ast.CatchClause) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression)

Example 42 with AstNode

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

the class IRFactory method transformVariableInitializers.

private Node transformVariableInitializers(VariableDeclaration node) {
    List<VariableInitializer> vars = node.getVariables();
    int size = vars.size(), i = 0;
    for (VariableInitializer var : vars) {
        AstNode target = var.getTarget();
        AstNode init = var.getInitializer();
        Node left = null;
        if (var.isDestructuring()) {
            // decompile but don't transform
            decompile(target);
            left = target;
        } else {
            left = transform(target);
        }
        Node right = null;
        if (init != null) {
            decompiler.addToken(Token.ASSIGN);
            right = transform(init);
        }
        if (var.isDestructuring()) {
            if (right == null) {
                // TODO:  should this ever happen?
                node.addChildToBack(left);
            } else {
                Node d = createDestructuringAssignment(node.getType(), left, right);
                node.addChildToBack(d);
            }
        } else {
            if (right != null) {
                left.addChildToBack(right);
            }
            node.addChildToBack(left);
        }
        if (i++ < size - 1) {
            decompiler.addToken(Token.COMMA);
        }
    }
    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) VariableInitializer(org.mozilla.javascript.ast.VariableInitializer) AstNode(org.mozilla.javascript.ast.AstNode)

Example 43 with AstNode

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

the class IRFactory method transformSwitch.

private Node transformSwitch(SwitchStatement node) {
    // The switch will be rewritten from:
    // 
    // switch (expr) {
    // case test1: statements1;
    // ...
    // default: statementsDefault;
    // ...
    // case testN: statementsN;
    // }
    // 
    // to:
    // 
    // {
    // switch (expr) {
    // case test1: goto label1;
    // ...
    // case testN: goto labelN;
    // }
    // goto labelDefault;
    // label1:
    // statements1;
    // ...
    // labelDefault:
    // statementsDefault;
    // ...
    // labelN:
    // statementsN;
    // breakLabel:
    // }
    // 
    // where inside switch each "break;" without label will be replaced
    // by "goto breakLabel".
    // 
    // If the original switch does not have the default label, then
    // after the switch he transformed code would contain this goto:
    // goto breakLabel;
    // instead of:
    // goto labelDefault;
    decompiler.addToken(Token.SWITCH);
    decompiler.addToken(Token.LP);
    Node switchExpr = transform(node.getExpression());
    decompiler.addToken(Token.RP);
    node.addChildToBack(switchExpr);
    Node block = new Node(Token.BLOCK, node, node.getLineno());
    decompiler.addEOL(Token.LC);
    for (SwitchCase sc : node.getCases()) {
        AstNode expr = sc.getExpression();
        Node caseExpr = null;
        if (expr != null) {
            decompiler.addToken(Token.CASE);
            caseExpr = transform(expr);
        } else {
            decompiler.addToken(Token.DEFAULT);
        }
        decompiler.addEOL(Token.COLON);
        List<AstNode> stmts = sc.getStatements();
        Node body = new Block();
        if (stmts != null) {
            for (AstNode kid : stmts) {
                body.addChildToBack(transform(kid));
            }
        }
        addSwitchCase(block, caseExpr, body);
    }
    decompiler.addEOL(Token.RC);
    closeSwitch(block);
    return block;
}
Also used : SwitchCase(org.mozilla.javascript.ast.SwitchCase) ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) Block(org.mozilla.javascript.ast.Block) AstNode(org.mozilla.javascript.ast.AstNode)

Example 44 with AstNode

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

the class IRFactory method transformForInLoop.

private Node transformForInLoop(ForInLoop loop) {
    decompiler.addToken(Token.FOR);
    if (loop.isForEach())
        decompiler.addName("each ");
    decompiler.addToken(Token.LP);
    loop.setType(Token.LOOP);
    pushScope(loop);
    try {
        int declType = -1;
        AstNode iter = loop.getIterator();
        if (iter instanceof VariableDeclaration) {
            declType = ((VariableDeclaration) iter).getType();
        }
        Node lhs = transform(iter);
        if (loop.isForOf()) {
            decompiler.addName("of ");
        } else {
            decompiler.addToken(Token.IN);
        }
        Node obj = transform(loop.getIteratedObject());
        decompiler.addToken(Token.RP);
        decompiler.addEOL(Token.LC);
        Node body = transform(loop.getBody());
        decompiler.addEOL(Token.RC);
        return createForIn(declType, loop, lhs, obj, body, loop.isForEach(), loop.isForOf());
    } finally {
        popScope();
    }
}
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) VariableDeclaration(org.mozilla.javascript.ast.VariableDeclaration) AstNode(org.mozilla.javascript.ast.AstNode)

Example 45 with AstNode

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

the class IRFactory method transformArrayLiteral.

private Node transformArrayLiteral(ArrayLiteral node) {
    if (node.isDestructuring()) {
        return node;
    }
    decompiler.addToken(Token.LB);
    List<AstNode> elems = node.getElements();
    Node array = new Node(Token.ARRAYLIT);
    List<Integer> skipIndexes = null;
    for (int i = 0; i < elems.size(); ++i) {
        AstNode elem = elems.get(i);
        if (elem.getType() != Token.EMPTY) {
            array.addChildToBack(transform(elem));
        } else {
            if (skipIndexes == null) {
                skipIndexes = new ArrayList<Integer>();
            }
            skipIndexes.add(i);
        }
        if (i < elems.size() - 1)
            decompiler.addToken(Token.COMMA);
    }
    decompiler.addToken(Token.RB);
    array.putIntProp(Node.DESTRUCTURING_ARRAY_LENGTH, node.getDestructuringLength());
    if (skipIndexes != null) {
        int[] skips = new int[skipIndexes.size()];
        for (int i = 0; i < skipIndexes.size(); i++) skips[i] = skipIndexes.get(i);
        array.putProp(Node.SKIP_INDEXES_PROP, skips);
    }
    return array;
}
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