java.lang.Object
org.htmlunit.corejs.javascript.Node
org.htmlunit.corejs.javascript.ast.AstNode
org.htmlunit.corejs.javascript.ast.Jump
org.htmlunit.corejs.javascript.ast.Scope
org.htmlunit.corejs.javascript.ast.ScriptNode
org.htmlunit.corejs.javascript.ast.FunctionNode
- All Implemented Interfaces:
Comparable<AstNode>,Iterable<Node>
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] ) ExpressionIn this case the FunctionNode node will have no body but will have an expression.
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class org.htmlunit.corejs.javascript.ast.AstNode
AstNode.DebugPrintVisitor, AstNode.PositionComparatorNested classes/interfaces inherited from class org.htmlunit.corejs.javascript.Node
Node.NodeIterator -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intstatic final intstatic final intstatic final intThere are three types of functions that can be defined.Fields inherited from class org.htmlunit.corejs.javascript.ast.Scope
parentScope, symbolTable, top, varSymbolTableFields inherited from class org.htmlunit.corejs.javascript.ast.AstNode
inlineComment, length, parent, positionFields inherited from class org.htmlunit.corejs.javascript.Node
ARROW_FUNCTION_PROP, ATTRIBUTE_FLAG, BOTH, CASEARRAY_PROP, CATCH_SCOPE_PROP, CONST_IDS_PROP, CONTROL_BLOCK_PROP, DECR_FLAG, DESCENDANTS_FLAG, DESTRUCTURING_ARRAY_LENGTH, DESTRUCTURING_NAMES, DESTRUCTURING_PARAMS, DIRECTCALL_PROP, END_DROPS_OFF, END_RETURNS, END_RETURNS_VALUE, END_UNREACHED, END_YIELDS, EXPRESSION_CLOSURE_PROP, first, FIRST_PROP, FUNCTION_PROP, GENERATOR_END_PROP, INCRDECR_PROP, IS_CONST_PROP, ISNUMBER_PROP, JSDOC_PROP, LABEL_ID_PROP, last, LAST_PROP, LEFT, lineno, LOCAL_BLOCK_PROP, LOCAL_PROP, MEMBER_TYPE_PROP, NAME_PROP, next, NON_SPECIALCALL, NUMBER_OF_SPREAD, OBJECT_IDS_PROP, OBJECT_LITERAL_DESTRUCTURING, OBJECT_REST_PROP, OPTIONAL_CHAINING, PARENTHESIZED_PROP, POST_FLAG, PROPERTY_FLAG, propListHead, REGEXP_PROP, RIGHT, SKIP_INDEXES_PROP, SPECIALCALL_EVAL, SPECIALCALL_PROP, SUPER_PROPERTY_ACCESS, TARGETBLOCK_PROP, TEMPLATE_LITERAL_PROP, TRAILING_COMMA, type, VARIABLE_PROP -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintaddFunction(FunctionNode fnNode) Adds aFunctionNodeto the functions table for codegen.voidaddLiveLocals(Node node, int[] locals) voidAdds a parameter to the function parameter list.voidaddResumptionPoint(Node target) static intcalculateFunctionArity(ScriptNode scriptOrFn) Calculate the arity (function.length) for a function.getBody()Returns function body.Returns function nameintReturns the function type (statement, expr, statement expr)intgetLp()Returns left paren position, -1 if missinggetName()Returns the function name as a stringReturns the function parameter listintgetRp()Returns right paren position, -1 if missingbooleanbooleanbooleanbooleanReturns whether this is a 1.8 function closurebooleanbooleanbooleanisMethod()booleanbooleanReturns true if the specifiedAstNodenode is a parameter of this Function node.booleanbooleanvoidputDefaultParams(Object left, Object right) voidputDestructuringRvalues(Node left, Node right) booleanReturn true if this function requires an Ecma-262 Activation object.booleanvoidsetAnnexBHoisted(boolean hoisted) voidSets function body, and sets its parent to this node.voidvoidvoidvoidsetFunctionName(Name name) Sets function name, and sets its parent to this node.voidsetFunctionType(int type) voidsetGeneratorParamInitBlock(Node block) voidsetHasRestParameter(boolean hasRestParameter) voidvoidsetIsExpressionClosure(boolean isExpressionClosure) Sets whether this is a 1.8 function closurevoidvoidvoidsetLp(int lp) Sets left paren positionvoidsetMemberExprNode(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) {...}voidSets the function parameter list, and sets the parent for each element of the list.voidsetParens(int lp, int rp) Sets both paren positionsvoidvoidvoidsetRp(int rp) Sets right paren positiontoSource(int depth) Jumps are only used directly during code generation, and do not support this interface.voidvisit(NodeVisitor v) Visits this node, the function name node if supplied, the parameters, and the body.Methods inherited from class org.htmlunit.corejs.javascript.ast.ScriptNode
addAnnexBFunction, addRegExp, addTemplateLiteral, doAnnexBHoisting, flattenSymbolTable, getBaseLineno, getCompilerData, getEndLineno, getFunctionCount, getFunctionNode, getFunctions, getIndexForNameNode, getNextTempName, getParamAndVarConst, getParamAndVarCount, getParamAndVarNames, getParamCount, getParamOrVarName, getRawSource, getRawSourceEnd, getRawSourceStart, getRegexpCount, getRegexpFlags, getRegexpString, getSourceName, getSymbols, getTemplateLiteralCount, getTemplateLiteralStrings, isInStrictMode, isMethodDefinition, setBaseLineno, setCompilerData, setEndLineno, setInStrictMode, setMethodDefinition, setRawSource, setRawSourceBounds, setRawSourceEnd, setRawSourceStart, setSourceName, setSymbolsMethods inherited from class org.htmlunit.corejs.javascript.ast.Scope
addChildScope, clearParentScope, getChildScopes, getDefiningScope, getParentScope, getStatements, getSymbol, getSymbolTable, getTop, getVarSymbol, getVarSymbolTable, joinScopes, joinVarScopes, moveSymbol, putSymbol, putVarSymbol, replaceWith, setParentScope, setSymbolTable, setTop, splitScopeMethods inherited from class org.htmlunit.corejs.javascript.ast.Jump
getContinue, getDefault, getFinally, getJumpStatement, getLoop, setContinue, setDefault, setFinally, setJumpStatement, setLoopMethods inherited from class org.htmlunit.corejs.javascript.ast.AstNode
addChild, assertNotNull, codeBug, compareTo, debugPrint, depth, getAbsolutePosition, getAstRoot, getEnclosingFunction, getEnclosingScope, getInlineComment, getLength, getLineno, getParent, getPosition, hasSideEffects, makeIndent, operatorToString, printList, setBounds, setInlineComment, setLength, setParent, setPosition, setRelative, shortName, toSourceMethods inherited from class org.htmlunit.corejs.javascript.Node
addChildAfter, addChildBefore, addChildrenToBack, addChildrenToFront, addChildToBack, addChildToFront, clearAllLabelIds, clearLabelId, getBigInt, getChildBefore, getColumn, getDouble, getExistingIntProp, getFirstChild, getIntProp, getJsDoc, getJsDocNode, getLastChild, getLastSibling, getNext, getProp, getScope, getString, getType, hasChildren, hasConsistentReturnUsage, iterator, labelId, labelId, newNumber, newString, newString, newTarget, putIntProp, putProp, removeChild, removeChildren, removeProp, replaceChild, replaceChildAfter, resetTargets, setBigInt, setDouble, setJsDocNode, setLineColumnNumber, setScope, setString, setType, toString, toStringTreeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
FUNCTION_STATEMENT
public static final int FUNCTION_STATEMENTThere 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:
-
FUNCTION_EXPRESSION
public static final int FUNCTION_EXPRESSION- See Also:
-
FUNCTION_EXPRESSION_STATEMENT
public static final int FUNCTION_EXPRESSION_STATEMENT- See Also:
-
ARROW_FUNCTION
public static final int ARROW_FUNCTION- See Also:
-
FUNCTION_BLOCK_SCOPED
public static final int FUNCTION_BLOCK_SCOPED- See Also:
-
-
Constructor Details
-
FunctionNode
public FunctionNode() -
FunctionNode
public FunctionNode(int pos) -
FunctionNode
-
-
Method Details
-
getDefaultParams
- Overrides:
getDefaultParamsin classScriptNode
-
putDefaultParams
-
getDestructuringRvalues
- Overrides:
getDestructuringRvaluesin classScriptNode
-
putDestructuringRvalues
- Overrides:
putDestructuringRvaluesin classScriptNode
-
getFunctionName
Returns function name- Returns:
- function name,
nullfor anonymous functions
-
setFunctionName
Sets function name, and sets its parent to this node.- Parameters:
name- function name,nullfor anonymous functions
-
getName
Returns the function name as a string- Returns:
- the function name,
""if anonymous
-
getParams
Returns the function parameter list- Returns:
- the function parameter list. Returns an immutable empty list if there are no parameters.
-
setParams
Sets the function parameter list, and sets the parent for each element of the list.- Parameters:
params- the function parameter list, ornullif no params
-
addParam
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 isnull
-
isParam
Returns true if the specifiedAstNodenode 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
- Returns:
- the body. Can be
nullonly if the AST is malformed.
-
setBody
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 isnull
-
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 byNativeCall, 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() -
isAnnexBHoisted
public boolean isAnnexBHoisted() -
setAnnexBHoisted
public void setAnnexBHoisted(boolean hoisted) -
requiresArgumentObject
public boolean requiresArgumentObject() -
setRequiresArgumentObject
public void setRequiresArgumentObject() -
isGenerator
public boolean isGenerator() -
setIsGenerator
public void setIsGenerator() -
isES6Generator
public boolean isES6Generator() -
setIsES6Generator
public void setIsES6Generator() -
hasRestParameter
public boolean hasRestParameter()- Overrides:
hasRestParameterin classScriptNode
-
setHasRestParameter
public void setHasRestParameter(boolean hasRestParameter) -
isShorthand
public boolean isShorthand()- Overrides:
isShorthandin classScriptNode
-
setIsShorthand
public void setIsShorthand() -
addResumptionPoint
-
getResumptionPoints
-
getLiveLocals
-
addLiveLocals
-
getGeneratorParamInitBlock
-
setGeneratorParamInitBlock
-
addFunction
Description copied from class:ScriptNodeAdds aFunctionNodeto the functions table for codegen. Does not set the parent of the node.- Overrides:
addFunctionin classScriptNode- 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
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
-
toSource
Description copied from class:JumpJumps are only used directly during code generation, and do not support this interface. -
visit
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:
visitin classScriptNode- Parameters:
v- the object to call with this node and its children
-
calculateFunctionArity
Calculate the arity (function.length) for a function. According to ECMAScript spec, the length property should only count parameters up to (but not including) the first one with a default value.Ref: ECMA 2026, 15.1.5 Static Semantics: ExpectedArgumentCount
- Parameters:
scriptOrFn- the function or script node- Returns:
- the arity (number of parameters before first default parameter)
-