2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-04-25 10:56:47 -04:00
|
|
|
use super::buffer::ZeroCopyBuf;
|
2023-01-14 23:18:58 -05:00
|
|
|
use super::transl8::FromV8;
|
|
|
|
use super::transl8::ToV8;
|
2023-04-26 20:12:39 -04:00
|
|
|
use crate::error::value_to_type_str;
|
2022-04-24 08:28:46 -04:00
|
|
|
use crate::magic::transl8::impl_magic;
|
|
|
|
use crate::Error;
|
2021-10-20 09:40:20 -04:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2022-04-24 08:28:46 -04:00
|
|
|
pub enum StringOrBuffer {
|
2022-04-25 10:56:47 -04:00
|
|
|
Buffer(ZeroCopyBuf),
|
2022-04-24 08:28:46 -04:00
|
|
|
String(String),
|
2021-10-20 09:40:20 -04:00
|
|
|
}
|
|
|
|
|
2022-04-24 08:28:46 -04:00
|
|
|
impl_magic!(StringOrBuffer);
|
2021-10-26 16:00:01 -04:00
|
|
|
|
2022-04-24 08:28:46 -04:00
|
|
|
impl Deref for StringOrBuffer {
|
|
|
|
type Target = [u8];
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
match self {
|
|
|
|
Self::Buffer(b) => b.as_ref(),
|
|
|
|
Self::String(s) => s.as_bytes(),
|
|
|
|
}
|
2021-10-20 09:40:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-20 11:52:28 -05:00
|
|
|
impl<'a> TryFrom<&'a StringOrBuffer> for &'a str {
|
|
|
|
type Error = std::str::Utf8Error;
|
|
|
|
fn try_from(value: &'a StringOrBuffer) -> Result<Self, Self::Error> {
|
|
|
|
match value {
|
|
|
|
StringOrBuffer::String(s) => Ok(s.as_str()),
|
|
|
|
StringOrBuffer::Buffer(b) => std::str::from_utf8(b.as_ref()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-24 08:28:46 -04:00
|
|
|
impl ToV8 for StringOrBuffer {
|
|
|
|
fn to_v8<'a>(
|
2022-09-01 06:24:40 -04:00
|
|
|
&mut self,
|
2022-04-24 08:28:46 -04:00
|
|
|
scope: &mut v8::HandleScope<'a>,
|
|
|
|
) -> Result<v8::Local<'a, v8::Value>, crate::Error> {
|
|
|
|
match self {
|
2022-10-20 06:33:57 -04:00
|
|
|
Self::Buffer(buf) => {
|
|
|
|
let buf: Box<[u8]> = match buf {
|
|
|
|
ZeroCopyBuf::FromV8(buf) => {
|
|
|
|
let value: &[u8] = buf;
|
|
|
|
value.into()
|
|
|
|
}
|
|
|
|
ZeroCopyBuf::Temp(_) => unreachable!(),
|
|
|
|
ZeroCopyBuf::ToV8(ref mut x) => {
|
|
|
|
x.take().expect("ZeroCopyBuf was empty")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let backing_store =
|
|
|
|
v8::ArrayBuffer::new_backing_store_from_boxed_slice(buf);
|
|
|
|
Ok(
|
|
|
|
v8::ArrayBuffer::with_backing_store(scope, &backing_store.into())
|
|
|
|
.into(),
|
|
|
|
)
|
|
|
|
}
|
2022-04-24 08:28:46 -04:00
|
|
|
Self::String(s) => crate::to_v8(scope, s),
|
|
|
|
}
|
|
|
|
}
|
2021-10-20 09:40:20 -04:00
|
|
|
}
|
|
|
|
|
2022-04-24 08:28:46 -04:00
|
|
|
impl FromV8 for StringOrBuffer {
|
|
|
|
fn from_v8(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
value: v8::Local<v8::Value>,
|
|
|
|
) -> Result<Self, crate::Error> {
|
2022-04-25 10:56:47 -04:00
|
|
|
if let Ok(buf) = ZeroCopyBuf::from_v8(scope, value) {
|
2022-04-24 08:28:46 -04:00
|
|
|
return Ok(Self::Buffer(buf));
|
|
|
|
} else if let Ok(s) = crate::from_v8(scope, value) {
|
|
|
|
return Ok(Self::String(s));
|
2021-10-20 09:40:20 -04:00
|
|
|
}
|
2023-04-26 20:12:39 -04:00
|
|
|
Err(Error::ExpectedBuffer(value_to_type_str(value)))
|
2021-10-20 09:40:20 -04:00
|
|
|
}
|
|
|
|
}
|
2022-05-13 06:53:13 -04:00
|
|
|
|
|
|
|
impl From<StringOrBuffer> for bytes::Bytes {
|
|
|
|
fn from(sob: StringOrBuffer) -> Self {
|
|
|
|
match sob {
|
|
|
|
StringOrBuffer::Buffer(b) => b.into(),
|
|
|
|
StringOrBuffer::String(s) => s.into_bytes().into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|