2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
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-06-29 23:17:18 -04:00
|
|
|
use crate::magic::transl8::impl_magic;
|
2022-03-23 16:15:01 -04:00
|
|
|
use crate::Error;
|
2022-06-29 23:17:18 -04:00
|
|
|
use smallvec::SmallVec;
|
|
|
|
use std::mem::size_of;
|
2021-10-19 18:00:45 -04:00
|
|
|
|
2022-08-27 08:35:32 -04:00
|
|
|
const USIZE2X: usize = size_of::<usize>() * 2;
|
|
|
|
|
2022-06-29 23:17:18 -04:00
|
|
|
#[derive(
|
|
|
|
PartialEq,
|
|
|
|
Eq,
|
|
|
|
Clone,
|
|
|
|
Debug,
|
|
|
|
Default,
|
|
|
|
derive_more::Deref,
|
|
|
|
derive_more::DerefMut,
|
|
|
|
derive_more::AsRef,
|
|
|
|
derive_more::AsMut,
|
|
|
|
)]
|
|
|
|
#[as_mut(forward)]
|
|
|
|
#[as_ref(forward)]
|
2022-08-27 08:35:32 -04:00
|
|
|
pub struct ByteString(SmallVec<[u8; USIZE2X]>);
|
2022-03-23 16:15:01 -04:00
|
|
|
impl_magic!(ByteString);
|
2021-10-19 18:00:45 -04:00
|
|
|
|
2022-08-27 08:35:32 -04:00
|
|
|
// const-assert that Vec<u8> and SmallVec<[u8; size_of::<usize>() * 2]> have a same size.
|
2022-06-29 23:17:18 -04:00
|
|
|
// Note from https://docs.rs/smallvec/latest/smallvec/#union -
|
|
|
|
// smallvec can still be larger than Vec if the inline buffer is
|
|
|
|
// larger than two machine words.
|
2022-08-27 08:35:32 -04:00
|
|
|
const _: () =
|
|
|
|
assert!(size_of::<Vec<u8>>() == size_of::<SmallVec<[u8; USIZE2X]>>());
|
2022-06-29 23:17:18 -04:00
|
|
|
|
2022-03-23 16:15:01 -04:00
|
|
|
impl ToV8 for ByteString {
|
|
|
|
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 v =
|
|
|
|
v8::String::new_from_one_byte(scope, self, v8::NewStringType::Normal)
|
|
|
|
.unwrap();
|
|
|
|
Ok(v.into())
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 16:15:01 -04:00
|
|
|
impl FromV8 for ByteString {
|
|
|
|
fn from_v8(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
value: v8::Local<v8::Value>,
|
|
|
|
) -> Result<Self, crate::Error> {
|
|
|
|
let v8str = v8::Local::<v8::String>::try_from(value)
|
2023-04-26 20:12:39 -04:00
|
|
|
.map_err(|_| Error::ExpectedString(value_to_type_str(value)))?;
|
2022-03-23 16:15:01 -04:00
|
|
|
if !v8str.contains_only_onebyte() {
|
|
|
|
return Err(Error::ExpectedLatin1);
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
2022-03-23 16:15:01 -04:00
|
|
|
let len = v8str.length();
|
2022-06-29 23:17:18 -04:00
|
|
|
let mut buffer = SmallVec::with_capacity(len);
|
2022-07-01 09:28:06 -04:00
|
|
|
#[allow(clippy::uninit_vec)]
|
2022-03-23 16:15:01 -04:00
|
|
|
// SAFETY: we set length == capacity (see previous line),
|
|
|
|
// before immediately writing into that buffer and sanity check with an assert
|
|
|
|
unsafe {
|
|
|
|
buffer.set_len(len);
|
|
|
|
let written = v8str.write_one_byte(
|
|
|
|
scope,
|
|
|
|
&mut buffer,
|
|
|
|
0,
|
|
|
|
v8::WriteOptions::NO_NULL_TERMINATION,
|
|
|
|
);
|
|
|
|
assert!(written == len);
|
|
|
|
}
|
2022-06-29 23:17:18 -04:00
|
|
|
Ok(Self(buffer))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// smallvec does not impl From/Into traits
|
|
|
|
// like Vec<u8> does. So here we are.
|
|
|
|
|
|
|
|
impl From<Vec<u8>> for ByteString {
|
|
|
|
fn from(vec: Vec<u8>) -> Self {
|
|
|
|
ByteString(SmallVec::from_vec(vec))
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
2022-05-10 16:36:40 -04:00
|
|
|
|
|
|
|
#[allow(clippy::from_over_into)]
|
|
|
|
impl Into<Vec<u8>> for ByteString {
|
|
|
|
fn into(self) -> Vec<u8> {
|
2022-06-29 23:17:18 -04:00
|
|
|
self.0.into_vec()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&[u8]> for ByteString {
|
|
|
|
fn from(s: &[u8]) -> Self {
|
|
|
|
ByteString(SmallVec::from_slice(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&str> for ByteString {
|
|
|
|
fn from(s: &str) -> Self {
|
|
|
|
let v: Vec<u8> = s.into();
|
|
|
|
ByteString::from(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<String> for ByteString {
|
|
|
|
fn from(s: String) -> Self {
|
|
|
|
ByteString::from(s.into_bytes())
|
2022-05-10 16:36:40 -04:00
|
|
|
}
|
|
|
|
}
|