Skip to content

Commit

Permalink
Optional key in v8::ArrayBuffer:detach (denoland#1141)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Nov 30, 2022
1 parent 0b9423a commit e57c3ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/array_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::cell::Cell;
use std::ffi::c_void;
use std::ops::Deref;
use std::ptr;
use std::ptr::null;
use std::ptr::null_mut;
use std::ptr::NonNull;
use std::slice;
Expand Down Expand Up @@ -412,11 +413,12 @@ impl ArrayBuffer {
/// `None` if the key didn't pass the `[[ArrayBufferDetachKey]]` check,
/// and `Some(true)` otherwise.
#[inline(always)]
pub fn detach(&self, key: Local<Value>) -> Option<bool> {
pub fn detach(&self, key: Option<Local<Value>>) -> Option<bool> {
// V8 terminates when the ArrayBuffer is not detachable. Non-detachable
// buffers are buffers that are in use by WebAssembly or asm.js.
if self.is_detachable() {
unsafe { v8__ArrayBuffer__Detach(self, &*key) }.into()
let key = key.map(|v| &*v as *const Value).unwrap_or(null());
unsafe { v8__ArrayBuffer__Detach(self, key) }.into()
} else {
Some(true)
}
Expand Down
17 changes: 8 additions & 9 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,16 +576,15 @@ fn array_buffer() {
assert!(!ab.was_detached());
assert!(ab.is_detachable());

let key = v8::undefined(scope);
assert!(ab.detach(key.into()).unwrap());
assert!(ab.detach(None).unwrap());
assert_eq!(0, ab.byte_length());
assert!(ab.was_detached());
assert!(ab.detach(key.into()).unwrap()); // Calling it twice should be a no-op.
assert!(ab.detach(None).unwrap()); // 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());
assert!(empty_ab.detach(key.into()).unwrap());
assert!(empty_ab.detach(None).unwrap());
assert!(empty_ab.was_detached());

let bs = v8::ArrayBuffer::new_backing_store(scope, 84);
Expand Down Expand Up @@ -8572,9 +8571,9 @@ fn test_detach_key() {
let buffer = v8::ArrayBuffer::new(scope, 1024);
buffer.set_detach_key(detach_key);
assert!(buffer.is_detachable());
assert_eq!(buffer.detach(v8::undefined(scope).into()), None);
assert_eq!(buffer.detach(None), None);
assert!(!buffer.was_detached());
assert_eq!(buffer.detach(detach_key), Some(true));
assert_eq!(buffer.detach(Some(detach_key)), Some(true));
assert!(buffer.was_detached());
}

Expand All @@ -8588,9 +8587,9 @@ fn test_detach_key() {
let buffer = v8::ArrayBuffer::new(scope, 1024);
buffer.set_detach_key(v8_detach_key.into());
assert!(buffer.is_detachable());
assert_eq!(buffer.detach(v8::undefined(scope).into()), None);
assert_eq!(buffer.detach(None), None);
assert!(!buffer.was_detached());
assert_eq!(buffer.detach(v8_detach_key.into()), Some(true));
assert_eq!(buffer.detach(Some(v8_detach_key.into())), Some(true));
assert!(buffer.was_detached());
}

Expand All @@ -8599,7 +8598,7 @@ fn test_detach_key() {
let buffer = v8::ArrayBuffer::new(scope, 1024);
buffer.set_detach_key(v8::undefined(scope).into());
assert!(buffer.is_detachable());
assert_eq!(buffer.detach(v8::undefined(scope).into()), Some(true));
assert_eq!(buffer.detach(Some(v8::undefined(scope).into())), Some(true));
assert!(buffer.was_detached());
}
}
Expand Down

0 comments on commit e57c3ec

Please sign in to comment.