Skip to content

Commit 91a9452

Browse files
committed
TA.map Symbol.species returning an instance backed by an immutable buffer
1 parent 49d7d5a commit 91a9452

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.map
6+
description: >
7+
Throws a TypeError exception when the receiver constructs an instance backed
8+
by an immutable buffer
9+
info: |
10+
%TypedArray%.prototype.map ( callback [ , thisArg ] )
11+
1. Let O be the this value.
12+
2. Let taRecord be ? ValidateTypedArray(O, seq-cst).
13+
3. Let len be TypedArrayLength(taRecord).
14+
4. If IsCallable(callback) is false, throw a TypeError exception.
15+
5. Let A be ? TypedArraySpeciesCreate(O, « 𝔽(len) », ~write~).
16+
6. Assert: IsImmutableBuffer(A.[[ViewedArrayBuffer]]) is false.
17+
18+
TypedArraySpeciesCreate ( exemplar, argumentList [ , accessMode ] )
19+
1. If accessMode is not present, set accessMode to ~read~.
20+
2. Let defaultConstructor be the intrinsic object associated with the constructor name exemplar.[[TypedArrayName]] in Table 73.
21+
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
22+
4. Let result be ? TypedArrayCreateFromConstructor(constructor, argumentList, accessMode).
23+
24+
TypedArrayCreateFromConstructor ( constructor, argumentList [ , accessMode ] )
25+
1. If accessMode is not present, set accessMode to ~read~.
26+
2. Let newTypedArray be ? Construct(constructor, argumentList).
27+
3. Let taRecord be ? ValidateTypedArray(newTypedArray, seq-cst, accessMode).
28+
29+
ValidateTypedArray ( O, order [ , accessMode ] )
30+
1. If accessMode is not present, set accessMode to read.
31+
2. Perform ? RequireInternalSlot(O, [[TypedArrayName]]).
32+
3. Assert: O has a [[ViewedArrayBuffer]] internal slot.
33+
4. If accessMode is ~write~ and IsImmutableBuffer(O.[[ViewedArrayBuffer]]) is true, throw a TypeError exception.
34+
features: [TypedArray, immutable-arraybuffer]
35+
includes: [testTypedArray.js, compareArray.js]
36+
---*/
37+
38+
testWithAllTypedArrayConstructors((TA, makeCtorArg) => {
39+
var calls = [];
40+
41+
var ta = new TA(makeCtorArg(["1", "2"]));
42+
var constructor = {};
43+
Object.defineProperty(ta, "constructor", {
44+
get: function() {
45+
calls.push("get ta.constructor");
46+
return constructor;
47+
}
48+
});
49+
Object.defineProperty(constructor, Symbol.species, {
50+
get: function() {
51+
calls.push("get ta.constructor[Symbol.species]");
52+
var iab = (new TA(["3", "4"])).buffer.transferToImmutable();
53+
var result = new TA(iab);
54+
calls.push("construct result");
55+
return result;
56+
}
57+
});
58+
59+
assert.throws(TypeError, function() {
60+
ta.map(function(value, index) {
61+
calls.push("map index " + index);
62+
return value + value;
63+
});
64+
});
65+
var expectCalls = [
66+
"get ta.constructor",
67+
"get ta.constructor[Symbol.species]",
68+
"construct result"
69+
];
70+
assert.compareArray(calls, expectCalls, "Must construct the result before visiting elements.");
71+
});

0 commit comments

Comments
 (0)