Reviewers: adamk,

Message:
PTAL

Description:
Use for-of loops in collection constructors

This is to reduce code duplication but also to get the correct
behavior when we make for-of handle abrupt completion correctly.

BUG=None
LOG=N
R=adamk

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

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

Affected files (+22, -62 lines):
  M src/collection.js
  M src/weak-collection.js


Index: src/collection.js
diff --git a/src/collection.js b/src/collection.js
index 8d2d18428602b85979ff98afde6f60ea2c30ff92..c8cf639f97d96c3396f463a97ad5181ac4ccc347 100644
--- a/src/collection.js
+++ b/src/collection.js
@@ -20,26 +20,17 @@ function SetConstructor(iterable) {
     throw MakeTypeError('constructor_not_function', ['Set']);
   }

-  var iter, adder;
+  %_SetInitialize(this);

   if (!IS_NULL_OR_UNDEFINED(iterable)) {
-    adder = this.add;
+    var adder = this.add;
     if (!IS_SPEC_FUNCTION(adder)) {
       throw MakeTypeError('property_not_function', ['add', this]);
     }
-    iter = GetIterator(iterable);
-  }
-
-  %_SetInitialize(this);
-
-  if (IS_UNDEFINED(iter)) return;

-  var next, done;
-  while (!(next = iter.next()).done) {
-    if (!IS_SPEC_OBJECT(next)) {
-      throw MakeTypeError('iterator_result_not_an_object', [next]);
+    for (var value of iterable) {
+      %_CallFunction(this, value, adder);
     }
-    %_CallFunction(this, next.value, adder);
   }
 }

@@ -160,30 +151,20 @@ function MapConstructor(iterable) {
     throw MakeTypeError('constructor_not_function', ['Map']);
   }

-  var iter, adder;
+  %_MapInitialize(this);

   if (!IS_NULL_OR_UNDEFINED(iterable)) {
-    adder = this.set;
+    var adder = this.set;
     if (!IS_SPEC_FUNCTION(adder)) {
       throw MakeTypeError('property_not_function', ['set', this]);
     }
-    iter = GetIterator(iterable);
-  }
-
-  %_MapInitialize(this);

-  if (IS_UNDEFINED(iter)) return;
-
-  var next, done, nextItem;
-  while (!(next = iter.next()).done) {
-    if (!IS_SPEC_OBJECT(next)) {
-      throw MakeTypeError('iterator_result_not_an_object', [next]);
-    }
-    nextItem = next.value;
-    if (!IS_SPEC_OBJECT(nextItem)) {
-      throw MakeTypeError('iterator_value_not_an_object', [nextItem]);
+    for (var nextItem of iterable) {
+      if (!IS_SPEC_OBJECT(nextItem)) {
+        throw MakeTypeError('iterator_value_not_an_object', [nextItem]);
+      }
+      %_CallFunction(this, nextItem[0], nextItem[1], adder);
     }
-    %_CallFunction(this, nextItem[0], nextItem[1], adder);
   }
 }

Index: src/weak-collection.js
diff --git a/src/weak-collection.js b/src/weak-collection.js
index 33e3d4c69758c8c052822c98f5d0890ee7be9e81..6266fb879e08fd69cdbf2329708ad8a401734427 100644
--- a/src/weak-collection.js
+++ b/src/weak-collection.js
@@ -20,30 +20,19 @@ function WeakMapConstructor(iterable) {
     throw MakeTypeError('constructor_not_function', ['WeakMap']);
   }

-  var iter, adder;
+  %WeakCollectionInitialize(this);

   if (!IS_NULL_OR_UNDEFINED(iterable)) {
-    adder = this.set;
+    var adder = this.set;
     if (!IS_SPEC_FUNCTION(adder)) {
       throw MakeTypeError('property_not_function', ['set', this]);
     }
-    iter = GetIterator(iterable);
-  }
-
-  %WeakCollectionInitialize(this);
-
-  if (IS_UNDEFINED(iter)) return;
-
-  var next, done, nextItem;
-  while (!(next = iter.next()).done) {
-    if (!IS_SPEC_OBJECT(next)) {
-      throw MakeTypeError('iterator_result_not_an_object', [next]);
-    }
-    nextItem = next.value;
-    if (!IS_SPEC_OBJECT(nextItem)) {
-      throw MakeTypeError('iterator_value_not_an_object', [nextItem]);
+    for (var nextItem of iterable) {
+      if (!IS_SPEC_OBJECT(nextItem)) {
+        throw MakeTypeError('iterator_value_not_an_object', [nextItem]);
+      }
+      %_CallFunction(this, nextItem[0], nextItem[1], adder);
     }
-    %_CallFunction(this, nextItem[0], nextItem[1], adder);
   }
 }

@@ -127,26 +116,16 @@ function WeakSetConstructor(iterable) {
     throw MakeTypeError('constructor_not_function', ['WeakSet']);
   }

-  var iter, adder;
+  %WeakCollectionInitialize(this);

   if (!IS_NULL_OR_UNDEFINED(iterable)) {
-    adder = this.add;
+    var adder = this.add;
     if (!IS_SPEC_FUNCTION(adder)) {
       throw MakeTypeError('property_not_function', ['add', this]);
     }
-    iter = GetIterator(iterable);
-  }
-
-  %WeakCollectionInitialize(this);
-
-  if (IS_UNDEFINED(iter)) return;
-
-  var next, done;
-  while (!(next = iter.next()).done) {
-    if (!IS_SPEC_OBJECT(next)) {
-      throw MakeTypeError('iterator_result_not_an_object', [next]);
+    for (var value of iterable) {
+      %_CallFunction(this, value, adder);
     }
-    %_CallFunction(this, next.value, adder);
   }
 }



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