Skip to content

Commit c3b2ba1

Browse files
authored
Fixes IteratorClose to throw correctly (#25)
This PR fixes `IteratorClose` to `throw` (instead of `return`) when a `ThrowCompletion` is passed to it. It also includes new tests in a few polyfills; those tests would fail, currently.
1 parent 60b564c commit c3b2ba1

File tree

22 files changed

+133
-46
lines changed

22 files changed

+133
-46
lines changed

polyfills/Array/from/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dependencies = [
2626
"_ESAbstract.ToObject",
2727
"_ESAbstract.ToLength",
2828
"_ESAbstract.Get",
29+
"_ESAbstract.ThrowCompletion",
2930
"Map",
3031
"Set"
3132
]

polyfills/Array/from/polyfill.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* globals
2-
IsCallable, GetMethod, Symbol, IsConstructor, Construct, ArrayCreate, GetIterator, IteratorClose,
2+
IsCallable, GetMethod, Symbol, IsConstructor, Construct, ArrayCreate, GetIterator, IteratorClose, ThrowCompletion,
33
ToString, IteratorStep, IteratorValue, Call, CreateDataPropertyOrThrow, ToObject, ToLength, Get, CreateMethodProperty
44
*/
55
(function () {
@@ -60,7 +60,7 @@
6060
// i. If k ≥ 2^53-1, then
6161
if (k >= (Math.pow(2, 53) - 1)) {
6262
// 1. Let error be Completion{[[Type]]: throw, [[Value]]: a newly created TypeError object, [[Target]]: empty}.
63-
var error = new TypeError('Iteration count can not be greater than or equal 9007199254740991.');
63+
var error = ThrowCompletion(new TypeError('Iteration count can not be greater than or equal 9007199254740991.'));
6464
// 2. Return ? IteratorClose(iteratorRecord, error).
6565
return IteratorClose(iteratorRecord, error);
6666
}
@@ -86,7 +86,7 @@
8686
// 2. If mappedValue is an abrupt completion, return ? IteratorClose(iteratorRecord, mappedValue).
8787
// 3. Let mappedValue be mappedValue.[[Value]].
8888
} catch (e) {
89-
return IteratorClose(iteratorRecord, e);
89+
return IteratorClose(iteratorRecord, ThrowCompletion(e));
9090
}
9191

9292
// vii. Else, let mappedValue be nextValue.
@@ -99,7 +99,7 @@
9999
CreateDataPropertyOrThrow(A, Pk, mappedValue);
100100
// ix. If defineStatus is an abrupt completion, return ? IteratorClose(iteratorRecord, defineStatus).
101101
} catch (e) {
102-
return IteratorClose(iteratorRecord, e);
102+
return IteratorClose(iteratorRecord, ThrowCompletion(e));
103103
}
104104
// x. Increase k by 1.
105105
k = k + 1;

polyfills/Array/from/tests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,12 @@ describe('throws', function () {
234234
Array.from([1, 2, 3], 3);
235235
});
236236
});
237+
238+
it("specified, mapping function throws", function () {
239+
proclaim.throws(function () {
240+
Array.from([1, 2, 3], function () {
241+
throw new Error("uh oh");
242+
});
243+
}, /uh oh/);
244+
});
237245
});

polyfills/Map/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies = [
1212
"_ESAbstract.IteratorValue",
1313
"_ESAbstract.OrdinaryCreateFromConstructor",
1414
"_ESAbstract.SameValueZero",
15+
"_ESAbstract.ThrowCompletion",
1516
"_ESAbstract.Type",
1617
"Symbol",
1718
"Symbol.iterator",

polyfills/Map/groupBy/tests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,11 @@ it("throws when callbackfn is not callable", function () {
5656
Map.groupBy(iterable, null);
5757
}, TypeError);
5858
});
59+
60+
it("throws when callbackfn throws", function () {
61+
proclaim.throws(function () {
62+
Map.groupBy(iterable, function () {
63+
throw new Error("uh oh");
64+
});
65+
}, /uh oh/);
66+
});

