Class FunctionNode

  • All Implemented Interfaces:
    Comparable<AstNode>, Iterable<Node>

    public class FunctionNode
    extends ScriptNode
    A JavaScript function declaration or expression.

    Node type is Token.FUNCTION.

    FunctionDeclaration :
            function Identifier ( FormalParameterListopt ) { FunctionBody }
     FunctionExpression :
            function Identifieropt ( FormalParameterListopt ) { FunctionBody }
     FormalParameterList :
            Identifier
            FormalParameterList , Identifier
     FunctionBody :
            SourceElements
     Program :
            SourceElements
     SourceElements :
            SourceElement
            SourceElements SourceElement
     SourceElement :
            Statement
            FunctionDeclaration
    JavaScript 1.8 introduces "function closures" of the form
    function ([params] ) Expression
    In this case the FunctionNode node will have no body but will have an expression.
    • Field Detail

      • FUNCTION_STATEMENT

        public static final int FUNCTION_STATEMENT
        There are three types of functions that can be defined. The first is a function statement. This is a function appearing as a top-level statement (i.e., not nested inside some other statement) in either a script or a function.

        The second is a function expression, which is a function appearing in an expression except for the third type, which is...

        The third type is a function expression where the expression is the top-level expression in an expression statement.

        The three types of functions have different treatment and must be distinguished.

        See Also:
        Constant Field Values
      • FUNCTION_EXPRESSION_STATEMENT

        public static final int FUNCTION_EXPRESSION_STATEMENT
        See Also:
        Constant Field Values
    • Constructor Detail

      • FunctionNode

        public FunctionNode()
      • FunctionNode

        public FunctionNode​(int pos)
      • FunctionNode

        public FunctionNode​(int pos,
                            Name name)
    • Method Detail

      • getFunctionName

        public Name getFunctionName()
        Returns function name
        Returns:
        function name, null for anonymous functions
      • setFunctionName

        public void setFunctionName​(Name name)
        Sets function name, and sets its parent to this node.
        Parameters:
        name - function name, null for anonymous functions
      • getName

        public String getName()
        Returns the function name as a string
        Returns:
        the function name, "" if anonymous
      • getParams

        public List<AstNode> getParams()
        Returns the function parameter list
        Returns:
        the function parameter list. Returns an immutable empty list if there are no parameters.
      • setParams

        public void setParams​(List<AstNode> params)
        Sets the function parameter list, and sets the parent for each element of the list.
        Parameters:
        params - the function parameter list, or null if no params
      • addParam

        public void addParam​(AstNode param)
        Adds a parameter to the function parameter list. Sets the parent of the param node to this node.
        Parameters:
        param - the parameter
        Throws:
        IllegalArgumentException - if param is null
      • isParam

        public boolean isParam​(AstNode node)
        Returns true if the specified AstNode node is a parameter of this Function node. This provides a way during AST traversal to disambiguate the function name node from the parameter nodes.
      • getBody

        public AstNode getBody()
        Returns function body. Normally a Block, but can be a plain AstNode if it's a function closure.
        Returns:
        the body. Can be null only if the AST is malformed.
      • setBody

        public void setBody​(AstNode body)
        Sets function body, and sets its parent to this node. Also sets the encoded source bounds based on the body bounds. Assumes the function node absolute position has already been set, and the body node's absolute position and length are set.

        Parameters:
        body - function body. Its parent is set to this node, and its position is updated to be relative to this node.
        Throws:
        IllegalArgumentException - if body is null
      • getLp

        public int getLp()
        Returns left paren position, -1 if missing
      • setLp

        public void setLp​(int lp)
        Sets left paren position
      • getRp

        public int getRp()
        Returns right paren position, -1 if missing
      • setRp

        public void setRp​(int rp)
        Sets right paren position
      • setParens

        public void setParens​(int lp,
                              int rp)
        Sets both paren positions
      • isExpressionClosure

        public boolean isExpressionClosure()
        Returns whether this is a 1.8 function closure
      • setIsExpressionClosure

        public void setIsExpressionClosure​(boolean isExpressionClosure)
        Sets whether this is a 1.8 function closure
      • requiresActivation

        public boolean requiresActivation()
        Return true if this function requires an Ecma-262 Activation object. The Activation object is implemented by NativeCall, and is fairly expensive to create, so when possible, the interpreter attempts to use a plain call frame instead.
        Returns:
        true if this function needs activation. It could be needed if there is a lexical closure, or in a number of other situations.
      • setRequiresActivation

        public void setRequiresActivation()
      • isGenerator

        public boolean isGenerator()
      • setIsGenerator

        public void setIsGenerator()
      • addResumptionPoint

        public void addResumptionPoint​(Node target)
      • getResumptionPoints

        public List<Node> getResumptionPoints()
      • getLiveLocals

        public Map<Node,​int[]> getLiveLocals()
      • addLiveLocals

        public void addLiveLocals​(Node node,
                                  int[] locals)
      • addFunction

        public int addFunction​(FunctionNode fnNode)
        Description copied from class: ScriptNode
        Adds a FunctionNode to the functions table for codegen. Does not set the parent of the node.
        Overrides:
        addFunction in class ScriptNode
        Returns:
        the index of the function within its parent
      • getFunctionType

        public int getFunctionType()
        Returns the function type (statement, expr, statement expr)
      • setFunctionType

        public void setFunctionType​(int type)
      • isMethod

        public boolean isMethod()
      • isGetterMethod

        public boolean isGetterMethod()
      • isSetterMethod

        public boolean isSetterMethod()
      • isNormalMethod

        public boolean isNormalMethod()
      • setFunctionIsGetterMethod

        public void setFunctionIsGetterMethod()
      • setFunctionIsSetterMethod

        public void setFunctionIsSetterMethod()
      • setFunctionIsNormalMethod

        public void setFunctionIsNormalMethod()
      • setMemberExprNode

        public void setMemberExprNode​(AstNode node)
        Rhino supports a nonstandard Ecma extension that allows you to say, for instance, function a.b.c(arg1, arg) {...}, and it will be rewritten at codegen time to: a.b.c = function(arg1, arg2) {...} If we detect an expression other than a simple Name in the position where a function name was expected, we record that expression here.

        This extension is only available by setting the CompilerEnv option "isAllowMemberExprAsFunctionName" in the Parser.

      • getMemberExprNode

        public AstNode getMemberExprNode()
      • toSource

        public String toSource​(int depth)
        Description copied from class: AstNode
        Emits source code for this node. Callee is responsible for calling this function recursively on children, incrementing indent as appropriate.

        Note: if the parser was in error-recovery mode, some AST nodes may have null children that are expected to be non-null when no errors are present. In this situation, the behavior of the toSource method is undefined: toSource implementations may assume that the AST node is error-free, since it is intended to be invoked only at runtime after a successful parse.

        Overrides:
        toSource in class Scope
        Parameters:
        depth - the current recursion depth, typically beginning at 0 when called on the root node.
      • visit

        public void visit​(NodeVisitor v)
        Visits this node, the function name node if supplied, the parameters, and the body. If there is a member-expr node, it is visited last.
        Overrides:
        visit in class ScriptNode
        Parameters:
        v - the object to call with this node and its children