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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index fc4138d18621260079dc4a5b7263e68155b1982a..e8a0180e67043a3ee3c8b83a2c02090f56f40c26 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -373,10 +373,12 @@ FunctionLiteral* Parser::DefaultConstructor(bool call_super, Scope* scope,
handler_count = function_state.handler_count();
}
+ ZoneList<Expression*>* default_values =
+ new (zone()) ZoneList<Expression*>(0, zone());
FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
name, ast_value_factory(), function_scope, body,
materialized_literal_count, expected_property_count, handler_count,
- parameter_count, FunctionLiteral::kNoDuplicateParameters,
+ parameter_count, default_values, FunctionLiteral::kNoDuplicateParameters,
FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kIsFunction,
FunctionLiteral::kNotParenthesized, kind, pos);
@@ -1036,11 +1038,13 @@ FunctionLiteral* Parser::DoParseProgram(ParseInfo* info, Scope** scope,
}
if (ok) {
+ ZoneList<Expression*>* default_values =
+ new (zone()) ZoneList<Expression*>(0, zone());
result = factory()->NewFunctionLiteral(
ast_value_factory()->empty_string(), ast_value_factory(), scope_,
body, function_state.materialized_literal_count(),
function_state.expected_property_count(),
- function_state.handler_count(), 0,
+ function_state.handler_count(), 0, default_values,
FunctionLiteral::kNoDuplicateParameters,
FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kGlobalOrEval,
FunctionLiteral::kNotParenthesized, FunctionKind::kNormalFunction, 0);
@@ -3840,6 +3844,9 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
FunctionLiteral::IsParenthesizedFlag parenthesized = parenthesized_function_
? FunctionLiteral::kIsParenthesized
: FunctionLiteral::kNotParenthesized;
+
+ ZoneList<Expression*>* default_params = new (zone()) ZoneList<Expression*>(0, zone());
+
// Parse function body.
{
AstNodeFactory function_factory(ast_value_factory());
@@ -3879,9 +3886,14 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
arity_restriction != FunctionLiteral::SETTER_ARITY);
while (!done) {
bool is_strict_reserved = false;
- is_rest = peek() == Token::ELLIPSIS && allow_harmony_rest_params();
- if (is_rest) {
+ int rest_pos;
+
+ ParameterKind kind = ParameterKind::Normal;
+
+ if (peek() == Token::ELLIPSIS && allow_harmony_rest_params()) {
Consume(Token::ELLIPSIS);
+ rest_pos = position();
+ kind = ParameterKind::Rest;
}
const AstRawString* param_name =
@@ -3900,7 +3912,27 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
dupe_error_loc = scanner()->location();
}
- Variable* var = scope_->DeclareParameter(param_name, VAR, is_rest);
+
+
+ if (peek() == Token::ASSIGN) {
+ // Default parameters:
+ Consume(Token::ASSIGN);
+ static const bool accept_IN = true;
+ Expression* defaultValue =
+ ParseAssignmentExpression(accept_IN, CHECK_OK);
+ if (kind.isRestParameter()) {
+ ReportMessageAt(
+ Scanner::Location(rest_pos, scanner()->location().end_pos),
+ "rest_param_default");
+ *ok = false;
+ return nullptr;
+ }
+ kind = ParameterKind::Optional;
+ default_params->Add(defaultValue, zone());
+ }
+
+ Variable* var = scope_->DeclareParameter(param_name, VAR, kind);
+
if (is_sloppy(scope->language_mode())) {
// TODO(sigurds) Mark every parameter as maybe assigned. This is a
// conservative approximation necessary to account for parameters
@@ -4032,7 +4064,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
function_name, ast_value_factory(), scope, body,
materialized_literal_count, expected_property_count, handler_count,
- num_parameters, duplicate_parameters, function_type,
+ num_parameters, default_params, duplicate_parameters, function_type,
FunctionLiteral::kIsFunction, parenthesized, kind, pos);
function_literal->set_function_token_position(function_token_pos);

Powered by Google App Engine
This is Rietveld 408576698