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

Add v8::ArrayBuffer::Data (#1068)

This commit is contained in:
Divy Srivastava 2022-09-16 15:00:05 +05:30 committed by GitHub
parent 780eb7946b
commit 31291f64a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 0 deletions

View file

@ -36,6 +36,7 @@ extern "C" {
backing_store: *const SharedRef<BackingStore>,
) -> *const ArrayBuffer;
fn v8__ArrayBuffer__Detach(this: *const ArrayBuffer);
fn v8__ArrayBuffer__Data(this: *const ArrayBuffer) -> *mut c_void;
fn v8__ArrayBuffer__IsDetachable(this: *const ArrayBuffer) -> bool;
fn v8__ArrayBuffer__ByteLength(this: *const ArrayBuffer) -> usize;
fn v8__ArrayBuffer__GetBackingStore(
@ -391,6 +392,13 @@ impl ArrayBuffer {
}
}
/// More efficient shortcut for GetBackingStore()->Data().
/// The returned pointer is valid as long as the ArrayBuffer is alive.
#[inline]
pub fn data(&self) -> *mut c_void {
unsafe { v8__ArrayBuffer__Data(self) }
}
/// Get a shared pointer to the backing store of this array buffer. This
/// pointer coordinates the lifetime management of the internal storage
/// with any live ArrayBuffers on the heap, even across isolates. The embedder

View file

@ -854,6 +854,10 @@ two_pointers_t v8__ArrayBuffer__GetBackingStore(const v8::ArrayBuffer& self) {
return make_pod<two_pointers_t>(ptr_to_local(&self)->GetBackingStore());
}
void* v8__ArrayBuffer__Data(const v8::ArrayBuffer& self) {
return ptr_to_local(&self)->Data();
}
void v8__ArrayBuffer__Detach(const v8::ArrayBuffer& self) {
ptr_to_local(&self)->Detach();
}

View file

@ -6678,6 +6678,26 @@ fn backing_store_from_empty_vec() {
let _ = v8::ArrayBuffer::with_backing_store(&mut scope, &store);
}
#[test]
fn backing_store_data() {
let _setup_guard = setup();
let mut isolate = v8::Isolate::new(Default::default());
let mut scope = v8::HandleScope::new(&mut isolate);
let context = v8::Context::new(&mut scope);
let mut scope = v8::ContextScope::new(&mut scope, context);
let v = vec![1, 2, 3, 4, 5];
let len = v.len();
let store = v8::ArrayBuffer::new_backing_store_from_vec(v).make_shared();
let buf = v8::ArrayBuffer::with_backing_store(&mut scope, &store);
assert_eq!(buf.byte_length(), len);
assert_eq!(
unsafe { std::slice::from_raw_parts_mut(buf.data() as *mut u8, len) },
&[1, 2, 3, 4, 5]
);
}
#[test]
fn current_stack_trace() {
// Setup isolate