Skip to content

Commit 158d02e

Browse files
Add CloneAsUint8Array helper
1 parent 7f3fdc9 commit 158d02e

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

index.bs

+17-6
Original file line numberDiff line numberDiff line change
@@ -2341,8 +2341,8 @@ create them does not matter.
23412341
1. [=Queue a microtask=] to perform the following steps:
23422342
1. Set |reading| to false.
23432343
1. Let |chunk1| and |chunk2| be |chunk|.
2344-
1. If |canceled1| is false and |canceled2| is false, set |chunk2| to !
2345-
[$StructuredDeserialize$](? [$StructuredSerialize$](|chunk|), [=the current Realm=]).
2344+
1. If |canceled1| is false and |canceled2| is false, set |chunk2| to ?
2345+
[$CloneAsUint8Array$](|chunk|).
23462346
1. If |canceled1| is false, perform !
23472347
[$ReadableByteStreamControllerEnqueue$](|branch1|.[=ReadableStream/[[controller]]=],
23482348
|chunk1|).
@@ -2387,8 +2387,7 @@ create them does not matter.
23872387
1. Set |reading| to false.
23882388
1. If |forBranch2| is true,
23892389
1. If |canceled1| is false,
2390-
1. Let |clonedChunk| be ! [$StructuredDeserialize$](? [$StructuredSerialize$](|chunk|),
2391-
[=the current Realm=]).
2390+
1. Let |clonedChunk| be ? [$CloneAsUint8Array$](|chunk|).
23922391
1. Perform ! [$ReadableByteStreamControllerEnqueue$](|branch1|.[=ReadableStream/[[controller]]=],
23932392
|clonedChunk|).
23942393
1. If |canceled2| is true,
@@ -2398,8 +2397,7 @@ create them does not matter.
23982397
|chunk|).
23992398
1. Otherwise,
24002399
1. If |canceled2| is false,
2401-
1. Let |clonedChunk| be ! [$StructuredDeserialize$](? [$StructuredSerialize$](|chunk|),
2402-
[=the current Realm=]).
2400+
1. Let |clonedChunk| be ? [$CloneAsUint8Array$](|chunk|).
24032401
1. Perform ! [$ReadableByteStreamControllerEnqueue$](|branch2|.[=ReadableStream/[[controller]]=],
24042402
|clonedChunk|).
24052403
1. If |canceled1| is true,
@@ -6324,6 +6322,19 @@ The following abstract operations are a grab-bag of utilities.
63246322
\[[ArrayBufferByteLength]] internal slot value is |arrayBufferByteLength|.
63256323
</div>
63266324

6325+
<div algorithm>
6326+
<dfn abstract-op lt="CloneAsUint8Array"
6327+
id="clone-as-uint8-array">CloneAsUint8Array(|O|)</dfn> performs the following steps:
6328+
6329+
1. Assert: [$Type$](|O|) is Object.
6330+
1. Assert: |O| has an \[[ViewedArrayBuffer]] internal slot.
6331+
1. Assert: ! [$IsDetachedBuffer$](|O|.\[[ViewedArrayBuffer]]) is false.
6332+
1. Let |buffer| be ? [$CloneArrayBuffer$](|O|.\[[ViewedArrayBuffer]], |O|.\[[ByteOffset]],
6333+
|O|.\[[ByteLength]], {{%ArrayBuffer%}}).
6334+
1. Let |array| be ! [$Construct$]({{%Uint8Array%}}, « |buffer| »).
6335+
1. Return |array|.
6336+
</div>
6337+
63276338
<h2 id="other-specs">Using streams in other specifications</h2>
63286339

63296340
Much of this standard concerns itself with the internal machinery of streams. Other specifications

reference-implementation/lib/abstract-ops/miscellaneous.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exports.IsNonNegativeNumber = v => {
1616
return true;
1717
};
1818

19-
// Implements StructuredDeserialize(StructuredSerialize(view)), but only for typed arrays and DataViews
20-
exports.CloneArrayBufferView = view => {
21-
return new view.constructor(view.buffer.slice(), view.byteOffset, view.byteLength);
19+
exports.CloneAsUint8Array = O => {
20+
const buffer = O.buffer.slice(O.byteOffset, O.byteOffset + O.byteLength);
21+
return new Uint8Array(buffer);
2222
};

reference-implementation/lib/abstract-ops/readable-streams.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { promiseResolvedWith, promiseRejectedWith, newPromise, resolvePromise, re
55
setPromiseIsHandledToTrue, waitForAllPromise, transformPromiseWith, uponFulfillment, uponRejection } =
66
require('../helpers/webidl.js');
77
const { CopyDataBlockBytes, CreateArrayFromList, TransferArrayBuffer } = require('./ecmascript.js');
8-
const { CloneArrayBufferView, IsNonNegativeNumber } = require('./miscellaneous.js');
8+
const { CloneAsUint8Array, IsNonNegativeNumber } = require('./miscellaneous.js');
99
const { EnqueueValueWithSize, ResetQueue } = require('./queue-with-sizes.js');
1010
const { AcquireWritableStreamDefaultWriter, IsWritableStreamLocked, WritableStreamAbort,
1111
WritableStreamDefaultWriterCloseWithErrorPropagation, WritableStreamDefaultWriterRelease,
@@ -501,7 +501,7 @@ function ReadableByteStreamTee(stream) {
501501
const chunk1 = chunk;
502502
let chunk2 = chunk;
503503
if (canceled1 === false && canceled2 === false) {
504-
chunk2 = CloneArrayBufferView(chunk);
504+
chunk2 = CloneAsUint8Array(chunk);
505505
}
506506

507507
if (canceled1 === false) {
@@ -556,7 +556,7 @@ function ReadableByteStreamTee(stream) {
556556

557557
if (forBranch2 === true) {
558558
if (canceled1 === false) {
559-
const clonedChunk = CloneArrayBufferView(chunk);
559+
const clonedChunk = CloneAsUint8Array(chunk);
560560
ReadableByteStreamControllerEnqueue(branch1._controller, clonedChunk);
561561
}
562562
if (canceled2 === true) {
@@ -565,7 +565,7 @@ function ReadableByteStreamTee(stream) {
565565
ReadableByteStreamControllerRespondWithNewView(branch2._controller, chunk);
566566
} else {
567567
if (canceled2 === false) {
568-
const clonedChunk = CloneArrayBufferView(chunk);
568+
const clonedChunk = CloneAsUint8Array(chunk);
569569
ReadableByteStreamControllerEnqueue(branch2._controller, clonedChunk);
570570
}
571571
if (canceled1 === true) {

0 commit comments

Comments
 (0)