[47] [DFN[[RUBYB[型付き配列]@en[typed array]]]]は、
[[バイナリーデータ]] ([[バイト列]]) を[[配列]]と似た [[API]]
で扱うことができる [[JavaScript]] の[[クラス]]群です。

* 仕様書

[REFS[
- [69] [CITE[ECMAScript® 2018 Language Specification]] ([TIME[2017-06-14 04:16:40 +09:00]]) <https://tc39.github.io/ecma262/#sec-typedarray-objects>
- [26] [CITE[Web IDL (Second Edition)]] ([TIME[2014-10-06 23:54:33 +09:00]] 版) <http://heycam.github.io/webidl/#dfn-typed-array-type>
- [39] [CITE[Web IDL (Second Edition)]] ([TIME[2014-10-06 23:54:33 +09:00]] 版) <http://heycam.github.io/webidl/#common-ArrayBufferView>
- [30] [CITE@en-GB-x-hixie[HTML Standard]] ([TIME[2014-05-23 20:05:44 +09:00]] 版) <http://www.whatwg.org/specs/web-apps/current-work/#arraybuffer>
]REFS]

* 意味

[27] [DFN[[RUBYB[型付き配列型]@en[typed array type]]]]は、次の[[型]]です [SRC[>>26]]。
[FIG(short list)[
- [CODE(DOMi)@en[[[Int8Array]]]]
- [CODE(DOMi)@en[[[Int16Array]]]]
- [CODE(DOMi)@en[[[Int32Array]]]]
- [CODE(DOMi)@en[[[Uint8Array]]]]
- [CODE(DOMi)@en[[[Uint16Array]]]]
- [CODE(DOMi)@en[[[Uint32Array]]]]
- [CODE(DOMi)@en[[[Uint8ClampedArray]]]]
- [CODE(DOMi)@en[[[Float32Array]]]]
- [CODE(DOMi)@en[[[Float64Array]]]]
]FIG]

[41] [DFN[[CODE(DOMi)@en[[[ArrayBufferView]]]]]] とは、[[型付き配列型]]または
[CODE(IDL)@en[[[DataView]]]] をいいます [SRC[>>39, >>30]]。

[31] [CODE(IDL)@en[ArrayBufferView]] は、 [CODE(IDL)@en[BufferSource]] にも含まれています。

* 状態

[71] [[型付き配列]]は、 [F(ss)[TypedArrayName]] を持ちます。
値は種別 ([CODE[Int8Array]] など) を表しています。
[[JavaScript]] の[[仕様書]]上の[[オブジェクト]]の取り扱いにおいては、
この[[内部スロット]]の有無で[[型付き配列]]かどうかを判別しています。

[72] [CODE(JS)@en[ArrayBufferView]] は、 [F(ss)[ViewedArrayBuffer]] を持ちます。
値は [CODE(JS)@en[ArrayBuffer]] です。
[[構造化クローン]]や [CITE[Streams Standard]] の[[オブジェクト]]の取り扱いにおいては、
この[[内部スロット]]の有無で [CODE(IDL)@en[ArrayBufferView]] かどうかを判別しています。

;; [29] ただの [CODE(IDL)@en[ArrayBuffer]] ではなく、
[CODE(IDL)@en[SharedArrayBuffer]] の場合もあります。

[74] [CODE(JS)@en[ArrayBufferView]] は、
[F(ss)[ByteOffset]] と [F(ss)[ByteLength]] を持ちます。それぞれ、
[F(ss)[ViewedArrayBuffer]] の[[バイト列]]における[[オフセット]]と[[長さ]]を表しています。
本 [CODE(JS)@en[ArrayBufferView]] は、[[バッファー]]上の該当範囲を表しています。

[75] [[型付き配列]]は、
[F(ss)[ArrayLength]] を持ちます。[[配列]]としての[[長さ]] ([[要素数]])
を表しています。各要素の[[バイト長]]が1バイトとは限らないので、
[F(ss)[ByteLength]] とは一致するとは限りません。

[73] 
これら[[内部スロット]]の値は、
[[コンストラクター]]内で設定され、以後変化することはありません。

* 文脈

[76] 
[CODE(JS)@en[ArrayBufferView]] の各[[クラス]]の[[インスタンス]]は、
[[コンストラクター]]により作成することができます。
[CODE(JS)@en[ArrayBuffer]]/[CODE(JS)@en[SharedArrayBuffer]]
を指定した場合は、それが [F(ss)[ViewedArrayBuffer]] に設定されます。
それ以外の場合、新しい [CODE(DOMi)@en[ArrayBuffer]] が作成されます。

