1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-27 16:10:57 -05:00
denoland-deno/serde_v8/magic/detached_buffer.rs
David Sherret 2fee8394a9
chore: update copyright year to 2023 (#17247)
Yearly tradition of creating extra noise in git.
2023-01-05 13:05:49 +01:00

69 lines
1.7 KiB
Rust

// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
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;
// A buffer that detaches when deserialized from JS
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(None); // Detach
Ok(Self(V8Slice { store, range }))
}
}