Search in sources :

Example 6 with FunctionNode

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

the class IRFactory method transformGenExpr.

private Node transformGenExpr(GeneratorExpression node) {
    Node pn;
    FunctionNode fn = new FunctionNode();
    fn.setSourceName(currentScriptOrFn.getNextTempName());
    fn.setIsGenerator();
    fn.setFunctionType(FunctionNode.FUNCTION_EXPRESSION);
    fn.setRequiresActivation();
    int functionType = fn.getFunctionType();
    int start = decompiler.markFunctionStart(functionType);
    Node mexpr = decompileFunctionHeader(fn);
    int index = currentScriptOrFn.addFunction(fn);
    PerFunctionVariables savedVars = new PerFunctionVariables(fn);
    try {
        // If we start needing to record much more codegen metadata during
        // function parsing, we should lump it all into a helper class.
        Node destructuring = (Node) fn.getProp(Node.DESTRUCTURING_PARAMS);
        fn.removeProp(Node.DESTRUCTURING_PARAMS);
        int lineno = node.lineno;
        // only for body, not params
        ++nestingOfFunction;
        Node body = genExprTransformHelper(node);
        if (!fn.isExpressionClosure()) {
            decompiler.addToken(Token.RC);
        }
        fn.setEncodedSourceBounds(start, decompiler.markFunctionEnd(start));
        if (functionType != FunctionNode.FUNCTION_EXPRESSION && !fn.isExpressionClosure()) {
            // Add EOL only if function is not part of expression
            // since it gets SEMI + EOL from Statement in that case
            decompiler.addToken(Token.EOL);
        }
        if (destructuring != null) {
            body.addChildToFront(new Node(Token.EXPR_VOID, destructuring, lineno));
        }
        int syntheticType = fn.getFunctionType();
        pn = initFunction(fn, index, body, syntheticType);
        if (mexpr != null) {
            pn = createAssignment(Token.ASSIGN, mexpr, pn);
            if (syntheticType != FunctionNode.FUNCTION_EXPRESSION) {
                pn = createExprStatementNoReturn(pn, fn.getLineno());
            }
        }
    } finally {
        --nestingOfFunction;
        savedVars.restore();
    }
    Node call = createCallOrNew(Token.CALL, pn);
    call.setLineno(node.getLineno());
    decompiler.addToken(Token.LP);
    decompiler.addToken(Token.RP);
    return call;
}
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) FunctionNode(org.mozilla.javascript.ast.FunctionNode)

Example 7 with FunctionNode

use of org.mozilla.javascript.ast.FunctionNode 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 8 with FunctionNode

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

the class Parser method methodDefinition.

private ObjectProperty methodDefinition(int pos, AstNode propName, int entryKind) throws IOException {
    FunctionNode fn = function(FunctionNode.FUNCTION_EXPRESSION);
    // We've already parsed the function name, so fn should be anonymous.
    Name name = fn.getFunctionName();
    if (name != null && name.length() != 0) {
        reportError("msg.bad.prop");
    }
    ObjectProperty pn = new ObjectProperty(pos);
    switch(entryKind) {
        case GET_ENTRY:
            pn.setIsGetterMethod();
            fn.setFunctionIsGetterMethod();
            break;
        case SET_ENTRY:
            pn.setIsSetterMethod();
            fn.setFunctionIsSetterMethod();
            break;
        case METHOD_ENTRY:
            pn.setIsNormalMethod();
            fn.setFunctionIsNormalMethod();
            break;
    }
    int end = getNodeEnd(fn);
    pn.setLeft(propName);
    pn.setRight(fn);
    pn.setLength(end - pos);
    return pn;
}
Also used : ObjectProperty(org.mozilla.javascript.ast.ObjectProperty) FunctionNode(org.mozilla.javascript.ast.FunctionNode) Name(org.mozilla.javascript.ast.Name)

Example 9 with FunctionNode

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

the class Parser method parseFunctionParams.

