Reviewers: adamk, wingo,

Description:
[es6] Fix default parameters in arrow functions

[email protected], [email protected]
BUG=v8:811
LOG=N

Please review this at https://codereview.chromium.org/1314543005/

Base URL: https://chromium.googlesource.com/v8/v8.git@master

Affected files (+28, -13 lines):
  M src/parser.cc
  M test/cctest/test-parsing.cc


Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index 9334a33195a1e21f474055863bc28dd47fa2cb93..11a255c94e3d568231846fc406898b4425a2844f 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -3904,6 +3904,7 @@ void ParserTraits::ParseArrowFunctionFormalParameters(
     parameters->is_simple = !is_rest && expr->IsVariableProxy();
   }

+  Expression* initializer = nullptr;
   if (expr->IsVariableProxy()) {
     // When the formal parameter was originally seen, it was parsed as a
// VariableProxy and recorded as unresolved in the scope. Here we undo that @@ -3911,18 +3912,12 @@ void ParserTraits::ParseArrowFunctionFormalParameters(
     // patterns; for patterns that happens uniformly in
     // PatternRewriter::VisitVariableProxy).
     parser_->scope_->RemoveUnresolved(expr->AsVariableProxy());
-  }
-
-  Expression* initializer = nullptr;
-  if (!is_rest && parser_->allow_harmony_default_parameters() &&
-      parser_->Check(Token::ASSIGN)) {
-    ExpressionClassifier init_classifier;
-    initializer =
-        parser_->ParseAssignmentExpression(true, &init_classifier, ok);
-    if (!*ok) return;
-    parser_->ValidateExpression(&init_classifier, ok);
-    if (!*ok) return;
-    parameters->is_simple = false;
+  } else if (expr->IsAssignment()) {
+    Assignment* assignment = expr->AsAssignment();
+    DCHECK(parser_->allow_harmony_default_parameters());
+    DCHECK(!assignment->is_compound());
+    initializer = assignment->value();
+    expr = assignment->target();
   }

   AddFormalParameter(parameters, expr, initializer, is_rest);
Index: test/cctest/test-parsing.cc
diff --git a/test/cctest/test-parsing.cc b/test/cctest/test-parsing.cc
index 23a3d2621a8fa78b1ce38d8a8dbbd04f58f6251d..f34315a7f20ccbc24d1e80e414d2f7b989ec4c7d 100644
--- a/test/cctest/test-parsing.cc
+++ b/test/cctest/test-parsing.cc
@@ -1426,6 +1426,7 @@ enum ParserFlag {
   kAllowLazy,
   kAllowNatives,
   kAllowHarmonyArrowFunctions,
+  kAllowHarmonyDefaultParameters,
   kAllowHarmonyRestParameters,
   kAllowHarmonySloppy,
   kAllowHarmonySloppyLet,
@@ -1451,6 +1452,8 @@ void SetParserFlags(i::ParserBase<Traits>* parser,
   parser->set_allow_natives(flags.Contains(kAllowNatives));
   parser->set_allow_harmony_arrow_functions(
       flags.Contains(kAllowHarmonyArrowFunctions));
+  parser->set_allow_harmony_default_parameters(
+      flags.Contains(kAllowHarmonyDefaultParameters));
   parser->set_allow_harmony_rest_parameters(
       flags.Contains(kAllowHarmonyRestParameters));
   parser->set_allow_harmony_spreadcalls(
@@ -3730,10 +3733,27 @@ TEST(NoErrorsArrowFunctions) {

// Arrow has more precedence, this is the same as: 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} = {}) => {}",
     NULL
   };

-  static const ParserFlag always_flags[] = {kAllowHarmonyArrowFunctions};
+  static const ParserFlag always_flags[] = {
+      kAllowHarmonyArrowFunctions, kAllowHarmonyDefaultParameters,
+      kAllowHarmonyRestParameters, kAllowHarmonyDestructuring};
   RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0,
                     always_flags, arraysize(always_flags));
 }


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to