From cb8bcb4f1b2fc9390581842ce98957d8f18bf193 Mon Sep 17 00:00:00 2001 From: Levente Kurusa Date: Mon, 27 Mar 2023 19:33:58 +0200 Subject: [PATCH] ArrayBuffer: add binding for `IsResizableByUserJavaScript` (#1205) --- src/array_buffer.rs | 11 +++++++++++ src/binding.cc | 4 ++++ tests/test_api.rs | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/src/array_buffer.rs b/src/array_buffer.rs index 52ed9c2f..523ea45d 100644 --- a/src/array_buffer.rs +++ b/src/array_buffer.rs @@ -64,6 +64,9 @@ extern "C" { fn v8__BackingStore__Data(this: *const BackingStore) -> *mut c_void; fn v8__BackingStore__ByteLength(this: *const BackingStore) -> usize; fn v8__BackingStore__IsShared(this: *const BackingStore) -> bool; + fn v8__BackingStore__IsResizableByUserJavaScript( + this: *const BackingStore, + ) -> bool; fn v8__BackingStore__DELETE(this: *mut BackingStore); fn std__shared_ptr__v8__BackingStore__COPY( @@ -304,6 +307,14 @@ impl BackingStore { pub fn is_shared(&self) -> bool { unsafe { v8__BackingStore__IsShared(self) } } + + /// Indicates whether the backing store was created for a resizable ArrayBuffer + /// or a growable SharedArrayBuffer, and thus may be resized by user + /// JavaScript code. + #[inline(always)] + pub fn is_resizable_by_user_javascript(&self) -> bool { + unsafe { v8__BackingStore__IsResizableByUserJavaScript(self) } + } } impl Deref for BackingStore { diff --git a/src/binding.cc b/src/binding.cc index f142f5a7..0125e712 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -846,6 +846,10 @@ two_pointers_t v8__ArrayBuffer__GetBackingStore(const v8::ArrayBuffer& self) { return make_pod(ptr_to_local(&self)->GetBackingStore()); } +bool v8__BackingStore__IsResizableByUserJavaScript(const v8::BackingStore& self) { + return ptr_to_local(&self)->IsResizableByUserJavaScript(); +} + void* v8__ArrayBuffer__Data(const v8::ArrayBuffer& self) { return ptr_to_local(&self)->Data(); } diff --git a/tests/test_api.rs b/tests/test_api.rs index c6cc8ec1..dea0bf27 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -7701,6 +7701,28 @@ fn backing_store_data() { ); } +#[test] +fn backing_store_resizable() { + let _setup_guard = setup::parallel_test(); + + let v = vec![1, 2, 3, 4, 5]; + let store_fixed = + v8::ArrayBuffer::new_backing_store_from_vec(v).make_shared(); + assert!(!store_fixed.is_resizable_by_user_javascript()); + + let mut isolate = v8::Isolate::new(Default::default()); + let mut scope = v8::HandleScope::new(&mut isolate); + let context = v8::Context::new(&mut scope); + let mut scope = v8::ContextScope::new(&mut scope, context); + + let ab_val = + eval(&mut scope, "new ArrayBuffer(100, {maxByteLength: 200})").unwrap(); + assert!(ab_val.is_array_buffer()); + let ab = v8::Local::::try_from(ab_val).unwrap(); + let store_resizable = ab.get_backing_store(); + assert!(store_resizable.is_resizable_by_user_javascript()); +} + #[test] fn current_stack_trace() { // Setup isolate