From c549b19df37ee2e82ab7ca06014b33fa52413a0f Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Tue, 20 Sep 2022 17:55:15 +0530 Subject: [PATCH] fastcall: Fix `get_storage_if_aligned` for non-uint8arrays (#1077) --- src/fast_api.rs | 12 ++++-------- tests/test_api.rs | 2 +- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/fast_api.rs b/src/fast_api.rs index a7894e1b..6fa3872e 100644 --- a/src/fast_api.rs +++ b/src/fast_api.rs @@ -192,7 +192,8 @@ pub struct FastApiCallbackOptions<'a> { // https://source.chromium.org/chromium/chromium/src/+/main:v8/include/v8-fast-api-calls.h;l=336 #[repr(C)] pub struct FastApiTypedArray { - pub byte_length: usize, + /// Returns the length in number of elements. + pub length: usize, // This pointer should include the typed array offset applied. // It's not guaranteed that it's aligned to sizeof(T), it's only // guaranteed that it's 4-byte aligned, so for 8-byte types we need to @@ -204,7 +205,7 @@ pub struct FastApiTypedArray { impl FastApiTypedArray { #[inline] pub fn get(&self, index: usize) -> T { - debug_assert!(index < self.byte_length); + debug_assert!(index < self.length); let mut t: T = Default::default(); unsafe { ptr::copy_nonoverlapping(self.data.add(index), &mut t, 1); @@ -217,12 +218,7 @@ impl FastApiTypedArray { if (self.data as usize) % align_of::() != 0 { return None; } - Some(unsafe { - std::slice::from_raw_parts_mut( - self.data, - self.byte_length / align_of::(), - ) - }) + Some(unsafe { std::slice::from_raw_parts_mut(self.data, self.length) }) } } diff --git a/tests/test_api.rs b/tests/test_api.rs index db60360e..7a1a8e71 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -7560,7 +7560,7 @@ fn test_fast_calls_overload() { ) { unsafe { WHO = "fast_buf" }; let buf = unsafe { &*data }; - assert_eq!(buf.byte_length, 2); + assert_eq!(buf.length, 2); assert_eq!(buf.get(0), 6); assert_eq!(buf.get(1), 9); }