Typed arrays
Packed numeric buffers — Float32Array, Int32Array, Uint8Array, Float64Array, Int16Array, Uint16Array. O(1) indexed access, flat memory layout. The natural shape for GPU buffer uploads, audio buffers, and wasm host roundtrips.
Construct
Every typed array class shares the same three constructors:
| Constructor | What it does |
|---|---|
X.new(count) | Zero-filled buffer with count elements. |
X.fromList(list) | One element per item of list, cast to the array's element type. |
ByteArray.fromString(text) | (ByteArray only) UTF-8 byte snapshot of text. |
var f32 = Float32Array.new(16) // 16 × f32, all 0.0
var i32 = Int32Array.fromList([1, 2, 3])
var hello = ByteArray.fromString("hello") // 5-byte UTF-8 view
Indexed access
Subscript syntax works the same on every typed array.
Reads return a Wren Num; writes round-trip
through the element width (truncating for integer arrays,
precision-clipping for f32).
var v = Float32Array.new(4)
v[0] = 0.25
v[1] = 0.50
System.print(v[0] + v[1]) // 0.75
var bytes = ByteArray.new(4)
bytes[0] = 0xFF
bytes[1] = 256 // wraps to 0 (u8 cast)
System.print(bytes[0]) // 255
System.print(bytes[1]) // 0
Out-of-range writes Fiber.abort with a clear
error rather than silently silently expanding the buffer.
Typed arrays are fixed length once constructed.
Iteration
Every typed array supports the for-in protocol via
iterate + iteratorValue:
var v = Int32Array.fromList([10, 20, 30])
var sum = 0
for (n in v) sum = sum + n
System.print(sum) // 60
Element widths
| Class | Bytes per element | Range |
|---|---|---|
ByteArray / Uint8Array | 1 | 0..255 |
Int16Array | 2 | -32768..32767 |
Uint16Array | 2 | 0..65535 |
Int32Array | 4 | -2³¹..2³¹-1 |
Float32Array | 4 | f32 finite range |
Float64Array | 8 | f64 finite range |
.count is the element count; .byteLength
is count × byteWidth. Both are read-only.
Interop
Typed arrays are the natural shape for crossing the Wren/host boundary — they're a flat pointer + length on the Rust side, so no per-element marshalling cost.
- GPU upload —
buffer.writeFloats(0, arr)in@hatch:gputakes a Float32Array directly. - Audio buffers —
@hatch:audioreads PCM samples from a Float32Array. - Physics —
@hatch:physicshaspositionInto(out, offset)/rotationInto(out, offset)variants that write straight into a Float32Array, no per-frame allocation. - SIMD —
Simd4f.load(arr, 0)/.store(arr, 0)bridge between SIMD and typed arrays.