0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-02-15 01:55:56 -05:00

feat: add get_contents_raw_parts (#1704)

* feat: add get_contents_raw_parts

* fix type

* fix null deref
This commit is contained in:
snek 2025-02-12 09:43:48 +01:00 committed by GitHub
parent 4a56d2d43b
commit a342dc8e5c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 32 additions and 12 deletions

View file

@ -87,20 +87,41 @@ impl ArrayBufferView {
}
}
/// Returns the contents of the ArrayBufferView's buffer as a MemorySpan. If
/// the contents are on the V8 heap, they get copied into `storage`. Otherwise
/// a view into the off-heap backing store is returned. The provided storage
/// should be at least as large as the maximum on-heap size of a TypedArray,
/// which is available as `v8::TYPED_ARRAY_MAX_SIZE_IN_HEAP`.
#[inline(always)]
pub unsafe fn get_contents_raw_parts(
&self,
storage: &mut [u8],
) -> (*mut u8, usize) {
unsafe {
let span = v8__ArrayBufferView__GetContents(
self,
memory_span_t {
data: storage.as_mut_ptr(),
size: storage.len(),
},
);
(span.data, span.size)
}
}
/// Returns the contents of the ArrayBufferView's buffer as a MemorySpan. If
/// the contents are on the V8 heap, they get copied into `storage`. Otherwise
/// a view into the off-heap backing store is returned. The provided storage
/// should be at least as large as the maximum on-heap size of a TypedArray,
/// which is available as `v8::TYPED_ARRAY_MAX_SIZE_IN_HEAP`.
#[inline(always)]
pub fn get_contents<'s, 'a>(&'s self, storage: &'a mut [u8]) -> &'a [u8]
where
's: 'a,
{
unsafe {
let span = v8__ArrayBufferView__GetContents(
self,
memory_span_t {
data: storage.as_mut_ptr() as _,
size: storage.len(),
},
);
std::slice::from_raw_parts(span.data as _, span.size)
let (data, size) = self.get_contents_raw_parts(storage);
std::slice::from_raw_parts(data, size)
}
}
}

View file

@ -1887,8 +1887,7 @@ size_t v8__ArrayBufferView__CopyContents(const v8::ArrayBufferView& self,
memory_span_t v8__ArrayBufferView__GetContents(const v8::ArrayBufferView& self,
memory_span_t rstorage) {
v8::MemorySpan<uint8_t> storage{static_cast<uint8_t*>(rstorage.data),
rstorage.size};
v8::MemorySpan<uint8_t> storage{rstorage.data, rstorage.size};
v8::MemorySpan<uint8_t> span = ptr_to_local(&self)->GetContents(storage);
return {span.data(), span.size()};
}

View file

@ -179,7 +179,7 @@ struct three_pointers_t {
} // namespace support
struct memory_span_t {
void* data;
uint8_t* data;
size_t size;
};

View file

@ -1017,7 +1017,7 @@ impl ObjectTemplate {
let getter = getter.map_or_else(std::ptr::null, |v| &*v);
let setter = setter.map_or_else(std::ptr::null, |v| &*v);
v8__ObjectTemplate__SetAccessorProperty(
self, &*key, &*getter, &*setter, attr,
self, &*key, getter, setter, attr,
);
}
}