Commit ff932fe8 authored by rossberg's avatar rossberg Committed by Commit bot

[es6] Fix default parameters in arrow functions

R=adamk@chromium.org, wingo@igalia.com
BUG=v8:811
LOG=N

Review URL: https://codereview.chromium.org/1314543005

Cr-Commit-Position: refs/heads/master@{#30338}
parent 032a35ff
...@@ -3904,6 +3904,7 @@ void ParserTraits::ParseArrowFunctionFormalParameters( ...@@ -3904,6 +3904,7 @@ void ParserTraits::ParseArrowFunctionFormalParameters(
parameters->is_simple = !is_rest && expr->IsVariableProxy(); parameters->is_simple = !is_rest && expr->IsVariableProxy();
} }
Expression* initializer = nullptr;
if (expr->IsVariableProxy()) { if (expr->IsVariableProxy()) {
// When the formal parameter was originally seen, it was parsed as a // When the formal parameter was originally seen, it was parsed as a
// VariableProxy and recorded as unresolved in the scope. Here we undo that // VariableProxy and recorded as unresolved in the scope. Here we undo that
...@@ -3911,18 +3912,12 @@ void ParserTraits::ParseArrowFunctionFormalParameters( ...@@ -3911,18 +3912,12 @@ void ParserTraits::ParseArrowFunctionFormalParameters(
// patterns; for patterns that happens uniformly in // patterns; for patterns that happens uniformly in
// PatternRewriter::VisitVariableProxy). // PatternRewriter::VisitVariableProxy).
parser_->scope_->RemoveUnresolved(expr->AsVariableProxy()); parser_->scope_->RemoveUnresolved(expr->AsVariableProxy());
} } else if (expr->IsAssignment()) {
Assignment* assignment = expr->AsAssignment();
Expression* initializer = nullptr; DCHECK(parser_->allow_harmony_default_parameters());
if (!is_rest && parser_->allow_harmony_default_parameters() && DCHECK(!assignment->is_compound());
parser_->Check(Token::ASSIGN)) { initializer = assignment->value();
ExpressionClassifier init_classifier; expr = assignment->target();
initializer =
parser_->ParseAssignmentExpression(true, &init_classifier, ok);
if (!*ok) return;
parser_->ValidateExpression(&init_classifier, ok);
if (!*ok) return;
parameters->is_simple = false;
} }
AddFormalParameter(parameters, expr, initializer, is_rest); AddFormalParameter(parameters, expr, initializer, is_rest);
......
...@@ -1426,6 +1426,7 @@ enum ParserFlag { ...@@ -1426,6 +1426,7 @@ enum ParserFlag {
kAllowLazy, kAllowLazy,
kAllowNatives, kAllowNatives,
kAllowHarmonyArrowFunctions, kAllowHarmonyArrowFunctions,
kAllowHarmonyDefaultParameters,
kAllowHarmonyRestParameters, kAllowHarmonyRestParameters,
kAllowHarmonySloppy, kAllowHarmonySloppy,
kAllowHarmonySloppyLet, kAllowHarmonySloppyLet,
...@@ -1451,6 +1452,8 @@ void SetParserFlags(i::ParserBase<Traits>* parser, ...@@ -1451,6 +1452,8 @@ void SetParserFlags(i::ParserBase<Traits>* parser,
parser->set_allow_natives(flags.Contains(kAllowNatives)); parser->set_allow_natives(flags.Contains(kAllowNatives));
parser->set_allow_harmony_arrow_functions( parser->set_allow_harmony_arrow_functions(
flags.Contains(kAllowHarmonyArrowFunctions)); flags.Contains(kAllowHarmonyArrowFunctions));
parser->set_allow_harmony_default_parameters(
flags.Contains(kAllowHarmonyDefaultParameters));
parser->set_allow_harmony_rest_parameters( parser->set_allow_harmony_rest_parameters(
flags.Contains(kAllowHarmonyRestParameters)); flags.Contains(kAllowHarmonyRestParameters));
parser->set_allow_harmony_spreadcalls( parser->set_allow_harmony_spreadcalls(
...@@ -3730,10 +3733,30 @@ TEST(NoErrorsArrowFunctions) { ...@@ -3730,10 +3733,30 @@ TEST(NoErrorsArrowFunctions) {
// Arrow has more precedence, this is the same as: foo ? bar : (baz = {}) // Arrow has more precedence, this is the same as: foo ? bar : (baz = {})
"foo ? bar : baz => {}", "foo ? bar : baz => {}",
// Arrows with non-simple parameters.
"({a}) => {}",
"(x = 9) => {}",
"(x, y = 9) => {}",
"(x = 9, y) => {}",
"(x, y = 9, z) => {}",
"(x, y = 9, z = 8) => {}",
"(...a) => {}",
"(x, ...a) => {}",
"(x = 9, ...a) => {}",
"(x, y = 9, ...a) => {}",
"(x, y = 9, {b}, z = 8, ...a) => {}",
// TODO(wingo, rossberg): This is not accepted right now.
// "({a} = {}) => {}",
// "([x] = []) => {}",
"({a = 42}) => {}",
"([x = 0]) => {}",
NULL NULL
}; };
static const ParserFlag always_flags[] = {kAllowHarmonyArrowFunctions}; static const ParserFlag always_flags[] = {
kAllowHarmonyArrowFunctions, kAllowHarmonyDefaultParameters,
kAllowHarmonyRestParameters, kAllowHarmonyDestructuring};
RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0, RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0,
always_flags, arraysize(always_flags)); always_flags, arraysize(always_flags));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment