ES6 collections: Fix order of constructor logic
authorarv <arv@chromium.org>
Mon, 23 Feb 2015 18:55:38 +0000 (10:55 -0800)
committerCommit bot <commit-bot@chromium.org>
Mon, 23 Feb 2015 18:55:46 +0000 (18:55 +0000)
The adder should be gotten before the iterator.

Motivation: Once this is done we should be able to use a for-of loop
instead which leads to cleaner code and correct behavior once the
for-of loop correctly supports abrupt completion.

BUG=None
LOG=N
R=adamk

Review URL: https://codereview.chromium.org/949933002

Cr-Commit-Position: refs/heads/master@{#26803}

src/collection.js
src/weak-collection.js
test/mjsunit/es6/collections.js

index 9b57166..8d2d184 100644 (file)
@@ -23,11 +23,11 @@ function SetConstructor(iterable) {
   var iter, adder;
 
   if (!IS_NULL_OR_UNDEFINED(iterable)) {
-    iter = GetIterator(iterable);
     adder = this.add;
     if (!IS_SPEC_FUNCTION(adder)) {
       throw MakeTypeError('property_not_function', ['add', this]);
     }
+    iter = GetIterator(iterable);
   }
 
   %_SetInitialize(this);
@@ -163,11 +163,11 @@ function MapConstructor(iterable) {
   var iter, adder;
 
   if (!IS_NULL_OR_UNDEFINED(iterable)) {
-    iter = GetIterator(iterable);
     adder = this.set;
     if (!IS_SPEC_FUNCTION(adder)) {
       throw MakeTypeError('property_not_function', ['set', this]);
     }
+    iter = GetIterator(iterable);
   }
 
   %_MapInitialize(this);
index 3cec9cc..33e3d4c 100644 (file)
@@ -23,11 +23,11 @@ function WeakMapConstructor(iterable) {
   var iter, adder;
 
   if (!IS_NULL_OR_UNDEFINED(iterable)) {
-    iter = GetIterator(iterable);
     adder = this.set;
     if (!IS_SPEC_FUNCTION(adder)) {
       throw MakeTypeError('property_not_function', ['set', this]);
     }
+    iter = GetIterator(iterable);
   }
 
   %WeakCollectionInitialize(this);
@@ -130,11 +130,11 @@ function WeakSetConstructor(iterable) {
   var iter, adder;
 
   if (!IS_NULL_OR_UNDEFINED(iterable)) {
-    iter = GetIterator(iterable);
     adder = this.add;
     if (!IS_SPEC_FUNCTION(adder)) {
       throw MakeTypeError('property_not_function', ['add', this]);
     }
+    iter = GetIterator(iterable);
   }
 
   %WeakCollectionInitialize(this);
index a663014..989ded8 100644 (file)
@@ -1408,3 +1408,38 @@ TestCollectionToString(Map);
 TestCollectionToString(Set);
 TestCollectionToString(WeakMap);
 TestCollectionToString(WeakSet);
+
+
+function TestConstructorOrderOfAdderIterator(ctor, adderName) {
+  var iterable = new Map();
+  iterable.set({}, {});
+  iterable.set({}, {});
+  var iterableFunction = iterable[Symbol.iterator];
+  Object.defineProperty(iterable, Symbol.iterator, {
+    get: function() {
+      log += 'iterator';
+      return iterableFunction;
+    }
+  });
+
+  var log = '';
+  var adderFunction = ctor.prototype[adderName];
+
+  Object.defineProperty(ctor.prototype, adderName, {
+    get: function() {
+      log += adderName;
+      return adderFunction;
+    }
+  });
+
+  new ctor(iterable);
+  assertEquals(adderName + 'iterator', log);
+
+  Object.defineProperty(ctor.prototype, adderName, {
+    value: adderFunction
+  });
+}
+TestConstructorOrderOfAdderIterator(Map, 'set');
+TestConstructorOrderOfAdderIterator(Set, 'add');
+TestConstructorOrderOfAdderIterator(WeakMap, 'set');
+TestConstructorOrderOfAdderIterator(WeakSet, 'add');