0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-21 15:04:33 -05:00

fastcall: Fix get_storage_if_aligned for non-uint8arrays (#1077)

This commit is contained in:
Divy Srivastava 2022-09-20 17:55:15 +05:30 committed by GitHub
parent 2ba52ed276
commit c549b19df3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 9 deletions

View file

@ -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 // https://source.chromium.org/chromium/chromium/src/+/main:v8/include/v8-fast-api-calls.h;l=336
#[repr(C)] #[repr(C)]
pub struct FastApiTypedArray<T: Default> { pub struct FastApiTypedArray<T: Default> {
pub byte_length: usize, /// Returns the length in number of elements.
pub length: usize,
// This pointer should include the typed array offset applied. // This pointer should include the typed array offset applied.
// It's not guaranteed that it's aligned to sizeof(T), it's only // 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 // guaranteed that it's 4-byte aligned, so for 8-byte types we need to
@ -204,7 +205,7 @@ pub struct FastApiTypedArray<T: Default> {
impl<T: Default> FastApiTypedArray<T> { impl<T: Default> FastApiTypedArray<T> {
#[inline] #[inline]
pub fn get(&self, index: usize) -> T { pub fn get(&self, index: usize) -> T {
debug_assert!(index < self.byte_length); debug_assert!(index < self.length);
let mut t: T = Default::default(); let mut t: T = Default::default();
unsafe { unsafe {
ptr::copy_nonoverlapping(self.data.add(index), &mut t, 1); ptr::copy_nonoverlapping(self.data.add(index), &mut t, 1);
@ -217,12 +218,7 @@ impl<T: Default> FastApiTypedArray<T> {
if (self.data as usize) % align_of::<T>() != 0 { if (self.data as usize) % align_of::<T>() != 0 {
return None; return None;
} }
Some(unsafe { Some(unsafe { std::slice::from_raw_parts_mut(self.data, self.length) })
std::slice::from_raw_parts_mut(
self.data,
self.byte_length / align_of::<T>(),
)
})
} }
} }

View file

@ -7560,7 +7560,7 @@ fn test_fast_calls_overload() {
) { ) {
unsafe { WHO = "fast_buf" }; unsafe { WHO = "fast_buf" };
let buf = unsafe { &*data }; 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(0), 6);
assert_eq!(buf.get(1), 9); assert_eq!(buf.get(1), 9);
} }