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

Side by Side Diff: src/parser.cc

Issue 1053773006: [es6] implement default/optional parameters (WIP / comments) (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 8 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/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 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 Runtime::FunctionForId(Runtime::kInlineDefaultConstructorCallSuper), 366 Runtime::FunctionForId(Runtime::kInlineDefaultConstructorCallSuper),
367 args, pos); 367 args, pos);
368 body->Add(factory()->NewReturnStatement(call, pos), zone()); 368 body->Add(factory()->NewReturnStatement(call, pos), zone());
369 } 369 }
370 370
371 materialized_literal_count = function_state.materialized_literal_count(); 371 materialized_literal_count = function_state.materialized_literal_count();
372 expected_property_count = function_state.expected_property_count(); 372 expected_property_count = function_state.expected_property_count();
373 handler_count = function_state.handler_count(); 373 handler_count = function_state.handler_count();
374 } 374 }
375 375
376 ZoneList<Expression*>* default_values =
377 new (zone()) ZoneList<Expression*>(0, zone());
376 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( 378 FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
377 name, ast_value_factory(), function_scope, body, 379 name, ast_value_factory(), function_scope, body,
378 materialized_literal_count, expected_property_count, handler_count, 380 materialized_literal_count, expected_property_count, handler_count,
379 parameter_count, FunctionLiteral::kNoDuplicateParameters, 381 parameter_count, default_values, FunctionLiteral::kNoDuplicateParameters,
380 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kIsFunction, 382 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kIsFunction,
381 FunctionLiteral::kNotParenthesized, kind, pos); 383 FunctionLiteral::kNotParenthesized, kind, pos);
382 384
383 return function_literal; 385 return function_literal;
384 } 386 }
385 387
386 388
387 // ---------------------------------------------------------------------------- 389 // ----------------------------------------------------------------------------
388 // Target is a support class to facilitate manipulation of the 390 // Target is a support class to facilitate manipulation of the
389 // Parser's target_stack_ (the stack of potential 'break' and 391 // Parser's target_stack_ (the stack of potential 'break' and
(...skipping 639 matching lines...) Expand 10 before | Expand all | Expand 10 after
1029 if (body->length() != 1 || 1031 if (body->length() != 1 ||
1030 !body->at(0)->IsExpressionStatement() || 1032 !body->at(0)->IsExpressionStatement() ||
1031 !body->at(0)->AsExpressionStatement()-> 1033 !body->at(0)->AsExpressionStatement()->
1032 expression()->IsFunctionLiteral()) { 1034 expression()->IsFunctionLiteral()) {
1033 ReportMessage("single_function_literal"); 1035 ReportMessage("single_function_literal");
1034 ok = false; 1036 ok = false;
1035 } 1037 }
1036 } 1038 }
1037 1039
1038 if (ok) { 1040 if (ok) {
1041 ZoneList<Expression*>* default_values =
1042 new (zone()) ZoneList<Expression*>(0, zone());
1039 result = factory()->NewFunctionLiteral( 1043 result = factory()->NewFunctionLiteral(
1040 ast_value_factory()->empty_string(), ast_value_factory(), scope_, 1044 ast_value_factory()->empty_string(), ast_value_factory(), scope_,
1041 body, function_state.materialized_literal_count(), 1045 body, function_state.materialized_literal_count(),
1042 function_state.expected_property_count(), 1046 function_state.expected_property_count(),
1043 function_state.handler_count(), 0, 1047 function_state.handler_count(), 0, default_values,
1044 FunctionLiteral::kNoDuplicateParameters, 1048 FunctionLiteral::kNoDuplicateParameters,
1045 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kGlobalOrEval, 1049 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kGlobalOrEval,
1046 FunctionLiteral::kNotParenthesized, FunctionKind::kNormalFunction, 0); 1050 FunctionLiteral::kNotParenthesized, FunctionKind::kNormalFunction, 0);
1047 } 1051 }
1048 } 1052 }
1049 1053
1050 // Make sure the target stack is empty. 1054 // Make sure the target stack is empty.
1051 DCHECK(target_stack_ == NULL); 1055 DCHECK(target_stack_ == NULL);
1052 1056
1053 return result; 1057 return result;
(...skipping 2779 matching lines...) Expand 10 before | Expand all | Expand 10 after
3833 : NewScope(scope_, FUNCTION_SCOPE, kind); 3837 : NewScope(scope_, FUNCTION_SCOPE, kind);
3834 ZoneList<Statement*>* body = NULL; 3838 ZoneList<Statement*>* body = NULL;
3835 int materialized_literal_count = -1; 3839 int materialized_literal_count = -1;
3836 int expected_property_count = -1; 3840 int expected_property_count = -1;
3837 int handler_count = 0; 3841 int handler_count = 0;
3838 FunctionLiteral::ParameterFlag duplicate_parameters = 3842 FunctionLiteral::ParameterFlag duplicate_parameters =
3839 FunctionLiteral::kNoDuplicateParameters; 3843 FunctionLiteral::kNoDuplicateParameters;
3840 FunctionLiteral::IsParenthesizedFlag parenthesized = parenthesized_function_ 3844 FunctionLiteral::IsParenthesizedFlag parenthesized = parenthesized_function_
3841 ? FunctionLiteral::kIsParenthesized 3845 ? FunctionLiteral::kIsParenthesized
3842 : FunctionLiteral::kNotParenthesized; 3846 : FunctionLiteral::kNotParenthesized;
3847
3848 ZoneList<Expression*>* default_params = new (zone()) ZoneList<Expression*>(0, zone());
3849
3843 // Parse function body. 3850 // Parse function body.
3844 { 3851 {
3845 AstNodeFactory function_factory(ast_value_factory()); 3852 AstNodeFactory function_factory(ast_value_factory());
3846 FunctionState function_state(&function_state_, &scope_, scope, kind, 3853 FunctionState function_state(&function_state_, &scope_, scope, kind,
3847 &function_factory); 3854 &function_factory);
3848 scope_->SetScopeName(function_name); 3855 scope_->SetScopeName(function_name);
3849 3856
3850 if (is_generator) { 3857 if (is_generator) {
3851 // For generators, allocating variables in contexts is currently a win 3858 // For generators, allocating variables in contexts is currently a win
3852 // because it minimizes the work needed to suspend and resume an 3859 // because it minimizes the work needed to suspend and resume an
(...skipping 19 matching lines...) Expand all
3872 Scanner::Location eval_args_error_loc = Scanner::Location::invalid(); 3879 Scanner::Location eval_args_error_loc = Scanner::Location::invalid();
3873 Scanner::Location dupe_error_loc = Scanner::Location::invalid(); 3880 Scanner::Location dupe_error_loc = Scanner::Location::invalid();
3874 Scanner::Location reserved_error_loc = Scanner::Location::invalid(); 3881 Scanner::Location reserved_error_loc = Scanner::Location::invalid();
3875 3882
3876 bool is_rest = false; 3883 bool is_rest = false;
3877 bool done = arity_restriction == FunctionLiteral::GETTER_ARITY || 3884 bool done = arity_restriction == FunctionLiteral::GETTER_ARITY ||
3878 (peek() == Token::RPAREN && 3885 (peek() == Token::RPAREN &&
3879 arity_restriction != FunctionLiteral::SETTER_ARITY); 3886 arity_restriction != FunctionLiteral::SETTER_ARITY);
3880 while (!done) { 3887 while (!done) {
3881 bool is_strict_reserved = false; 3888 bool is_strict_reserved = false;
3882 is_rest = peek() == Token::ELLIPSIS && allow_harmony_rest_params(); 3889 int rest_pos;
3883 if (is_rest) { 3890
3891 ParameterKind kind = ParameterKind::Normal;
3892
3893 if (peek() == Token::ELLIPSIS && allow_harmony_rest_params()) {
3884 Consume(Token::ELLIPSIS); 3894 Consume(Token::ELLIPSIS);
3895 rest_pos = position();
3896 kind = ParameterKind::Rest;
3885 } 3897 }
3886 3898
3887 const AstRawString* param_name = 3899 const AstRawString* param_name =
3888 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK); 3900 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK);
3889 3901
3890 // Store locations for possible future error reports. 3902 // Store locations for possible future error reports.
3891 if (!eval_args_error_loc.IsValid() && IsEvalOrArguments(param_name)) { 3903 if (!eval_args_error_loc.IsValid() && IsEvalOrArguments(param_name)) {
3892 eval_args_error_loc = scanner()->location(); 3904 eval_args_error_loc = scanner()->location();
3893 } 3905 }
3894 if (!reserved_error_loc.IsValid() && is_strict_reserved) { 3906 if (!reserved_error_loc.IsValid() && is_strict_reserved) {
3895 reserved_error_loc = scanner()->location(); 3907 reserved_error_loc = scanner()->location();
3896 } 3908 }
3897 if (!dupe_error_loc.IsValid() && 3909 if (!dupe_error_loc.IsValid() &&
3898 scope_->IsDeclaredParameter(param_name)) { 3910 scope_->IsDeclaredParameter(param_name)) {
3899 duplicate_parameters = FunctionLiteral::kHasDuplicateParameters; 3911 duplicate_parameters = FunctionLiteral::kHasDuplicateParameters;
3900 dupe_error_loc = scanner()->location(); 3912 dupe_error_loc = scanner()->location();
3901 } 3913 }
3902 3914
3903 Variable* var = scope_->DeclareParameter(param_name, VAR, is_rest); 3915
3916
3917 if (peek() == Token::ASSIGN) {
3918 // Default parameters:
3919 Consume(Token::ASSIGN);
3920 static const bool accept_IN = true;
3921 Expression* defaultValue =
3922 ParseAssignmentExpression(accept_IN, CHECK_OK);
3923 if (kind.isRestParameter()) {
3924 ReportMessageAt(
3925 Scanner::Location(rest_pos, scanner()->location().end_pos),
3926 "rest_param_default");
3927 *ok = false;
3928 return nullptr;
3929 }
3930 kind = ParameterKind::Optional;
3931 default_params->Add(defaultValue, zone());
3932 }
3933
3934 Variable* var = scope_->DeclareParameter(param_name, VAR, kind);
3935
3904 if (is_sloppy(scope->language_mode())) { 3936 if (is_sloppy(scope->language_mode())) {
3905 // TODO(sigurds) Mark every parameter as maybe assigned. This is a 3937 // TODO(sigurds) Mark every parameter as maybe assigned. This is a
3906 // conservative approximation necessary to account for parameters 3938 // conservative approximation necessary to account for parameters
3907 // that are assigned via the arguments array. 3939 // that are assigned via the arguments array.
3908 var->set_maybe_assigned(); 3940 var->set_maybe_assigned();
3909 } 3941 }
3910 3942
3911 num_parameters++; 3943 num_parameters++;
3912 if (num_parameters > Code::kMaxArguments) { 3944 if (num_parameters > Code::kMaxArguments) {
3913 ReportMessage("too_many_parameters"); 3945 ReportMessage("too_many_parameters");
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
4025 kReferenceError); 4057 kReferenceError);
4026 *ok = false; 4058 *ok = false;
4027 return nullptr; 4059 return nullptr;
4028 } 4060 }
4029 } 4061 }
4030 } 4062 }
4031 4063
4032 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( 4064 FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
4033 function_name, ast_value_factory(), scope, body, 4065 function_name, ast_value_factory(), scope, body,
4034 materialized_literal_count, expected_property_count, handler_count, 4066 materialized_literal_count, expected_property_count, handler_count,
4035 num_parameters, duplicate_parameters, function_type, 4067 num_parameters, default_params, duplicate_parameters, function_type,
4036 FunctionLiteral::kIsFunction, parenthesized, kind, pos); 4068 FunctionLiteral::kIsFunction, parenthesized, kind, pos);
4037 function_literal->set_function_token_position(function_token_pos); 4069 function_literal->set_function_token_position(function_token_pos);
4038 4070
4039 if (scope->has_rest_parameter()) { 4071 if (scope->has_rest_parameter()) {
4040 // TODO(caitp): enable optimization of functions with rest params 4072 // TODO(caitp): enable optimization of functions with rest params
4041 function_literal->set_dont_optimize_reason(kRestParameter); 4073 function_literal->set_dont_optimize_reason(kRestParameter);
4042 } 4074 }
4043 4075
4044 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); 4076 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal);
4045 return function_literal; 4077 return function_literal;
(...skipping 1689 matching lines...) Expand 10 before | Expand all | Expand 10 after
5735 5767
5736 Expression* Parser::SpreadCallNew(Expression* function, 5768 Expression* Parser::SpreadCallNew(Expression* function,
5737 ZoneList<v8::internal::Expression*>* args, 5769 ZoneList<v8::internal::Expression*>* args,
5738 int pos) { 5770 int pos) {
5739 args->InsertAt(0, function, zone()); 5771 args->InsertAt(0, function, zone());
5740 5772
5741 return factory()->NewCallRuntime( 5773 return factory()->NewCallRuntime(
5742 ast_value_factory()->reflect_construct_string(), NULL, args, pos); 5774 ast_value_factory()->reflect_construct_string(), NULL, args, pos);
5743 } 5775 }
5744 } } // namespace v8::internal 5776 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698