Skip to content

Commit c5c7a89

Browse files
committed
proposal-arraybuffer-base64 Uint8Array.prototype.set* methods backed by an immutable ArrayBuffer
1 parent 3028379 commit c5c7a89

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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-uint8array.prototype.setfrombase64
6+
description: Throws a TypeError exception when the backing buffer is immutable
7+
info: |
8+
Uint8Array.prototype.setFromBase64 ( string [ , options ] )
9+
1. Let into be the this value.
10+
2. Perform ? ValidateUint8Array(into).
11+
3. If string is not a String, throw a TypeError exception.
12+
4. Let opts be ? GetOptionsObject(options).
13+
5. Let alphabet be ? Get(opts, "alphabet").
14+
6. If alphabet is undefined, set alphabet to "base64".
15+
7. If alphabet is neither "base64" nor "base64url", throw a TypeError exception.
16+
8. Let lastChunkHandling be ? Get(opts, "lastChunkHandling").
17+
features: [TypedArray, uint8array-base64, immutable-arraybuffer]
18+
includes: [testTypedArray.js, compareArray.js]
19+
---*/
20+
21+
testWithAllTypedArrayConstructors((TA, makeCtorArg) => {
22+
var calls = [];
23+
24+
var ta = new TA(makeCtorArg(["1", "2", "3", "4"]));
25+
var options = {
26+
get alphabet() {
27+
calls.push("get options.alphabet");
28+
return undefined;
29+
},
30+
get lastChunkHandling() {
31+
calls.push("get options.lastChunkHandling");
32+
return undefined;
33+
},
34+
};
35+
36+
assert.throws(TypeError, function() {
37+
ta.setFromBase64("Zm9v", options);
38+
});
39+
assert.compareArray(calls, [], "Must verify mutability before reading arguments.");
40+
assert.compareArray(ta, new TA(["1", "2", "3", "4"]), "Must not mutate contents.");
41+
}, [Uint8Array], ["immutable"]);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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-uint8array.prototype.setfromhex
6+
description: Throws a TypeError exception when the backing buffer is immutable
7+
info: |
8+
Uint8Array.prototype.setFromHex ( string )
9+
1. Let into be the this value.
10+
2. Perform ? ValidateUint8Array(into).
11+
features: [TypedArray, uint8array-base64, immutable-arraybuffer]
12+
includes: [testTypedArray.js, compareArray.js]
13+
---*/
14+
15+
testWithAllTypedArrayConstructors((TA, makeCtorArg) => {
16+
var calls = [];
17+
18+
var ta = new TA(makeCtorArg(["1", "2", "3", "4"]));
19+
20+
assert.throws(TypeError, function() {
21+
ta.setFromHex("666f6f");
22+
});
23+
assert.compareArray(calls, [], "Must verify mutability.");
24+
assert.compareArray(ta, new TA(["1", "2", "3", "4"]), "Must not mutate contents.");
25+
}, [Uint8Array], ["immutable"]);

0 commit comments

Comments
 (0)