[JSC] support Computed Property Names in destructuring Patterns
https://bugs.webkit.org/show_bug.cgi?id=151494

Patch by Caitlin Potter <caitp@igalia.com> on 2015-11-24
Reviewed by Saam Barati.

Add support for computed property names in destructuring BindingPatterns
and AssignmentPatterns.

Productions BindingProperty(1) and AssignmentProperty(2) allow for any valid
PropertName(3), including ComputedPropertyName(4)

1: http://tc39.github.io/ecma262/#prod-BindingProperty
2: http://tc39.github.io/ecma262/#prod-AssignmentProperty
3: http://tc39.github.io/ecma262/#prod-PropertyName
4: http://tc39.github.io/ecma262/#prod-ComputedPropertyName

* bytecompiler/NodesCodegen.cpp:
(JSC::ObjectPatternNode::bindValue):
* parser/ASTBuilder.h:
(JSC::ASTBuilder::appendObjectPatternEntry):
* parser/Nodes.h:
(JSC::ObjectPatternNode::appendEntry):
* parser/Parser.cpp:
(JSC::Parser<LexerType>::parseDestructuringPattern):
* parser/SyntaxChecker.h:
(JSC::SyntaxChecker::operatorStackPop):
* tests/es6.yaml:
* tests/es6/destructuring_assignment_computed_properties.js: Added.
(test):
(test.computeName):
(test.loadValue):
(test.out.get a):
(test.out.set a):
(test.out.get b):
(test.out.set b):
(test.out.get c):
(test.out.set c):
(test.get var):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@192768 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/JavaScriptCore/ChangeLog b/Source/JavaScriptCore/ChangeLog
index a0b6674..644d76b 100644
--- a/Source/JavaScriptCore/ChangeLog
+++ b/Source/JavaScriptCore/ChangeLog
@@ -1,3 +1,44 @@
+2015-11-24  Caitlin Potter  <caitp@igalia.com>
+
+        [JSC] support Computed Property Names in destructuring Patterns
+        https://bugs.webkit.org/show_bug.cgi?id=151494
+
+        Reviewed by Saam Barati.
+
+        Add support for computed property names in destructuring BindingPatterns
+        and AssignmentPatterns.
+
+        Productions BindingProperty(1) and AssignmentProperty(2) allow for any valid
+        PropertName(3), including ComputedPropertyName(4)
+
+        1: http://tc39.github.io/ecma262/#prod-BindingProperty
+        2: http://tc39.github.io/ecma262/#prod-AssignmentProperty
+        3: http://tc39.github.io/ecma262/#prod-PropertyName
+        4: http://tc39.github.io/ecma262/#prod-ComputedPropertyName
+
+        * bytecompiler/NodesCodegen.cpp:
+        (JSC::ObjectPatternNode::bindValue):
+        * parser/ASTBuilder.h:
+        (JSC::ASTBuilder::appendObjectPatternEntry):
+        * parser/Nodes.h:
+        (JSC::ObjectPatternNode::appendEntry):
+        * parser/Parser.cpp:
+        (JSC::Parser<LexerType>::parseDestructuringPattern):
+        * parser/SyntaxChecker.h:
+        (JSC::SyntaxChecker::operatorStackPop):
+        * tests/es6.yaml:
+        * tests/es6/destructuring_assignment_computed_properties.js: Added.
+        (test):
+        (test.computeName):
+        (test.loadValue):
+        (test.out.get a):
+        (test.out.set a):
+        (test.out.get b):
+        (test.out.set b):
+        (test.out.get c):
+        (test.out.set c):
+        (test.get var):
+
 2015-11-24  Commit Queue  <commit-queue@webkit.org>
 
         Unreviewed, rolling out r192536, r192722, and r192743.
