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
use core::ops::Range;
use std::ops::Deref;
use std::ops::DerefMut;
use super::transl8::FromV8;
use super::transl8::ToV8;
use super::v8slice::to_ranged_buffer;
use super::v8slice::V8Slice;
use crate::magic::transl8::impl_magic;
pub struct DetachedBuffer(V8Slice);
impl_magic!(DetachedBuffer);
impl AsRef<[u8]> for DetachedBuffer {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl AsMut<[u8]> for DetachedBuffer {
fn as_mut(&mut self) -> &mut [u8] {
self.0.as_mut()
}
}
impl Deref for DetachedBuffer {
type Target = [u8];
fn deref(&self) -> &[u8] {
self.0.deref()
}
}
impl DerefMut for DetachedBuffer {
fn deref_mut(&mut self) -> &mut [u8] {
self.0.deref_mut()
}
}
impl ToV8 for DetachedBuffer {
fn to_v8<'a>(
&mut self,
scope: &mut v8::HandleScope<'a>,
) -> Result<v8::Local<'a, v8::Value>, crate::Error> {
let buffer = v8::ArrayBuffer::with_backing_store(scope, &self.0.store);
let Range { start, end } = self.0.range;
let (off, len) = (start, end - start);
let v = v8::Uint8Array::new(scope, buffer, off, len).unwrap();
Ok(v.into())
}
}
impl FromV8 for DetachedBuffer {
fn from_v8(
scope: &mut v8::HandleScope,
value: v8::Local<v8::Value>,
) -> Result<Self, crate::Error> {
let (b, range) =
to_ranged_buffer(scope, value).or(Err(crate::Error::ExpectedBuffer))?;
if !b.is_detachable() {
return Err(crate::Error::ExpectedDetachable);
}
let store = b.get_backing_store();
b.detach(); Ok(Self(V8Slice { store, range }))
}
}