Skip to content

Commit 6a1bf36

Browse files
committed
Add test verifying that iterator helpers do not close non-object receivers.
1 parent 5fe838c commit 6a1bf36

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
6+
*/
7+
8+
/**
9+
* Iterator helpers should only close the receiver if it is an Object.
10+
*
11+
* @option iterator-helpers
12+
*/
13+
14+
load("./assert.js")
15+
16+
let returnCalled = 0;
17+
Number.prototype.return = function() { returnCalled++ };
18+
19+
for (const iteratorMethod of [
20+
Iterator.prototype.drop,
21+
Iterator.prototype.take,
22+
]) {
23+
returnCalled = 0;
24+
assertThrows(() => iteratorMethod.call(42), TypeError, "not an Object");
25+
assertSame(0, returnCalled);
26+
assertThrows(() => iteratorMethod.call(new Number(42)), RangeError);
27+
assertSame(1, returnCalled);
28+
}
29+
30+
for (const iteratorMethod of [
31+
Iterator.prototype.filter,
32+
Iterator.prototype.map,
33+
Iterator.prototype.flatMap,
34+
Iterator.prototype.reduce,
35+
Iterator.prototype.find,
36+
Iterator.prototype.some,
37+
Iterator.prototype.every,
38+
Iterator.prototype.forEach,
39+
]) {
40+
returnCalled = 0;
41+
assertThrows(() => iteratorMethod.call(42, function() {}), TypeError, "not an Object");
42+
assertSame(0, returnCalled);
43+
assertThrows(() => iteratorMethod.call(42), TypeError, "not an Object");
44+
assertSame(0, returnCalled);
45+
assertThrows(() => iteratorMethod.call(new Number(42)), TypeError, "not a function");
46+
assertSame(1, returnCalled);
47+
}

0 commit comments

Comments
 (0)