0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-25 00:29:14 -05:00

feat: Add v8::TypedArray::length (#1166)

This commit is contained in:
Bartek Iwańczuk 2023-01-08 15:11:19 +01:00 committed by GitHub
parent 4381727867
commit 961a4940b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 0 deletions

View file

@ -463,6 +463,9 @@ uint32_t v8__ScriptCompiler__CachedDataVersionTag() {
return v8::ScriptCompiler::CachedDataVersionTag(); return v8::ScriptCompiler::CachedDataVersionTag();
} }
size_t v8__TypedArray__Length(const v8::TypedArray* self) {
return ptr_to_local(self)->Length();
}
size_t v8__TypedArray__kMaxLength() { return v8::TypedArray::kMaxLength; } size_t v8__TypedArray__kMaxLength() { return v8::TypedArray::kMaxLength; }
bool v8__Data__EQ(const v8::Data& self, const v8::Data& other) { bool v8__Data__EQ(const v8::Data& self, const v8::Data& other) {

View file

@ -7,6 +7,7 @@ use crate::TypedArray;
extern "C" { extern "C" {
fn v8__TypedArray__kMaxLength() -> size_t; fn v8__TypedArray__kMaxLength() -> size_t;
fn v8__TypedArray__Length(this: *const TypedArray) -> size_t;
} }
impl TypedArray { impl TypedArray {
@ -17,6 +18,13 @@ impl TypedArray {
pub fn max_length() -> usize { pub fn max_length() -> usize {
unsafe { v8__TypedArray__kMaxLength() } unsafe { v8__TypedArray__kMaxLength() }
} }
/// Number of elements in this typed array
/// (e.g. for Int16Array, |ByteLength|/2).
#[inline(always)]
pub fn length(&self) -> usize {
unsafe { v8__TypedArray__Length(self) }
}
} }
macro_rules! typed_array { macro_rules! typed_array {

View file

@ -4290,36 +4290,47 @@ fn typed_array_constructors() {
let t = v8::Uint8Array::new(scope, ab, 0, 0).unwrap(); let t = v8::Uint8Array::new(scope, ab, 0, 0).unwrap();
assert!(t.is_uint8_array()); assert!(t.is_uint8_array());
assert_eq!(t.length(), 0);
let t = v8::Uint8ClampedArray::new(scope, ab, 0, 0).unwrap(); let t = v8::Uint8ClampedArray::new(scope, ab, 0, 0).unwrap();
assert!(t.is_uint8_clamped_array()); assert!(t.is_uint8_clamped_array());
assert_eq!(t.length(), 0);
let t = v8::Int8Array::new(scope, ab, 0, 0).unwrap(); let t = v8::Int8Array::new(scope, ab, 0, 0).unwrap();
assert!(t.is_int8_array()); assert!(t.is_int8_array());
assert_eq!(t.length(), 0);
let t = v8::Uint16Array::new(scope, ab, 0, 0).unwrap(); let t = v8::Uint16Array::new(scope, ab, 0, 0).unwrap();
assert!(t.is_uint16_array()); assert!(t.is_uint16_array());
assert_eq!(t.length(), 0);
let t = v8::Int16Array::new(scope, ab, 0, 0).unwrap(); let t = v8::Int16Array::new(scope, ab, 0, 0).unwrap();
assert!(t.is_int16_array()); assert!(t.is_int16_array());
assert_eq!(t.length(), 0);
let t = v8::Uint32Array::new(scope, ab, 0, 0).unwrap(); let t = v8::Uint32Array::new(scope, ab, 0, 0).unwrap();
assert!(t.is_uint32_array()); assert!(t.is_uint32_array());
assert_eq!(t.length(), 0);
let t = v8::Int32Array::new(scope, ab, 0, 0).unwrap(); let t = v8::Int32Array::new(scope, ab, 0, 0).unwrap();
assert!(t.is_int32_array()); assert!(t.is_int32_array());
assert_eq!(t.length(), 0);
let t = v8::Float32Array::new(scope, ab, 0, 0).unwrap(); let t = v8::Float32Array::new(scope, ab, 0, 0).unwrap();
assert!(t.is_float32_array()); assert!(t.is_float32_array());
assert_eq!(t.length(), 0);
let t = v8::Float64Array::new(scope, ab, 0, 0).unwrap(); let t = v8::Float64Array::new(scope, ab, 0, 0).unwrap();
assert!(t.is_float64_array()); assert!(t.is_float64_array());
assert_eq!(t.length(), 0);
let t = v8::BigUint64Array::new(scope, ab, 0, 0).unwrap(); let t = v8::BigUint64Array::new(scope, ab, 0, 0).unwrap();
assert!(t.is_big_uint64_array()); assert!(t.is_big_uint64_array());
assert_eq!(t.length(), 0);
let t = v8::BigInt64Array::new(scope, ab, 0, 0).unwrap(); let t = v8::BigInt64Array::new(scope, ab, 0, 0).unwrap();
assert!(t.is_big_int64_array()); assert!(t.is_big_int64_array());
assert_eq!(t.length(), 0);
// TypedArray::max_length() ought to be >= 2^30 < 2^32 in 64 bits // TypedArray::max_length() ought to be >= 2^30 < 2^32 in 64 bits
#[cfg(target_pointer_width = "64")] #[cfg(target_pointer_width = "64")]