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

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: Created 6 years, 4 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
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) 737 Parser::Parser(CompilationInfo* info)
743 : ParserBase<ParserTraits>( 738 : ParserBase<ParserTraits>(
744 &scanner_, info->isolate()->stack_guard()->real_climit(), 739 &scanner_, info->isolate()->stack_guard()->real_climit(),
745 info->extension(), NULL, info->zone(), info->ast_node_id_gen(), this), 740 info->extension(), NULL, info->zone(), info->ast_node_id_gen(), this),
746 isolate_(info->isolate()), 741 isolate_(info->isolate()),
747 script_(info->script()), 742 script_(info->script()),
748 scanner_(isolate_->unicode_cache()), 743 scanner_(isolate_->unicode_cache()),
749 reusable_preparser_(NULL), 744 reusable_preparser_(NULL),
750 original_scope_(NULL), 745 original_scope_(NULL),
751 target_stack_(NULL), 746 target_stack_(NULL),
752 cached_parse_data_(NULL), 747 cached_parse_data_(NULL),
753 ast_value_factory_(NULL), 748 ast_value_factory_(NULL),
754 info_(info), 749 info_(info),
755 has_pending_error_(false), 750 has_pending_error_(false),
756 pending_error_message_(NULL), 751 pending_error_message_(NULL),
757 pending_error_arg_(NULL), 752 pending_error_arg_(NULL),
758 pending_error_char_arg_(NULL) { 753 pending_error_char_arg_(NULL) {
759 DCHECK(!script_.is_null()); 754 DCHECK(!script_.is_null());
760 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping); 755 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping);
761 set_allow_modules(!info->is_native() && FLAG_harmony_modules); 756 set_allow_modules(!info->is_native() && FLAG_harmony_modules);
762 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native()); 757 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native());
763 set_allow_lazy(false); // Must be explicitly enabled. 758 set_allow_lazy(false); // Must be explicitly enabled.
764 set_allow_generators(FLAG_harmony_generators); 759 set_allow_generators(FLAG_harmony_generators);
765 set_allow_arrow_functions(FLAG_harmony_arrow_functions); 760 set_allow_arrow_functions(FLAG_harmony_arrow_functions);
766 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals); 761 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals);
767 set_allow_classes(FLAG_harmony_classes); 762 set_allow_classes(FLAG_harmony_classes);
763 set_allow_harmony_object_literals(FLAG_harmony_object_literals);
768 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; 764 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
769 ++feature) { 765 ++feature) {
770 use_counts_[feature] = 0; 766 use_counts_[feature] = 0;
771 } 767 }
772 } 768 }
773 769
774 770
775 FunctionLiteral* Parser::ParseProgram() { 771 FunctionLiteral* Parser::ParseProgram() {
776 // TODO(bmeurer): We temporarily need to pass allow_nesting = true here, 772 // TODO(bmeurer): We temporarily need to pass allow_nesting = true here,
777 // see comment for HistogramTimerScope class. 773 // see comment for HistogramTimerScope class.
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
898 894
899 ast_value_factory_->Internalize(isolate()); 895 ast_value_factory_->Internalize(isolate());
900 if (ok) { 896 if (ok) {
901 result = factory()->NewFunctionLiteral( 897 result = factory()->NewFunctionLiteral(
902 ast_value_factory_->empty_string(), ast_value_factory_, scope_, body, 898 ast_value_factory_->empty_string(), ast_value_factory_, scope_, body,
903 function_state.materialized_literal_count(), 899 function_state.materialized_literal_count(),
904 function_state.expected_property_count(), 900 function_state.expected_property_count(),
905 function_state.handler_count(), 0, 901 function_state.handler_count(), 0,
906 FunctionLiteral::kNoDuplicateParameters, 902 FunctionLiteral::kNoDuplicateParameters,
907 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kGlobalOrEval, 903 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kGlobalOrEval,
908 FunctionLiteral::kNotParenthesized, FunctionLiteral::kNormalFunction, 904 FunctionLiteral::kNotParenthesized, FunctionKind::kNormalFunction, 0);
909 0);
910 result->set_ast_properties(factory()->visitor()->ast_properties()); 905 result->set_ast_properties(factory()->visitor()->ast_properties());
911 result->set_dont_optimize_reason( 906 result->set_dont_optimize_reason(
912 factory()->visitor()->dont_optimize_reason()); 907 factory()->visitor()->dont_optimize_reason());
913 } else if (stack_overflow()) { 908 } else if (stack_overflow()) {
914 isolate()->StackOverflow(); 909 isolate()->StackOverflow();
915 } else { 910 } else {
916 ThrowPendingError(); 911 ThrowPendingError();
917 } 912 }
918 } 913 }
919 914
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
988 FunctionState function_state(&function_state_, &scope_, scope, zone(), 983 FunctionState function_state(&function_state_, &scope_, scope, zone(),
989 ast_value_factory_, info()->ast_node_id_gen()); 984 ast_value_factory_, info()->ast_node_id_gen());
990 DCHECK(scope->strict_mode() == SLOPPY || info()->strict_mode() == STRICT); 985 DCHECK(scope->strict_mode() == SLOPPY || info()->strict_mode() == STRICT);
991 DCHECK(info()->strict_mode() == shared_info->strict_mode()); 986 DCHECK(info()->strict_mode() == shared_info->strict_mode());
992 scope->SetStrictMode(shared_info->strict_mode()); 987 scope->SetStrictMode(shared_info->strict_mode());
993 FunctionLiteral::FunctionType function_type = shared_info->is_expression() 988 FunctionLiteral::FunctionType function_type = shared_info->is_expression()
994 ? (shared_info->is_anonymous() 989 ? (shared_info->is_anonymous()
995 ? FunctionLiteral::ANONYMOUS_EXPRESSION 990 ? FunctionLiteral::ANONYMOUS_EXPRESSION
996 : FunctionLiteral::NAMED_EXPRESSION) 991 : FunctionLiteral::NAMED_EXPRESSION)
997 : FunctionLiteral::DECLARATION; 992 : FunctionLiteral::DECLARATION;
998 bool is_generator = shared_info->is_generator();
999 bool ok = true; 993 bool ok = true;
1000 994
1001 if (shared_info->is_arrow()) { 995 if (shared_info->is_arrow()) {
1002 DCHECK(!is_generator);
1003 Expression* expression = ParseExpression(false, &ok); 996 Expression* expression = ParseExpression(false, &ok);
1004 DCHECK(expression->IsFunctionLiteral()); 997 DCHECK(expression->IsFunctionLiteral());
1005 result = expression->AsFunctionLiteral(); 998 result = expression->AsFunctionLiteral();
1006 } else { 999 } else {
1007 result = ParseFunctionLiteral(raw_name, Scanner::Location::invalid(), 1000 result = ParseFunctionLiteral(raw_name, Scanner::Location::invalid(),
1008 false, // Strict mode name already checked. 1001 false, // Strict mode name already checked.
1009 is_generator, RelocInfo::kNoPosition, 1002 shared_info->kind(), RelocInfo::kNoPosition,
1010 function_type, 1003 function_type,
1011 FunctionLiteral::NORMAL_ARITY, &ok); 1004 FunctionLiteral::NORMAL_ARITY, &ok);
1012 } 1005 }
1013 // Make sure the results agree. 1006 // Make sure the results agree.
1014 DCHECK(ok == (result != NULL)); 1007 DCHECK(ok == (result != NULL));
1015 } 1008 }
1016 1009
1017 // Make sure the target stack is empty. 1010 // Make sure the target stack is empty.
1018 DCHECK(target_stack_ == NULL); 1011 DCHECK(target_stack_ == NULL);
1019 1012
(...skipping 870 matching lines...) Expand 10 before | Expand all | Expand 10 after
1890 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' 1883 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}'
1891 // GeneratorDeclaration :: 1884 // GeneratorDeclaration ::
1892 // 'function' '*' Identifier '(' FormalParameterListopt ')' 1885 // 'function' '*' Identifier '(' FormalParameterListopt ')'
1893 // '{' FunctionBody '}' 1886 // '{' FunctionBody '}'
1894 Expect(Token::FUNCTION, CHECK_OK); 1887 Expect(Token::FUNCTION, CHECK_OK);
1895 int pos = position(); 1888 int pos = position();
1896 bool is_generator = allow_generators() && Check(Token::MUL); 1889 bool is_generator = allow_generators() && Check(Token::MUL);
1897 bool is_strict_reserved = false; 1890 bool is_strict_reserved = false;
1898 const AstRawString* name = ParseIdentifierOrStrictReservedWord( 1891 const AstRawString* name = ParseIdentifierOrStrictReservedWord(
1899 &is_strict_reserved, CHECK_OK); 1892 &is_strict_reserved, CHECK_OK);
1900 FunctionLiteral* fun = ParseFunctionLiteral(name, 1893 FunctionLiteral* fun =
1901 scanner()->location(), 1894 ParseFunctionLiteral(name, scanner()->location(), is_strict_reserved,
1902 is_strict_reserved, 1895 is_generator ? FunctionKind::kGeneratorFunction
1903 is_generator, 1896 : FunctionKind::kNormalFunction,
1904 pos, 1897 pos, FunctionLiteral::DECLARATION,
1905 FunctionLiteral::DECLARATION, 1898 FunctionLiteral::NORMAL_ARITY, CHECK_OK);
1906 FunctionLiteral::NORMAL_ARITY,
1907 CHECK_OK);
1908 // Even if we're not at the top-level of the global or a function 1899 // Even if we're not at the top-level of the global or a function
1909 // scope, we treat it as such and introduce the function with its 1900 // scope, we treat it as such and introduce the function with its
1910 // initial value upon entering the corresponding scope. 1901 // initial value upon entering the corresponding scope.
1911 // In ES6, a function behaves as a lexical binding, except in the 1902 // In ES6, a function behaves as a lexical binding, except in the
1912 // global scope, or the initial scope of eval or another function. 1903 // global scope, or the initial scope of eval or another function.
1913 VariableMode mode = 1904 VariableMode mode =
1914 allow_harmony_scoping() && strict_mode() == STRICT && 1905 allow_harmony_scoping() && strict_mode() == STRICT &&
1915 !(scope_->is_global_scope() || scope_->is_eval_scope() || 1906 !(scope_->is_global_scope() || scope_->is_eval_scope() ||
1916 scope_->is_function_scope()) ? LET : VAR; 1907 scope_->is_function_scope()) ? LET : VAR;
1917 VariableProxy* proxy = NewUnresolved(name, mode, Interface::NewValue()); 1908 VariableProxy* proxy = NewUnresolved(name, mode, Interface::NewValue());
(...skipping 1443 matching lines...) Expand 10 before | Expand all | Expand 10 after
3361 Expression* expression, Scope* scope, Scanner::Location* dupe_loc, 3352 Expression* expression, Scope* scope, Scanner::Location* dupe_loc,
3362 bool* ok) { 3353 bool* ok) {
3363 int num_params = 0; 3354 int num_params = 0;
3364 *ok = CheckAndDeclareArrowParameter(this, expression, scope, &num_params, 3355 *ok = CheckAndDeclareArrowParameter(this, expression, scope, &num_params,
3365 dupe_loc); 3356 dupe_loc);
3366 return num_params; 3357 return num_params;
3367 } 3358 }
3368 3359
3369 3360
3370 FunctionLiteral* Parser::ParseFunctionLiteral( 3361 FunctionLiteral* Parser::ParseFunctionLiteral(
3371 const AstRawString* function_name, 3362 const AstRawString* function_name, Scanner::Location function_name_location,
3372 Scanner::Location function_name_location, 3363 bool name_is_strict_reserved, FunctionKind kind, int function_token_pos,
3373 bool name_is_strict_reserved,
3374 bool is_generator,
3375 int function_token_pos,
3376 FunctionLiteral::FunctionType function_type, 3364 FunctionLiteral::FunctionType function_type,
3377 FunctionLiteral::ArityRestriction arity_restriction, 3365 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) {
3378 bool* ok) {
3379 // Function :: 3366 // Function ::
3380 // '(' FormalParameterList? ')' '{' FunctionBody '}' 3367 // '(' FormalParameterList? ')' '{' FunctionBody '}'
3381 // 3368 //
3382 // Getter :: 3369 // Getter ::
3383 // '(' ')' '{' FunctionBody '}' 3370 // '(' ')' '{' FunctionBody '}'
3384 // 3371 //
3385 // Setter :: 3372 // Setter ::
3386 // '(' PropertySetParameterList ')' '{' FunctionBody '}' 3373 // '(' PropertySetParameterList ')' '{' FunctionBody '}'
3387 3374
3388 int pos = function_token_pos == RelocInfo::kNoPosition 3375 int pos = function_token_pos == RelocInfo::kNoPosition
3389 ? peek_position() : function_token_pos; 3376 ? peek_position() : function_token_pos;
3390 3377
3378 bool is_generator = IsGeneratorFunction(kind);
3379
3391 // Anonymous functions were passed either the empty symbol or a null 3380 // Anonymous functions were passed either the empty symbol or a null
3392 // handle as the function name. Remember if we were passed a non-empty 3381 // handle as the function name. Remember if we were passed a non-empty
3393 // handle to decide whether to invoke function name inference. 3382 // handle to decide whether to invoke function name inference.
3394 bool should_infer_name = function_name == NULL; 3383 bool should_infer_name = function_name == NULL;
3395 3384
3396 // We want a non-null handle as the function name. 3385 // We want a non-null handle as the function name.
3397 if (should_infer_name) { 3386 if (should_infer_name) {
3398 function_name = ast_value_factory_->empty_string(); 3387 function_name = ast_value_factory_->empty_string();
3399 } 3388 }
3400 3389
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
3589 &expected_property_count, CHECK_OK); 3578 &expected_property_count, CHECK_OK);
3590 } else { 3579 } else {
3591 body = ParseEagerFunctionBody(function_name, pos, fvar, fvar_init_op, 3580 body = ParseEagerFunctionBody(function_name, pos, fvar, fvar_init_op,
3592 is_generator, CHECK_OK); 3581 is_generator, CHECK_OK);
3593 materialized_literal_count = function_state.materialized_literal_count(); 3582 materialized_literal_count = function_state.materialized_literal_count();
3594 expected_property_count = function_state.expected_property_count(); 3583 expected_property_count = function_state.expected_property_count();
3595 handler_count = function_state.handler_count(); 3584 handler_count = function_state.handler_count();
3596 } 3585 }
3597 3586
3598 // Validate strict mode. 3587 // Validate strict mode.
3599 if (strict_mode() == STRICT) { 3588 // Concise methods use StrictFormalParameters.
3589 if (strict_mode() == STRICT || IsConciseMethod(kind)) {
3600 CheckStrictFunctionNameAndParameters(function_name, 3590 CheckStrictFunctionNameAndParameters(function_name,
3601 name_is_strict_reserved, 3591 name_is_strict_reserved,
3602 function_name_location, 3592 function_name_location,
3603 eval_args_error_log, 3593 eval_args_error_log,
3604 dupe_error_loc, 3594 dupe_error_loc,
3605 reserved_loc, 3595 reserved_loc,
3606 CHECK_OK); 3596 CHECK_OK);
3597 }
3598 if (strict_mode() == STRICT) {
3607 CheckOctalLiteral(scope->start_position(), 3599 CheckOctalLiteral(scope->start_position(),
3608 scope->end_position(), 3600 scope->end_position(),
3609 CHECK_OK); 3601 CHECK_OK);
3610 } 3602 }
3611 ast_properties = *factory()->visitor()->ast_properties(); 3603 ast_properties = *factory()->visitor()->ast_properties();
3612 dont_optimize_reason = factory()->visitor()->dont_optimize_reason(); 3604 dont_optimize_reason = factory()->visitor()->dont_optimize_reason();
3613 3605
3614 if (allow_harmony_scoping() && strict_mode() == STRICT) { 3606 if (allow_harmony_scoping() && strict_mode() == STRICT) {
3615 CheckConflictingVarDeclarations(scope, CHECK_OK); 3607 CheckConflictingVarDeclarations(scope, CHECK_OK);
3616 } 3608 }
3617 } 3609 }
3618 3610
3619 FunctionLiteral::KindFlag kind = is_generator
3620 ? FunctionLiteral::kGeneratorFunction
3621 : FunctionLiteral::kNormalFunction;
3622 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( 3611 FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
3623 function_name, ast_value_factory_, scope, body, 3612 function_name, ast_value_factory_, scope, body,
3624 materialized_literal_count, expected_property_count, handler_count, 3613 materialized_literal_count, expected_property_count, handler_count,
3625 num_parameters, duplicate_parameters, function_type, 3614 num_parameters, duplicate_parameters, function_type,
3626 FunctionLiteral::kIsFunction, parenthesized, kind, pos); 3615 FunctionLiteral::kIsFunction, parenthesized, kind, pos);
3627 function_literal->set_function_token_position(function_token_pos); 3616 function_literal->set_function_token_position(function_token_pos);
3628 function_literal->set_ast_properties(&ast_properties); 3617 function_literal->set_ast_properties(&ast_properties);
3629 function_literal->set_dont_optimize_reason(dont_optimize_reason); 3618 function_literal->set_dont_optimize_reason(dont_optimize_reason);
3630 3619
3631 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); 3620 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal);
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
3770 reusable_preparser_ = new PreParser(&scanner_, NULL, stack_limit); 3759 reusable_preparser_ = new PreParser(&scanner_, NULL, stack_limit);
3771 reusable_preparser_->set_allow_harmony_scoping(allow_harmony_scoping()); 3760 reusable_preparser_->set_allow_harmony_scoping(allow_harmony_scoping());
3772 reusable_preparser_->set_allow_modules(allow_modules()); 3761 reusable_preparser_->set_allow_modules(allow_modules());
3773 reusable_preparser_->set_allow_natives_syntax(allow_natives_syntax()); 3762 reusable_preparser_->set_allow_natives_syntax(allow_natives_syntax());
3774 reusable_preparser_->set_allow_lazy(true); 3763 reusable_preparser_->set_allow_lazy(true);
3775 reusable_preparser_->set_allow_generators(allow_generators()); 3764 reusable_preparser_->set_allow_generators(allow_generators());
3776 reusable_preparser_->set_allow_arrow_functions(allow_arrow_functions()); 3765 reusable_preparser_->set_allow_arrow_functions(allow_arrow_functions());
3777 reusable_preparser_->set_allow_harmony_numeric_literals( 3766 reusable_preparser_->set_allow_harmony_numeric_literals(
3778 allow_harmony_numeric_literals()); 3767 allow_harmony_numeric_literals());
3779 reusable_preparser_->set_allow_classes(allow_classes()); 3768 reusable_preparser_->set_allow_classes(allow_classes());
3769 reusable_preparser_->set_allow_harmony_object_literals(
3770 allow_harmony_object_literals());
3780 } 3771 }
3781 PreParser::PreParseResult result = 3772 PreParser::PreParseResult result =
3782 reusable_preparser_->PreParseLazyFunction(strict_mode(), 3773 reusable_preparser_->PreParseLazyFunction(strict_mode(),
3783 is_generator(), 3774 is_generator(),
3784 logger); 3775 logger);
3785 return result; 3776 return result;
3786 } 3777 }
3787 3778
3788 3779
3789 Expression* Parser::ParseV8Intrinsic(bool* ok) { 3780 Expression* Parser::ParseV8Intrinsic(bool* ok) {
(...skipping 1047 matching lines...) Expand 10 before | Expand all | Expand 10 after
4837 info()->SetAstValueFactory(ast_value_factory_); 4828 info()->SetAstValueFactory(ast_value_factory_);
4838 } 4829 }
4839 ast_value_factory_ = NULL; 4830 ast_value_factory_ = NULL;
4840 4831
4841 InternalizeUseCounts(); 4832 InternalizeUseCounts();
4842 4833
4843 return (result != NULL); 4834 return (result != NULL);
4844 } 4835 }
4845 4836
4846 } } // namespace v8::internal 4837 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698