Skip to content

Commit 24ad7ea

Browse files
slowcheetahzloirock
authored andcommitted
Iterator close on argument validation error
1 parent cf6aa2c commit 24ad7ea

File tree

6 files changed

+44
-6
lines changed

6 files changed

+44
-6
lines changed

packages/core-js/modules/esnext.iterator.chunks.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var anObject = require('../internals/an-object');
44
var call = require('../internals/function-call');
55
var createIteratorProxy = require('../internals/iterator-create-proxy');
66
var getIteratorDirect = require('../internals/get-iterator-direct');
7+
var iteratorClose = require('../internals/iterator-close');
78
var uncurryThis = require('../internals/function-uncurry-this');
89

910
var $RangeError = RangeError;
@@ -34,7 +35,7 @@ $({ target: 'Iterator', proto: true, real: true, forced: true }, {
3435
chunks: function chunks(chunkSize) {
3536
var O = anObject(this);
3637
if (!chunkSize || chunkSize >>> 0 !== chunkSize) {
37-
throw $RangeError('chunkSize must be integer in [1, 2^32-1]');
38+
return iteratorClose(O, 'throw', new $RangeError('chunkSize must be integer in [1, 2^32-1]'));
3839
}
3940
return new IteratorProxy(getIteratorDirect(O), {
4041
chunkSize: chunkSize

packages/core-js/modules/esnext.iterator.windows.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var anObject = require('../internals/an-object');
44
var call = require('../internals/function-call');
55
var createIteratorProxy = require('../internals/iterator-create-proxy');
66
var getIteratorDirect = require('../internals/get-iterator-direct');
7+
var iteratorClose = require('../internals/iterator-close');
78
var uncurryThis = require('../internals/function-uncurry-this');
89

910
var $RangeError = RangeError;
@@ -35,7 +36,7 @@ $({ target: 'Iterator', proto: true, real: true, forced: true }, {
3536
windows: function windows(windowSize) {
3637
var O = anObject(this);
3738
if (!windowSize || windowSize >>> 0 !== windowSize) {
38-
throw $RangeError('windowSize must be integer in [1, 2^32-1]');
39+
return iteratorClose(O, 'throw', new $RangeError('windowSize must be integer in [1, 2^32-1]'));
3940
}
4041
return new IteratorProxy(getIteratorDirect(O), {
4142
windowSize: windowSize,

tests/unit-global/esnext.iterator.chunks.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,14 @@ QUnit.test('Iterator#chunks', assert => {
3131

3232
assert.throws(() => chunks.call(it), RangeError, 'throws on empty argument');
3333
assert.throws(() => chunks.call(it, -1), RangeError, 'throws on negative argument');
34-
assert.throws(() => chunks.call(it, 0x100000000), RangeError, 'throws on argument more then 2^32 - 1');
34+
35+
const observableReturn = {
36+
return() {
37+
this.called = true;
38+
return { done: true, value: undefined };
39+
},
40+
};
41+
const itObservable = createIterator([1, 2, 3], observableReturn);
42+
assert.throws(() => chunks.call(itObservable, 0x100000000), RangeError, 'throws on argument more then 2^32 - 1');
43+
assert.true(itObservable.called, 'iterator closed on argument validation error');
3544
});

tests/unit-global/esnext.iterator.windows.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,14 @@ QUnit.test('Iterator#windows', assert => {
3131

3232
assert.throws(() => windows.call(it), RangeError, 'throws on empty argument');
3333
assert.throws(() => windows.call(it, -1), RangeError, 'throws on negative argument');
34-
assert.throws(() => windows.call(it, 0x100000000), RangeError, 'throws on argument more then 2^32 - 1');
34+
35+
const observableReturn = {
36+
return() {
37+
this.called = true;
38+
return { done: true, value: undefined };
39+
},
40+
};
41+
const itObservable = createIterator([1, 2, 3], observableReturn);
42+
assert.throws(() => windows.call(itObservable, 0x100000000), RangeError, 'throws on argument more then 2^32 - 1');
43+
assert.true(itObservable.called, 'iterator closed on argument validation error');
3544
});

tests/unit-pure/esnext.iterator.chunks.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,14 @@ QUnit.test('Iterator#chunks', assert => {
3030

3131
assert.throws(() => chunks.call(it), RangeError, 'throws on empty argument');
3232
assert.throws(() => chunks.call(it, -1), RangeError, 'throws on negative argument');
33-
assert.throws(() => chunks.call(it, 0x100000000), RangeError, 'throws on argument more then 2^32 - 1');
33+
34+
const observableReturn = {
35+
return() {
36+
this.called = true;
37+
return { done: true, value: undefined };
38+
},
39+
};
40+
const itObservable = createIterator([1, 2, 3], observableReturn);
41+
assert.throws(() => chunks.call(itObservable, 0x100000000), RangeError, 'throws on argument more then 2^32 - 1');
42+
assert.true(itObservable.called, 'iterator closed on argument validation error');
3443
});

tests/unit-pure/esnext.iterator.windows.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,14 @@ QUnit.test('Iterator#windows', assert => {
3030

3131
assert.throws(() => windows.call(it), RangeError, 'throws on empty argument');
3232
assert.throws(() => windows.call(it, -1), RangeError, 'throws on negative argument');
33-
assert.throws(() => windows.call(it, 0x100000000), RangeError, 'throws on argument more then 2^32 - 1');
33+
34+
const observableReturn = {
35+
return() {
36+
this.called = true;
37+
return { done: true, value: undefined };
38+
},
39+
};
40+
const itObservable = createIterator([1, 2, 3], observableReturn);
41+
assert.throws(() => windows.call(itObservable, 0x100000000), RangeError, 'throws on argument more then 2^32 - 1');
42+
assert.true(itObservable.called, 'iterator closed on argument validation error');
3443
});

0 commit comments

Comments
 (0)