Skip to content

Commit

Permalink
Make sure that all Set shim forms properly add an iterable to the `…
Browse files Browse the repository at this point in the history
…Set` instance.
  • Loading branch information
ljharb committed Jun 14, 2015
1 parent a0e867d commit 45e9de3
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -2172,6 +2172,37 @@
}
}
};
var addIterableToSet = function addIterableToSet(SetConstructor, set, iterable) {
if (Array.isArray(iterable) || Type.string(iterable)) {
_forEach(iterable, function (value) {
set.add(value);
});
} else if (iterable instanceof SetConstructor) {
_call(SetConstructor.prototype.forEach, iterable, function (value) {
set.add(value);
});
} else {
var iter, adder;
if (iterable !== null && typeof iterable !== 'undefined') {
adder = set.add;
if (!ES.IsCallable(adder)) { throw new TypeError('bad set'); }
iter = ES.GetIterator(iterable);
}
if (typeof iter !== 'undefined') {
while (true) {
var next = ES.IteratorStep(iter);
if (next === false) { break; }
var nextValue = next.value;
try {
_call(adder, set, nextValue);
} catch (e) {
ES.IteratorClose(iter, true);
throw e;
}
}
}
}
};

var collectionShims = {
Map: (function () {
Expand Down Expand Up @@ -2445,25 +2476,8 @@
}

// Optionally initialize Set from iterable
var iterable = (arguments.length > 0) ? arguments[0] : void 0;
var iter, adder;
if (iterable !== null && iterable !== void 0) {
adder = set.add;
if (!ES.IsCallable(adder)) { throw new TypeError('bad set'); }
iter = ES.GetIterator(iterable);
}
if (iter !== void 0) {
while (true) {
var next = ES.IteratorStep(iter);
if (next === false) { break; }
var nextValue = next.value;
try {
_call(adder, set, nextValue);
} catch (e) {
ES.IteratorClose(iter, true);
throw e;
}
}
if (arguments.length > 0) {
addIterableToSet(Set, set, arguments[0]);
}
return set;
};
Expand Down Expand Up @@ -2707,7 +2721,10 @@
if (!(this instanceof Set)) {
throw new TypeError('Constructor Set requires "new"');
}
var s = arguments.length > 0 ? new OrigSet(arguments[0]) : new OrigSet();
var s = new OrigSet();
if (arguments.length > 0) {
addIterableToSet(Set, s, arguments[0]);
}
Object.setPrototypeOf(s, Set.prototype);
defineProperty(s, 'constructor', Set, true);
return s;
Expand Down

0 comments on commit 45e9de3

Please sign in to comment.