private FunctionLiteral parseFunctionLiteral(bool nameFlag) { Identifier name = null; ArrayList parameterVector = new ArrayList(); ArrayList statementVector = new ArrayList(); readToken(Token.KEYWORD_FUNCTION); if (nameFlag || nextToken != Token.OPERATOR_OPENPAREN) { name = parseIdentifier(); } // function literals may have zero or more parameters. readToken(Token.OPERATOR_OPENPAREN); if (nextToken != Token.OPERATOR_CLOSEPAREN) { parameterVector.Add(parseIdentifier()); while (nextToken != Token.OPERATOR_CLOSEPAREN) { readToken(Token.OPERATOR_COMMA); parameterVector.Add(parseIdentifier()); } } readToken(Token.OPERATOR_CLOSEPAREN); // function literals are required the have at least one SourceElement. readToken(Token.OPERATOR_OPENBRACE); // statementVector.addElement(parseStatement()); while (nextToken != Token.OPERATOR_CLOSEBRACE) { statementVector.Add(parseSourceElement()); } readToken(Token.OPERATOR_CLOSEBRACE); return new FunctionLiteral(name, CompilerUtil.vectorToIdentifierArray(parameterVector), CompilerUtil.vectorToStatementArray(statementVector) ); }
public override Expression visit(FunctionLiteral literal) { ArrayList oldFunctionVector = functionVector; ArrayList oldVariableVector = variableVector; bool oldHasWithStatement = hasWithStatement; bool oldHasArgumentsVariable = hasArgumentsVariable; functionVector = new ArrayList(); variableVector = new ArrayList(); hasWithStatement = false; hasArgumentsVariable = false; hasFunctionLiteral = false; Identifier[] parameters = literal.parameters; for (int i = 0; i < parameters.Length; i++) { addVariable(parameters[i]); } literal = (FunctionLiteral)base.visit(literal); literal.functions = CompilerUtil.vectorToStatementArray(functionVector); literal.variables = CompilerUtil.vectorToIdentifierArray(variableVector); // if this function literal: // * contains a function literal // * contains a 'with' statement // * contains a reference to 'arguments' // // then we need to disable the "access locals by index" optimisation for // this function literal. literal.enableLocalsOptimization = !(hasWithStatement | hasArgumentsVariable | hasFunctionLiteral); functionVector = oldFunctionVector; variableVector = oldVariableVector; hasWithStatement = oldHasWithStatement; hasArgumentsVariable = oldHasArgumentsVariable; hasFunctionLiteral = true; return(literal); }