0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-03 17:09:00 -05:00
denoland-rusty-v8/src/typed_array.rs

56 lines
1.6 KiB
Rust
Raw Normal View History

2021-02-13 07:31:18 -05:00
// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license.
2020-10-06 14:16:22 -04:00
use crate::ArrayBuffer;
use crate::HandleScope;
use crate::Local;
use crate::TypedArray;
extern "C" {
fn v8__TypedArray__kMaxLength() -> libc::size_t;
}
impl TypedArray {
/// The maximum length (in bytes) of the buffer backing a v8::TypedArray
/// instance. Attempting to create a v8::ArrayBuffer from a larger buffer will
/// result in a fatal error.
2022-09-20 22:45:33 -04:00
#[inline(always)]
pub fn max_length() -> usize {
unsafe { v8__TypedArray__kMaxLength() }
}
}
2020-10-06 14:16:22 -04:00
macro_rules! typed_array {
($name:ident, $func:ident) => {
use crate::$name;
impl $name {
2022-09-20 22:45:33 -04:00
#[inline(always)]
2020-10-06 14:16:22 -04:00
pub fn new<'s>(
scope: &mut HandleScope<'s>,
buf: Local<ArrayBuffer>,
byte_offset: usize,
length: usize,
) -> Option<Local<'s, $name>> {
extern "C" {
fn $func(
buf_ptr: *const ArrayBuffer,
byte_offset: usize,
length: usize,
) -> *const $name;
}
unsafe { scope.cast_local(|_| $func(&*buf, byte_offset, length)) }
}
}
};
}
typed_array!(Uint8Array, v8__Uint8Array__New);
typed_array!(Uint8ClampedArray, v8__Uint8ClampedArray__New);
typed_array!(Int8Array, v8__Int8Array__New);
typed_array!(Uint16Array, v8__Uint16Array__New);
typed_array!(Int16Array, v8__Int16Array__New);
typed_array!(Uint32Array, v8__Uint32Array__New);
typed_array!(Int32Array, v8__Int32Array__New);
typed_array!(Float32Array, v8__Float32Array__New);
typed_array!(Float64Array, v8__Float64Array__New);
typed_array!(BigUint64Array, v8__BigUint64Array__New);
typed_array!(BigInt64Array, v8__BigInt64Array__New);