Java Code Examples for org.mozilla.javascript.ast.FunctionNode#setRequiresActivation()

The following examples show how to use org.mozilla.javascript.ast.FunctionNode#setRequiresActivation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IRFactory.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
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;
        ++nestingOfFunction;  // only for body, not params
        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;
}
 
Example 2
Source File: IRFactory.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
private Node initFunction(FunctionNode fnNode, int functionIndex,
                          Node statements, int functionType) {
    fnNode.setFunctionType(functionType);
    fnNode.addChildToBack(statements);

    int functionCount = fnNode.getFunctionCount();
    if (functionCount != 0) {
        // Functions containing other functions require activation objects
        fnNode.setRequiresActivation();
    }

    if (functionType == FunctionNode.FUNCTION_EXPRESSION) {
        Name name = fnNode.getFunctionName();
        if (name != null && name.length() != 0
                && fnNode.getSymbol(name.getIdentifier()) == null) {
            // A function expression needs to have its name as a
            // variable (if it isn't already allocated as a variable).
            // See ECMA Ch. 13.  We add code to the beginning of the
            // function to initialize a local variable of the
            // function's name to the function value, but only if the
            // function doesn't already define a formal parameter, var,
            // or nested function with the same name.
            fnNode.putSymbol(new Symbol(Token.FUNCTION, name.getIdentifier()));
            Node setFn = new Node(Token.EXPR_VOID,
                             new Node(Token.SETNAME,
                                      Node.newString(Token.BINDNAME,
                                                     name.getIdentifier()),
                                 new Node(Token.THISFN)));
            statements.addChildrenToFront(setFn);
        }
    }

    // Add return to end if needed.
    Node lastStmt = statements.getLastChild();
    if (lastStmt == null || lastStmt.getType() != Token.RETURN) {
        statements.addChildToBack(new Node(Token.RETURN));
    }

    Node result = Node.newString(Token.FUNCTION, fnNode.getName());
    result.putIntProp(Node.FUNCTION_PROP, functionIndex);
    return result;
}