Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: src/parser.cc

Issue 477263002: ES6: Add support for method shorthand in object literals (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: merge Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 706 matching lines...) Expand 10 before | Expand all | Expand 10 after
717 return factory->NewTheHoleLiteral(RelocInfo::kNoPosition); 717 return factory->NewTheHoleLiteral(RelocInfo::kNoPosition);
718 } 718 }
719 719
720 720
721 Expression* ParserTraits::ParseV8Intrinsic(bool* ok) { 721 Expression* ParserTraits::ParseV8Intrinsic(bool* ok) {
722 return parser_->ParseV8Intrinsic(ok); 722 return parser_->ParseV8Intrinsic(ok);
723 } 723 }
724 724
725 725
726 FunctionLiteral* ParserTraits::ParseFunctionLiteral( 726 FunctionLiteral* ParserTraits::ParseFunctionLiteral(
727 const AstRawString* name, 727 const AstRawString* name, Scanner::Location function_name_location,
728 Scanner::Location function_name_location, 728 bool name_is_strict_reserved, FunctionKind kind,
729 bool name_is_strict_reserved, 729 int function_token_position, FunctionLiteral::FunctionType type,
730 bool is_generator, 730 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) {
731 int function_token_position, 731 return parser_->ParseFunctionLiteral(
732 FunctionLiteral::FunctionType type, 732 name, function_name_location, name_is_strict_reserved, kind,
733 FunctionLiteral::ArityRestriction arity_restriction, 733 function_token_position, type, arity_restriction, ok);
734 bool* ok) {
735 return parser_->ParseFunctionLiteral(name, function_name_location,
736 name_is_strict_reserved, is_generator,
737 function_token_position, type,
738 arity_restriction, ok);
739 } 734 }
740 735
741 736
742 Parser::Parser(CompilationInfo* info, ParseInfo* parse_info) 737 Parser::Parser(CompilationInfo* info, ParseInfo* parse_info)
743 : ParserBase<ParserTraits>(&scanner_, parse_info->stack_limit, 738 : ParserBase<ParserTraits>(&scanner_, parse_info->stack_limit,
744 info->extension(), NULL, info->zone(), 739 info->extension(), NULL, info->zone(),
745 info->ast_node_id_gen(), this), 740 info->ast_node_id_gen(), this),
746 isolate_(info->isolate()), 741 isolate_(info->isolate()),
747 script_(info->script()), 742 script_(info->script()),
748 scanner_(parse_info->unicode_cache), 743 scanner_(parse_info->unicode_cache),
(...skipping 11 matching lines...) Expand all
760 pre_parse_timer_(NULL) { 755 pre_parse_timer_(NULL) {
761 DCHECK(!script_.is_null()); 756 DCHECK(!script_.is_null());
762 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping); 757 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping);
763 set_allow_modules(!info->is_native() && FLAG_harmony_modules); 758 set_allow_modules(!info->is_native() && FLAG_harmony_modules);
764 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native()); 759 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native());
765 set_allow_lazy(false); // Must be explicitly enabled. 760 set_allow_lazy(false); // Must be explicitly enabled.
766 set_allow_generators(FLAG_harmony_generators); 761 set_allow_generators(FLAG_harmony_generators);
767 set_allow_arrow_functions(FLAG_harmony_arrow_functions); 762 set_allow_arrow_functions(FLAG_harmony_arrow_functions);
768 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals); 763 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals);
769 set_allow_classes(FLAG_harmony_classes); 764 set_allow_classes(FLAG_harmony_classes);
765 set_allow_harmony_object_literals(FLAG_harmony_object_literals);
770 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; 766 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
771 ++feature) { 767 ++feature) {
772 use_counts_[feature] = 0; 768 use_counts_[feature] = 0;
773 } 769 }
774 if (ast_value_factory_ == NULL) { 770 if (ast_value_factory_ == NULL) {
775 ast_value_factory_ = new AstValueFactory(zone(), parse_info->hash_seed); 771 ast_value_factory_ = new AstValueFactory(zone(), parse_info->hash_seed);
776 } 772 }
777 } 773 }
778 774
779 775
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
904 } 900 }
905 901
906 if (ok) { 902 if (ok) {
907 result = factory()->NewFunctionLiteral( 903 result = factory()->NewFunctionLiteral(
908 ast_value_factory_->empty_string(), ast_value_factory_, scope_, body, 904 ast_value_factory_->empty_string(), ast_value_factory_, scope_, body,
909 function_state.materialized_literal_count(), 905 function_state.materialized_literal_count(),
910 function_state.expected_property_count(), 906 function_state.expected_property_count(),
911 function_state.handler_count(), 0, 907 function_state.handler_count(), 0,
912 FunctionLiteral::kNoDuplicateParameters, 908 FunctionLiteral::kNoDuplicateParameters,
913 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kGlobalOrEval, 909 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kGlobalOrEval,
914 FunctionLiteral::kNotParenthesized, FunctionLiteral::kNormalFunction, 910 FunctionLiteral::kNotParenthesized, FunctionKind::kNormalFunction, 0);
915 0);
916 result->set_ast_properties(factory()->visitor()->ast_properties()); 911 result->set_ast_properties(factory()->visitor()->ast_properties());
917 result->set_dont_optimize_reason( 912 result->set_dont_optimize_reason(
918 factory()->visitor()->dont_optimize_reason()); 913 factory()->visitor()->dont_optimize_reason());
919 } 914 }
920 } 915 }
921 916
922 // Make sure the target stack is empty. 917 // Make sure the target stack is empty.
923 DCHECK(target_stack_ == NULL); 918 DCHECK(target_stack_ == NULL);
924 919
925 return result; 920 return result;
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
992 FunctionState function_state(&function_state_, &scope_, scope, zone(), 987 FunctionState function_state(&function_state_, &scope_, scope, zone(),
993 ast_value_factory_, info()->ast_node_id_gen()); 988 ast_value_factory_, info()->ast_node_id_gen());
994 DCHECK(scope->strict_mode() == SLOPPY || info()->strict_mode() == STRICT); 989 DCHECK(scope->strict_mode() == SLOPPY || info()->strict_mode() == STRICT);
995 DCHECK(info()->strict_mode() == shared_info->strict_mode()); 990 DCHECK(info()->strict_mode() == shared_info->strict_mode());
996 scope->SetStrictMode(shared_info->strict_mode()); 991 scope->SetStrictMode(shared_info->strict_mode());
997 FunctionLiteral::FunctionType function_type = shared_info->is_expression() 992 FunctionLiteral::FunctionType function_type = shared_info->is_expression()
998 ? (shared_info->is_anonymous() 993 ? (shared_info->is_anonymous()
999 ? FunctionLiteral::ANONYMOUS_EXPRESSION 994 ? FunctionLiteral::ANONYMOUS_EXPRESSION
1000 : FunctionLiteral::NAMED_EXPRESSION) 995 : FunctionLiteral::NAMED_EXPRESSION)
1001 : FunctionLiteral::DECLARATION; 996 : FunctionLiteral::DECLARATION;
1002 bool is_generator = shared_info->is_generator();
1003 bool ok = true; 997 bool ok = true;
1004 998
1005 if (shared_info->is_arrow()) { 999 if (shared_info->is_arrow()) {
1006 DCHECK(!is_generator);
1007 Expression* expression = ParseExpression(false, &ok); 1000 Expression* expression = ParseExpression(false, &ok);
1008 DCHECK(expression->IsFunctionLiteral()); 1001 DCHECK(expression->IsFunctionLiteral());
1009 result = expression->AsFunctionLiteral(); 1002 result = expression->AsFunctionLiteral();
1010 } else { 1003 } else {
1011 result = ParseFunctionLiteral(raw_name, Scanner::Location::invalid(), 1004 result = ParseFunctionLiteral(raw_name, Scanner::Location::invalid(),
1012 false, // Strict mode name already checked. 1005 false, // Strict mode name already checked.
1013 is_generator, RelocInfo::kNoPosition, 1006 shared_info->kind(), RelocInfo::kNoPosition,
1014 function_type, 1007 function_type,
1015 FunctionLiteral::NORMAL_ARITY, &ok); 1008 FunctionLiteral::NORMAL_ARITY, &ok);
1016 } 1009 }
1017 // Make sure the results agree. 1010 // Make sure the results agree.
1018 DCHECK(ok == (result != NULL)); 1011 DCHECK(ok == (result != NULL));
1019 } 1012 }
1020 1013
1021 // Make sure the target stack is empty. 1014 // Make sure the target stack is empty.
1022 DCHECK(target_stack_ == NULL); 1015 DCHECK(target_stack_ == NULL);
1023 1016
(...skipping 863 matching lines...) Expand 10 before | Expand all | Expand 10 after
1887 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' 1880 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}'
1888 // GeneratorDeclaration :: 1881 // GeneratorDeclaration ::
1889 // 'function' '*' Identifier '(' FormalParameterListopt ')' 1882 // 'function' '*' Identifier '(' FormalParameterListopt ')'
1890 // '{' FunctionBody '}' 1883 // '{' FunctionBody '}'
1891 Expect(Token::FUNCTION, CHECK_OK); 1884 Expect(Token::FUNCTION, CHECK_OK);
1892 int pos = position(); 1885 int pos = position();
1893 bool is_generator = allow_generators() && Check(Token::MUL); 1886 bool is_generator = allow_generators() && Check(Token::MUL);
1894 bool is_strict_reserved = false; 1887 bool is_strict_reserved = false;
1895 const AstRawString* name = ParseIdentifierOrStrictReservedWord( 1888 const AstRawString* name = ParseIdentifierOrStrictReservedWord(
1896 &is_strict_reserved, CHECK_OK); 1889 &is_strict_reserved, CHECK_OK);
1897 FunctionLiteral* fun = ParseFunctionLiteral(name, 1890 FunctionLiteral* fun =
1898 scanner()->location(), 1891 ParseFunctionLiteral(name, scanner()->location(), is_strict_reserved,
1899 is_strict_reserved, 1892 is_generator ? FunctionKind::kGeneratorFunction
1900 is_generator, 1893 : FunctionKind::kNormalFunction,
1901 pos, 1894 pos, FunctionLiteral::DECLARATION,
1902 FunctionLiteral::DECLARATION, 1895 FunctionLiteral::NORMAL_ARITY, CHECK_OK);
1903 FunctionLiteral::NORMAL_ARITY,
1904 CHECK_OK);
1905 // Even if we're not at the top-level of the global or a function 1896 // Even if we're not at the top-level of the global or a function
1906 // scope, we treat it as such and introduce the function with its 1897 // scope, we treat it as such and introduce the function with its
1907 // initial value upon entering the corresponding scope. 1898 // initial value upon entering the corresponding scope.
1908 // In ES6, a function behaves as a lexical binding, except in the 1899 // In ES6, a function behaves as a lexical binding, except in the
1909 // global scope, or the initial scope of eval or another function. 1900 // global scope, or the initial scope of eval or another function.
1910 VariableMode mode = 1901 VariableMode mode =
1911 allow_harmony_scoping() && strict_mode() == STRICT && 1902 allow_harmony_scoping() && strict_mode() == STRICT &&
1912 !(scope_->is_global_scope() || scope_->is_eval_scope() || 1903 !(scope_->is_global_scope() || scope_->is_eval_scope() ||
1913 scope_->is_function_scope()) ? LET : VAR; 1904 scope_->is_function_scope()) ? LET : VAR;
1914 VariableProxy* proxy = NewUnresolved(name, mode, Interface::NewValue()); 1905 VariableProxy* proxy = NewUnresolved(name, mode, Interface::NewValue());
(...skipping 1443 matching lines...) Expand 10 before | Expand all | Expand 10 after
3358 Expression* expression, Scope* scope, Scanner::Location* dupe_loc, 3349 Expression* expression, Scope* scope, Scanner::Location* dupe_loc,
3359 bool* ok) { 3350 bool* ok) {
3360 int num_params = 0; 3351 int num_params = 0;
3361 *ok = CheckAndDeclareArrowParameter(this, expression, scope, &num_params, 3352 *ok = CheckAndDeclareArrowParameter(this, expression, scope, &num_params,
3362 dupe_loc); 3353 dupe_loc);
3363 return num_params; 3354 return num_params;
3364 } 3355 }
3365 3356
3366 3357
3367 FunctionLiteral* Parser::ParseFunctionLiteral( 3358 FunctionLiteral* Parser::ParseFunctionLiteral(
3368 const AstRawString* function_name, 3359 const AstRawString* function_name, Scanner::Location function_name_location,
3369 Scanner::Location function_name_location, 3360 bool name_is_strict_reserved, FunctionKind kind, int function_token_pos,
3370 bool name_is_strict_reserved,
3371 bool is_generator,
3372 int function_token_pos,
3373 FunctionLiteral::FunctionType function_type, 3361 FunctionLiteral::FunctionType function_type,
3374 FunctionLiteral::ArityRestriction arity_restriction, 3362 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) {
3375 bool* ok) {
3376 // Function :: 3363 // Function ::
3377 // '(' FormalParameterList? ')' '{' FunctionBody '}' 3364 // '(' FormalParameterList? ')' '{' FunctionBody '}'
3378 // 3365 //
3379 // Getter :: 3366 // Getter ::
3380 // '(' ')' '{' FunctionBody '}' 3367 // '(' ')' '{' FunctionBody '}'
3381 // 3368 //
3382 // Setter :: 3369 // Setter ::
3383 // '(' PropertySetParameterList ')' '{' FunctionBody '}' 3370 // '(' PropertySetParameterList ')' '{' FunctionBody '}'
3384 3371
3385 int pos = function_token_pos == RelocInfo::kNoPosition 3372 int pos = function_token_pos == RelocInfo::kNoPosition
3386 ? peek_position() : function_token_pos; 3373 ? peek_position() : function_token_pos;
3387 3374
3375 bool is_generator = IsGeneratorFunction(kind);
3376
3388 // Anonymous functions were passed either the empty symbol or a null 3377 // Anonymous functions were passed either the empty symbol or a null
3389 // handle as the function name. Remember if we were passed a non-empty 3378 // handle as the function name. Remember if we were passed a non-empty
3390 // handle to decide whether to invoke function name inference. 3379 // handle to decide whether to invoke function name inference.
3391 bool should_infer_name = function_name == NULL; 3380 bool should_infer_name = function_name == NULL;
3392 3381
3393 // We want a non-null handle as the function name. 3382 // We want a non-null handle as the function name.
3394 if (should_infer_name) { 3383 if (should_infer_name) {
3395 function_name = ast_value_factory_->empty_string(); 3384 function_name = ast_value_factory_->empty_string();
3396 } 3385 }
3397 3386
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
3586 &expected_property_count, CHECK_OK); 3575 &expected_property_count, CHECK_OK);
3587 } else { 3576 } else {
3588 body = ParseEagerFunctionBody(function_name, pos, fvar, fvar_init_op, 3577 body = ParseEagerFunctionBody(function_name, pos, fvar, fvar_init_op,
3589 is_generator, CHECK_OK); 3578 is_generator, CHECK_OK);
3590 materialized_literal_count = function_state.materialized_literal_count(); 3579 materialized_literal_count = function_state.materialized_literal_count();
3591 expected_property_count = function_state.expected_property_count(); 3580 expected_property_count = function_state.expected_property_count();
3592 handler_count = function_state.handler_count(); 3581 handler_count = function_state.handler_count();
3593 } 3582 }
3594 3583
3595 // Validate strict mode. 3584 // Validate strict mode.
3596 if (strict_mode() == STRICT) { 3585 // Concise methods use StrictFormalParameters.
3586 if (strict_mode() == STRICT || IsConciseMethod(kind)) {
3597 CheckStrictFunctionNameAndParameters(function_name, 3587 CheckStrictFunctionNameAndParameters(function_name,
3598 name_is_strict_reserved, 3588 name_is_strict_reserved,
3599 function_name_location, 3589 function_name_location,
3600 eval_args_error_log, 3590 eval_args_error_log,
3601 dupe_error_loc, 3591 dupe_error_loc,
3602 reserved_loc, 3592 reserved_loc,
3603 CHECK_OK); 3593 CHECK_OK);
3594 }
3595 if (strict_mode() == STRICT) {
3604 CheckOctalLiteral(scope->start_position(), 3596 CheckOctalLiteral(scope->start_position(),
3605 scope->end_position(), 3597 scope->end_position(),
3606 CHECK_OK); 3598 CHECK_OK);
3607 } 3599 }
3608 ast_properties = *factory()->visitor()->ast_properties(); 3600 ast_properties = *factory()->visitor()->ast_properties();
3609 dont_optimize_reason = factory()->visitor()->dont_optimize_reason(); 3601 dont_optimize_reason = factory()->visitor()->dont_optimize_reason();
3610 3602
3611 if (allow_harmony_scoping() && strict_mode() == STRICT) { 3603 if (allow_harmony_scoping() && strict_mode() == STRICT) {
3612 CheckConflictingVarDeclarations(scope, CHECK_OK); 3604 CheckConflictingVarDeclarations(scope, CHECK_OK);
3613 } 3605 }
3614 } 3606 }
3615 3607
3616 FunctionLiteral::KindFlag kind = is_generator
3617 ? FunctionLiteral::kGeneratorFunction
3618 : FunctionLiteral::kNormalFunction;
3619 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( 3608 FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
3620 function_name, ast_value_factory_, scope, body, 3609 function_name, ast_value_factory_, scope, body,
3621 materialized_literal_count, expected_property_count, handler_count, 3610 materialized_literal_count, expected_property_count, handler_count,
3622 num_parameters, duplicate_parameters, function_type, 3611 num_parameters, duplicate_parameters, function_type,
3623 FunctionLiteral::kIsFunction, parenthesized, kind, pos); 3612 FunctionLiteral::kIsFunction, parenthesized, kind, pos);
3624 function_literal->set_function_token_position(function_token_pos); 3613 function_literal->set_function_token_position(function_token_pos);
3625 function_literal->set_ast_properties(&ast_properties); 3614 function_literal->set_ast_properties(&ast_properties);
3626 function_literal->set_dont_optimize_reason(dont_optimize_reason); 3615 function_literal->set_dont_optimize_reason(dont_optimize_reason);
3627 3616
3628 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); 3617 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal);
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
3768 reusable_preparser_ = new PreParser(&scanner_, NULL, stack_limit_); 3757 reusable_preparser_ = new PreParser(&scanner_, NULL, stack_limit_);
3769 reusable_preparser_->set_allow_harmony_scoping(allow_harmony_scoping()); 3758 reusable_preparser_->set_allow_harmony_scoping(allow_harmony_scoping());
3770 reusable_preparser_->set_allow_modules(allow_modules()); 3759 reusable_preparser_->set_allow_modules(allow_modules());
3771 reusable_preparser_->set_allow_natives_syntax(allow_natives_syntax()); 3760 reusable_preparser_->set_allow_natives_syntax(allow_natives_syntax());
3772 reusable_preparser_->set_allow_lazy(true); 3761 reusable_preparser_->set_allow_lazy(true);
3773 reusable_preparser_->set_allow_generators(allow_generators()); 3762 reusable_preparser_->set_allow_generators(allow_generators());
3774 reusable_preparser_->set_allow_arrow_functions(allow_arrow_functions()); 3763 reusable_preparser_->set_allow_arrow_functions(allow_arrow_functions());
3775 reusable_preparser_->set_allow_harmony_numeric_literals( 3764 reusable_preparser_->set_allow_harmony_numeric_literals(
3776 allow_harmony_numeric_literals()); 3765 allow_harmony_numeric_literals());
3777 reusable_preparser_->set_allow_classes(allow_classes()); 3766 reusable_preparser_->set_allow_classes(allow_classes());
3767 reusable_preparser_->set_allow_harmony_object_literals(
3768 allow_harmony_object_literals());
3778 } 3769 }
3779 PreParser::PreParseResult result = 3770 PreParser::PreParseResult result =
3780 reusable_preparser_->PreParseLazyFunction(strict_mode(), 3771 reusable_preparser_->PreParseLazyFunction(strict_mode(),
3781 is_generator(), 3772 is_generator(),
3782 logger); 3773 logger);
3783 if (pre_parse_timer_ != NULL) { 3774 if (pre_parse_timer_ != NULL) {
3784 pre_parse_timer_->Stop(); 3775 pre_parse_timer_->Stop();
3785 } 3776 }
3786 return result; 3777 return result;
3787 } 3778 }
(...skipping 1062 matching lines...) Expand 10 before | Expand all | Expand 10 after
4850 DCHECK(ast_value_factory_->IsInternalized()); 4841 DCHECK(ast_value_factory_->IsInternalized());
4851 // info takes ownership of ast_value_factory_. 4842 // info takes ownership of ast_value_factory_.
4852 if (info()->ast_value_factory() == NULL) { 4843 if (info()->ast_value_factory() == NULL) {
4853 info()->SetAstValueFactory(ast_value_factory_); 4844 info()->SetAstValueFactory(ast_value_factory_);
4854 } 4845 }
4855 ast_value_factory_ = NULL; 4846 ast_value_factory_ = NULL;
4856 return (result != NULL); 4847 return (result != NULL);
4857 } 4848 }
4858 4849
4859 } } // namespace v8::internal 4850 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698