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

Optional key in v8::ArrayBuffer:detach (#1141)

This commit is contained in:
Divy Srivastava 2022-11-30 06:33:28 -08:00 committed by GitHub
parent 0b9423a0ee
commit e57c3ec90f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 11 deletions

View file

@ -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;
@ -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)
}

View file

@ -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);
@ -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());
}
@ -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());
}
@ -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());
}
}