[40] [[型付き配列]]の 
[CODE(DOMm)@en[subarray]],
[CODE(DOMm)@en[slice]],
[CODE(DOMm)@en[filter]],
[CODE(DOMm)@en[map]]
各[[メソッド]]は新しい[[型付き配列]]の[[インスタンス]]を作成します。
[CODE(DOMm)@en[subarray]] は同じ [CODE(JS)@en[ArrayBuffer]]/[CODE(JS)@en[SharedArrayBuffer]]
を共有することになりますが、それ以外は新しい [CODE(DOMi)@en[ArrayBuffer]]
を作成します。

[77] 
[CODE(JS)@en[ArrayBufferView]] の各[[クラス]]の[[インスタンス]]は、
[CODE(DOMi)@en[ReadableByteStreamController]] により (入力と同じ[[クラス]]のものが)
作成される場合があります。

[78] [CODE(JS)@en[Uint8Array]] の[[インスタンス]]は、
[CODE(JS)@en[ReadableByteStreamController]] により作成される場合があります。

[79] 
[CODE(DOMi)@en[Uint8ClampedArray]] の[[インスタンス]]は、
[CODE(DOMi)@en[ImageData]] [[オブジェクト]]の [CODE(DOMa)@en[data][ImageData]]
[[IDL属性]]から得られます。

[81] 
[[fetch]] の処理も [CODE(DOMi)@en[ArrayBuffer]] や
[CODE(DOMi)@en[Uint8Array]] を内部的に作成することがあります。

[45] 
[[構造化クローン]]により作成されることがあります。

* 比較

[83] 
[[TypedArray]]
が[[等しいかどうか][等価性]]の[[比較]]は、
組み込みの方法はなく、
自分で実装するほかないようです。

* 変換

[84] [CODE[ArrayBuffer]] から [[TypedArray]] を得るには、
[[TypedArray]] の[[コンストラクター]]を使うといいです。

[EG[
[PRE(js code)[
typedArray = new Uint8Array (arrayBuffer);
]PRE]
]EG]

[85] [[TypedArray]] から [CODE[Array]] を得るには、
[CODE[Array.from]] を使うといいです。

[EG[
[PRE(js code)[
array = Array.from (typedArray);
]PRE]
]EG]

* 歴史

** バイト列のない時代

[86] 
[[JavaScript]] には長らく[[バイト列]]専用の[[データ型]]がありませんでしたが、
[[バイト列]]を表す需要がないわけではありませんでした。
[[慣用句]]的な表現方法がいくつかありました。

- [87] [[JavaScript文字列]]: [[同型符号化]], [[同型復号]]を想定した
[[Unicode文字列]]の [CODE[U+0000]] - [CODE[U+00FF]]
で表す方法。
[CODE[btoa]], [CODE[atob]] が採用。
- [88] [CODE[Byte[__&&]&&__]]: [[int8]] の[[配列]]で表す方法。
[[Google Apps Script]] が採用。
- [89] [[パーセント符号化]], [[Base16]], [[Base64]] など: 
直接操作しないで右から左に転送するだけのときに使われました。


** [CODE(DOMi)@en[CanvasPixelArray]]

[52] [[Web Applications 1.0]] は [CODE(HTMLe)@en[[[canvas]]]] 
の [CODE(DOMm)@en[[[getImageData]]]] と共に 
[DFN[[CODE(DOMi)@en[[[CanvasPixelArray]]]]]] を規定しました。

** 型付き配列型

[21] [DFN[[CITE[ECMAScript Typed Array Specification]]]] ははじめ [[Khronos]] で標準化されました。

