From 5f037a1c9c36ac93bb7c2be66ff21ec646bed73e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sun, 8 Jan 2023 15:11:19 +0100 Subject: [PATCH] feat: Add v8::TypedArray::length (#1166) --- src/binding.cc | 3 +++ src/typed_array.rs | 8 ++++++++ tests/test_api.rs | 11 +++++++++++ 3 files changed, 22 insertions(+) diff --git a/src/binding.cc b/src/binding.cc index d1278f80..c9231e1d 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -464,6 +464,9 @@ uint32_t 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; } bool v8__Data__EQ(const v8::Data& self, const v8::Data& other) { diff --git a/src/typed_array.rs b/src/typed_array.rs index 7d50effb..97a4cbfc 100644 --- a/src/typed_array.rs +++ b/src/typed_array.rs @@ -7,6 +7,7 @@ use crate::TypedArray; extern "C" { fn v8__TypedArray__kMaxLength() -> size_t; + fn v8__TypedArray__Length(this: *const TypedArray) -> size_t; } impl TypedArray { @@ -17,6 +18,13 @@ impl TypedArray { pub fn max_length() -> usize { 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 { diff --git a/tests/test_api.rs b/tests/test_api.rs index dd956b2c..e308ac0d 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -4268,36 +4268,47 @@ fn typed_array_constructors() { let t = v8::Uint8Array::new(scope, ab, 0, 0).unwrap(); assert!(t.is_uint8_array()); + assert_eq!(t.length(), 0); let t = v8::Uint8ClampedArray::new(scope, ab, 0, 0).unwrap(); assert!(t.is_uint8_clamped_array()); + assert_eq!(t.length(), 0); let t = v8::Int8Array::new(scope, ab, 0, 0).unwrap(); assert!(t.is_int8_array()); + assert_eq!(t.length(), 0); let t = v8::Uint16Array::new(scope, ab, 0, 0).unwrap(); assert!(t.is_uint16_array()); + assert_eq!(t.length(), 0); let t = v8::Int16Array::new(scope, ab, 0, 0).unwrap(); assert!(t.is_int16_array()); + assert_eq!(t.length(), 0); let t = v8::Uint32Array::new(scope, ab, 0, 0).unwrap(); assert!(t.is_uint32_array()); + assert_eq!(t.length(), 0); let t = v8::Int32Array::new(scope, ab, 0, 0).unwrap(); assert!(t.is_int32_array()); + assert_eq!(t.length(), 0); let t = v8::Float32Array::new(scope, ab, 0, 0).unwrap(); assert!(t.is_float32_array()); + assert_eq!(t.length(), 0); let t = v8::Float64Array::new(scope, ab, 0, 0).unwrap(); assert!(t.is_float64_array()); + assert_eq!(t.length(), 0); let t = v8::BigUint64Array::new(scope, ab, 0, 0).unwrap(); assert!(t.is_big_uint64_array()); + assert_eq!(t.length(), 0); let t = v8::BigInt64Array::new(scope, ab, 0, 0).unwrap(); assert!(t.is_big_int64_array()); + assert_eq!(t.length(), 0); // TypedArray::max_length() ought to be >= 2^30 < 2^32 in 64 bits #[cfg(target_pointer_width = "64")]