Skip to content

Commit

Permalink
fastcall: Fix get_storage_if_aligned for non-uint8arrays (denoland#…
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Sep 20, 2022
1 parent 2ba52ed commit c549b19
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
12 changes: 4 additions & 8 deletions src/fast_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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.
// 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
Expand All @@ -204,7 +205,7 @@ pub struct FastApiTypedArray<T: Default> {
impl<T: Default> FastApiTypedArray<T> {
#[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);
Expand All @@ -217,12 +218,7 @@ impl<T: Default> FastApiTypedArray<T> {
if (self.data as usize) % align_of::<T>() != 0 {
return None;
}
Some(unsafe {
std::slice::from_raw_parts_mut(
self.data,
self.byte_length / align_of::<T>(),
)
})
Some(unsafe { std::slice::from_raw_parts_mut(self.data, self.length) })
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit c549b19

Please sign in to comment.