diff --git a/Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp b/Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp
index 312a8c7..4a7e2e7 100644
--- a/Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp
+++ b/Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp
@@ -3340,7 +3340,13 @@
     for (size_t i = 0; i < m_targetPatterns.size(); i++) {
         auto& target = m_targetPatterns[i];
         RefPtr<RegisterID> temp = generator.newTemporary();
-        generator.emitGetById(temp.get(), rhs, target.propertyName);
+        if (!target.propertyExpression)
+            generator.emitGetById(temp.get(), rhs, target.propertyName);
+        else {
+            RefPtr<RegisterID> propertyName = generator.emitNode(target.propertyExpression);
+            generator.emitGetByVal(temp.get(), rhs, propertyName.get());
+        }
+
         if (target.defaultValue)
             assignDefaultValueIfUndefined(generator, temp.get(), target.defaultValue);
         target.pattern->bindValue(generator, temp.get());
diff --git a/Source/JavaScriptCore/parser/ASTBuilder.h b/Source/JavaScriptCore/parser/ASTBuilder.h
index acc2162..ce985ca 100644
--- a/Source/JavaScriptCore/parser/ASTBuilder.h
+++ b/Source/JavaScriptCore/parser/ASTBuilder.h
@@ -851,7 +851,12 @@
     {
         node->appendEntry(location, identifier, wasString, pattern, defaultValue);
     }
-    
+
+    void appendObjectPatternEntry(ObjectPattern node, const JSTokenLocation& location, ExpressionNode* propertyExpression, DestructuringPattern pattern, ExpressionNode* defaultValue)
+    {
+        node->appendEntry(location, propertyExpression, pattern, defaultValue);
+    }
+
     BindingPattern createBindingLocation(const JSTokenLocation&, const Identifier& boundProperty, const JSTextPosition& start, const JSTextPosition& end, AssignmentContext context)
     {
         return new (m_parserArena) BindingNode(boundProperty, start, end, context);
diff --git a/Source/JavaScriptCore/parser/Nodes.h b/Source/JavaScriptCore/parser/Nodes.h
index 0aa311d..5ca3466 100644
--- a/Source/JavaScriptCore/parser/Nodes.h
+++ b/Source/JavaScriptCore/parser/Nodes.h
@@ -2017,7 +2017,12 @@
         ObjectPatternNode();
         void appendEntry(const JSTokenLocation&, const Identifier& identifier, bool wasString, DestructuringPatternNode* pattern, ExpressionNode* defaultValue)
         {
-            m_targetPatterns.append(Entry{ identifier, wasString, pattern, defaultValue });
+            m_targetPatterns.append(Entry{ identifier, nullptr, wasString, pattern, defaultValue });
+        }
+
+        void appendEntry(const JSTokenLocation&, ExpressionNode* propertyExpression, DestructuringPatternNode* pattern, ExpressionNode* defaultValue)
+        {
+            m_targetPatterns.append(Entry{ Identifier(), propertyExpression, false, pattern, defaultValue });
         }
 
     private:
@@ -2026,6 +2031,7 @@
         virtual void toString(StringBuilder&) const override;
         struct Entry {
             const Identifier& propertyName;
+            ExpressionNode* propertyExpression;
             bool wasString;
             DestructuringPatternNode* pattern;
             ExpressionNode* defaultValue;
diff --git a/Source/JavaScriptCore/parser/Parser.cpp b/Source/JavaScriptCore/parser/Parser.cpp
index 6874b27..0af2c5a 100644
--- a/Source/JavaScriptCore/parser/Parser.cpp
+++ b/Source/JavaScriptCore/parser/Parser.cpp
@@ -861,6 +861,7 @@
                 break;
 
             const Identifier* propertyName = nullptr;
+            TreeExpression propertyExpression = 0;
             TreeDestructuringPattern innerPattern = 0;
             JSTokenLocation location = m_token.m_location;
             if (matchSpecIdentifier()) {
@@ -883,6 +884,12 @@
                     propertyName = m_token.m_data.ident;
                     wasString = true;
                     break;
+                case OPENBRACKET:
+                    next();
+                    propertyExpression = parseAssignmentExpression(context);
+                    failIfFalse(propertyExpression, "Cannot parse computed property name");
+                    matchOrFail(CLOSEBRACKET, "Expected ']' to end end a computed property name");
+                    break;
                 default:
                     if (m_token.m_type != RESERVED && m_token.m_type != RESERVED_IF_STRICT && !(m_token.m_type & KeywordTokenFlag)) {
                         if (kind == DestructureToExpressions)
@@ -908,8 +915,12 @@
                 return 0;
             failIfFalse(innerPattern, "Cannot parse this destructuring pattern");
             TreeExpression defaultValue = parseDefaultValueForDestructuringPattern(context);
-            ASSERT(propertyName);
-            context.appendObjectPatternEntry(objectPattern, location, wasString, *propertyName, innerPattern, defaultValue);
+            if (propertyExpression)
+                context.appendObjectPatternEntry(objectPattern, location, propertyExpression, innerPattern, defaultValue);
+            else {
+                ASSERT(propertyName);
+                context.appendObjectPatternEntry(objectPattern, location, wasString, *propertyName, innerPattern, defaultValue);
+            }
         } while (consume(COMMA));
 
         if (kind == DestructureToExpressions && !match(CLOSEBRACE))
diff --git a/Source/JavaScriptCore/parser/SyntaxChecker.h b/Source/JavaScriptCore/parser/SyntaxChecker.h
index 95536c3..547e44f 100644
--- a/Source/JavaScriptCore/parser/SyntaxChecker.h
+++ b/Source/JavaScriptCore/parser/SyntaxChecker.h
@@ -343,6 +343,10 @@
     void appendObjectPatternEntry(ArrayPattern, const JSTokenLocation&, bool, const Identifier&, DestructuringPattern, int)
     {
     }
+    void appendObjectPatternEntry(ArrayPattern, const JSTokenLocation&, Expression, DestructuringPattern, Expression)
+    {
+    }
+
     DestructuringPattern createBindingLocation(const JSTokenLocation&, const Identifier&, const JSTextPosition&, const JSTextPosition&, AssignmentContext)
     {
         return BindingDestructuring;
diff --git a/Source/JavaScriptCore/tests/es6.yaml b/Source/JavaScriptCore/tests/es6.yaml
index e0bc3d5..88d4a7c 100644
--- a/Source/JavaScriptCore/tests/es6.yaml
+++ b/Source/JavaScriptCore/tests/es6.yaml
@@ -216,6 +216,12 @@
   cmd: runES6 :normal
 - path: es6/destructuring_assignment_non_simple_target.js
   cmd: runES6 :normal
+- path: es6/destructuring_assignment_computed_properties.js
+  cmd: runES6 :normal
+- path: es6/destructuring_assignment_computed_property_simple.js
+  cmd: runES6 :normal
+- path: es6/destructuring_assignment_computed_property_default.js
+  cmd: runES6 :normal
 - path: es6/destructuring_initializer_scoping.js
   cmd: runES6 :normal
 - path: es6/for..of_loops_with_arrays.js
@@ -745,7 +751,7 @@
 - path: es6/block-level_function_declaration.js
   cmd: runES6 :fail
 - path: es6/destructuring_computed_properties.js
-  cmd: runES6 :fail
+  cmd: runES6 :normal
 - path: es6/destructuring_defaults_in_parameters_separate_scope.js
   cmd: runES6 :fail
 - path: es6/destructuring_iterator_closing.js
diff --git a/Source/JavaScriptCore/tests/es6/destructuring_assignment_computed_properties.js b/Source/JavaScriptCore/tests/es6/destructuring_assignment_computed_properties.js
new file mode 100644
index 0000000..003a49a
--- /dev/null
+++ b/Source/JavaScriptCore/tests/es6/destructuring_assignment_computed_properties.js
@@ -0,0 +1,72 @@
+function test() {
+    var steps = [];
+    var backingStore = {};
+
+    function storeProperty(name, value) {
+        steps.push(`store: ${name} = ${value}`);
+        backingStore[name] = value;
+    }
+
+    function computeName(name) {
+        steps.push(`compute: ${name}`);
+        return name;
+    }
+
+    function loadValue(name, value) {
+        steps.push(`load: ${name} > ${value}`);
+        return value;
+    }
+
+    var out = {
+        get a() { return backingStore.a; },
+        set a(v) { storeProperty("a", v); },
+        get b() { return backingStore.b; },
+        set b(v) { storeProperty("b", v); },
+        get c() { return backingStore.c; },
+        set c(v) { storeProperty("c", v); },
+        get d() { return backingStore.d; },
+        set d(v) { storeProperty("d", v); }
+    };
+    ({
+        [computeName("propA")]: out.a,
+        [computeName("propB")]: out.b,
+        [computeName("propC")]: [...out["c"]],
+        [computeName("propD")]: out.d = "default"
+    } = {
+        get propA() { return loadValue("propA", "hello"); },
+        get propB() { return loadValue("propB", "world"); },
+        get propC() { return loadValue("propC", [1, 2, 3]); },
+        get propD() { return loadValue("propD"); }
+    });
+
+    var expectedSteps = [
+        "compute: propA",
+        "load: propA > hello",
+        "store: a = hello",
+
+        "compute: propB",
+        "load: propB > world",
+        "store: b = world",
+
+        "compute: propC",
+        "load: propC > 1,2,3",
+        "store: c = 1,2,3",
+
+        "compute: propD",
+        "load: propD > undefined",
+        "store: d = default"
+    ];
+
+    if (expectedSteps.length !== steps.length)
+        return false;
+    for (var i = 0; i < expectedSteps.length; ++i)
+        if (expectedSteps[i] !== steps[i])
+            return false;
+    if (`${backingStore.a} ${backingStore.b} ${backingStore.c.join(":")}` !== "hello world 1:2:3")
+        return false;
+
+    return true;
+}
+
+if (!test())
+    throw new Error("Test failed");
diff --git a/Source/JavaScriptCore/tests/es6/destructuring_assignment_computed_property_default.js b/Source/JavaScriptCore/tests/es6/destructuring_assignment_computed_property_default.js
new file mode 100644
index 0000000..f6a4e46
--- /dev/null
+++ b/Source/JavaScriptCore/tests/es6/destructuring_assignment_computed_property_default.js
@@ -0,0 +1,8 @@
+function test() {
+  var result0, result1, i = 0;
+  ({ [i++]: result0 = "hungryByDefault", [i++]: result1 = "hippoByDefault"} = []);
+  return result0 === "hungryByDefault" && result1 === "hippoByDefault" && i === 2;
+}
+
+if (!test())
+  throw new Error("Test failed");
diff --git a/Source/JavaScriptCore/tests/es6/destructuring_assignment_computed_property_simple.js b/Source/JavaScriptCore/tests/es6/destructuring_assignment_computed_property_simple.js
new file mode 100644
index 0000000..12dcc79
--- /dev/null
+++ b/Source/JavaScriptCore/tests/es6/destructuring_assignment_computed_property_simple.js
@@ -0,0 +1,8 @@
+function test() {
+  var result0, result1, i = 0;
+  ({ [i++]: result0, [i++]: result1 } = ["hungryhungry", "hippos"]);
+  return result0 === "hungryhungry" && result1 === "hippos" && i === 2;
+}
+
+if (!test())
+  throw new Error("Test failed");