Reviewers: arv, rossberg,

Message:
PTAL (based on destructuring-initializers
https://codereview.chromium.org/1146683002)

Description:
[destructuring] Support computed property names in patterns.

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

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

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

Affected files (+75, -8 lines):
  M src/pattern-rewriter.cc
  M src/preparser.h
  M test/cctest/test-parsing.cc
  M test/mjsunit/harmony/destructuring.js


Index: src/pattern-rewriter.cc
diff --git a/src/pattern-rewriter.cc b/src/pattern-rewriter.cc
index a97c009708cdd1ffbb793654a1b5838339cf3e8c..5871740c14bf16a064c78f9c0c05fdddc228ff80 100644
--- a/src/pattern-rewriter.cc
+++ b/src/pattern-rewriter.cc
@@ -224,13 +224,11 @@ Variable* Parser::PatternRewriter::CreateTempVar(Expression* value) {

 void Parser::PatternRewriter::VisitObjectLiteral(ObjectLiteral* pattern) {
   auto temp = CreateTempVar(current_value_);
-  if (pattern->properties()->length() == 0) {
-    block_->AddStatement(descriptor_->parser->BuildAssertIsCoercible(temp),
-                         zone());
-  }
+
+  block_->AddStatement(descriptor_->parser->BuildAssertIsCoercible(temp),
+                       zone());

   for (ObjectLiteralProperty* property : *pattern->properties()) {
-    // TODO(dslomov): computed property names.
     RecurseIntoSubpattern(
         property->value(),
         factory()->NewProperty(factory()->NewVariableProxy(temp),
Index: src/preparser.h
diff --git a/src/preparser.h b/src/preparser.h
index b08888834eb9ba4a42c557f8cd2d3dfa849c9520..116a8eac9b891721e208bea92d9f3b7e4ad30528 100644
--- a/src/preparser.h
+++ b/src/preparser.h
@@ -2560,8 +2560,10 @@ typename ParserBase<Traits>::ExpressionT ParserBase<Traits>::ParsePropertyName(
       if (allow_harmony_computed_property_names_) {
         *is_computed_name = true;
         Consume(Token::LBRACK);
-        ExpressionT expression =
-            ParseAssignmentExpression(true, classifier, CHECK_OK);
+        ExpressionClassifier computed_name_classifier;
+        ExpressionT expression = ParseAssignmentExpression(
+            true, &computed_name_classifier, CHECK_OK);
+ classifier->AccumulateReclassifyingAsPattern(computed_name_classifier);
         Expect(Token::RBRACK, CHECK_OK);
         return expression;
       }
Index: test/cctest/test-parsing.cc
diff --git a/test/cctest/test-parsing.cc b/test/cctest/test-parsing.cc
index 16f0cf37494e59df333f881827b897949d3218af..a12aa5cdece1375db5293a45ba644032dd1af02b 100644
--- a/test/cctest/test-parsing.cc
+++ b/test/cctest/test-parsing.cc
@@ -6356,6 +6356,7 @@ TEST(StrongModeFreeVariablesNotDeclared) {

 TEST(DestructuringPositiveTests) {
   i::FLAG_harmony_destructuring = true;
+  i::FLAG_harmony_computed_property_names = true;

   const char* context_data[][2] = {{"'use strict'; let ", " = {};"},
                                    {"var ", " = {};"},
@@ -6390,10 +6391,14 @@ TEST(DestructuringPositiveTests) {
     "{'hi' : x = 42}",
     "{var: x}",
     "{var: x = 42}",
+    "{[x] : z}",
+    "{[1+1] : z}",
+    "{[foo()] : z}",
     "{}",
     NULL};
   // clang-format on
   static const ParserFlag always_flags[] = {kAllowHarmonyObjectLiterals,
+ kAllowHarmonyComputedPropertyNames,
                                             kAllowHarmonyDestructuring};
   RunParserSyncTest(context_data, data, kSuccess, NULL, 0, always_flags,
                     arraysize(always_flags));
@@ -6459,6 +6464,7 @@ TEST(DestructuringNegativeTests) {
         "{x : x = (a+)}",
         "{x : x += a}",
         "{m() {} = 0}",
+        "{[1+1]}",
         NULL};
     // clang-format on
     RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags,
Index: test/mjsunit/harmony/destructuring.js
diff --git a/test/mjsunit/harmony/destructuring.js b/test/mjsunit/harmony/destructuring.js index 3e1726fdc2d6ab99b152548c32f401b2701a6072..6e386efa0d024aab6e38c9e643fe1a9855e2f4a5 100644
--- a/test/mjsunit/harmony/destructuring.js
+++ b/test/mjsunit/harmony/destructuring.js
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 //
-// Flags: --harmony-destructuring
+// Flags: --harmony-destructuring --harmony-computed-property-names

 (function TestObjectLiteralPattern() {
   var { x : x, y : y } = { x : 1, y : 2 };
@@ -283,6 +283,67 @@
 }());


+(function TestComputedNames() {
+  var x = 1;
+  var {[x]:y} = {1:2};
+  assertSame(2, y);
+
+  (function(){
+    'use strict';
+    let {[x]:y} = {1:2};
+    assertSame(2, y);
+  }());
+
+  var callCount = 0;
+  function foo(v) { callCount++; return v; }
+
+  (function() {
+    callCount = 0;
+    var {[foo("abc")]:x} = {abc:42};
+    assertSame(42, x);
+    assertEquals(1, callCount);
+  }());
+
+  (function() {
+    'use strict';
+    callCount = 0;
+    let {[foo("abc")]:x} = {abc:42};
+    assertSame(42, x);
+    assertEquals(1, callCount);
+  }());
+
+  (function() {
+    callCount = 0;
+    var {[foo("abc")]:x} = {};
+    assertSame(undefined, x);
+    assertEquals(1, callCount);
+  }());
+
+  (function() {
+    'use strict';
+    callCount = 0;
+    let {[foo("abc")]:x} = {};
+    assertSame(undefined, x);
+    assertEquals(1, callCount);
+  }());
+
+  for (val of [null, undefined]) {
+    callCount = 0;
+    assertThrows(function() {
+      var {[foo()]:x} = val;
+    }, TypeError);
+    assertEquals(0, callCount);
+
+    callCount = 0;
+    assertThrows(function() {
+      'use strict';
+      let {[foo()]:x} = val;
+    }, TypeError);
+    assertEquals(0, callCount);
+  }
+}());
+
+
 (function TestExceptions() {
   for (var val of [null, undefined]) {
     assertThrows(function() { var {} = val; }, TypeError);


--
--
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