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

Side by Side Diff: src/parsing/parser.cc

Issue 1712833002: Don't reflect ES2015 Function name inference in Function.prototype.toString (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 10 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
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/parsing/parser.h" 5 #include "src/parsing/parser.h"
6 6
7 #include "src/api.h" 7 #include "src/api.h"
8 #include "src/ast/ast.h" 8 #include "src/ast/ast.h"
9 #include "src/ast/ast-expression-rewriter.h" 9 #include "src/ast/ast-expression-rewriter.h"
10 #include "src/ast/ast-expression-visitor.h" 10 #include "src/ast/ast-expression-visitor.h"
(...skipping 710 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 721
722 Expression* ParserTraits::ParseV8Intrinsic(bool* ok) { 722 Expression* ParserTraits::ParseV8Intrinsic(bool* ok) {
723 return parser_->ParseV8Intrinsic(ok); 723 return parser_->ParseV8Intrinsic(ok);
724 } 724 }
725 725
726 726
727 FunctionLiteral* ParserTraits::ParseFunctionLiteral( 727 FunctionLiteral* ParserTraits::ParseFunctionLiteral(
728 const AstRawString* name, Scanner::Location function_name_location, 728 const AstRawString* name, Scanner::Location function_name_location,
729 FunctionNameValidity function_name_validity, FunctionKind kind, 729 FunctionNameValidity function_name_validity, FunctionKind kind,
730 int function_token_position, FunctionLiteral::FunctionType type, 730 int function_token_position, FunctionLiteral::FunctionType type,
731 FunctionLiteral::ArityRestriction arity_restriction,
732 LanguageMode language_mode, bool* ok) { 731 LanguageMode language_mode, bool* ok) {
733 return parser_->ParseFunctionLiteral( 732 return parser_->ParseFunctionLiteral(
734 name, function_name_location, function_name_validity, kind, 733 name, function_name_location, function_name_validity, kind,
735 function_token_position, type, arity_restriction, language_mode, ok); 734 function_token_position, type, language_mode, ok);
736 } 735 }
737 736
738 737
739 ClassLiteral* ParserTraits::ParseClassLiteral( 738 ClassLiteral* ParserTraits::ParseClassLiteral(
740 const AstRawString* name, Scanner::Location class_name_location, 739 const AstRawString* name, Scanner::Location class_name_location,
741 bool name_is_strict_reserved, int pos, bool* ok) { 740 bool name_is_strict_reserved, int pos, bool* ok) {
742 return parser_->ParseClassLiteral(name, class_name_location, 741 return parser_->ParseClassLiteral(name, class_name_location,
743 name_is_strict_reserved, pos, ok); 742 name_is_strict_reserved, pos, ok);
744 } 743 }
745 744
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
937 !body->at(0)->IsExpressionStatement() || 936 !body->at(0)->IsExpressionStatement() ||
938 !body->at(0)->AsExpressionStatement()-> 937 !body->at(0)->AsExpressionStatement()->
939 expression()->IsFunctionLiteral()) { 938 expression()->IsFunctionLiteral()) {
940 ReportMessage(MessageTemplate::kSingleFunctionLiteral); 939 ReportMessage(MessageTemplate::kSingleFunctionLiteral);
941 ok = false; 940 ok = false;
942 } 941 }
943 } 942 }
944 943
945 if (ok) { 944 if (ok) {
946 ParserTraits::RewriteDestructuringAssignments(); 945 ParserTraits::RewriteDestructuringAssignments();
947 result = factory()->NewFunctionLiteral( 946 result = factory()->NewGlobalOrEvalFunctionLiteral(
948 ast_value_factory()->empty_string(), scope_, body, 947 scope_, body, function_state.materialized_literal_count(),
949 function_state.materialized_literal_count(), 948 function_state.expected_property_count());
950 function_state.expected_property_count(), 0,
951 FunctionLiteral::kNoDuplicateParameters,
952 FunctionLiteral::kGlobalOrEval, FunctionLiteral::kShouldLazyCompile,
953 FunctionKind::kNormalFunction, 0);
954 } 949 }
955 } 950 }
956 951
957 // Make sure the target stack is empty. 952 // Make sure the target stack is empty.
958 DCHECK(target_stack_ == NULL); 953 DCHECK(target_stack_ == NULL);
959 954
960 return result; 955 return result;
961 } 956 }
962 957
963 958
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1030 scope = Scope::DeserializeScopeChain(isolate, zone(), 1025 scope = Scope::DeserializeScopeChain(isolate, zone(),
1031 info->closure()->context(), scope); 1026 info->closure()->context(), scope);
1032 } 1027 }
1033 original_scope_ = scope; 1028 original_scope_ = scope;
1034 AstNodeFactory function_factory(ast_value_factory()); 1029 AstNodeFactory function_factory(ast_value_factory());
1035 FunctionState function_state(&function_state_, &scope_, scope, 1030 FunctionState function_state(&function_state_, &scope_, scope,
1036 shared_info->kind(), &function_factory); 1031 shared_info->kind(), &function_factory);
1037 DCHECK(is_sloppy(scope->language_mode()) || 1032 DCHECK(is_sloppy(scope->language_mode()) ||
1038 is_strict(info->language_mode())); 1033 is_strict(info->language_mode()));
1039 DCHECK(info->language_mode() == shared_info->language_mode()); 1034 DCHECK(info->language_mode() == shared_info->language_mode());
1035 // TODO(adamk): kAnonymousExpression isn't necessarily correct, but it's
1036 // a safe default.
Dan Ehrenberg 2016/02/19 00:10:41 Could this cause the getter/setter/method-ness to
adamk 2016/02/19 01:09:15 So this is indeed the only really ugly bit of the
adamk 2016/02/19 02:02:19 Since I had the bit free anyway, split FunctionKin
1040 FunctionLiteral::FunctionType function_type = 1037 FunctionLiteral::FunctionType function_type =
1041 shared_info->is_expression() 1038 shared_info->is_declaration()
1042 ? (shared_info->is_anonymous() 1039 ? FunctionLiteral::kDeclaration
1043 ? FunctionLiteral::kAnonymousExpression 1040 : (shared_info->is_named_expression()
1044 : FunctionLiteral::kNamedExpression) 1041 ? FunctionLiteral::kNamedExpression
1045 : FunctionLiteral::kDeclaration; 1042 : FunctionLiteral::kAnonymousExpression);
1046 bool ok = true; 1043 bool ok = true;
1047 1044
1048 if (shared_info->is_arrow()) { 1045 if (shared_info->is_arrow()) {
1049 // TODO(adamk): We should construct this scope from the ScopeInfo. 1046 // TODO(adamk): We should construct this scope from the ScopeInfo.
1050 Scope* scope = 1047 Scope* scope =
1051 NewScope(scope_, FUNCTION_SCOPE, FunctionKind::kArrowFunction); 1048 NewScope(scope_, FUNCTION_SCOPE, FunctionKind::kArrowFunction);
1052 1049
1053 // These two bits only need to be explicitly set because we're 1050 // These two bits only need to be explicitly set because we're
1054 // not passing the ScopeInfo to the Scope constructor. 1051 // not passing the ScopeInfo to the Scope constructor.
1055 // TODO(adamk): Remove these calls once the above NewScope call 1052 // TODO(adamk): Remove these calls once the above NewScope call
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1104 ok = false; 1101 ok = false;
1105 } 1102 }
1106 } 1103 }
1107 } 1104 }
1108 } else if (shared_info->is_default_constructor()) { 1105 } else if (shared_info->is_default_constructor()) {
1109 result = DefaultConstructor( 1106 result = DefaultConstructor(
1110 raw_name, IsSubclassConstructor(shared_info->kind()), scope, 1107 raw_name, IsSubclassConstructor(shared_info->kind()), scope,
1111 shared_info->start_position(), shared_info->end_position(), 1108 shared_info->start_position(), shared_info->end_position(),
1112 shared_info->language_mode()); 1109 shared_info->language_mode());
1113 } else { 1110 } else {
1114 result = ParseFunctionLiteral( 1111 result = ParseFunctionLiteral(raw_name, Scanner::Location::invalid(),
1115 raw_name, Scanner::Location::invalid(), kSkipFunctionNameCheck, 1112 kSkipFunctionNameCheck, shared_info->kind(),
1116 shared_info->kind(), RelocInfo::kNoPosition, function_type, 1113 RelocInfo::kNoPosition, function_type,
1117 FunctionLiteral::kNormalArity, shared_info->language_mode(), &ok); 1114 shared_info->language_mode(), &ok);
1118 } 1115 }
1119 // Make sure the results agree. 1116 // Make sure the results agree.
1120 DCHECK(ok == (result != NULL)); 1117 DCHECK(ok == (result != NULL));
1121 } 1118 }
1122 1119
1123 // Make sure the target stack is empty. 1120 // Make sure the target stack is empty.
1124 DCHECK(target_stack_ == NULL); 1121 DCHECK(target_stack_ == NULL);
1125 1122
1126 if (result != NULL) { 1123 if (result != NULL) {
1127 Handle<String> inferred_name(shared_info->inferred_name()); 1124 Handle<String> inferred_name(shared_info->inferred_name());
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after
1571 // FunctionDeclaration[+Default] :: 1568 // FunctionDeclaration[+Default] ::
1572 // 'function' '(' FormalParameters ')' '{' FunctionBody '}' 1569 // 'function' '(' FormalParameters ')' '{' FunctionBody '}'
1573 // 1570 //
1574 // GeneratorDeclaration[+Default] :: 1571 // GeneratorDeclaration[+Default] ::
1575 // 'function' '*' '(' FormalParameters ')' '{' FunctionBody '}' 1572 // 'function' '*' '(' FormalParameters ')' '{' FunctionBody '}'
1576 default_export = ParseFunctionLiteral( 1573 default_export = ParseFunctionLiteral(
1577 default_string, Scanner::Location::invalid(), 1574 default_string, Scanner::Location::invalid(),
1578 kSkipFunctionNameCheck, 1575 kSkipFunctionNameCheck,
1579 is_generator ? FunctionKind::kGeneratorFunction 1576 is_generator ? FunctionKind::kGeneratorFunction
1580 : FunctionKind::kNormalFunction, 1577 : FunctionKind::kNormalFunction,
1581 pos, FunctionLiteral::kDeclaration, FunctionLiteral::kNormalArity, 1578 pos, FunctionLiteral::kDeclaration, language_mode(), CHECK_OK);
1582 language_mode(), CHECK_OK);
1583 result = factory()->NewEmptyStatement(RelocInfo::kNoPosition); 1579 result = factory()->NewEmptyStatement(RelocInfo::kNoPosition);
1584 } else { 1580 } else {
1585 result = ParseFunctionDeclaration(pos, is_generator, &names, CHECK_OK); 1581 result = ParseFunctionDeclaration(pos, is_generator, &names, CHECK_OK);
1586 } 1582 }
1587 break; 1583 break;
1588 } 1584 }
1589 1585
1590 case Token::CLASS: 1586 case Token::CLASS:
1591 Consume(Token::CLASS); 1587 Consume(Token::CLASS);
1592 if (peek() == Token::EXTENDS || peek() == Token::LBRACE) { 1588 if (peek() == Token::EXTENDS || peek() == Token::LBRACE) {
(...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after
2143 &is_strict_reserved, CHECK_OK); 2139 &is_strict_reserved, CHECK_OK);
2144 2140
2145 FuncNameInferrer::State fni_state(fni_); 2141 FuncNameInferrer::State fni_state(fni_);
2146 if (fni_ != NULL) fni_->PushEnclosingName(name); 2142 if (fni_ != NULL) fni_->PushEnclosingName(name);
2147 FunctionLiteral* fun = ParseFunctionLiteral( 2143 FunctionLiteral* fun = ParseFunctionLiteral(
2148 name, scanner()->location(), 2144 name, scanner()->location(),
2149 is_strict_reserved ? kFunctionNameIsStrictReserved 2145 is_strict_reserved ? kFunctionNameIsStrictReserved
2150 : kFunctionNameValidityUnknown, 2146 : kFunctionNameValidityUnknown,
2151 is_generator ? FunctionKind::kGeneratorFunction 2147 is_generator ? FunctionKind::kGeneratorFunction
2152 : FunctionKind::kNormalFunction, 2148 : FunctionKind::kNormalFunction,
2153 pos, FunctionLiteral::kDeclaration, FunctionLiteral::kNormalArity, 2149 pos, FunctionLiteral::kDeclaration, language_mode(), CHECK_OK);
2154 language_mode(), CHECK_OK);
2155 2150
2156 // Even if we're not at the top-level of the global or a function 2151 // Even if we're not at the top-level of the global or a function
2157 // scope, we treat it as such and introduce the function with its 2152 // scope, we treat it as such and introduce the function with its
2158 // initial value upon entering the corresponding scope. 2153 // initial value upon entering the corresponding scope.
2159 // In ES6, a function behaves as a lexical binding, except in 2154 // In ES6, a function behaves as a lexical binding, except in
2160 // a script scope, or the initial scope of eval or another function. 2155 // a script scope, or the initial scope of eval or another function.
2161 VariableMode mode = 2156 VariableMode mode =
2162 is_strong(language_mode()) 2157 is_strong(language_mode())
2163 ? CONST 2158 ? CONST
2164 : (is_strict(language_mode()) || allow_harmony_sloppy_function()) && 2159 : (is_strict(language_mode()) || allow_harmony_sloppy_function()) &&
(...skipping 1974 matching lines...) Expand 10 before | Expand all | Expand 10 after
4139 DCHECK(reindexer.count() <= 4134 DCHECK(reindexer.count() <=
4140 parser_->function_state_->materialized_literal_count()); 4135 parser_->function_state_->materialized_literal_count());
4141 } 4136 }
4142 } 4137 }
4143 4138
4144 4139
4145 FunctionLiteral* Parser::ParseFunctionLiteral( 4140 FunctionLiteral* Parser::ParseFunctionLiteral(
4146 const AstRawString* function_name, Scanner::Location function_name_location, 4141 const AstRawString* function_name, Scanner::Location function_name_location,
4147 FunctionNameValidity function_name_validity, FunctionKind kind, 4142 FunctionNameValidity function_name_validity, FunctionKind kind,
4148 int function_token_pos, FunctionLiteral::FunctionType function_type, 4143 int function_token_pos, FunctionLiteral::FunctionType function_type,
4149 FunctionLiteral::ArityRestriction arity_restriction,
4150 LanguageMode language_mode, bool* ok) { 4144 LanguageMode language_mode, bool* ok) {
4151 // Function :: 4145 // Function ::
4152 // '(' FormalParameterList? ')' '{' FunctionBody '}' 4146 // '(' FormalParameterList? ')' '{' FunctionBody '}'
4153 // 4147 //
4154 // Getter :: 4148 // Getter ::
4155 // '(' ')' '{' FunctionBody '}' 4149 // '(' ')' '{' FunctionBody '}'
4156 // 4150 //
4157 // Setter :: 4151 // Setter ::
4158 // '(' PropertySetParameterList ')' '{' FunctionBody '}' 4152 // '(' PropertySetParameterList ')' '{' FunctionBody '}'
4159 4153
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
4242 4236
4243 Expect(Token::LPAREN, CHECK_OK); 4237 Expect(Token::LPAREN, CHECK_OK);
4244 int start_position = scanner()->location().beg_pos; 4238 int start_position = scanner()->location().beg_pos;
4245 scope_->set_start_position(start_position); 4239 scope_->set_start_position(start_position);
4246 ParserFormalParameters formals(scope); 4240 ParserFormalParameters formals(scope);
4247 ParseFormalParameterList(&formals, &formals_classifier, CHECK_OK); 4241 ParseFormalParameterList(&formals, &formals_classifier, CHECK_OK);
4248 arity = formals.Arity(); 4242 arity = formals.Arity();
4249 Expect(Token::RPAREN, CHECK_OK); 4243 Expect(Token::RPAREN, CHECK_OK);
4250 int formals_end_position = scanner()->location().end_pos; 4244 int formals_end_position = scanner()->location().end_pos;
4251 4245
4252 CheckArityRestrictions(arity, arity_restriction, 4246 CheckArityRestrictions(arity, function_type, formals.has_rest,
4253 formals.has_rest, start_position, 4247 start_position, formals_end_position, CHECK_OK);
4254 formals_end_position, CHECK_OK);
4255 Expect(Token::LBRACE, CHECK_OK); 4248 Expect(Token::LBRACE, CHECK_OK);
4256 4249
4257 // Don't include the rest parameter into the function's formal parameter 4250 // Don't include the rest parameter into the function's formal parameter
4258 // count (esp. the SharedFunctionInfo::internal_formal_parameter_count, 4251 // count (esp. the SharedFunctionInfo::internal_formal_parameter_count,
4259 // which says whether we need to create an arguments adaptor frame). 4252 // which says whether we need to create an arguments adaptor frame).
4260 if (formals.has_rest) arity--; 4253 if (formals.has_rest) arity--;
4261 4254
4262 // Determine if the function can be parsed lazily. Lazy parsing is different 4255 // Determine if the function can be parsed lazily. Lazy parsing is different
4263 // from lazy compilation; we need to parse more eagerly than we compile. 4256 // from lazy compilation; we need to parse more eagerly than we compile.
4264 4257
(...skipping 1486 matching lines...) Expand 10 before | Expand all | Expand 10 after
5751 void ParserTraits::QueueDestructuringAssignmentForRewriting(Expression* expr) { 5744 void ParserTraits::QueueDestructuringAssignmentForRewriting(Expression* expr) {
5752 DCHECK(expr->IsRewritableAssignmentExpression()); 5745 DCHECK(expr->IsRewritableAssignmentExpression());
5753 parser_->function_state_->AddDestructuringAssignment( 5746 parser_->function_state_->AddDestructuringAssignment(
5754 Parser::DestructuringAssignment(expr, parser_->scope_)); 5747 Parser::DestructuringAssignment(expr, parser_->scope_));
5755 } 5748 }
5756 5749
5757 5750
5758 void ParserTraits::SetFunctionNameFromPropertyName( 5751 void ParserTraits::SetFunctionNameFromPropertyName(
5759 ObjectLiteralProperty* property, const AstRawString* name) { 5752 ObjectLiteralProperty* property, const AstRawString* name) {
5760 Expression* value = property->value(); 5753 Expression* value = property->value();
5761 if (!value->IsAnonymousFunctionDefinition()) return;
5762 5754
5763 // Computed name setting must happen at runtime. 5755 // Computed name setting must happen at runtime.
5764 if (property->is_computed_name()) return; 5756 if (property->is_computed_name()) return;
5765 5757
5758 // Getter and setter names are handled here because their names
5759 // change in ES2015, even though they are not anonymous.
5760 auto function = value->AsFunctionLiteral();
5761 if (function != nullptr) {
5762 bool is_getter = property->kind() == ObjectLiteralProperty::GETTER;
5763 bool is_setter = property->kind() == ObjectLiteralProperty::SETTER;
5764 if (is_getter || is_setter) {
5765 DCHECK_NOT_NULL(name);
5766 const AstRawString* prefix =
5767 is_getter ? parser_->ast_value_factory()->get_space_string()
5768 : parser_->ast_value_factory()->set_space_string();
5769 function->set_raw_name(
5770 parser_->ast_value_factory()->NewConsString(prefix, name));
5771 return;
5772 }
5773 }
5774
5775 if (!value->IsAnonymousFunctionDefinition()) return;
5766 DCHECK_NOT_NULL(name); 5776 DCHECK_NOT_NULL(name);
5767 5777
5768 // Ignore "__proto__" as a name when it's being used to set the [[Prototype]] 5778 // Ignore "__proto__" as a name when it's being used to set the [[Prototype]]
5769 // of an object literal. 5779 // of an object literal.
5770 if (property->kind() == ObjectLiteralProperty::PROTOTYPE) return; 5780 if (property->kind() == ObjectLiteralProperty::PROTOTYPE) return;
5771 5781
5772 auto function = value->AsFunctionLiteral();
5773 if (function != nullptr) { 5782 if (function != nullptr) {
5774 if (property->kind() == ObjectLiteralProperty::GETTER) { 5783 function->set_raw_name(name);
5775 function->set_raw_name(parser_->ast_value_factory()->NewConsString( 5784 DCHECK_EQ(ObjectLiteralProperty::COMPUTED, property->kind());
5776 parser_->ast_value_factory()->get_space_string(), name));
5777 } else if (property->kind() == ObjectLiteralProperty::SETTER) {
5778 function->set_raw_name(parser_->ast_value_factory()->NewConsString(
5779 parser_->ast_value_factory()->set_space_string(), name));
5780 } else {
5781 function->set_raw_name(name);
5782 DCHECK_EQ(ObjectLiteralProperty::COMPUTED, property->kind());
5783 }
5784 } else { 5785 } else {
5785 DCHECK(value->IsClassLiteral()); 5786 DCHECK(value->IsClassLiteral());
5786 DCHECK_EQ(ObjectLiteralProperty::COMPUTED, property->kind()); 5787 DCHECK_EQ(ObjectLiteralProperty::COMPUTED, property->kind());
5787 value->AsClassLiteral()->constructor()->set_raw_name(name); 5788 value->AsClassLiteral()->constructor()->set_raw_name(name);
5788 } 5789 }
5789 } 5790 }
5790 5791
5791 5792
5792 void ParserTraits::SetFunctionNameFromIdentifierRef(Expression* value, 5793 void ParserTraits::SetFunctionNameFromIdentifierRef(Expression* value,
5793 Expression* identifier) { 5794 Expression* identifier) {
(...skipping 933 matching lines...) Expand 10 before | Expand all | Expand 10 after
6727 new_body->statements()->Add(loop->body(), zone); 6728 new_body->statements()->Add(loop->body(), zone);
6728 new_body->statements()->Add(set_completion_normal, zone); 6729 new_body->statements()->Add(set_completion_normal, zone);
6729 6730
6730 loop->set_body(new_body); 6731 loop->set_body(new_body);
6731 return final_loop; 6732 return final_loop;
6732 } 6733 }
6733 6734
6734 6735
6735 } // namespace internal 6736 } // namespace internal
6736 } // namespace v8 6737 } // namespace v8
OLDNEW
« src/objects-printer.cc ('K') | « src/parsing/parser.h ('k') | src/parsing/parser-base.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698