[REFS[
- [49] [CITE@en[ECMAScript Typed Array Specification]]
([TIME[2010-01-26 10:27:56 +09:00]] 版)
<http://people.mozilla.com/~vladimir/jsvec/TypedArray-spec.html>
- [1] [CITE@en[ECMAScript Typed Array Specification]]
([TIME[2010-03-04 09:01:49 +09:00]] 版)
<https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/doc/spec/TypedArray-spec.html>
- [24] [CITE@en[Typed Array Specification]]
( ([TIME[2014-04-12 22:52:49 +09:00]] 版))
<https://www.khronos.org/registry/typedarray/specs/latest/>
-- [34] [CITE@en[Typed Array Specification]]
( ([TIME[2014-04-12 22:52:49 +09:00]] 版))
<https://www.khronos.org/registry/typedarray/specs/latest/#ARRAYBUFFER>
-- [35] [CITE@en[Typed Array Specification]]
( ([TIME[2014-04-12 22:52:49 +09:00]] 版))
<https://www.khronos.org/registry/typedarray/specs/latest/#ARRAYBUFFERVIEW>
- [2] [CITE@en[Binary - CommonJS Spec Wiki]]
([TIME[2010-05-14 04:12:31 +09:00]] 版)
<http://wiki.commonjs.org/wiki/Binary>
- [3] [CITE[IRC logs: freenode / #whatwg / 20100625]]
([TIME[2010-06-29 00:39:18 +09:00]] 版)
<http://krijnhoetmer.nl/irc-logs/whatwg/20100625#l-347>
- [4] [CITE[IRC logs: freenode / #whatwg / 20100625]]
([TIME[2010-06-29 00:39:18 +09:00]] 版)
<http://krijnhoetmer.nl/irc-logs/whatwg/20100625>
- [5] [CITE[IRC logs: freenode / #whatwg / 20101212]]
( ([TIME[2011-01-03 18:17:01 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20101212>
- [6] [CITE[IRC logs: freenode / #whatwg / 20110212]]
( ([TIME[2011-03-22 22:45:03 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20110212>
- [7] [CITE['''['''whatwg''']''' ArrayBuffer and the structured clone algorithm]]
([TIME[2011-05-10 12:49:48 +09:00]] 版)
<http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-May/031518.html>
- [8] [CITE[IRC logs: freenode / #whatwg / 20110525]]
( ([TIME[2011-05-28 03:13:09 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20110525>
- [9] [CITE[IRC logs: freenode / #whatwg / 20120121]]
( ([TIME[2012-01-22 22:55:46 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20120121>
- [10] [CITE[''''''[''''''whatwg'''''']'''''' API for encoding/decoding ArrayBuffers into text]]
( ([TIME[2012-03-17 12:12:10 +09:00]] 版))
<http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2012-March/035093.html>
- [11] [CITE[''''''[''''''whatwg'''''']'''''' API for encoding/decoding ArrayBuffers into text]]
( ([TIME[2012-03-17 12:12:44 +09:00]] 版))
<http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2012-March/035094.html>
- [12] [CITE[IRC logs: freenode / #whatwg / 20120327]]
( ([TIME[2012-04-09 21:35:50 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20120327>
- [13] [CITE[IRC logs: freenode / #whatwg / 20120328]]
( ([TIME[2012-04-09 21:56:14 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20120328>
- [14] [CITE[IRC logs: freenode / #whatwg / 20120329]]
( ([TIME[2012-04-09 23:38:43 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20120329>
- [15] [CITE@en[Web Applications 1.0 r7085     You send ArrayBufferViews, not ArrayBuffers.]]
( ([TIME[2012-05-03 05:17:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=7084&to=7085>
- [16] [CITE@en[Web Applications 1.0 r7203     Four browser vendors have told me they don't want to drop this, so yeah, I'm putting it back. Sorry Anne. :-)]]
( ([TIME[2012-07-26 04:11:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=7202&to=7203>
- [17] [CITE[IRC logs: freenode / #whatwg / 20130926]]
( ([TIME[2013-09-27 17:31:03 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20130926>
]REFS]

[23] [CODE(HTMLe)@en[[[canvas]]]] [[要素]]の [CODE(DOMi)@en[[[CanvasPixelArray]]]]
は、2011年に [CODE(DOMi)@en[[[Uint8ClampedArray]]]] に変更されています。

[REFS[
- [50] [CITE[IRC logs: freenode / #whatwg / 20100203]]
([TIME[2010-02-06 01:45:27 +09:00]] 版)
<http://krijnhoetmer.nl/irc-logs/whatwg/20100203>
- [51] [CITE@en[Web Applications 1.0 r6559     Kill CanvasPixelArray in favour of the Typed Array stuff.]]
( ([TIME[2011-09-22 07:58:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=6558&to=6559>
]REFS]

@@ [54] [[エンディアン]]問題

** ES6 への統合

[22] その後 [[TypedArray]] は [[ES6]] に取り込まれました。

[53] [[Web IDL]] でも[[型付き配列型]]その他を扱うための[[データ型]]が追加されました。

[REFS[
- [18] [CITE@en-GB[ECMAScript Language Specification ECMA-262 6th Edition – DRAFT]]
( ([TIME[2014-01-24 00:08:47 +09:00]] 版))
<https://people.mozilla.org/~jorendorff/es6-draft.html#sec-typedarray-objects>
- [32] [CITE@en[Web Applications 1.0 r7972 Integrate ArrayBuffer structured cloning]]
( ([TIME[2013-06-13 08:46:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=7971&to=7972>
- [33] [CITE@en[HTML Standard Tracker]]
( ([TIME[2014-05-16 03:15:32 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=8627&to=8628>
]REFS]

[REFS[
- [19] [CITE@en[Web Applications 1.0 r8454 First hack attempt at converting from referring to the old TypedArray spec to referring to the JS spec's version. I couldn't work out how to actually do a good job of referring to all the JS algorithms, so please file bugs with proposed replacement text for anything that's poorly done.]]
( ([TIME[2014-02-04 08:16:00 +09:00]] 版))
<http://html5.org/tools/web-apps-tracker?from=8453&to=8454>
- [20] [CITE[IRC logs: freenode / #whatwg / 20140203]]
( ([TIME[2014-02-05 00:12:34 +09:00]] 版))
<http://krijnhoetmer.nl/irc-logs/whatwg/20140203>
]REFS]

[REFS[
- [25] [CITE@en[Bug 23369 – Provide hooks for Typed Arrays (ArrayBuffer and friends)]]
( ([TIME[2014-06-10 12:18:10 +09:00]] 版))
<https://www.w3.org/Bugs/Public/show_bug.cgi?id=23369>
- [28] [CITE@en[Add types for ArrayBuffer, DataView and typed array objects. · 631316b · heycam/webidl]] ([TIME[2014-10-10 08:07:12 +09:00]] 版) <https://github.com/heycam/webidl/commit/631316b8d513d414223de265b157153c032958c2>
]REFS]

[REFS[
- [36] [CITE@en[Add an ArrayBufferData typedef. · 760fa16 · heycam/webidl]]
( ([TIME[2014-09-25 02:42:46 +09:00]] 版))
<https://github.com/heycam/webidl/commit/760fa160330d0b07d1c8179d99274b64b58145aa>
]REFS]

[38] >>37 で[[バッファー源型]]が導入されました。また [[HTML Standard]]
にあった [CODE(IDL)@en[[[ArrayBufferView]]]] の定義が [[Web IDL]] に含まれるようになりました。
旧 [CODE(IDL)@en[[[ArrayBufferData]]]] は [CODE(IDL)@en[[[BufferSource]]]]
に改称されました。

[REFS[
- [37] [CITE@en[Add types for ArrayBuffer, DataView and typed array objects. · 631316b · heycam/webidl]] ([TIME[2014-10-10 08:11:57 +09:00]] 版) <https://github.com/heycam/webidl/commit/631316b8d513d414223de265b157153c032958c2>
- [42] [CITE@en[Recommend BufferSource for inputs. · c497edf · heycam/webidl]] ([TIME[2014-10-10 08:31:10 +09:00]] 版) <https://github.com/heycam/webidl/commit/c497edf5108bdccd2038158ea15f03e93a274308>
- [43] [CITE@en[Use BufferSource rather than ArrayBufferView https://www.w3.org/Bugs/Pub... · 99cfaf7 · whatwg/encoding]] ([TIME[2014-10-10 08:31:52 +09:00]] 版) <https://github.com/whatwg/encoding/commit/99cfaf7981f2d8a3aae96249c9431286433aca8a>
- [44] [CITE@en[Use new BufferSource concept from IDL · 0511b50 · whatwg/fetch]] ([TIME[2014-10-10 08:32:05 +09:00]] 版) <https://github.com/whatwg/fetch/commit/0511b509137630f78edb395fb1b019be0d3adec7>
]REFS]

[46] [CITE@en[W3C bug 27110. Fix the typo in the BufferSource typedef. · 74480d0 · heycam/webidl]]
( ([TIME[2014-10-21 09:50:15 +09:00]] 版))
<https://github.com/heycam/webidl/commit/74480d06c359ca407691818f1963f208d60434ea>

[55] [CITE@en[Update ECMAScript and IDL integration · whatwg/html@52f96c4]]
([TIME[2015-12-16 12:24:49 +09:00]] 版)
<https://github.com/whatwg/html/commit/52f96c455a53763a7fe00162c067da6adb20e62c>

[56] [CITE@en[Fix #26: allocating an ArrayBuffer can fail · whatwg/xhr@6484330]]
([TIME[2016-01-22 23:27:23 +09:00]] 版)
<https://github.com/whatwg/xhr/commit/6484330ec70ba66b9ee3a6b7244227180c0b4dce>

[57] [CITE@en[Write structured clone algorithm in terms of ECMAScript · whatwg/html@bfb960c]]
([TIME[2016-03-02 16:43:01 +09:00]] 版)
<https://github.com/whatwg/html/commit/bfb960c938580c95e77365e614218b952f96375b>

[58] [CITE@en[Handle an ArrayBuffer allocation failure · whatwg/fetch@3004ebc]]
([TIME[2016-03-09 15:27:35 +09:00]] 版)
<https://github.com/whatwg/fetch/commit/3004ebc878c9baf55cb22b32f66b8c5805824a70>

[59] [CITE@en[Use BufferSource instead of ArrayBuffer or ArrayBufferView.]]
([[mkruisselbrink]]著, [TIME[2016-07-22 07:26:26 +09:00]])
<https://github.com/w3c/FileAPI/commit/3eaebc7f1aea2a3c345d40401a3bed464414f63b>

[60] [CITE@en[24072 – Clarify handling of neutered objects]]
([TIME[2016-08-25 12:01:48 +09:00]])
<https://www.w3.org/Bugs/Public/show_bug.cgi?id=24072>

[61] [CITE@en[Merge algo and dfn for "detaching an ArrayBuffer" (#210)]]
([[tobie]]著, [TIME[2016-10-28 02:15:30 +09:00]])
<https://github.com/heycam/webidl/commit/9d054100b8d599a31a549d664b9dcb780b3d638c>

[62] [CITE@en[Only fail to allocate an ArrayBuffer once]]
([[annevk]]著, [TIME[2017-03-07 19:51:45 +09:00]])
<https://github.com/whatwg/xhr/commit/33e1e973af1ba0dc640699e2a3a3175a3a3cfd05>

[63] [CITE@en[Breaking: refactor structured clone into serialize/deserialize]]
([[domenic]]著, [TIME[2017-03-21 06:09:33 +09:00]])
<https://github.com/whatwg/html/commit/97d644c97335956610a31e8ad98d1a388c063e84>

[64] [CITE@en[Breaking: refactor structured clone into serialize/deserialize]]
([[domenic]]著, [TIME[2017-03-21 06:09:33 +09:00]])
<https://github.com/whatwg/html/commit/97d644c97335956610a31e8ad98d1a388c063e84>

[65] [CITE@en[Add '''['''AllowShared''']''' to allow accepting SharedArrayBuffers]]
([[domenic]]著, [TIME[2017-04-29 07:10:34 +09:00]])
<https://github.com/heycam/webidl/commit/c59bdcbcf2039c89eb7ea9a35e1e2dba621519ec>

[66] [CITE@en[Throwing when converting detached ArrayBuffers is not what browsers do · Issue #352 · heycam/webidl]]
([TIME[2017-05-14 21:40:05 +09:00]])
<https://github.com/heycam/webidl/issues/352>

[67] [CITE@en[Replace TypedArray spec and IDL links with links to ECMAScript spec · Issue #2203 · KhronosGroup/WebGL]]
([TIME[2017-05-14 21:41:32 +09:00]])
<https://github.com/KhronosGroup/WebGL/issues/2203>

[48] [CITE@en[Uint8ClampedArray does not have a '''[''''''['''Detached''']'''''']''' internal slot]]
([[annevk]]著, [TIME[2018-01-13 03:52:06 +09:00]])
<https://github.com/whatwg/html/commit/29d83435dac041e56f0f4d7076edfd6f2afadd4b>

[68] [CITE@en['''[''''''['''ViewedArrayBuffer''']'''''']''' cannot be undefined during usage]]
([[annevk]]著, [TIME[2019-01-09 23:43:23 +09:00]])
<https://github.com/heycam/webidl/commit/2084c30eb54c9c1dd6662e56345a5f17797653e5>

[70] [CITE@en['''[''''''['''ViewedArrayBuffer''']'''''']''' cannot be undefined during usage]]
([[annevk]]著, [TIME[2019-01-09 23:43:23 +09:00]])
<https://github.com/heycam/webidl/commit/2084c30eb54c9c1dd6662e56345a5f17797653e5>

[80] [CITE@en["get a copy of the bytes held by the buffer source" should not fail for detached buffers · Issue #151 · heycam/webidl]]
([TIME[2019-06-05 20:05:32 +09:00]])
<https://github.com/heycam/webidl/issues/151>

[82] [CITE@en['''[''''''['''ViewedArrayBuffer''']'''''']''' cannot be undefined during usage by annevk · Pull Request #604 · heycam/webidl]]
([TIME[2019-06-05 20:05:38 +09:00]])
<https://github.com/heycam/webidl/pull/604>