From 36a1de7f659da0416058f86ba1828506e78c2716 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Wed, 30 Nov 2022 17:19:52 +0530 Subject: [PATCH] Optional key in `v8::ArrayBuffer:detach` --- src/array_buffer.rs | 6 ++++-- tests/test_api.rs | 17 ++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/array_buffer.rs b/src/array_buffer.rs index b77f4642ec..52ed9c2fca 100644 --- a/src/array_buffer.rs +++ b/src/array_buffer.rs @@ -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) -> Option { + pub fn detach(&self, key: Option>) -> Option { // 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) } diff --git a/tests/test_api.rs b/tests/test_api.rs index 1b281e265e..3730fdb878 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -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()); } }