Expand description
ArrayBuffer and the typed-array views.
§A view, not a copy
A typed array is a window onto an ArrayBuffer’s bytes: new Float32Array(buffer, 8, 1)
shares storage with new Uint8Array(buffer), so a write through one is visible through
the other. That is the whole point of the type — it is how a script reinterprets a byte
range as numbers — and it is why the bytes live in the buffer object rather than in each
view. The views hold a Gc<JsObject> of their buffer rather than an Rc of its storage,
so the collector can see the reference and the buffer cannot be freed while a view lives.
§Coercion on write
Every store runs ToNumber and then the type’s own conversion. The interesting cases are
two:
- an integer type wraps, so
new Int8Array(1)[0] = 255reads back-1; Uint8ClampedArrayclamps instead, so-100becomes0and1233.5becomes255, and it rounds to nearest-even rather than truncating (0.5becomes0,1.5becomes2).
§Float16
Rust has no half-precision type, so the encoding is done by hand. It only has to be exact
enough to round-trip the values a script stores; the code below is the standard
round-to-nearest-even conversion with subnormals, infinities and NaN handled.
§Detached buffers
ArrayBuffer.prototype.transfer() hands the bytes to a new buffer and leaves this one
detached. A view of a detached buffer reads undefined from every index and reports a
length of 0, which is what the reference does; the storage stays behind the Rc so all
the views see the transition at once.
Enums§
- Element
Type - Which element type a view reads and writes.
Constants§
- ELEMENT_
TYPES - The view types, in the order their prototypes are installed.
- NOT_
A_ BIGINT - Why an element write was refused: the target is a BigInt type and the value is not one.
- NOT_
A_ NUMBER - Why an element write was refused: the target is numeric and the value has no number form.
Functions§
- install
- Install
ArrayBufferand every typed-array view. - install_
data_ view - Install
DataViewand its prototype.