Reviewers: rossberg,

Message:
PTAL

Description:
Fix for-loop with const/let and empty condition/iteration statements.

BUG=v8:3425, v8:3424
TEST=mjsunit/harmony/empty-for.js

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

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files (+67, -29 lines):
  M src/parser.cc
  A + test/mjsunit/harmony/empty-for.js


Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index 261de4eed78e2021a3c8ff4dfa21dd7513bcb977..89d3ecb6180506df96171ec715261c7cf575a17c 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -3001,7 +3001,7 @@ Statement* Parser::DesugarLetBindingsInForStatement(
   }

   // Make statement: if (flag == 1) { flag = 0; } else { next; }.
-  {
+  if (next) {
     Expression* compare = NULL;
     // Make compare expresion: flag == 1.
     {
@@ -3026,7 +3026,7 @@ Statement* Parser::DesugarLetBindingsInForStatement(


   // Make statement: if (cond) { } else { break; }.
-  {
+  if (cond) {
Statement* empty = factory()->NewEmptyStatement(RelocInfo::kNoPosition);
     BreakableStatement* t = LookupBreakTarget(NULL, CHECK_OK);
Statement* stop = factory()->NewBreakStatement(t, RelocInfo::kNoPosition); @@ -3248,11 +3248,31 @@ Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
     scope_ = saved_scope;
     for_scope->set_end_position(scanner()->location().end_pos);
   } else {
-    loop->Initialize(init, cond, next, body);
-    result = loop;
     scope_ = saved_scope;
     for_scope->set_end_position(scanner()->location().end_pos);
-    for_scope->FinalizeBlockScope();
+    for_scope = for_scope->FinalizeBlockScope();
+    if (for_scope) {
+      // Rewrite a for statement of the form
+      //   for (const x = i; c; n) b
+      //
+      // into
+      //
+      //   {
+      //     const x = i;
+      //     for (; c; n) b
+      //   }
+      ASSERT(init != NULL);
+      Block* block =
+          factory()->NewBlock(NULL, 2, false, RelocInfo::kNoPosition);
+      block->AddStatement(init, zone());
+      block->AddStatement(loop, zone());
+      block->set_scope(for_scope);
+      loop->Initialize(NULL, cond, next, body);
+      result = block;
+    } else {
+      loop->Initialize(init, cond, next, body);
+      result = loop;
+    }
   }
   return result;
 }
Index: test/mjsunit/harmony/empty-for.js
diff --git a/test/mjsunit/regress/regress-354433.js b/test/mjsunit/harmony/empty-for.js
similarity index 74%
copy from test/mjsunit/regress/regress-354433.js
copy to test/mjsunit/harmony/empty-for.js
index 80ea28623021bafc8ab291cc2bdd1078424610fb..02211260ff5d1bd2cd50feb16c6b09b1984f69ea 100644
--- a/test/mjsunit/regress/regress-354433.js
+++ b/test/mjsunit/harmony/empty-for.js
@@ -25,30 +25,48 @@
 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-// Flags: --allow-natives-syntax
-
-var __v_0 = {};
-var __v_5 = {};
-function __f_2() {
-  this.__defineGetter__('str', function() { return __f_2(this); });
-  this.str = "1";
-  this.toString = function() {
-    return this.str;
-  };
-};
-
-__v_5 = new __f_2();
-__v_0 = new __f_2();
-
-function __f_5(fun,a,b) {
-  __v_5.str = a;
-  __v_0.str = b;
-  fun(__v_5, __v_0);
+// Flags: --harmony-scoping
+
+"use strict";
+
+function for_const() {
+  for (const x = 1;;) {
+    if (x == 1) break;
+  }
+  for (const x = 1; x < 2;) {
+    if (x == 1) break;
+  }
+  for (const x = 1;; 0) {
+    if (x == 1) break;
+  }
 }

-function __f_8(a,b) { return a%b };
+for_const();
+
+function for_let() {
+  for (let x;;) {
+    if (!x) break;
+  }
+  for (let x; x < 2;) {
+    if (!x) break;
+  }
+  for (let x = 1;; x++) {
+    if (x == 2) break;
+  }
+}
+
+for_let();
+
+function for_var() {
+  for (var x;;) {
+    if (!x) break;
+  }
+  for (var x; x < 2;) {
+    if (!x) break;
+  }
+  for (var x = 1;; x++) {
+    if (x == 2) break;
+  }
+}

-__f_5(__f_8, 1 << 30, 1);
-__f_5(__f_8, 1, 1 << 30);
-%OptimizeFunctionOnNextCall(__f_8);
-__f_5(__f_8, 1, 1 << 30);
+for_var();


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