1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::ffi::c_void;
use std::ptr::null_mut;
use crate::array_buffer::boxed_slice_deleter_callback;
use crate::array_buffer::vec_deleter_callback;
use crate::support::SharedRef;
use crate::support::UniqueRef;
use crate::BackingStore;
use crate::BackingStoreDeleterCallback;
use crate::HandleScope;
use crate::Isolate;
use crate::Local;
use crate::SharedArrayBuffer;
extern "C" {
fn v8__SharedArrayBuffer__New__with_byte_length(
isolate: *mut Isolate,
byte_length: usize,
) -> *const SharedArrayBuffer;
fn v8__SharedArrayBuffer__New__with_backing_store(
isolate: *mut Isolate,
backing_store: *const SharedRef<BackingStore>,
) -> *const SharedArrayBuffer;
fn v8__SharedArrayBuffer__ByteLength(this: *const SharedArrayBuffer)
-> usize;
fn v8__SharedArrayBuffer__GetBackingStore(
this: *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 {
#[inline(always)]
pub fn new<'s>(
scope: &mut HandleScope<'s>,
byte_length: usize,
) -> Option<Local<'s, SharedArrayBuffer>> {
unsafe {
scope.cast_local(|sd| {
v8__SharedArrayBuffer__New__with_byte_length(
sd.get_isolate_ptr(),
byte_length,
)
})
}
}
#[inline(always)]
pub fn with_backing_store<'s>(
scope: &mut HandleScope<'s>,
backing_store: &SharedRef<BackingStore>,
) -> Local<'s, SharedArrayBuffer> {
unsafe {
scope.cast_local(|sd| {
v8__SharedArrayBuffer__New__with_backing_store(
sd.get_isolate_ptr(),
backing_store,
)
})
}
.unwrap()
}
#[inline(always)]
pub fn byte_length(&self) -> usize {
unsafe { v8__SharedArrayBuffer__ByteLength(self) }
}
#[inline(always)]
pub fn get_backing_store(&self) -> SharedRef<BackingStore> {
unsafe { v8__SharedArrayBuffer__GetBackingStore(self) }
}
#[inline(always)]
pub fn new_backing_store(
scope: &mut Isolate,
byte_length: usize,
) -> UniqueRef<BackingStore> {
unsafe {
UniqueRef::from_raw(
v8__SharedArrayBuffer__NewBackingStore__with_byte_length(
scope,
byte_length,
),
)
}
}
#[inline(always)]
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,
boxed_slice_deleter_callback,
null_mut(),
))
}
}
#[inline(always)]
pub fn new_backing_store_from_vec(
mut data: Vec<u8>,
) -> UniqueRef<BackingStore> {
let byte_length = data.len();
let data_ptr = data.as_mut_ptr() as *mut c_void;
std::mem::forget(data);
unsafe {
UniqueRef::from_raw(v8__SharedArrayBuffer__NewBackingStore__with_data(
data_ptr,
byte_length,
vec_deleter_callback,
null_mut(),
))
}
}
}