Typed Arrays
Float32Array, Float64Array, and the integer typed-array views in Tish.
Overview
Tish supports the typed-array views — Float64Array, Float32Array, Int8Array, Uint8Array,
Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, and Uint32Array — across every
backend (interpreter, bytecode VM, and native/AOT). Each constructor coerces every element to the
view's element type: Float32Array rounds to 32-bit float precision, the integer views
truncate-and-wrap, and Uint8ClampedArray clamps to 0..=255.
new Uint8Array([300, -1, 256]) // [44, 255, 0] (wrap mod 256)
new Int8Array([128, -129]) // [-128, 127] (signed wrap)
new Float32Array([0.1])[0] // 0.10000000149011612 (f32-rounded)
new Uint8ClampedArray([-5, 300]) // [0, 255] (clamped)Constructors
Statics
Element coercion
Non-numeric elements become NaN first, which is 0 for the integer views.
Working with views
A typed array is backed by a regular array, so indexing, .length, for…of, and the array methods
all work:
let data = new Uint8Array([10, 20, 30])
data.length // 3
data[0] // 10
let total = 0
for (let x of data) {
total = total + x
}
total // 60
new Float64Array([1, 2, 3, 4]).reduce((a, b) => a + b, 0) // 10Packed Float64Array (native, opt-in)
On the native (AOT) backend, Float64Array can use a packed Vec<f64> representation — one
contiguous buffer of doubles instead of a generic boxed array of values. Enable it with the
environment variable TISH_PACKED_ARRAYS=1 (default off); with the flag off, behaviour is
byte-for-byte identical to the boxed backing.
Float64Array is the natural fit because its element type already is a 64-bit float, so no
coercion is needed. The packed form is ~3× denser (8 bytes per element vs 24) and constructs
dramatically faster — a length-form new Float64Array(n) is a single zero-fill rather than building
n boxed values. Indexing, .length, for…of, and the array methods all work unchanged.
A useful side effect: with packing on, writes to a Float64Array coerce to f64, so the
construction-only rule below does not apply to this one view:
// With TISH_PACKED_ARRAYS=1 on the native backend:
let f = new Float64Array([1, 2, 3])
f[0] = "oops" // stored as NaN (coerced to f64), not the raw stringThe integer views and Float32Array have no packed representation yet and always use the boxed
backing. Array methods (reduce, map, filter, forEach, find, some, every, …) iterate the
packed buffer directly without first expanding it to a boxed array, so they keep the speed and memory
benefit too. A numeric map/filter also returns a packed result, so chains like
xs.filter(...).map(...).reduce(...) stay packed from end to end.
Compatibility notes (v1)
- Coercion happens at construction (
new/.from/.of), not on element assignment.ta[i] = 300on aUint8Arraystores300(a plain array write, no wrap). Rebuild withUint8Array.of(...)/Uint8Array.from(...)when you need the value re-coerced. (The one exception is a packedFloat64Arrayon the native backend — see above — which coerces writes tof64.) - No
ArrayBuffer/DataView/.buffer/.byteLength/.subarray/.setyet. - A typed array is a regular array at runtime, so
Array.isArray(ta)istrueand there is noinstanceofdistinction between view types.