polyfills/Map/polyfill.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global CreateIterResultObject, CreateMethodProperty, GetIterator, IsCallable, IteratorClose, IteratorStep, IteratorValue, OrdinaryCreateFromConstructor, SameValueZero, Type, Symbol */
1+
/* global CreateIterResultObject, CreateMethodProperty, GetIterator, IsCallable, IteratorClose, IteratorStep, IteratorValue, OrdinaryCreateFromConstructor, SameValueZero, ThrowCompletion, Type, Symbol */
22
(function (global) {
33
// Need an internal counter to assign unique IDs to a key map
44
var _uniqueHashId = 0;
@@ -149,12 +149,9 @@
149149
// d. If Type(nextItem) is not Object, then
150150
if (Type(nextItem) !== 'object') {
151151
// i. Let error be Completion{[[Type]]: throw, [[Value]]: a newly created TypeError object, [[Target]]: empty}.
152-
try {
153-
throw new TypeError('Iterator value ' + nextItem + ' is not an entry object');
154-
} catch (error) {
155-
// ii. Return ? IteratorClose(iteratorRecord, error).
156-
return IteratorClose(iteratorRecord, error);
157-
}
152+
var error = ThrowCompletion(new TypeError('Iterator value ' + nextItem + ' is not an entry object'));
153+
// ii. Return ? IteratorClose(iteratorRecord, error).
154+
return IteratorClose(iteratorRecord, error);
158155
}
159156
try {
160157
// The try catch accounts for steps: f, h, and j.
@@ -169,7 +166,7 @@
169166
adder.call(map, k, v);
170167
} catch (e) {
171168
// j. If status is an abrupt completion, return ? IteratorClose(iteratorRecord, status).
172-
return IteratorClose(iteratorRecord, e);
169+
return IteratorClose(iteratorRecord, ThrowCompletion(e));
173170
}
174171
}
175172
} catch (e) {

polyfills/Object/groupBy/tests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,11 @@ it("throws when callbackfn is not callable", function () {
5454
Object.groupBy(iterable, null);
5555
}, TypeError);
5656
});
57+
58+
it("throws when callbackfn throws", function () {
59+
proclaim.throws(function () {
60+
Object.groupBy(iterable, function () {
61+
throw new Error("uh oh");
62+
});
63+
}, /uh oh/);
64+
});

polyfills/Set/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies = [
1212
"_ESAbstract.IteratorValue",
1313
"_ESAbstract.OrdinaryCreateFromConstructor",
1414
"_ESAbstract.SameValueZero",
15+
"_ESAbstract.ThrowCompletion",
1516
"Symbol",
1617
"Symbol.iterator",
1718
"Symbol.species",

polyfills/Set/polyfill.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global CreateIterResultObject, CreateMethodProperty, GetIterator, IsCallable, IteratorClose, IteratorStep, IteratorValue, OrdinaryCreateFromConstructor, SameValueZero, Symbol */
1+
/* global CreateIterResultObject, CreateMethodProperty, GetIterator, IsCallable, IteratorClose, IteratorStep, IteratorValue, OrdinaryCreateFromConstructor, SameValueZero, Symbol, ThrowCompletion */
22
(function (global) {
33
// Deleted set items mess with iterator pointers, so rather than removing them mark them as deleted. Can't use undefined or null since those both valid keys so use a private symbol.
44
var undefMarker = Symbol('undef');
@@ -52,7 +52,7 @@
5252
adder.call(set, nextValue);
5353
} catch (e) {
5454
// e. If status is an abrupt completion, return ? IteratorClose(iteratorRecord, status).
55-
return IteratorClose(iteratorRecord, e);
55+
return IteratorClose(iteratorRecord, ThrowCompletion(e));
5656
}
5757
}
5858
} catch (e) {

polyfills/WeakMap/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies = [
1212
"_ESAbstract.IsArray",
1313
"_ESAbstract.Type",
1414
"_ESAbstract.SameValue",
15+
"_ESAbstract.ThrowCompletion",
1516
"Symbol",
1617
"Symbol.toStringTag",
1718
]

0 commit comments

Comments
 (0)