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:
parent
09cac0aa04
commit
c06986c12e
3 changed files with 23 additions and 1 deletions
|
@ -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.
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in a new issue