Skip to content

Commit 49d7d5a

Browse files
committed
TA.filter Symbol.species returning an instance backed by an immutable buffer
1 parent 755d54d commit 49d7d5a

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (C) 2025 Richard Gibson. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-%typedarray%.prototype.filter
6+
description: >
7+
Throws a TypeError exception when the receiver constructs an instance backed
8+
by an immutable buffer
9+
info: |
10+
%TypedArray%.prototype.filter ( callback [ , thisArg ] )
11+
...
12+
8. Repeat, while k < len,
13+
a. Let Pk be ! ToString(𝔽(k)).
14+
b. Let kValue be ! Get(O, Pk).
15+
c. Let selected be ToBoolean(? Call(callback, thisArg, « kValue, 𝔽(k), O »)).
16+
d. If selected is true, then
17+
i. Append kValue to kept.
18+
ii. Set captured to captured + 1.
19+
e. Set k to k + 1.
20+
9. Let A be ? TypedArraySpeciesCreate(O, « 𝔽(captured) », ~write~).
21+
10. Assert: IsImmutableBuffer(A.[[ViewedArrayBuffer]]) is false.
22+
23+
TypedArraySpeciesCreate ( exemplar, argumentList [ , accessMode ] )
24+
1. If accessMode is not present, set accessMode to ~read~.
25+
2. Let defaultConstructor be the intrinsic object associated with the constructor name exemplar.[[TypedArrayName]] in Table 73.
26+
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
27+
4. Let result be ? TypedArrayCreateFromConstructor(constructor, argumentList, accessMode).
28+
29+
TypedArrayCreateFromConstructor ( constructor, argumentList [ , accessMode ] )
30+
1. If accessMode is not present, set accessMode to ~read~.
31+
2. Let newTypedArray be ? Construct(constructor, argumentList).
32+
3. Let taRecord be ? ValidateTypedArray(newTypedArray, seq-cst, accessMode).
33+
34+
ValidateTypedArray ( O, order [ , accessMode ] )
35+
1. If accessMode is not present, set accessMode to read.
36+
2. Perform ? RequireInternalSlot(O, [[TypedArrayName]]).
37+
3. Assert: O has a [[ViewedArrayBuffer]] internal slot.
38+
4. If accessMode is ~write~ and IsImmutableBuffer(O.[[ViewedArrayBuffer]]) is true, throw a TypeError exception.
39+
features: [TypedArray, immutable-arraybuffer]
40+
includes: [testTypedArray.js, compareArray.js]
41+
---*/
42+
43+
testWithAllTypedArrayConstructors((TA, makeCtorArg) => {
44+
var calls = [];
45+
46+
var ta = new TA(makeCtorArg(["1", "2"]));
47+
var constructor = {};
48+
Object.defineProperty(ta, "constructor", {
49+
get: function() {
50+
calls.push("get ta.constructor");
51+
return constructor;
52+
}
53+
});
54+
Object.defineProperty(constructor, Symbol.species, {
55+
get: function() {
56+
calls.push("get ta.constructor[Symbol.species]");
57+
var iab = (new TA(["3", "4"])).buffer.transferToImmutable();
58+
var result = new TA(iab);
59+
calls.push("construct result");
60+
return result;
61+
}
62+
});
63+
64+
assert.throws(TypeError, function() {
65+
ta.filter(function(value, index) {
66+
calls.push("filter index " + index);
67+
return !index;
68+
});
69+
});
70+
var expectCalls = [
71+
"filter index 0",
72+
"filter index 1",
73+
"get ta.constructor",
74+
"get ta.constructor[Symbol.species]",
75+
"construct result"
76+
];
77+
assert.compareArray(calls, expectCalls, "Must visit elements before constructing the result.");
78+
});

0 commit comments

Comments
 (0)