use std::ffi::c_void;
use std::ptr::null_mut;
use crate::array_buffer::backing_store_deleter_callback;
use crate::support::SharedRef;
use crate::support::UniqueRef;
use crate::BackingStore;
use crate::BackingStoreDeleterCallback;
use crate::InIsolate;
use crate::Isolate;
use crate::Local;
use crate::SharedArrayBuffer;
use crate::ToLocal;
extern "C" {
fn v8__SharedArrayBuffer__New__with_byte_length(
isolate: *mut Isolate,
byte_length: usize,
) -> *mut SharedArrayBuffer;
fn v8__SharedArrayBuffer__New__with_backing_store(
isolate: *mut Isolate,
backing_store: *mut SharedRef<BackingStore>,
) -> *mut SharedArrayBuffer;
fn v8__SharedArrayBuffer__ByteLength(
self_: *const SharedArrayBuffer,
) -> usize;
fn v8__SharedArrayBuffer__GetBackingStore(
self_: *const SharedArrayBuffer,
) -> SharedRef<BackingStore>;
fn v8__SharedArrayBuffer__NewBackingStore__with_byte_length(
isolate: *mut Isolate,
byte_length: usize,
) -> *mut BackingStore;
fn v8__SharedArrayBuffer__NewBackingStore__with_data(
data: *mut c_void,
byte_length: usize,
deleter: BackingStoreDeleterCallback,
deleter_data: *mut c_void,
) -> *mut BackingStore;
}
impl SharedArrayBuffer {
pub fn new<'sc>(
scope: &mut impl ToLocal<'sc>,
byte_length: usize,
) -> Option<Local<'sc, SharedArrayBuffer>> {
unsafe {
Local::from_raw(v8__SharedArrayBuffer__New__with_byte_length(
scope.isolate(),
byte_length,
))
}
}
pub fn with_backing_store<'sc>(
scope: &mut impl ToLocal<'sc>,
backing_store: &mut SharedRef<BackingStore>,
) -> Local<'sc, SharedArrayBuffer> {
let isolate = scope.isolate();
let ptr = unsafe {
v8__SharedArrayBuffer__New__with_backing_store(
isolate,
&mut *backing_store,
)
};
unsafe { scope.to_local(ptr) }.unwrap()
}
pub fn byte_length(&self) -> usize {
unsafe { v8__SharedArrayBuffer__ByteLength(self) }
}
pub fn get_backing_store(&self) -> SharedRef<BackingStore> {
unsafe { v8__SharedArrayBuffer__GetBackingStore(self) }
}
pub fn new_backing_store(
scope: &mut impl InIsolate,
byte_length: usize,
) -> UniqueRef<BackingStore> {
unsafe {
UniqueRef::from_raw(
v8__SharedArrayBuffer__NewBackingStore__with_byte_length(
scope.isolate(),
byte_length,
),
)
}
}
pub fn new_backing_store_from_boxed_slice(
data: Box<[u8]>,
) -> UniqueRef<BackingStore> {
let byte_length = data.len();
let data_ptr = Box::into_raw(data) as *mut c_void;
unsafe {
UniqueRef::from_raw(v8__SharedArrayBuffer__NewBackingStore__with_data(
data_ptr,
byte_length,
backing_store_deleter_callback,
null_mut(),
))
}
}
}