0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-11 08:34:01 -05:00

feat: add ArrayBuffer::was_detached() (#1103)

This commit is contained in:
Marcos Casagrande 2022-10-16 22:03:48 +02:00 committed by GitHub
parent 09cac0aa04
commit c06986c12e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 1 deletions

View file

@ -38,6 +38,7 @@ extern "C" {
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__WasDetached(this: *const ArrayBuffer) -> bool;
fn v8__ArrayBuffer__ByteLength(this: *const ArrayBuffer) -> usize;
fn v8__ArrayBuffer__GetBackingStore(
this: *const ArrayBuffer,
@ -389,6 +390,15 @@ impl ArrayBuffer {
unsafe { v8__ArrayBuffer__IsDetachable(self) }
}
/// Returns true if this ArrayBuffer was detached.
#[inline(always)]
pub fn was_detached(&self) -> bool {
if self.byte_length() != 0 {
return false;
}
unsafe { v8__ArrayBuffer__WasDetached(self) }
}
/// Detaches this ArrayBuffer and all its views (typed arrays).
/// Detaching sets the byte length of the buffer and all typed arrays to zero,
/// preventing JavaScript from ever accessing underlying backing store.

View file

@ -828,6 +828,10 @@ bool v8__ArrayBuffer__IsDetachable(const v8::ArrayBuffer& self) {
return ptr_to_local(&self)->IsDetachable();
}
bool v8__ArrayBuffer__WasDetached(const v8::ArrayBuffer& self) {
return v8::Utils::OpenHandle(&self)->was_detached();
}
void* v8__BackingStore__Data(const v8::BackingStore& self) {
return self.Data();
}

View file

@ -573,12 +573,20 @@ fn array_buffer() {
let ab = v8::ArrayBuffer::new(scope, 42);
assert_eq!(42, ab.byte_length());
assert!(!ab.was_detached());
assert!(ab.is_detachable());
ab.detach();
assert_eq!(0, ab.byte_length());
assert!(ab.was_detached());
ab.detach(); // Calling it twice should be a no-op.
// detecting if it was detached on a zero-length ArrayBuffer should work
let empty_ab = v8::ArrayBuffer::new(scope, 0);
assert!(!empty_ab.was_detached());
empty_ab.detach();
assert!(empty_ab.was_detached());
let bs = v8::ArrayBuffer::new_backing_store(scope, 84);
assert_eq!(84, bs.byte_length());
assert!(!bs.is_shared());