Bug 789239 - Unravel 30-line if condition in Parser.cpp. Part 1 - Move the condition into a helper function. r=Waldo.
authorJason Orendorff <jorendorff@mozilla.com>
Tue, 18 Sep 2012 06:03:22 -0500
changeset 107480 2085233469a0e140c9dd499b47bd9156a43c3e00
parent 107479 5acfcf041a4002ead1914420f19eeb4719cfc453
child 107481 2e9d543ae2ec6de6e5b56c39c5a1e4c7c66e653d
push id82
push usershu@rfrn.org
push dateFri, 05 Oct 2012 13:20:22 +0000
reviewersWaldo
bugs789239
milestone18.0a1
Bug 789239 - Unravel 30-line if condition in Parser.cpp. Part 1 - Move the condition into a helper function. r=Waldo.
js/src/frontend/Parser.cpp
--- a/js/src/frontend/Parser.cpp
+++ b/js/src/frontend/Parser.cpp
@@ -2955,16 +2955,49 @@ Parser::matchInOrOf(bool *isForOfp)
             *isForOfp = true;
             return true;
         }
         tokenStream.ungetToken();
     }
     return false;
 }
 
+static bool
+IsValidForStatementLHS(ParseNode *pn1, JSVersion version, bool forDecl, bool forEach, bool forOf)
+{
+    return !(forDecl
+             ? (pn1->pn_count > 1 || pn1->isOp(JSOP_DEFCONST)
+#if JS_HAS_DESTRUCTURING
+                || (version == JSVERSION_1_7 &&
+                    !forEach &&
+                    !forOf &&
+                    (pn1->pn_head->isKind(PNK_OBJECT) ||
+                     (pn1->pn_head->isKind(PNK_ARRAY) &&
+                      pn1->pn_head->pn_count != 2) ||
+                     (pn1->pn_head->isKind(PNK_ASSIGN) &&
+                      (!pn1->pn_head->pn_left->isKind(PNK_ARRAY) ||
+                       pn1->pn_head->pn_left->pn_count != 2))))
+#endif
+                 )
+             : (!pn1->isKind(PNK_NAME) &&
+                !pn1->isKind(PNK_DOT) &&
+#if JS_HAS_DESTRUCTURING
+                ((version == JSVERSION_1_7 &&
+                  !forEach &&
+                  !forOf)
+                 ? (!pn1->isKind(PNK_ARRAY) || pn1->pn_count != 2)
+                 : (!pn1->isKind(PNK_ARRAY) && !pn1->isKind(PNK_OBJECT))) &&
+#endif
+                !pn1->isKind(PNK_CALL) &&
+#if JS_HAS_XML_SUPPORT
+                !pn1->isKind(PNK_XMLUNARY) &&
+#endif
+                !pn1->isKind(PNK_ELEM)));
+}
+
 ParseNode *
 Parser::forStatement()
 {
     JS_ASSERT(tokenStream.isCurrentTokenType(TOK_FOR));
 
     /* A FOR node is binary, left is loop control and right is the body. */
     ParseNode *pn = BinaryNode::create(PNK_FOR, this);
     if (!pn)
@@ -3082,47 +3115,18 @@ Parser::forStatement()
         if (forOf && pn->pn_iflags != 0) {
             JS_ASSERT(pn->pn_iflags == JSITER_FOREACH);
             reportError(NULL, JSMSG_BAD_FOR_EACH_LOOP);
             return NULL;
         }
         pn->pn_iflags |= (forOf ? JSITER_FOR_OF : JSITER_ENUMERATE);
 
         /* Check that the left side of the 'in' or 'of' is valid. */
-        if (forDecl
-            ? (pn1->pn_count > 1 || pn1->isOp(JSOP_DEFCONST)
-#if JS_HAS_DESTRUCTURING
-               || (versionNumber() == JSVERSION_1_7 &&
-                   pn->isOp(JSOP_ITER) &&
-                   !(pn->pn_iflags & JSITER_FOREACH) &&
-                   !forOf &&
-                   (pn1->pn_head->isKind(PNK_OBJECT) ||
-                    (pn1->pn_head->isKind(PNK_ARRAY) &&
-                     pn1->pn_head->pn_count != 2) ||
-                    (pn1->pn_head->isKind(PNK_ASSIGN) &&
-                     (!pn1->pn_head->pn_left->isKind(PNK_ARRAY) ||
-                      pn1->pn_head->pn_left->pn_count != 2))))
-#endif
-              )
-            : (!pn1->isKind(PNK_NAME) &&
-               !pn1->isKind(PNK_DOT) &&
-#if JS_HAS_DESTRUCTURING
-               ((versionNumber() == JSVERSION_1_7 &&
-                 pn->isOp(JSOP_ITER) &&
-                 !(pn->pn_iflags & JSITER_FOREACH) &&
-                 !forOf)
-                ? (!pn1->isKind(PNK_ARRAY) || pn1->pn_count != 2)
-                : (!pn1->isKind(PNK_ARRAY) && !pn1->isKind(PNK_OBJECT))) &&
-#endif
-               !pn1->isKind(PNK_CALL) &&
-#if JS_HAS_XML_SUPPORT
-               !pn1->isKind(PNK_XMLUNARY) &&
-#endif
-               !pn1->isKind(PNK_ELEM)))
-        {
+        bool forEach = bool(pn->pn_iflags & JSITER_FOREACH);
+        if (!IsValidForStatementLHS(pn1, versionNumber(), forDecl, forEach, forOf)) {
             reportError(pn1, JSMSG_BAD_FOR_LEFTSIDE);
             return NULL;
         }
 
         /*
          * After the following if-else, pn2 will point to the name or
          * destructuring pattern on in's left. pn1 will point to the decl, if
          * any, else NULL. Note that the "declaration with initializer" case