2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-10-19 18:00:45 -04:00
|
|
|
|
2022-04-24 08:28:46 -04:00
|
|
|
use std::fmt::Debug;
|
2021-10-19 18:00:45 -04:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::ops::DerefMut;
|
|
|
|
|
2022-03-23 16:15:01 -04:00
|
|
|
use super::transl8::FromV8;
|
|
|
|
use super::transl8::ToV8;
|
2022-04-25 10:56:47 -04:00
|
|
|
use super::v8slice::V8Slice;
|
2022-03-23 16:15:01 -04:00
|
|
|
use crate::magic::transl8::impl_magic;
|
2021-10-19 18:00:45 -04:00
|
|
|
|
2022-04-25 10:56:47 -04:00
|
|
|
// An asymmetric wrapper around V8Slice,
|
2021-10-19 18:00:45 -04:00
|
|
|
// allowing us to use a single type for familiarity
|
2022-04-25 10:56:47 -04:00
|
|
|
pub enum ZeroCopyBuf {
|
|
|
|
FromV8(V8Slice),
|
2022-09-13 11:26:59 -04:00
|
|
|
ToV8(Option<Box<[u8]>>),
|
2022-04-25 10:56:47 -04:00
|
|
|
// Variant of the ZeroCopyBuf than is never exposed to the JS.
|
2022-04-22 06:49:08 -04:00
|
|
|
// Generally used to pass Vec<u8> backed buffers to resource methods.
|
|
|
|
Temp(Vec<u8>),
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
2022-04-22 06:49:08 -04:00
|
|
|
|
2022-04-25 10:56:47 -04:00
|
|
|
impl_magic!(ZeroCopyBuf);
|
2021-10-19 18:00:45 -04:00
|
|
|
|
2022-04-25 10:56:47 -04:00
|
|
|
impl Debug for ZeroCopyBuf {
|
2022-04-24 08:28:46 -04:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_list().entries(self.as_ref().iter()).finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 10:56:47 -04:00
|
|
|
impl ZeroCopyBuf {
|
2022-01-20 09:11:09 -05:00
|
|
|
pub fn empty() -> Self {
|
2022-09-13 11:26:59 -04:00
|
|
|
ZeroCopyBuf::ToV8(Some(vec![0_u8; 0].into_boxed_slice()))
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
2022-04-22 06:49:08 -04:00
|
|
|
|
|
|
|
pub fn new_temp(vec: Vec<u8>) -> Self {
|
2022-04-25 10:56:47 -04:00
|
|
|
ZeroCopyBuf::Temp(vec)
|
2022-04-22 06:49:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(@littledivy): Temporary, this needs a refactor.
|
|
|
|
pub fn to_temp(self) -> Vec<u8> {
|
|
|
|
match self {
|
2022-04-25 10:56:47 -04:00
|
|
|
ZeroCopyBuf::Temp(vec) => vec,
|
2022-04-22 06:49:08 -04:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 09:11:09 -05:00
|
|
|
}
|
2021-10-19 18:00:45 -04:00
|
|
|
|
2022-04-25 10:56:47 -04:00
|
|
|
impl Clone for ZeroCopyBuf {
|
2021-10-19 18:00:45 -04:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
match self {
|
|
|
|
Self::FromV8(zbuf) => Self::FromV8(zbuf.clone()),
|
2022-04-22 06:49:08 -04:00
|
|
|
Self::Temp(vec) => Self::Temp(vec.clone()),
|
2022-04-25 10:56:47 -04:00
|
|
|
Self::ToV8(_) => panic!("Don't Clone a ZeroCopyBuf sent to v8"),
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 10:56:47 -04:00
|
|
|
impl AsRef<[u8]> for ZeroCopyBuf {
|
2021-10-19 18:00:45 -04:00
|
|
|
fn as_ref(&self) -> &[u8] {
|
2022-08-21 13:31:14 -04:00
|
|
|
self
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 10:56:47 -04:00
|
|
|
impl AsMut<[u8]> for ZeroCopyBuf {
|
2021-10-19 18:00:45 -04:00
|
|
|
fn as_mut(&mut self) -> &mut [u8] {
|
|
|
|
&mut *self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 10:56:47 -04:00
|
|
|
impl Deref for ZeroCopyBuf {
|
2021-10-19 18:00:45 -04:00
|
|
|
type Target = [u8];
|
|
|
|
fn deref(&self) -> &[u8] {
|
|
|
|
match self {
|
2022-08-21 13:31:14 -04:00
|
|
|
Self::FromV8(buf) => buf,
|
|
|
|
Self::Temp(vec) => vec,
|
2022-04-25 10:56:47 -04:00
|
|
|
Self::ToV8(_) => panic!("Don't Deref a ZeroCopyBuf sent to v8"),
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 10:56:47 -04:00
|
|
|
impl DerefMut for ZeroCopyBuf {
|
2021-10-19 18:00:45 -04:00
|
|
|
fn deref_mut(&mut self) -> &mut [u8] {
|
|
|
|
match self {
|
|
|
|
Self::FromV8(buf) => &mut *buf,
|
2022-04-22 06:49:08 -04:00
|
|
|
Self::Temp(vec) => &mut *vec,
|
2022-04-25 10:56:47 -04:00
|
|
|
Self::ToV8(_) => panic!("Don't Deref a ZeroCopyBuf sent to v8"),
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 10:56:47 -04:00
|
|
|
impl From<Box<[u8]>> for ZeroCopyBuf {
|
2021-10-19 18:00:45 -04:00
|
|
|
fn from(buf: Box<[u8]>) -> Self {
|
2022-09-13 11:26:59 -04:00
|
|
|
ZeroCopyBuf::ToV8(Some(buf))
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 10:56:47 -04:00
|
|
|
impl From<Vec<u8>> for ZeroCopyBuf {
|
2021-10-19 18:00:45 -04:00
|
|
|
fn from(vec: Vec<u8>) -> Self {
|
|
|
|
vec.into_boxed_slice().into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 10:56:47 -04:00
|
|
|
impl ToV8 for ZeroCopyBuf {
|
2022-03-23 16:15:01 -04:00
|
|
|
fn to_v8<'a>(
|
2022-09-01 06:24:40 -04:00
|
|
|
&mut self,
|
2022-03-23 16:15:01 -04:00
|
|
|
scope: &mut v8::HandleScope<'a>,
|
|
|
|
) -> Result<v8::Local<'a, v8::Value>, crate::Error> {
|
|
|
|
let buf: Box<[u8]> = match self {
|
2021-10-19 18:00:45 -04:00
|
|
|
Self::FromV8(buf) => {
|
|
|
|
let value: &[u8] = buf;
|
|
|
|
value.into()
|
|
|
|
}
|
2022-04-22 06:49:08 -04:00
|
|
|
Self::Temp(_) => unreachable!(),
|
2022-09-13 11:26:59 -04:00
|
|
|
Self::ToV8(ref mut x) => x.take().expect("ZeroCopyBuf was empty"),
|
2021-10-19 18:00:45 -04:00
|
|
|
};
|
|
|
|
|
2022-03-23 16:15:01 -04:00
|
|
|
if buf.is_empty() {
|
|
|
|
let ab = v8::ArrayBuffer::new(scope, 0);
|
|
|
|
return Ok(
|
|
|
|
v8::Uint8Array::new(scope, ab, 0, 0)
|
|
|
|
.expect("Failed to create Uint8Array")
|
|
|
|
.into(),
|
|
|
|
);
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
2022-03-23 16:15:01 -04:00
|
|
|
let buf_len = buf.len();
|
|
|
|
let backing_store =
|
|
|
|
v8::ArrayBuffer::new_backing_store_from_boxed_slice(buf);
|
|
|
|
let backing_store_shared = backing_store.make_shared();
|
|
|
|
let ab = v8::ArrayBuffer::with_backing_store(scope, &backing_store_shared);
|
|
|
|
Ok(
|
|
|
|
v8::Uint8Array::new(scope, ab, 0, buf_len)
|
|
|
|
.expect("Failed to create Uint8Array")
|
|
|
|
.into(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-10-19 18:00:45 -04:00
|
|
|
|
2022-04-25 10:56:47 -04:00
|
|
|
impl FromV8 for ZeroCopyBuf {
|
2022-03-23 16:15:01 -04:00
|
|
|
fn from_v8(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
value: v8::Local<v8::Value>,
|
|
|
|
) -> Result<Self, crate::Error> {
|
2022-04-25 10:56:47 -04:00
|
|
|
Ok(Self::FromV8(V8Slice::from_v8(scope, value)?))
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
2022-05-13 06:53:13 -04:00
|
|
|
|
|
|
|
impl From<ZeroCopyBuf> for bytes::Bytes {
|
|
|
|
fn from(zbuf: ZeroCopyBuf) -> bytes::Bytes {
|
|
|
|
match zbuf {
|
|
|
|
ZeroCopyBuf::FromV8(v) => v.into(),
|
2022-09-13 11:26:59 -04:00
|
|
|
ZeroCopyBuf::ToV8(mut v) => {
|
|
|
|
v.take().expect("ZeroCopyBuf was empty").into()
|
|
|
|
}
|
2022-05-13 06:53:13 -04:00
|
|
|
ZeroCopyBuf::Temp(v) => v.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|