private void parseFunctionParams(FunctionNode fnNode) throws IOException {
    if (matchToken(Token.RP)) {
        fnNode.setRp(ts.tokenBeg - fnNode.getPosition());
        return;
    }
    // Would prefer not to call createDestructuringAssignment until codegen,
    // but the symbol definitions have to happen now, before body is parsed.
    Map<String, Node> destructuring = null;
    Set<String> paramNames = new HashSet<String>();
    do {
        int tt = peekToken();
        if (tt == Token.LB || tt == Token.LC) {
            AstNode expr = destructuringPrimaryExpr();
            markDestructuring(expr);
            fnNode.addParam(expr);
            // variables from the destructuring assignment
            if (destructuring == null) {
                destructuring = new HashMap<String, Node>();
            }
            String pname = currentScriptOrFn.getNextTempName();
            defineSymbol(Token.LP, pname, false);
            destructuring.put(pname, expr);
        } else {
            if (mustMatchToken(Token.NAME, "msg.no.parm")) {
                Name paramNameNode = createNameNode();
                Comment jsdocNodeForName = getAndResetJsDoc();
                if (jsdocNodeForName != null) {
                    paramNameNode.setJsDocNode(jsdocNodeForName);
                }
                fnNode.addParam(paramNameNode);
                String paramName = ts.getString();
                defineSymbol(Token.LP, paramName);
                if (this.inUseStrictDirective) {
                    if ("eval".equals(paramName) || "arguments".equals(paramName)) {
                        reportError("msg.bad.id.strict", paramName);
                    }
                    if (paramNames.contains(paramName))
                        addError("msg.dup.param.strict", paramName);
                    paramNames.add(paramName);
                }
            } else {
                fnNode.addParam(makeErrorNode());
            }
        }
    } while (matchToken(Token.COMMA));
    if (destructuring != null) {
        Node destructuringNode = new Node(Token.COMMA);
        // Add assignment helper for each destructuring parameter
        for (Map.Entry<String, Node> param : destructuring.entrySet()) {
            Node assign = createDestructuringAssignment(Token.VAR, param.getValue(), createName(param.getKey()));
            destructuringNode.addChildToBack(assign);
        }
        fnNode.putProp(Node.DESTRUCTURING_PARAMS, destructuringNode);
    }
    if (mustMatchToken(Token.RP, "msg.no.paren.after.parms")) {
        fnNode.setRp(ts.tokenBeg - fnNode.getPosition());
    }
}
Also used : Comment(org.mozilla.javascript.ast.Comment) ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) ErrorNode(org.mozilla.javascript.ast.ErrorNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) XmlString(org.mozilla.javascript.ast.XmlString) Map(java.util.Map) HashMap(java.util.HashMap) AstNode(org.mozilla.javascript.ast.AstNode) HashSet(java.util.HashSet) Name(org.mozilla.javascript.ast.Name)

Example 10 with FunctionNode

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

the class Node method toStringTreeHelper.

private static void toStringTreeHelper(ScriptNode treeTop, Node n, ObjToIntMap printIds, int level, StringBuilder sb) {
    if (Token.printTrees) {
        if (printIds == null) {
            printIds = new ObjToIntMap();
            generatePrintIds(treeTop, printIds);
        }
        for (int i = 0; i != level; ++i) {
            sb.append("    ");
        }
        n.toString(printIds, sb);
        sb.append('\n');
        for (Node cursor = n.getFirstChild(); cursor != null; cursor = cursor.getNext()) {
            if (cursor.getType() == Token.FUNCTION) {
                int fnIndex = cursor.getExistingIntProp(Node.FUNCTION_PROP);
                FunctionNode fn = treeTop.getFunctionNode(fnIndex);
                toStringTreeHelper(fn, fn, null, level + 1, sb);
            } else {
                toStringTreeHelper(treeTop, cursor, printIds, level + 1, sb);
            }
        }
    }
}
Also used : ScriptNode(org.mozilla.javascript.ast.ScriptNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode)

Aggregations

FunctionNode (org.mozilla.javascript.ast.FunctionNode)32 ScriptNode (org.mozilla.javascript.ast.ScriptNode)20 AstNode (org.mozilla.javascript.ast.AstNode)8 Name (org.mozilla.javascript.ast.Name)7 LetNode (org.mozilla.javascript.ast.LetNode)6 Jump (org.mozilla.javascript.ast.Jump)4 Scope (org.mozilla.javascript.ast.Scope)4 XmlString (org.mozilla.javascript.ast.XmlString)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 AstRoot (org.mozilla.javascript.ast.AstRoot)2 ErrorNode (org.mozilla.javascript.ast.ErrorNode)2 Block (org.mozilla.javascript.ast.Block)1 Comment (org.mozilla.javascript.ast.Comment)1 EmptyExpression (org.mozilla.javascript.ast.EmptyExpression)1 ExpressionStatement (org.mozilla.javascript.ast.ExpressionStatement)1 ObjectProperty (org.mozilla.javascript.ast.ObjectProperty)1 ParenthesizedExpression (org.mozilla.javascript.ast.ParenthesizedExpression)1