[heycam/webidl] Is a TypedArray a sequence? (#868)

Consider the following from File API

> To process blob parts given a sequence of BlobPart's parts and BlobPropertyBag options, run the following steps:
>     [...]
>     2. For each element in parts:
>           [...]
>           2. If element is a [BufferSource](https://heycam.github.io/webidl/#BufferSource), get a copy of the bytes held by the buffer source, and append those bytes to bytes.

and this repository description of sequence

> [2.13.27. Sequence types — sequence<T>](https://heycam.github.io/webidl/#idl-sequence)
> The sequence<T> type is a parameterized type whose values are (possibly zero-length) lists of values of type T.
> 
> Sequences are always passed by value. In language bindings where a sequence is represented by an object of some kind, passing a sequence to a platform object will not result in a reference to the sequence being kept by that object. Similarly, any sequence returned from a platform object will be a copy and modifications made to it will not be visible to the platform object.
> 
> The literal syntax for lists may also be used to represent sequences, when it is implicitly understood from context that the list is being treated as a sequences. However, there is no way to represent a constant sequence value inside IDL fragments.
> 
> Sequences must not be used as the type of an attribute or constant.
> 
> Note: This restriction exists so that it is clear to specification writers and API users that sequences are copied rather than having references to them passed around. Instead of a writable attribute of a sequence type, it is suggested that a pair of operations to get and set the sequence is used.
> 
> The type name of a sequence type is the concatenation of the type name for T and the string "Sequence".
> 
> Any list can be implicitly treated as a sequence<T>, as long as it contains only items that are of type T.

It is not immediately clear if a JavaScript `TypedArray` is considered a **_sequence_** either in File API or in this specification.

When `[]` is not used in `Blob` constructor and a `TypedArray` is passed the values of the `TypedArray` are converted to string, resulting in a `RangeError` when attempting to convert the `ArrayBuffer` representation of the `Blob` back to a `TypedArray`

```
var floats = new Float32Array([0.00005549501292989589, 0.00006459458381868899, 0.000058644378441385925, 0.00006201512587722391]);
var file = new Blob(floats); // pass TypedArray
file.arrayBuffer().then(b =>  console.log(new Float32Array(b))).catch(console.error);
// Chromium error message
RangeError: byte length of Float32Array should be a multiple of 4
    at new Float32Array (<anonymous>)
// Nightly error message
RangeError: "attempting to construct out-of-bounds TypedArray on ArrayBuffer"
// why?
file.text().then(console.log).catch(console.error);
Promise {<pending>}
0.000055495012929895890.000064594583818688990.0000586443784413859250.00006201512587722391
```

the `RangeError` can be avoided and conversion back to `TypedArray` from `ArrayBuffer` is possible when the value passed at `Blob` constructor is within `[]`

```
var floats = new Float32Array([0.00005549501292989589, 0.00006459458381868899, 0.000058644378441385925, 0.00006201512587722391]);
var file = new Blob([floats]); // pass TypedArray within []
file.arrayBuffer().then(b =>  console.log(new Float32Array(b))).catch(console.error);
Promise {<pending>}
Float32Array(4) [0.00005549501292989589, 0.00006459458381868899, 0.000058644378441385925, 0.00006201512587722391]
// why?
file.text().then(console.log).catch(console.error);
Promise {<pending>}
Q�h8�v�8��u8���8
```

If a `TypedArray` is already a sequence it should not be necessary to wrap the `TypedArray` in array literal in `Blob` constructor to avoid the `TypedArray` values being converted to a string.

See https://github.com/w3c/FileAPI/issues/147#issuecomment-602306894 and the test cases that follow at https://github.com/w3c/FileAPI/issues/147#issuecomment-605553187.


Is a `TypedArray` a **_sequence_**? 

Is this working as intended? 

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/heycam/webidl/issues/868

Received on Sunday, 5 April 2020 16:58:25 UTC