Skip to main content

Module typedarray

Module typedarray 

Source
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] = 255 reads back -1;
  • Uint8ClampedArray clamps instead, so -100 becomes 0 and 1233.5 becomes 255, and it rounds to nearest-even rather than truncating (0.5 becomes 0, 1.5 becomes 2).

§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§

ElementType
Which element type a view reads and writes.

Constants§

ELEMENT_TYPES
The view types, in the order their prototypes are installed.

Functions§

install
Install ArrayBuffer and every typed-array view.