Special property ‣ typedArray - chung-leong/zigar GitHub Wiki

TypedArray view the object's underlying bytes. Should be consider volatile as well.

Arrays and slices of numeric types have this property:

pub const Int32Array4 = [4]i32;
pub const Float32Slice = []f32;
import { Float32Slice, Int32Array4 } from './typed-array-example-1.zig';

const array = new Int32Array4([ 1, 2, 3, 4 ]);
console.log(array.typedArray);
const slice = new Float32Slice((function*() {
  for (let i = 1; i <= 10; i++) {
    yield Math.PI * i;
  }
})());
console.log(slice.typedArray);
Int32Array(4) [ 1, 2, 3, 4 ]
Float32Array(10) [
  3.1415927410125732,
  6.2831854820251465,
  9.42477798461914,
  12.566370964050293,
  15.707962989807129,
  18.84955596923828,
  21.991147994995117,
  25.132741928100586,
  28.274333953857422,
  31.415925979614258
]

Vectors have it too:

pub const Float32Vector4 = @Vector(4, f32);
import { Float32Vector4 } from './typed-array-example-2.zig';

const vector = new Float32Vector4([ 1, 2, 3, 4 ]);
console.log(vector.typedArray);
Float32Array(4) [ 1, 2, 3, 4 ]

Multi-dimensional arrays and slices inherit this property from their elements:

pub const Float32Vector3Slice = []@Vector(3, f32);
import { Float32Vector3Slice } from './typed-array-example-3.zig';

const slice = new Float32Vector3Slice((function*() {
    for (let i = 1; i <= 4; i++) {
        yield [ 1, 2, 3 ].map(n => n * i);
    }
})());
console.log(slice.valueOf());
console.log(slice.typedArray);
[ [ 1, 2, 3 ], [ 2, 4, 6 ], [ 3, 6, 9 ], [ 4, 8, 12 ] ]
Float32Array(16) [
  1, 2,  3, 0, 2, 4,
  6, 0,  3, 6, 9, 0,
  4, 8, 12, 0
]

Note the gaps within the Float32Array due to []@Vector(3, f32) having a 16-byte alignment.


Special properites