| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/v8.h" | 5 #include "src/v8.h" |
| 6 | 6 |
| 7 #include "src/api.h" | 7 #include "src/api.h" |
| 8 #include "src/ast.h" | 8 #include "src/ast.h" |
| 9 #include "src/bailout-reason.h" | 9 #include "src/bailout-reason.h" |
| 10 #include "src/base/platform/platform.h" | 10 #include "src/base/platform/platform.h" |
| (...skipping 769 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 780 return NULL; | 780 return NULL; |
| 781 } | 781 } |
| 782 | 782 |
| 783 | 783 |
| 784 Expression* ParserTraits::ExpressionFromIdentifier(const AstRawString* name, | 784 Expression* ParserTraits::ExpressionFromIdentifier(const AstRawString* name, |
| 785 int start_position, | 785 int start_position, |
| 786 int end_position, | 786 int end_position, |
| 787 Scope* scope, | 787 Scope* scope, |
| 788 AstNodeFactory* factory) { | 788 AstNodeFactory* factory) { |
| 789 if (parser_->fni_ != NULL) parser_->fni_->PushVariableName(name); | 789 if (parser_->fni_ != NULL) parser_->fni_->PushVariableName(name); |
| 790 | 790 return scope->NewUnresolved(factory, name, start_position, end_position); |
| 791 // Arrow function parameters are parsed as an expression. When | |
| 792 // parsing lazily, it is enough to create a VariableProxy in order | |
| 793 // for Traits::DeclareArrowParametersFromExpression() to be able to | |
| 794 // pick the names of the parameters. | |
| 795 return parser_->parsing_lazy_arrow_parameters_ | |
| 796 ? factory->NewVariableProxy(name, Variable::NORMAL, start_position, | |
| 797 end_position) | |
| 798 : scope->NewUnresolved(factory, name, start_position, | |
| 799 end_position); | |
| 800 } | 791 } |
| 801 | 792 |
| 802 | 793 |
| 803 Expression* ParserTraits::ExpressionFromString(int pos, Scanner* scanner, | 794 Expression* ParserTraits::ExpressionFromString(int pos, Scanner* scanner, |
| 804 AstNodeFactory* factory) { | 795 AstNodeFactory* factory) { |
| 805 const AstRawString* symbol = GetSymbol(scanner); | 796 const AstRawString* symbol = GetSymbol(scanner); |
| 806 if (parser_->fni_ != NULL) parser_->fni_->PushLiteralName(symbol); | 797 if (parser_->fni_ != NULL) parser_->fni_->PushLiteralName(symbol); |
| 807 return factory->NewStringLiteral(symbol, pos); | 798 return factory->NewStringLiteral(symbol, pos); |
| 808 } | 799 } |
| 809 | 800 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 854 Parser::Parser(ParseInfo* info) | 845 Parser::Parser(ParseInfo* info) |
| 855 : ParserBase<ParserTraits>(info->zone(), &scanner_, info->stack_limit(), | 846 : ParserBase<ParserTraits>(info->zone(), &scanner_, info->stack_limit(), |
| 856 info->extension(), info->ast_value_factory(), | 847 info->extension(), info->ast_value_factory(), |
| 857 NULL, this), | 848 NULL, this), |
| 858 scanner_(info->unicode_cache()), | 849 scanner_(info->unicode_cache()), |
| 859 reusable_preparser_(NULL), | 850 reusable_preparser_(NULL), |
| 860 original_scope_(NULL), | 851 original_scope_(NULL), |
| 861 target_stack_(NULL), | 852 target_stack_(NULL), |
| 862 compile_options_(info->compile_options()), | 853 compile_options_(info->compile_options()), |
| 863 cached_parse_data_(NULL), | 854 cached_parse_data_(NULL), |
| 864 parsing_lazy_arrow_parameters_(false), | |
| 865 total_preparse_skipped_(0), | 855 total_preparse_skipped_(0), |
| 866 pre_parse_timer_(NULL), | 856 pre_parse_timer_(NULL), |
| 867 parsing_on_main_thread_(true) { | 857 parsing_on_main_thread_(true) { |
| 868 // Even though we were passed ParseInfo, we should not store it in | 858 // Even though we were passed ParseInfo, we should not store it in |
| 869 // Parser - this makes sure that Isolate is not accidentally accessed via | 859 // Parser - this makes sure that Isolate is not accidentally accessed via |
| 870 // ParseInfo during background parsing. | 860 // ParseInfo during background parsing. |
| 871 DCHECK(!info->script().is_null() || info->source_stream() != NULL); | 861 DCHECK(!info->script().is_null() || info->source_stream() != NULL); |
| 872 set_allow_lazy(info->allow_lazy_parsing()); | 862 set_allow_lazy(info->allow_lazy_parsing()); |
| 873 set_allow_natives(FLAG_allow_natives_syntax || info->is_native()); | 863 set_allow_natives(FLAG_allow_natives_syntax || info->is_native()); |
| 874 set_allow_harmony_modules(!info->is_native() && FLAG_harmony_modules); | 864 set_allow_harmony_modules(!info->is_native() && FLAG_harmony_modules); |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1135 DCHECK(info->language_mode() == shared_info->language_mode()); | 1125 DCHECK(info->language_mode() == shared_info->language_mode()); |
| 1136 scope->SetLanguageMode(shared_info->language_mode()); | 1126 scope->SetLanguageMode(shared_info->language_mode()); |
| 1137 FunctionLiteral::FunctionType function_type = shared_info->is_expression() | 1127 FunctionLiteral::FunctionType function_type = shared_info->is_expression() |
| 1138 ? (shared_info->is_anonymous() | 1128 ? (shared_info->is_anonymous() |
| 1139 ? FunctionLiteral::ANONYMOUS_EXPRESSION | 1129 ? FunctionLiteral::ANONYMOUS_EXPRESSION |
| 1140 : FunctionLiteral::NAMED_EXPRESSION) | 1130 : FunctionLiteral::NAMED_EXPRESSION) |
| 1141 : FunctionLiteral::DECLARATION; | 1131 : FunctionLiteral::DECLARATION; |
| 1142 bool ok = true; | 1132 bool ok = true; |
| 1143 | 1133 |
| 1144 if (shared_info->is_arrow()) { | 1134 if (shared_info->is_arrow()) { |
| 1145 // The first expression being parsed is the parameter list of the arrow | 1135 Scope* scope = NewScope(scope_, ARROW_SCOPE); |
| 1146 // function. Setting this avoids prevents ExpressionFromIdentifier() | 1136 scope->set_start_position(shared_info->start_position()); |
| 1147 // from creating unresolved variables in already-resolved scopes. | 1137 FormalParameterErrorLocations error_locs; |
| 1148 parsing_lazy_arrow_parameters_ = true; | 1138 bool has_rest = false; |
| 1149 Expression* expression = ParseExpression(false, &ok); | 1139 if (Check(Token::LPAREN)) { |
| 1140 // '(' StrictFormalParameters ')' |
| 1141 ParseFormalParameterList(scope, &error_locs, &has_rest, &ok); |
| 1142 if (ok) ok = Check(Token::RPAREN); |
| 1143 } else { |
| 1144 // BindingIdentifier |
| 1145 ParseFormalParameter(scope, &error_locs, has_rest, &ok); |
| 1146 } |
| 1147 |
| 1150 if (ok) { | 1148 if (ok) { |
| 1151 // Scanning must end at the same position that was recorded | 1149 Expression* expression = |
| 1152 // previously. If not, parsing has been interrupted due to a | 1150 ParseArrowFunctionLiteral(scope, error_locs, has_rest, &ok); |
| 1153 // stack overflow, at which point the partially parsed arrow | 1151 if (ok) { |
| 1154 // function concise body happens to be a valid expression. This | 1152 // Scanning must end at the same position that was recorded |
| 1155 // is a problem only for arrow functions with single statement | 1153 // previously. If not, parsing has been interrupted due to a stack |
| 1156 // bodies, since there is no end token such as "}" for normal | 1154 // overflow, at which point the partially parsed arrow function |
| 1157 // functions. | 1155 // concise body happens to be a valid expression. This is a problem |
| 1158 if (scanner()->location().end_pos == shared_info->end_position()) { | 1156 // only for arrow functions with single expression bodies, since there |
| 1159 // The pre-parser saw an arrow function here, so the full parser | 1157 // is no end token such as "}" for normal functions. |
| 1160 // must produce a FunctionLiteral. | 1158 if (scanner()->location().end_pos == shared_info->end_position()) { |
| 1161 DCHECK(expression->IsFunctionLiteral()); | 1159 // The pre-parser saw an arrow function here, so the full parser |
| 1162 result = expression->AsFunctionLiteral(); | 1160 // must produce a FunctionLiteral. |
| 1163 } else { | 1161 DCHECK(expression->IsFunctionLiteral()); |
| 1164 result = NULL; | 1162 result = expression->AsFunctionLiteral(); |
| 1165 ok = false; | 1163 } else { |
| 1164 ok = false; |
| 1165 } |
| 1166 } | 1166 } |
| 1167 } | 1167 } |
| 1168 } else if (shared_info->is_default_constructor()) { | 1168 } else if (shared_info->is_default_constructor()) { |
| 1169 result = DefaultConstructor(IsSubclassConstructor(shared_info->kind()), | 1169 result = DefaultConstructor(IsSubclassConstructor(shared_info->kind()), |
| 1170 scope, shared_info->start_position(), | 1170 scope, shared_info->start_position(), |
| 1171 shared_info->end_position()); | 1171 shared_info->end_position()); |
| 1172 } else { | 1172 } else { |
| 1173 result = ParseFunctionLiteral(raw_name, Scanner::Location::invalid(), | 1173 result = ParseFunctionLiteral(raw_name, Scanner::Location::invalid(), |
| 1174 false, // Strict mode name already checked. | 1174 false, // Strict mode name already checked. |
| 1175 shared_info->kind(), RelocInfo::kNoPosition, | 1175 shared_info->kind(), RelocInfo::kNoPosition, |
| (...skipping 2549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3725 Smi* literal_type = Smi::cast(value->get(kLiteralTypeSlot)); | 3725 Smi* literal_type = Smi::cast(value->get(kLiteralTypeSlot)); |
| 3726 return static_cast<LiteralType>(literal_type->value()); | 3726 return static_cast<LiteralType>(literal_type->value()); |
| 3727 } | 3727 } |
| 3728 | 3728 |
| 3729 | 3729 |
| 3730 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) { | 3730 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) { |
| 3731 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot))); | 3731 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot))); |
| 3732 } | 3732 } |
| 3733 | 3733 |
| 3734 | 3734 |
| 3735 bool CheckAndDeclareArrowParameter(ParserTraits* traits, Expression* expression, | 3735 void ParserTraits::DeclareArrowFunctionParameters( |
| 3736 Scope* scope, int* num_params, | 3736 Scope* scope, Expression* expr, const Scanner::Location& params_loc, |
| 3737 Scanner::Location* undefined_loc, | 3737 FormalParameterErrorLocations* error_locs, bool* ok) { |
| 3738 Scanner::Location* dupe_loc) { | 3738 if (scope->num_parameters() >= Code::kMaxArguments) { |
| 3739 // Case for empty parameter lists: | 3739 ReportMessageAt(params_loc, "malformed_arrow_function_parameter_list"); |
| 3740 // () => ... | 3740 *ok = false; |
| 3741 if (expression == NULL) return true; | 3741 return; |
| 3742 | |
| 3743 // Too many parentheses around expression: | |
| 3744 // (( ... )) => ... | |
| 3745 if (expression->is_multi_parenthesized()) return false; | |
| 3746 | |
| 3747 // Case for a single parameter: | |
| 3748 // (foo) => ... | |
| 3749 // foo => ... | |
| 3750 if (expression->IsVariableProxy()) { | |
| 3751 if (expression->AsVariableProxy()->is_this()) return false; | |
| 3752 | |
| 3753 const AstRawString* raw_name = expression->AsVariableProxy()->raw_name(); | |
| 3754 if (traits->IsEvalOrArguments(raw_name) || | |
| 3755 traits->IsFutureStrictReserved(raw_name)) | |
| 3756 return false; | |
| 3757 if (traits->IsUndefined(raw_name) && !undefined_loc->IsValid()) { | |
| 3758 *undefined_loc = Scanner::Location( | |
| 3759 expression->position(), expression->position() + raw_name->length()); | |
| 3760 } | |
| 3761 if (scope->IsDeclared(raw_name)) { | |
| 3762 *dupe_loc = Scanner::Location( | |
| 3763 expression->position(), expression->position() + raw_name->length()); | |
| 3764 return false; | |
| 3765 } | |
| 3766 | |
| 3767 // When the variable was seen, it was recorded as unresolved in the outer | |
| 3768 // scope. But it's really not unresolved. | |
| 3769 scope->outer_scope()->RemoveUnresolved(expression->AsVariableProxy()); | |
| 3770 | |
| 3771 scope->DeclareParameter(raw_name, VAR); | |
| 3772 ++(*num_params); | |
| 3773 return true; | |
| 3774 } | 3742 } |
| 3775 | 3743 |
| 3776 // Case for more than one parameter: | 3744 // ArrowFunctionFormals :: |
| 3777 // (foo, bar [, ...]) => ... | 3745 // Binary(Token::COMMA, ArrowFunctionFormals, VariableProxy) |
| 3778 if (expression->IsBinaryOperation()) { | 3746 // VariableProxy |
| 3779 BinaryOperation* binop = expression->AsBinaryOperation(); | 3747 // |
| 3780 if (binop->op() != Token::COMMA || binop->left()->is_parenthesized() || | 3748 // As we need to visit the parameters in left-to-right order, we recurse on |
| 3781 binop->right()->is_parenthesized()) | 3749 // the left-hand side of comma expressions. |
| 3782 return false; | 3750 // |
| 3783 | 3751 // Sadly, for the various malformed_arrow_function_parameter_list errors, we |
| 3784 return CheckAndDeclareArrowParameter(traits, binop->left(), scope, | 3752 // can't be more specific on the error message or on the location because we |
| 3785 num_params, undefined_loc, dupe_loc) && | 3753 // need to match the pre-parser's behavior. |
| 3786 CheckAndDeclareArrowParameter(traits, binop->right(), scope, | 3754 if (expr->IsBinaryOperation()) { |
| 3787 num_params, undefined_loc, dupe_loc); | 3755 BinaryOperation* binop = expr->AsBinaryOperation(); |
| 3756 if (binop->op() != Token::COMMA) { |
| 3757 ReportMessageAt(params_loc, "malformed_arrow_function_parameter_list"); |
| 3758 *ok = false; |
| 3759 return; |
| 3760 } |
| 3761 Expression* left = binop->left(); |
| 3762 Expression* right = binop->right(); |
| 3763 if (left->is_parenthesized() || right->is_parenthesized()) { |
| 3764 ReportMessageAt(params_loc, "malformed_arrow_function_parameter_list"); |
| 3765 *ok = false; |
| 3766 return; |
| 3767 } |
| 3768 DeclareArrowFunctionParameters(scope, left, params_loc, error_locs, ok); |
| 3769 if (!*ok) return; |
| 3770 // LHS of comma expression should be unparenthesized. |
| 3771 expr = right; |
| 3788 } | 3772 } |
| 3789 | 3773 |
| 3790 // Any other kind of expression is not a valid parameter list. | 3774 // TODO(wingo): Support rest parameters. |
| 3791 return false; | 3775 if (!expr->IsVariableProxy()) { |
| 3776 ReportMessageAt(params_loc, "malformed_arrow_function_parameter_list"); |
| 3777 *ok = false; |
| 3778 return; |
| 3779 } |
| 3780 |
| 3781 const AstRawString* raw_name = expr->AsVariableProxy()->raw_name(); |
| 3782 Scanner::Location param_location(expr->position(), |
| 3783 expr->position() + raw_name->length()); |
| 3784 |
| 3785 if (expr->AsVariableProxy()->is_this()) { |
| 3786 ReportMessageAt(param_location, "this_formal_parameter"); |
| 3787 *ok = false; |
| 3788 return; |
| 3789 } |
| 3790 |
| 3791 if (!error_locs->eval_or_arguments.IsValid() && IsEvalOrArguments(raw_name)) |
| 3792 error_locs->eval_or_arguments = param_location; |
| 3793 if (!error_locs->reserved.IsValid() && IsFutureStrictReserved(raw_name)) |
| 3794 error_locs->reserved = param_location; |
| 3795 if (!error_locs->undefined.IsValid() && IsUndefined(raw_name)) |
| 3796 error_locs->undefined = param_location; |
| 3797 |
| 3798 // Arrow function parameter lists are parsed as StrictFormalParameters, which |
| 3799 // means that they cannot have duplicates. Note that this is a subset of the |
| 3800 // restrictions placed on parameters to functions whose body is strict. |
| 3801 if (scope->IsDeclared(raw_name)) { |
| 3802 ReportMessageAt(param_location, |
| 3803 "duplicate_arrow_function_formal_parameter"); |
| 3804 *ok = false; |
| 3805 return; |
| 3806 } |
| 3807 |
| 3808 // When the formal parameter was originally seen, it was parsed as a |
| 3809 // VariableProxy and recorded as unresolved in the scope. Here we undo that |
| 3810 // parse-time side-effect. |
| 3811 parser_->scope_->RemoveUnresolved(expr->AsVariableProxy()); |
| 3812 |
| 3813 bool is_rest = false; |
| 3814 scope->DeclareParameter(raw_name, VAR, is_rest); |
| 3792 } | 3815 } |
| 3793 | 3816 |
| 3794 | 3817 |
| 3795 int ParserTraits::DeclareArrowParametersFromExpression( | 3818 void ParserTraits::ParseArrowFunctionFormalParameters( |
| 3796 Expression* expression, Scope* scope, Scanner::Location* undefined_loc, | 3819 Scope* scope, Expression* params, const Scanner::Location& params_loc, |
| 3797 Scanner::Location* dupe_loc, bool* ok) { | 3820 FormalParameterErrorLocations* error_locs, bool* is_rest, bool* ok) { |
| 3798 int num_params = 0; | 3821 // Too many parentheses around expression: |
| 3799 // Always reset the flag: It only needs to be set for the first expression | 3822 // (( ... )) => ... |
| 3800 // parsed as arrow function parameter list, because only top-level functions | 3823 if (params->is_multi_parenthesized()) { |
| 3801 // are parsed lazily. | 3824 // TODO(wingo): Make a better message. |
| 3802 parser_->parsing_lazy_arrow_parameters_ = false; | 3825 ReportMessageAt(params_loc, "malformed_arrow_function_parameter_list"); |
| 3803 *ok = CheckAndDeclareArrowParameter(this, expression, scope, &num_params, | 3826 *ok = false; |
| 3804 undefined_loc, dupe_loc); | 3827 return; |
| 3805 return num_params; | 3828 } |
| 3829 |
| 3830 DeclareArrowFunctionParameters(scope, params, params_loc, error_locs, ok); |
| 3806 } | 3831 } |
| 3807 | 3832 |
| 3808 | 3833 |
| 3809 FunctionLiteral* Parser::ParseFunctionLiteral( | 3834 FunctionLiteral* Parser::ParseFunctionLiteral( |
| 3810 const AstRawString* function_name, Scanner::Location function_name_location, | 3835 const AstRawString* function_name, Scanner::Location function_name_location, |
| 3811 bool name_is_strict_reserved, FunctionKind kind, int function_token_pos, | 3836 bool name_is_strict_reserved, FunctionKind kind, int function_token_pos, |
| 3812 FunctionLiteral::FunctionType function_type, | 3837 FunctionLiteral::FunctionType function_type, |
| 3813 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) { | 3838 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) { |
| 3814 // Function :: | 3839 // Function :: |
| 3815 // '(' FormalParameterList? ')' '{' FunctionBody '}' | 3840 // '(' FormalParameterList? ')' '{' FunctionBody '}' |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3868 Scope* scope = function_type == FunctionLiteral::DECLARATION && | 3893 Scope* scope = function_type == FunctionLiteral::DECLARATION && |
| 3869 is_sloppy(language_mode()) && | 3894 is_sloppy(language_mode()) && |
| 3870 (original_scope_ == original_declaration_scope || | 3895 (original_scope_ == original_declaration_scope || |
| 3871 declaration_scope != original_declaration_scope) | 3896 declaration_scope != original_declaration_scope) |
| 3872 ? NewScope(declaration_scope, FUNCTION_SCOPE, kind) | 3897 ? NewScope(declaration_scope, FUNCTION_SCOPE, kind) |
| 3873 : NewScope(scope_, FUNCTION_SCOPE, kind); | 3898 : NewScope(scope_, FUNCTION_SCOPE, kind); |
| 3874 ZoneList<Statement*>* body = NULL; | 3899 ZoneList<Statement*>* body = NULL; |
| 3875 int materialized_literal_count = -1; | 3900 int materialized_literal_count = -1; |
| 3876 int expected_property_count = -1; | 3901 int expected_property_count = -1; |
| 3877 int handler_count = 0; | 3902 int handler_count = 0; |
| 3878 FunctionLiteral::ParameterFlag duplicate_parameters = | 3903 FormalParameterErrorLocations error_locs; |
| 3879 FunctionLiteral::kNoDuplicateParameters; | |
| 3880 FunctionLiteral::IsParenthesizedFlag parenthesized = parenthesized_function_ | 3904 FunctionLiteral::IsParenthesizedFlag parenthesized = parenthesized_function_ |
| 3881 ? FunctionLiteral::kIsParenthesized | 3905 ? FunctionLiteral::kIsParenthesized |
| 3882 : FunctionLiteral::kNotParenthesized; | 3906 : FunctionLiteral::kNotParenthesized; |
| 3883 // Parse function body. | 3907 // Parse function body. |
| 3884 { | 3908 { |
| 3885 AstNodeFactory function_factory(ast_value_factory()); | 3909 AstNodeFactory function_factory(ast_value_factory()); |
| 3886 FunctionState function_state(&function_state_, &scope_, scope, kind, | 3910 FunctionState function_state(&function_state_, &scope_, scope, kind, |
| 3887 &function_factory); | 3911 &function_factory); |
| 3888 scope_->SetScopeName(function_name); | 3912 scope_->SetScopeName(function_name); |
| 3889 | 3913 |
| 3890 if (is_generator) { | 3914 if (is_generator) { |
| 3891 // For generators, allocating variables in contexts is currently a win | 3915 // For generators, allocating variables in contexts is currently a win |
| 3892 // because it minimizes the work needed to suspend and resume an | 3916 // because it minimizes the work needed to suspend and resume an |
| 3893 // activation. | 3917 // activation. |
| 3894 scope_->ForceContextAllocation(); | 3918 scope_->ForceContextAllocation(); |
| 3895 | 3919 |
| 3896 // Calling a generator returns a generator object. That object is stored | 3920 // Calling a generator returns a generator object. That object is stored |
| 3897 // in a temporary variable, a definition that is used by "yield" | 3921 // in a temporary variable, a definition that is used by "yield" |
| 3898 // expressions. This also marks the FunctionState as a generator. | 3922 // expressions. This also marks the FunctionState as a generator. |
| 3899 Variable* temp = scope_->DeclarationScope()->NewTemporary( | 3923 Variable* temp = scope_->DeclarationScope()->NewTemporary( |
| 3900 ast_value_factory()->dot_generator_object_string()); | 3924 ast_value_factory()->dot_generator_object_string()); |
| 3901 function_state.set_generator_object_variable(temp); | 3925 function_state.set_generator_object_variable(temp); |
| 3902 } | 3926 } |
| 3903 | 3927 |
| 3904 // FormalParameterList :: | 3928 bool has_rest = false; |
| 3905 // '(' (Identifier)*[','] ')' | |
| 3906 Expect(Token::LPAREN, CHECK_OK); | 3929 Expect(Token::LPAREN, CHECK_OK); |
| 3907 scope->set_start_position(scanner()->location().beg_pos); | 3930 int start_position = scanner()->location().beg_pos; |
| 3931 scope_->set_start_position(start_position); |
| 3932 num_parameters = |
| 3933 ParseFormalParameterList(scope, &error_locs, &has_rest, CHECK_OK); |
| 3934 Expect(Token::RPAREN, CHECK_OK); |
| 3935 int formals_end_position = scanner()->location().end_pos; |
| 3908 | 3936 |
| 3909 // We don't yet know if the function will be strict, so we cannot yet | 3937 CheckArityRestrictions(num_parameters, arity_restriction, start_position, |
| 3910 // produce errors for parameter names or duplicates. However, we remember | 3938 formals_end_position, CHECK_OK); |
| 3911 // the locations of these errors if they occur and produce the errors later. | |
| 3912 Scanner::Location eval_args_loc = Scanner::Location::invalid(); | |
| 3913 Scanner::Location dupe_loc = Scanner::Location::invalid(); | |
| 3914 Scanner::Location reserved_loc = Scanner::Location::invalid(); | |
| 3915 | |
| 3916 // Similarly for strong mode. | |
| 3917 Scanner::Location undefined_loc = Scanner::Location::invalid(); | |
| 3918 | |
| 3919 bool is_rest = false; | |
| 3920 bool done = arity_restriction == FunctionLiteral::GETTER_ARITY || | |
| 3921 (peek() == Token::RPAREN && | |
| 3922 arity_restriction != FunctionLiteral::SETTER_ARITY); | |
| 3923 while (!done) { | |
| 3924 bool is_strict_reserved = false; | |
| 3925 is_rest = peek() == Token::ELLIPSIS && allow_harmony_rest_params(); | |
| 3926 if (is_rest) { | |
| 3927 Consume(Token::ELLIPSIS); | |
| 3928 } | |
| 3929 | |
| 3930 const AstRawString* param_name = | |
| 3931 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK); | |
| 3932 | |
| 3933 // Store locations for possible future error reports. | |
| 3934 if (!eval_args_loc.IsValid() && IsEvalOrArguments(param_name)) { | |
| 3935 eval_args_loc = scanner()->location(); | |
| 3936 } | |
| 3937 if (!undefined_loc.IsValid() && IsUndefined(param_name)) { | |
| 3938 undefined_loc = scanner()->location(); | |
| 3939 } | |
| 3940 if (!reserved_loc.IsValid() && is_strict_reserved) { | |
| 3941 reserved_loc = scanner()->location(); | |
| 3942 } | |
| 3943 if (!dupe_loc.IsValid() && | |
| 3944 scope_->IsDeclaredParameter(param_name)) { | |
| 3945 duplicate_parameters = FunctionLiteral::kHasDuplicateParameters; | |
| 3946 dupe_loc = scanner()->location(); | |
| 3947 } | |
| 3948 | |
| 3949 Variable* var = scope_->DeclareParameter(param_name, VAR, is_rest); | |
| 3950 if (is_sloppy(scope->language_mode())) { | |
| 3951 // TODO(sigurds) Mark every parameter as maybe assigned. This is a | |
| 3952 // conservative approximation necessary to account for parameters | |
| 3953 // that are assigned via the arguments array. | |
| 3954 var->set_maybe_assigned(); | |
| 3955 } | |
| 3956 | |
| 3957 num_parameters++; | |
| 3958 if (num_parameters > Code::kMaxArguments) { | |
| 3959 ReportMessage("too_many_parameters"); | |
| 3960 *ok = false; | |
| 3961 return NULL; | |
| 3962 } | |
| 3963 if (arity_restriction == FunctionLiteral::SETTER_ARITY) break; | |
| 3964 done = (peek() == Token::RPAREN); | |
| 3965 if (!done) { | |
| 3966 if (is_rest) { | |
| 3967 ReportMessageAt(scanner()->peek_location(), "param_after_rest"); | |
| 3968 *ok = false; | |
| 3969 return NULL; | |
| 3970 } | |
| 3971 Expect(Token::COMMA, CHECK_OK); | |
| 3972 } | |
| 3973 } | |
| 3974 Expect(Token::RPAREN, CHECK_OK); | |
| 3975 | 3939 |
| 3976 Expect(Token::LBRACE, CHECK_OK); | 3940 Expect(Token::LBRACE, CHECK_OK); |
| 3977 | 3941 |
| 3978 // If we have a named function expression, we add a local variable | 3942 // If we have a named function expression, we add a local variable |
| 3979 // declaration to the body of the function with the name of the | 3943 // declaration to the body of the function with the name of the |
| 3980 // function and let it refer to the function itself (closure). | 3944 // function and let it refer to the function itself (closure). |
| 3981 // NOTE: We create a proxy and resolve it here so that in the | 3945 // NOTE: We create a proxy and resolve it here so that in the |
| 3982 // future we can change the AST to only refer to VariableProxies | 3946 // future we can change the AST to only refer to VariableProxies |
| 3983 // instead of Variables and Proxis as is the case now. | 3947 // instead of Variables and Proxis as is the case now. |
| 3984 Variable* fvar = NULL; | 3948 Variable* fvar = NULL; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4046 materialized_literal_count = function_state.materialized_literal_count(); | 4010 materialized_literal_count = function_state.materialized_literal_count(); |
| 4047 expected_property_count = function_state.expected_property_count(); | 4011 expected_property_count = function_state.expected_property_count(); |
| 4048 handler_count = function_state.handler_count(); | 4012 handler_count = function_state.handler_count(); |
| 4049 } | 4013 } |
| 4050 | 4014 |
| 4051 // Validate name and parameter names. We can do this only after parsing the | 4015 // Validate name and parameter names. We can do this only after parsing the |
| 4052 // function, since the function can declare itself strict. | 4016 // function, since the function can declare itself strict. |
| 4053 CheckFunctionName(language_mode(), kind, function_name, | 4017 CheckFunctionName(language_mode(), kind, function_name, |
| 4054 name_is_strict_reserved, function_name_location, | 4018 name_is_strict_reserved, function_name_location, |
| 4055 CHECK_OK); | 4019 CHECK_OK); |
| 4056 const bool use_strict_params = is_rest || IsConciseMethod(kind); | 4020 const bool use_strict_params = has_rest || IsConciseMethod(kind); |
| 4057 CheckFunctionParameterNames(language_mode(), use_strict_params, | 4021 CheckFunctionParameterNames(language_mode(), use_strict_params, error_locs, |
| 4058 eval_args_loc, undefined_loc, dupe_loc, | 4022 CHECK_OK); |
| 4059 reserved_loc, CHECK_OK); | |
| 4060 | 4023 |
| 4061 if (is_strict(language_mode())) { | 4024 if (is_strict(language_mode())) { |
| 4062 CheckStrictOctalLiteral(scope->start_position(), scope->end_position(), | 4025 CheckStrictOctalLiteral(scope->start_position(), scope->end_position(), |
| 4063 CHECK_OK); | 4026 CHECK_OK); |
| 4064 } | 4027 } |
| 4065 if (is_strict(language_mode())) { | 4028 if (is_strict(language_mode())) { |
| 4066 CheckConflictingVarDeclarations(scope, CHECK_OK); | 4029 CheckConflictingVarDeclarations(scope, CHECK_OK); |
| 4067 } | 4030 } |
| 4068 if (is_strong(language_mode()) && IsSubclassConstructor(kind)) { | 4031 if (is_strong(language_mode()) && IsSubclassConstructor(kind)) { |
| 4069 if (!function_state.super_call_location().IsValid()) { | 4032 if (!function_state.super_call_location().IsValid()) { |
| 4070 ReportMessageAt(function_name_location, "strong_super_call_missing", | 4033 ReportMessageAt(function_name_location, "strong_super_call_missing", |
| 4071 kReferenceError); | 4034 kReferenceError); |
| 4072 *ok = false; | 4035 *ok = false; |
| 4073 return nullptr; | 4036 return nullptr; |
| 4074 } | 4037 } |
| 4075 } | 4038 } |
| 4076 } | 4039 } |
| 4077 | 4040 |
| 4041 FunctionLiteral::ParameterFlag duplicate_parameters = |
| 4042 error_locs.duplicate.IsValid() ? FunctionLiteral::kHasDuplicateParameters |
| 4043 : FunctionLiteral::kNoDuplicateParameters; |
| 4044 |
| 4078 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( | 4045 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( |
| 4079 function_name, ast_value_factory(), scope, body, | 4046 function_name, ast_value_factory(), scope, body, |
| 4080 materialized_literal_count, expected_property_count, handler_count, | 4047 materialized_literal_count, expected_property_count, handler_count, |
| 4081 num_parameters, duplicate_parameters, function_type, | 4048 num_parameters, duplicate_parameters, function_type, |
| 4082 FunctionLiteral::kIsFunction, parenthesized, kind, pos); | 4049 FunctionLiteral::kIsFunction, parenthesized, kind, pos); |
| 4083 function_literal->set_function_token_position(function_token_pos); | 4050 function_literal->set_function_token_position(function_token_pos); |
| 4084 | 4051 |
| 4085 if (scope->has_rest_parameter()) { | 4052 if (scope->has_rest_parameter()) { |
| 4086 // TODO(caitp): enable optimization of functions with rest params | 4053 // TODO(caitp): enable optimization of functions with rest params |
| 4087 function_literal->set_dont_optimize_reason(kRestParameter); | 4054 function_literal->set_dont_optimize_reason(kRestParameter); |
| (...skipping 1691 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5779 | 5746 |
| 5780 Expression* Parser::SpreadCallNew(Expression* function, | 5747 Expression* Parser::SpreadCallNew(Expression* function, |
| 5781 ZoneList<v8::internal::Expression*>* args, | 5748 ZoneList<v8::internal::Expression*>* args, |
| 5782 int pos) { | 5749 int pos) { |
| 5783 args->InsertAt(0, function, zone()); | 5750 args->InsertAt(0, function, zone()); |
| 5784 | 5751 |
| 5785 return factory()->NewCallRuntime( | 5752 return factory()->NewCallRuntime( |
| 5786 ast_value_factory()->reflect_construct_string(), NULL, args, pos); | 5753 ast_value_factory()->reflect_construct_string(), NULL, args, pos); |
| 5787 } | 5754 } |
| 5788 } } // namespace v8::internal | 5755 } } // namespace v8::internal |
| OLD | NEW |