Skip to content

Commit abc22b5

Browse files
authored
Add tests for not calling well-known symbols on primitives for string methods (#4404)
* Add tests for not calling WK symbols on primitives for string methods * clean up tests * fix info in test files
1 parent f250da9 commit abc22b5

24 files changed

+720
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (C) 2025 Luca Casonato. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-string.prototype.match
6+
description: >
7+
If a regexp property is a bigint primitive, its Symbol.match property is not accessed.
8+
info: |
9+
String.prototype.match ( regexp )
10+
11+
[...]
12+
2. If regexp is not Object, then
13+
[...]
14+
[...]
15+
16+
includes: [compareArray.js]
17+
features: [Symbol.match]
18+
---*/
19+
20+
Object.defineProperty(BigInt.prototype, Symbol.match, {
21+
get: function() {
22+
throw new Test262Error("should not be called");
23+
},
24+
});
25+
26+
var separator = 1n;
27+
28+
const matched = "a1b1c".match(separator);
29+
assert.sameValue(matched.index, 1);
30+
assert.sameValue(matched.input, "a1b1c");
31+
assert.compareArray(matched, ["1"]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (C) 2025 Luca Casonato. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-string.prototype.match
6+
description: >
7+
If a regexp property is a boolean primitive, its Symbol.match property is not accessed.
8+
info: |
9+
String.prototype.match ( regexp )
10+
11+
[...]
12+
2. If regexp is not Object, then
13+
[...]
14+
[...]
15+
16+
includes: [compareArray.js]
17+
features: [Symbol.match]
18+
---*/
19+
20+
Object.defineProperty(Boolean.prototype, Symbol.match, {
21+
get: function() {
22+
throw new Test262Error("should not be called");
23+
},
24+
});
25+
26+
var separator = true;
27+
28+
const matched = "atruebtruec".match(separator);
29+
assert.sameValue(matched.index, 1);
30+
assert.sameValue(matched.input, "atruebtruec");
31+
assert.compareArray(matched, ["true"]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (C) 2025 Luca Casonato. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-string.prototype.match
6+
description: >
7+
If a regexp property is a number primitive, its Symbol.match property is not accessed.
8+
info: |
9+
String.prototype.match ( regexp )
10+
11+
[...]
12+
2. If regexp is not Object, then
13+
[...]
14+
[...]
15+
16+
includes: [compareArray.js]
17+
features: [Symbol.match]
18+
---*/
19+
20+
Object.defineProperty(Number.prototype, Symbol.match, {
21+
get: function() {
22+
throw new Test262Error("should not be called");
23+
},
24+
});
25+
26+
var separator = 1;
27+
28+
const matched = "a1b1c".match(separator);
29+
assert.sameValue(matched.index, 1);
30+
assert.sameValue(matched.input, "a1b1c");
31+
assert.compareArray(matched, ["1"]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (C) 2025 Luca Casonato. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-string.prototype.match
6+
description: >
7+
If a regexp property is a string primitive, its Symbol.match property is not accessed.
8+
info: |
9+
String.prototype.match ( regexp )
10+
11+
[...]
12+
2. If regexp is not Object, then
13+
[...]
14+
[...]
15+
16+
includes: [compareArray.js]
17+
features: [Symbol.match]
18+
---*/
19+
20+
Object.defineProperty(String.prototype, Symbol.match, {
21+
get: function() {
22+
throw new Test262Error("should not be called");
23+
},
24+
});
25+
26+
var separator = ",";
27+
28+
const matched = "a,b,c".match(separator);
29+
assert.sameValue(matched.index, 1);
30+
assert.sameValue(matched.input, "a,b,c");
31+
assert.compareArray(matched, [","]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (C) 2025 Luca Casonato. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-string.prototype.matchall
6+
description: >
7+
If a regexp property is a bigint primitive, its Symbol.matchAll property is not accessed.
8+
info: |
9+
String.prototype.matchAll ( regexp )
10+
11+
[...]
12+
2. If regexp is not Object, then
13+
[...]
14+
[...]
15+
16+
includes: [compareArray.js]
17+
features: [Symbol.matchAll]
18+
---*/
19+
20+
Object.defineProperty(BigInt.prototype, Symbol.matchAll, {
21+
get: function() {
22+
throw new Test262Error("should not be called");
23+
},
24+
});
25+
26+
var matcher = 1n;
27+
28+
const matched = "a1b1c".matchAll(matcher);
29+
const matchesArray = Array.from(matched);
30+
assert.sameValue(matchesArray[0].index, 1);
31+
assert.sameValue(matchesArray[0].input, "a1b1c");
32+
assert.compareArray(matchesArray[0], ["1"]);
33+
assert.sameValue(matchesArray[1].index, 3);
34+
assert.sameValue(matchesArray[1].input, "a1b1c");
35+
assert.compareArray(matchesArray[1], ["1"]);
36+
assert.sameValue(matchesArray.length, 2);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (C) 2025 Luca Casonato. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-string.prototype.matchall
6+
description: >
7+
If a regexp property is a boolean primitive, its Symbol.matchAll property is not accessed.
8+
info: |
9+
String.prototype.matchAll ( regexp )
10+
11+
[...]
12+
2. If regexp is not Object, then
13+
[...]
14+
[...]
15+
16+
includes: [compareArray.js]
17+
features: [Symbol.matchAll]
18+
---*/
19+
20+
Object.defineProperty(Boolean.prototype, Symbol.match, {
21+
get: function() {
22+
throw new Test262Error("should not be called");
23+
},
24+
});
25+
26+
var matcher = true;
27+
28+
const matched = "atruebtruec".matchAll(matcher);
29+
const matchesArray = Array.from(matched);
30+
assert.sameValue(matchesArray[0].index, 1);
31+
assert.sameValue(matchesArray[0].input, "atruebtruec");
32+
assert.compareArray(matchesArray[0], ["true"]);
33+
assert.sameValue(matchesArray[1].index, 6);
34+
assert.sameValue(matchesArray[1].input, "atruebtruec");
35+
assert.compareArray(matchesArray[1], ["true"]);
36+
assert.sameValue(matchesArray.length, 2);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (C) 2025 Luca Casonato. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-string.prototype.matchall
6+
description: >
7+
If a regexp property is a number primitive, its Symbol.matchAll property is not accessed.
8+
info: |
9+
String.prototype.matchAll ( regexp )
10+
11+
[...]
12+
2. If regexp is not Object, then
13+
[...]
14+
[...]
15+
16+
includes: [compareArray.js]
17+
features: [Symbol.matchAll]
18+
---*/
19+
20+
Object.defineProperty(Number.prototype, Symbol.matchAll, {
21+
get: function() {
22+
throw new Test262Error("should not be called");
23+
},
24+
});
25+
26+
var matcher = 1;
27+
28+
const matched = "a1b1c".matchAll(matcher);
29+
const matchesArray = Array.from(matched);
30+
assert.sameValue(matchesArray[0].index, 1);
31+
assert.sameValue(matchesArray[0].input, "a1b1c");
32+
assert.compareArray(matchesArray[0], ["1"]);
33+
assert.sameValue(matchesArray[1].index, 3);
34+
assert.sameValue(matchesArray[1].input, "a1b1c");
35+
assert.compareArray(matchesArray[1], ["1"]);
36+
assert.sameValue(matchesArray.length, 2);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (C) 2025 Luca Casonato. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-string.prototype.matchall
6+
description: >
7+
If a regexp property is a string primitive, its Symbol.matchAll property is not accessed.
8+
info: |
9+
String.prototype.matchAll ( regexp )
10+
11+
[...]
12+
2. If regexp is not Object, then
13+
[...]
14+
[...]
15+
16+
includes: [compareArray.js]
17+
features: [Symbol.matchAll]
18+
---*/
19+
20+
Object.defineProperty(String.prototype, Symbol.matchAll, {
21+
get: function() {
22+
throw new Test262Error("should not be called");
23+
},
24+
});
25+
26+
var matcher = ",";
27+
28+
const matched = "a,b,c".matchAll(matcher);
29+
const matchesArray = Array.from(matched);
30+
assert.sameValue(matchesArray[0].index, 1);
31+
assert.sameValue(matchesArray[0].input, "a,b,c");
32+
assert.compareArray(matchesArray[0], [","]);
33+
assert.sameValue(matchesArray[1].index, 3);
34+
assert.sameValue(matchesArray[1].input, "a,b,c");
35+
assert.compareArray(matchesArray[1], [","]);
36+
assert.sameValue(matchesArray.length, 2);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (C) 2025 Luca Casonato. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-string.prototype.replace
6+
description: >
7+
If a searchValue is a bigint primitive, its Symbol.replace property is not accessed.
8+
info: |
9+
String.prototype.replace ( searchValue, replaceValue )
10+
11+
[...]
12+
2. If searchValue is not Object, then
13+
[...]
14+
[...]
15+
16+
features: [Symbol.replace]
17+
---*/
18+
19+
Object.defineProperty(BigInt.prototype, Symbol.replace, {
20+
get: function() {
21+
throw new Test262Error("should not be called");
22+
},
23+
});
24+
25+
var searchValue = 1n;
26+
27+
const replaced = "a1b1c".replace(searchValue, "X");
28+
assert.sameValue(replaced, "aXb1c");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (C) 2025 Luca Casonato. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-string.prototype.replace
6+
description: >
7+
If a searchValue is a boolean primitive, its Symbol.replace property is not accessed.
8+
info: |
9+
String.prototype.replace ( searchValue, replaceValue )
10+
11+
[...]
12+
2. If searchValue is not Object, then
13+
[...]
14+
[...]
15+
16+
features: [Symbol.replace]
17+
---*/
18+
19+
Object.defineProperty(Boolean.prototype, Symbol.replace, {
20+
get: function() {
21+
throw new Test262Error("should not be called");
22+
},
23+
});
24+
25+
var searchValue = true;
26+
27+
const replaced = "atruebtruec".replace(searchValue, "X");
28+
assert.sameValue(replaced, "aXbtruec");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (C) 2025 Luca Casonato. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-string.prototype.replace
6+
description: >
7+
If a searchValue is a number primitive, its Symbol.replace property is not accessed.
8+
info: |
9+
String.prototype.replace ( searchValue, replaceValue )
10+
11+
[...]
12+
2. If searchValue is not Object, then
13+
[...]
14+
[...]
15+
16+
features: [Symbol.replace]
17+
---*/
18+
19+
Object.defineProperty(Number.prototype, Symbol.replace, {
20+
get: function() {
21+
throw new Test262Error("should not be called");
22+
},
23+
});
24+
25+
var searchValue = 1;
26+
27+
const replaced = "a1b1c".replace(searchValue, "X");
28+
assert.sameValue(replaced, "aXb1c");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (C) 2025 Luca Casonato. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-string.prototype.replace
6+
description: >
7+
If a searchValue is a string primitive, its Symbol.replace property is not accessed.
8+
info: |
9+
String.prototype.replace ( searchValue, replaceValue )
10+
11+
[...]
12+
2. If searchValue is not Object, then
13+
[...]
14+
[...]
15+
16+
features: [Symbol.replace]
17+
---*/
18+
19+
Object.defineProperty(String.prototype, Symbol.replace, {
20+
get: function() {
21+
throw new Test262Error("should not be called");
22+
},
23+
});
24+
25+
var searchValue = ",";
26+
27+
const replaced = "a,b,c".replace(searchValue, "X");
28+
assert.sameValue(replaced, "aXb,c");

0 commit comments

Comments
 (0)