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/base/platform/platform.h" | 9 #include "src/base/platform/platform.h" |
10 #include "src/bootstrapper.h" | 10 #include "src/bootstrapper.h" |
(...skipping 698 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
709 return factory->NewTheHoleLiteral(RelocInfo::kNoPosition); | 709 return factory->NewTheHoleLiteral(RelocInfo::kNoPosition); |
710 } | 710 } |
711 | 711 |
712 | 712 |
713 Expression* ParserTraits::ParseV8Intrinsic(bool* ok) { | 713 Expression* ParserTraits::ParseV8Intrinsic(bool* ok) { |
714 return parser_->ParseV8Intrinsic(ok); | 714 return parser_->ParseV8Intrinsic(ok); |
715 } | 715 } |
716 | 716 |
717 | 717 |
718 FunctionLiteral* ParserTraits::ParseFunctionLiteral( | 718 FunctionLiteral* ParserTraits::ParseFunctionLiteral( |
719 const AstRawString* name, | 719 const AstRawString* name, Scanner::Location function_name_location, |
720 Scanner::Location function_name_location, | 720 bool name_is_strict_reserved, FunctionLiteral::IsGeneratorFlag is_generator, |
721 bool name_is_strict_reserved, | 721 FunctionLiteral::IsArrowFlag is_arrow, |
722 bool is_generator, | 722 FunctionLiteral::IsConciseMethodFlag is_concise_method, |
723 int function_token_position, | 723 int function_token_position, FunctionLiteral::FunctionType type, |
724 FunctionLiteral::FunctionType type, | 724 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) { |
725 FunctionLiteral::ArityRestriction arity_restriction, | 725 return parser_->ParseFunctionLiteral( |
726 bool* ok) { | 726 name, function_name_location, name_is_strict_reserved, is_generator, |
727 return parser_->ParseFunctionLiteral(name, function_name_location, | 727 is_arrow, is_concise_method, function_token_position, type, |
728 name_is_strict_reserved, is_generator, | 728 arity_restriction, ok); |
729 function_token_position, type, | |
730 arity_restriction, ok); | |
731 } | 729 } |
732 | 730 |
733 | 731 |
734 Parser::Parser(CompilationInfo* info) | 732 Parser::Parser(CompilationInfo* info) |
735 : ParserBase<ParserTraits>(&scanner_, | 733 : ParserBase<ParserTraits>(&scanner_, |
736 info->isolate()->stack_guard()->real_climit(), | 734 info->isolate()->stack_guard()->real_climit(), |
737 info->extension(), NULL, info->zone(), this), | 735 info->extension(), NULL, info->zone(), this), |
738 isolate_(info->isolate()), | 736 isolate_(info->isolate()), |
739 script_(info->script()), | 737 script_(info->script()), |
740 scanner_(isolate_->unicode_cache()), | 738 scanner_(isolate_->unicode_cache()), |
(...skipping 10 matching lines...) Expand all Loading... |
751 DCHECK(!script_.is_null()); | 749 DCHECK(!script_.is_null()); |
752 isolate_->set_ast_node_id(0); | 750 isolate_->set_ast_node_id(0); |
753 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping); | 751 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping); |
754 set_allow_modules(!info->is_native() && FLAG_harmony_modules); | 752 set_allow_modules(!info->is_native() && FLAG_harmony_modules); |
755 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native()); | 753 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native()); |
756 set_allow_lazy(false); // Must be explicitly enabled. | 754 set_allow_lazy(false); // Must be explicitly enabled. |
757 set_allow_generators(FLAG_harmony_generators); | 755 set_allow_generators(FLAG_harmony_generators); |
758 set_allow_arrow_functions(FLAG_harmony_arrow_functions); | 756 set_allow_arrow_functions(FLAG_harmony_arrow_functions); |
759 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals); | 757 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals); |
760 set_allow_classes(FLAG_harmony_classes); | 758 set_allow_classes(FLAG_harmony_classes); |
| 759 set_allow_harmony_object_literals(FLAG_harmony_object_literals); |
761 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; | 760 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; |
762 ++feature) { | 761 ++feature) { |
763 use_counts_[feature] = 0; | 762 use_counts_[feature] = 0; |
764 } | 763 } |
765 } | 764 } |
766 | 765 |
767 | 766 |
768 FunctionLiteral* Parser::ParseProgram() { | 767 FunctionLiteral* Parser::ParseProgram() { |
769 // TODO(bmeurer): We temporarily need to pass allow_nesting = true here, | 768 // TODO(bmeurer): We temporarily need to pass allow_nesting = true here, |
770 // see comment for HistogramTimerScope class. | 769 // see comment for HistogramTimerScope class. |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
891 | 890 |
892 ast_value_factory_->Internalize(isolate()); | 891 ast_value_factory_->Internalize(isolate()); |
893 if (ok) { | 892 if (ok) { |
894 result = factory()->NewFunctionLiteral( | 893 result = factory()->NewFunctionLiteral( |
895 ast_value_factory_->empty_string(), ast_value_factory_, scope_, body, | 894 ast_value_factory_->empty_string(), ast_value_factory_, scope_, body, |
896 function_state.materialized_literal_count(), | 895 function_state.materialized_literal_count(), |
897 function_state.expected_property_count(), | 896 function_state.expected_property_count(), |
898 function_state.handler_count(), 0, | 897 function_state.handler_count(), 0, |
899 FunctionLiteral::kNoDuplicateParameters, | 898 FunctionLiteral::kNoDuplicateParameters, |
900 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kGlobalOrEval, | 899 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kGlobalOrEval, |
901 FunctionLiteral::kNotParenthesized, FunctionLiteral::kNormalFunction, | 900 FunctionLiteral::kNotParenthesized, FunctionLiteral::kNotGenerator, |
902 0); | 901 FunctionLiteral::kNotArrow, FunctionLiteral::kNotConciseMethod, 0); |
903 result->set_ast_properties(factory()->visitor()->ast_properties()); | 902 result->set_ast_properties(factory()->visitor()->ast_properties()); |
904 result->set_dont_optimize_reason( | 903 result->set_dont_optimize_reason( |
905 factory()->visitor()->dont_optimize_reason()); | 904 factory()->visitor()->dont_optimize_reason()); |
906 } else if (stack_overflow()) { | 905 } else if (stack_overflow()) { |
907 isolate()->StackOverflow(); | 906 isolate()->StackOverflow(); |
908 } else { | 907 } else { |
909 ThrowPendingError(); | 908 ThrowPendingError(); |
910 } | 909 } |
911 } | 910 } |
912 | 911 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
990 : FunctionLiteral::DECLARATION; | 989 : FunctionLiteral::DECLARATION; |
991 bool is_generator = shared_info->is_generator(); | 990 bool is_generator = shared_info->is_generator(); |
992 bool ok = true; | 991 bool ok = true; |
993 | 992 |
994 if (shared_info->is_arrow()) { | 993 if (shared_info->is_arrow()) { |
995 DCHECK(!is_generator); | 994 DCHECK(!is_generator); |
996 Expression* expression = ParseExpression(false, &ok); | 995 Expression* expression = ParseExpression(false, &ok); |
997 DCHECK(expression->IsFunctionLiteral()); | 996 DCHECK(expression->IsFunctionLiteral()); |
998 result = expression->AsFunctionLiteral(); | 997 result = expression->AsFunctionLiteral(); |
999 } else { | 998 } else { |
1000 result = ParseFunctionLiteral(raw_name, Scanner::Location::invalid(), | 999 result = ParseFunctionLiteral( |
1001 false, // Strict mode name already checked. | 1000 raw_name, Scanner::Location::invalid(), |
1002 is_generator, RelocInfo::kNoPosition, | 1001 false, // Strict mode name already checked. |
1003 function_type, | 1002 is_generator ? FunctionLiteral::kIsGenerator |
1004 FunctionLiteral::NORMAL_ARITY, &ok); | 1003 : FunctionLiteral::kNotGenerator, |
| 1004 FunctionLiteral::kNotArrow, |
| 1005 shared_info->is_concise_method() ? FunctionLiteral::kIsConciseMethod |
| 1006 : FunctionLiteral::kNotConciseMethod, |
| 1007 RelocInfo::kNoPosition, function_type, FunctionLiteral::NORMAL_ARITY, |
| 1008 &ok); |
1005 } | 1009 } |
1006 // Make sure the results agree. | 1010 // Make sure the results agree. |
1007 DCHECK(ok == (result != NULL)); | 1011 DCHECK(ok == (result != NULL)); |
1008 } | 1012 } |
1009 | 1013 |
1010 // Make sure the target stack is empty. | 1014 // Make sure the target stack is empty. |
1011 DCHECK(target_stack_ == NULL); | 1015 DCHECK(target_stack_ == NULL); |
1012 | 1016 |
1013 ast_value_factory_->Internalize(isolate()); | 1017 ast_value_factory_->Internalize(isolate()); |
1014 if (result == NULL) { | 1018 if (result == NULL) { |
(...skipping 868 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1883 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' | 1887 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' |
1884 // GeneratorDeclaration :: | 1888 // GeneratorDeclaration :: |
1885 // 'function' '*' Identifier '(' FormalParameterListopt ')' | 1889 // 'function' '*' Identifier '(' FormalParameterListopt ')' |
1886 // '{' FunctionBody '}' | 1890 // '{' FunctionBody '}' |
1887 Expect(Token::FUNCTION, CHECK_OK); | 1891 Expect(Token::FUNCTION, CHECK_OK); |
1888 int pos = position(); | 1892 int pos = position(); |
1889 bool is_generator = allow_generators() && Check(Token::MUL); | 1893 bool is_generator = allow_generators() && Check(Token::MUL); |
1890 bool is_strict_reserved = false; | 1894 bool is_strict_reserved = false; |
1891 const AstRawString* name = ParseIdentifierOrStrictReservedWord( | 1895 const AstRawString* name = ParseIdentifierOrStrictReservedWord( |
1892 &is_strict_reserved, CHECK_OK); | 1896 &is_strict_reserved, CHECK_OK); |
1893 FunctionLiteral* fun = ParseFunctionLiteral(name, | 1897 FunctionLiteral* fun = ParseFunctionLiteral( |
1894 scanner()->location(), | 1898 name, scanner()->location(), is_strict_reserved, |
1895 is_strict_reserved, | 1899 is_generator ? FunctionLiteral::kIsGenerator |
1896 is_generator, | 1900 : FunctionLiteral::kNotGenerator, |
1897 pos, | 1901 FunctionLiteral::kNotArrow, FunctionLiteral::kNotConciseMethod, pos, |
1898 FunctionLiteral::DECLARATION, | 1902 FunctionLiteral::DECLARATION, FunctionLiteral::NORMAL_ARITY, CHECK_OK); |
1899 FunctionLiteral::NORMAL_ARITY, | |
1900 CHECK_OK); | |
1901 // Even if we're not at the top-level of the global or a function | 1903 // Even if we're not at the top-level of the global or a function |
1902 // scope, we treat it as such and introduce the function with its | 1904 // scope, we treat it as such and introduce the function with its |
1903 // initial value upon entering the corresponding scope. | 1905 // initial value upon entering the corresponding scope. |
1904 // In ES6, a function behaves as a lexical binding, except in the | 1906 // In ES6, a function behaves as a lexical binding, except in the |
1905 // global scope, or the initial scope of eval or another function. | 1907 // global scope, or the initial scope of eval or another function. |
1906 VariableMode mode = | 1908 VariableMode mode = |
1907 allow_harmony_scoping() && strict_mode() == STRICT && | 1909 allow_harmony_scoping() && strict_mode() == STRICT && |
1908 !(scope_->is_global_scope() || scope_->is_eval_scope() || | 1910 !(scope_->is_global_scope() || scope_->is_eval_scope() || |
1909 scope_->is_function_scope()) ? LET : VAR; | 1911 scope_->is_function_scope()) ? LET : VAR; |
1910 VariableProxy* proxy = NewUnresolved(name, mode, Interface::NewValue()); | 1912 VariableProxy* proxy = NewUnresolved(name, mode, Interface::NewValue()); |
(...skipping 1443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3354 Expression* expression, Scope* scope, Scanner::Location* dupe_loc, | 3356 Expression* expression, Scope* scope, Scanner::Location* dupe_loc, |
3355 bool* ok) { | 3357 bool* ok) { |
3356 int num_params = 0; | 3358 int num_params = 0; |
3357 *ok = CheckAndDeclareArrowParameter(this, expression, scope, &num_params, | 3359 *ok = CheckAndDeclareArrowParameter(this, expression, scope, &num_params, |
3358 dupe_loc); | 3360 dupe_loc); |
3359 return num_params; | 3361 return num_params; |
3360 } | 3362 } |
3361 | 3363 |
3362 | 3364 |
3363 FunctionLiteral* Parser::ParseFunctionLiteral( | 3365 FunctionLiteral* Parser::ParseFunctionLiteral( |
3364 const AstRawString* function_name, | 3366 const AstRawString* function_name, Scanner::Location function_name_location, |
3365 Scanner::Location function_name_location, | 3367 bool name_is_strict_reserved, FunctionLiteral::IsGeneratorFlag is_generator, |
3366 bool name_is_strict_reserved, | 3368 FunctionLiteral::IsArrowFlag is_arrow, |
3367 bool is_generator, | 3369 FunctionLiteral::IsConciseMethodFlag is_concise_method, |
3368 int function_token_pos, | 3370 int function_token_pos, FunctionLiteral::FunctionType function_type, |
3369 FunctionLiteral::FunctionType function_type, | 3371 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) { |
3370 FunctionLiteral::ArityRestriction arity_restriction, | |
3371 bool* ok) { | |
3372 // Function :: | 3372 // Function :: |
3373 // '(' FormalParameterList? ')' '{' FunctionBody '}' | 3373 // '(' FormalParameterList? ')' '{' FunctionBody '}' |
3374 // | 3374 // |
3375 // Getter :: | 3375 // Getter :: |
3376 // '(' ')' '{' FunctionBody '}' | 3376 // '(' ')' '{' FunctionBody '}' |
3377 // | 3377 // |
3378 // Setter :: | 3378 // Setter :: |
3379 // '(' PropertySetParameterList ')' '{' FunctionBody '}' | 3379 // '(' PropertySetParameterList ')' '{' FunctionBody '}' |
3380 | 3380 |
3381 int pos = function_token_pos == RelocInfo::kNoPosition | 3381 int pos = function_token_pos == RelocInfo::kNoPosition |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3582 &expected_property_count, CHECK_OK); | 3582 &expected_property_count, CHECK_OK); |
3583 } else { | 3583 } else { |
3584 body = ParseEagerFunctionBody(function_name, pos, fvar, fvar_init_op, | 3584 body = ParseEagerFunctionBody(function_name, pos, fvar, fvar_init_op, |
3585 is_generator, CHECK_OK); | 3585 is_generator, CHECK_OK); |
3586 materialized_literal_count = function_state.materialized_literal_count(); | 3586 materialized_literal_count = function_state.materialized_literal_count(); |
3587 expected_property_count = function_state.expected_property_count(); | 3587 expected_property_count = function_state.expected_property_count(); |
3588 handler_count = function_state.handler_count(); | 3588 handler_count = function_state.handler_count(); |
3589 } | 3589 } |
3590 | 3590 |
3591 // Validate strict mode. | 3591 // Validate strict mode. |
3592 if (strict_mode() == STRICT) { | 3592 // Concise methods use StrictFormalParameters. |
| 3593 if (strict_mode() == STRICT || is_concise_method) { |
3593 CheckStrictFunctionNameAndParameters(function_name, | 3594 CheckStrictFunctionNameAndParameters(function_name, |
3594 name_is_strict_reserved, | 3595 name_is_strict_reserved, |
3595 function_name_location, | 3596 function_name_location, |
3596 eval_args_error_log, | 3597 eval_args_error_log, |
3597 dupe_error_loc, | 3598 dupe_error_loc, |
3598 reserved_loc, | 3599 reserved_loc, |
3599 CHECK_OK); | 3600 CHECK_OK); |
| 3601 } |
| 3602 if (strict_mode() == STRICT) { |
3600 CheckOctalLiteral(scope->start_position(), | 3603 CheckOctalLiteral(scope->start_position(), |
3601 scope->end_position(), | 3604 scope->end_position(), |
3602 CHECK_OK); | 3605 CHECK_OK); |
3603 } | 3606 } |
3604 ast_properties = *factory()->visitor()->ast_properties(); | 3607 ast_properties = *factory()->visitor()->ast_properties(); |
3605 dont_optimize_reason = factory()->visitor()->dont_optimize_reason(); | 3608 dont_optimize_reason = factory()->visitor()->dont_optimize_reason(); |
3606 | 3609 |
3607 if (allow_harmony_scoping() && strict_mode() == STRICT) { | 3610 if (allow_harmony_scoping() && strict_mode() == STRICT) { |
3608 CheckConflictingVarDeclarations(scope, CHECK_OK); | 3611 CheckConflictingVarDeclarations(scope, CHECK_OK); |
3609 } | 3612 } |
3610 } | 3613 } |
3611 | 3614 |
3612 FunctionLiteral::KindFlag kind = is_generator | |
3613 ? FunctionLiteral::kGeneratorFunction | |
3614 : FunctionLiteral::kNormalFunction; | |
3615 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( | 3615 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( |
3616 function_name, ast_value_factory_, scope, body, | 3616 function_name, ast_value_factory_, scope, body, |
3617 materialized_literal_count, expected_property_count, handler_count, | 3617 materialized_literal_count, expected_property_count, handler_count, |
3618 num_parameters, duplicate_parameters, function_type, | 3618 num_parameters, duplicate_parameters, function_type, |
3619 FunctionLiteral::kIsFunction, parenthesized, kind, pos); | 3619 FunctionLiteral::kIsFunction, parenthesized, is_generator, is_arrow, |
| 3620 is_concise_method, pos); |
3620 function_literal->set_function_token_position(function_token_pos); | 3621 function_literal->set_function_token_position(function_token_pos); |
3621 function_literal->set_ast_properties(&ast_properties); | 3622 function_literal->set_ast_properties(&ast_properties); |
3622 function_literal->set_dont_optimize_reason(dont_optimize_reason); | 3623 function_literal->set_dont_optimize_reason(dont_optimize_reason); |
3623 | 3624 |
3624 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); | 3625 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); |
3625 return function_literal; | 3626 return function_literal; |
3626 } | 3627 } |
3627 | 3628 |
3628 | 3629 |
3629 void Parser::SkipLazyFunctionBody(const AstRawString* function_name, | 3630 void Parser::SkipLazyFunctionBody(const AstRawString* function_name, |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3763 reusable_preparser_ = new PreParser(&scanner_, NULL, stack_limit); | 3764 reusable_preparser_ = new PreParser(&scanner_, NULL, stack_limit); |
3764 reusable_preparser_->set_allow_harmony_scoping(allow_harmony_scoping()); | 3765 reusable_preparser_->set_allow_harmony_scoping(allow_harmony_scoping()); |
3765 reusable_preparser_->set_allow_modules(allow_modules()); | 3766 reusable_preparser_->set_allow_modules(allow_modules()); |
3766 reusable_preparser_->set_allow_natives_syntax(allow_natives_syntax()); | 3767 reusable_preparser_->set_allow_natives_syntax(allow_natives_syntax()); |
3767 reusable_preparser_->set_allow_lazy(true); | 3768 reusable_preparser_->set_allow_lazy(true); |
3768 reusable_preparser_->set_allow_generators(allow_generators()); | 3769 reusable_preparser_->set_allow_generators(allow_generators()); |
3769 reusable_preparser_->set_allow_arrow_functions(allow_arrow_functions()); | 3770 reusable_preparser_->set_allow_arrow_functions(allow_arrow_functions()); |
3770 reusable_preparser_->set_allow_harmony_numeric_literals( | 3771 reusable_preparser_->set_allow_harmony_numeric_literals( |
3771 allow_harmony_numeric_literals()); | 3772 allow_harmony_numeric_literals()); |
3772 reusable_preparser_->set_allow_classes(allow_classes()); | 3773 reusable_preparser_->set_allow_classes(allow_classes()); |
| 3774 reusable_preparser_->set_allow_harmony_object_literals( |
| 3775 allow_harmony_object_literals()); |
3773 } | 3776 } |
3774 PreParser::PreParseResult result = | 3777 PreParser::PreParseResult result = |
3775 reusable_preparser_->PreParseLazyFunction(strict_mode(), | 3778 reusable_preparser_->PreParseLazyFunction(strict_mode(), |
3776 is_generator(), | 3779 is_generator(), |
3777 logger); | 3780 logger); |
3778 return result; | 3781 return result; |
3779 } | 3782 } |
3780 | 3783 |
3781 | 3784 |
3782 Expression* Parser::ParseV8Intrinsic(bool* ok) { | 3785 Expression* Parser::ParseV8Intrinsic(bool* ok) { |
(...skipping 1047 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4830 info()->SetAstValueFactory(ast_value_factory_); | 4833 info()->SetAstValueFactory(ast_value_factory_); |
4831 } | 4834 } |
4832 ast_value_factory_ = NULL; | 4835 ast_value_factory_ = NULL; |
4833 | 4836 |
4834 InternalizeUseCounts(); | 4837 InternalizeUseCounts(); |
4835 | 4838 |
4836 return (result != NULL); | 4839 return (result != NULL); |
4837 } | 4840 } |
4838 | 4841 |
4839 } } // namespace v8::internal | 4842 } } // namespace v8::internal |
OLD | NEW |