Special property ‣ __typed_array ᴾᴴᴾ - chung-leong/zigar GitHub Wiki
JavaScript | PHP
TypedArray view of the object's underlying bytes.
Arrays and slices of numeric types have this property:
pub const Int32Array4 = [4]i32;
pub const Float32Slice = []f32;
<?php
$m = zigar_use(__DIR__ . '/typed-array-example-1.zig');
$array = new $m->Int32Array4([ 1, 2, 3, 4 ]);
print_r($array->__typed_array);
$slice = new $m->Float32Slice((function() {
for ($i = 0; $i < 10; $i++) {
yield M_PI * $i;
}
})());
print_r($slice->__typed_array);
Int32Array Object
(
[ITEMS](/chung-leong/zigar/wiki/ITEMS) => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[buffer] => ArrayBuffer Object
(
[byteLength] => 16
[detached] =>
[readOnly] =>
)
[byteLength] => 16
[byteOffset] => 0
[length] => 4
)
Float32Array Object
(
[ITEMS](/chung-leong/zigar/wiki/ITEMS) => Array
(
[0] => 0
[1] => 3.1415927410126
[2] => 6.2831854820251
[3] => 9.4247779846191
[4] => 12.56637096405
[5] => 15.707962989807
[6] => 18.849555969238
[7] => 21.991147994995
[8] => 25.132741928101
[9] => 28.274333953857
)
[buffer] => ArrayBuffer Object
(
[byteLength] => 40
[detached] =>
[readOnly] =>
)
[byteLength] => 40
[byteOffset] => 0
[length] => 10
)
Vectors have it too:
pub const Float32Vector4 = @Vector(4, f32);
<?php
$m = zigar_use(__DIR__ . '/typed-array-example-2.zig');
$vector = new $m->Float32Vector4([ 1, 2, 3, 4 ]);
print_r($vector->__typed_array);
Float32Array Object
(
[ITEMS](/chung-leong/zigar/wiki/ITEMS) => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[buffer] => ArrayBuffer Object
(
[byteLength] => 16
[detached] =>
[readOnly] =>
)
[byteLength] => 16
[byteOffset] => 0
[length] => 4
)
Multi-dimensional arrays and slices inherit this property from their elements:
pub const Float32Vector3Slice = []@Vector(3, f32);
<?php
$m = zigar_use(__DIR__ . '/typed-array-example-3.zig');
$slice = new $m->Float32Vector3Slice((function() {
for ($i = 1; $i <= 4; $i++) {
yield [ 1 * $i, 2 * $i, 3 * $i ];
}
})());
print_r($slice);
print_r($slice->__typed_array);
[]@Vector(3, u32) Object
(
[0] => @Vector(3, u32) Object
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => @Vector(3, u32) Object
(
[0] => 2
[1] => 4
[2] => 6
)
[2] => @Vector(3, u32) Object
(
[0] => 3
[1] => 6
[2] => 9
)
[3] => @Vector(3, u32) Object
(
[0] => 4
[1] => 8
[2] => 12
)
)
Float32Array Object
(
[ITEMS](/chung-leong/zigar/wiki/ITEMS) => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => NAN
[4] => 2
[5] => 4
[6] => 6
[7] => 0
[8] => 3
[9] => 6
[10] => 9
[11] => -0
[12] => 4
[13] => 8
[14] => 12
[15] => 0
)
[buffer] => ArrayBuffer Object
(
[byteLength] => 64
[detached] =>
[readOnly] =>
)
[byteLength] => 64
[byteOffset] => 0
[length] => 16
)
Note the gaps within the Float32Array due to []@Vector(3, f32) having a 16-byte alignment.