1use crate::ArrayBuffer;
3use crate::HandleScope;
4use crate::Local;
5use crate::TypedArray;
6use crate::binding::v8__TypedArray__kMaxByteLength;
7use crate::support::size_t;
8use paste::paste;
9
10unsafe extern "C" {
11 fn v8__TypedArray__Length(this: *const TypedArray) -> size_t;
12}
13
14impl TypedArray {
15 pub const MAX_BYTE_LENGTH: usize = v8__TypedArray__kMaxByteLength;
18
19 #[inline(always)]
22 pub fn length(&self) -> usize {
23 unsafe { v8__TypedArray__Length(self) }
24 }
25}
26
27macro_rules! typed_array {
28 ($name:ident) => {
29 paste! {
30 use crate::$name;
31
32 unsafe extern "C" {
33 fn [< v8__ $name __New >](
34 buf_ptr: *const ArrayBuffer,
35 byte_offset: usize,
36 length: usize,
37 ) -> *const $name;
38 }
39
40 impl $name {
41 #[inline(always)]
42 pub fn new<'s>(
43 scope: &mut HandleScope<'s>,
44 buf: Local<ArrayBuffer>,
45 byte_offset: usize,
46 length: usize,
47 ) -> Option<Local<'s, $name>> {
48 unsafe { scope.cast_local(|_| [< v8__ $name __New >](&*buf, byte_offset, length)) }
49 }
50
51 #[doc = concat!("The largest ", stringify!($name), " size that can be constructed using `new`.")]
52 pub const MAX_LENGTH: usize = crate::binding::[< v8__ $name __kMaxLength >];
53 }
54 }
55 };
56}
57
58typed_array!(Uint8Array);
59typed_array!(Uint8ClampedArray);
60typed_array!(Int8Array);
61typed_array!(Uint16Array);
62typed_array!(Int16Array);
63typed_array!(Uint32Array);
64typed_array!(Int32Array);
65typed_array!(Float32Array);
66typed_array!(Float64Array);
67typed_array!(BigUint64Array);
68typed_array!(BigInt64Array);