diff options
Diffstat (limited to 'serde_v8/magic')
-rw-r--r-- | serde_v8/magic/buffer.rs | 16 | ||||
-rw-r--r-- | serde_v8/magic/mod.rs | 1 | ||||
-rw-r--r-- | serde_v8/magic/rawbytes.rs | 96 | ||||
-rw-r--r-- | serde_v8/magic/string_or_buffer.rs | 9 | ||||
-rw-r--r-- | serde_v8/magic/v8slice.rs | 51 |
5 files changed, 173 insertions, 0 deletions
diff --git a/serde_v8/magic/buffer.rs b/serde_v8/magic/buffer.rs index 81f550bcb..da87c8b86 100644 --- a/serde_v8/magic/buffer.rs +++ b/serde_v8/magic/buffer.rs @@ -144,3 +144,19 @@ impl FromV8 for ZeroCopyBuf { Ok(Self::FromV8(V8Slice::from_v8(scope, value)?)) } } + +impl From<ZeroCopyBuf> for bytes::Bytes { + fn from(zbuf: ZeroCopyBuf) -> bytes::Bytes { + match zbuf { + ZeroCopyBuf::FromV8(v) => v.into(), + // WARNING(AaronO): potential footgun, but will disappear in future ZeroCopyBuf refactor + ZeroCopyBuf::ToV8(v) => v + .lock() + .unwrap() + .take() + .expect("ZeroCopyBuf was empty") + .into(), + ZeroCopyBuf::Temp(v) => v.into(), + } + } +} diff --git a/serde_v8/magic/mod.rs b/serde_v8/magic/mod.rs index 076699302..c9bfce573 100644 --- a/serde_v8/magic/mod.rs +++ b/serde_v8/magic/mod.rs @@ -2,6 +2,7 @@ pub mod buffer; pub mod bytestring; pub mod detached_buffer; +pub(super) mod rawbytes; pub mod string_or_buffer; pub mod transl8; pub mod u16string; diff --git a/serde_v8/magic/rawbytes.rs b/serde_v8/magic/rawbytes.rs new file mode 100644 index 000000000..d82e68717 --- /dev/null +++ b/serde_v8/magic/rawbytes.rs @@ -0,0 +1,96 @@ +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. +pub(crate) type AtomicPtr<T> = *mut T; +#[allow(unused)] +pub(crate) struct RawBytes { + ptr: *const u8, + len: usize, + // inlined "trait object" + data: AtomicPtr<()>, + vtable: &'static Vtable, +} + +impl RawBytes { + pub fn new_raw( + ptr: *const u8, + len: usize, + data: AtomicPtr<()>, + vtable: &'static Vtable, + ) -> bytes::Bytes { + RawBytes { + ptr, + len, + data, + vtable, + } + .into() + } +} + +#[allow(unused)] +pub(crate) struct Vtable { + /// fn(data, ptr, len) + pub clone: unsafe fn(&AtomicPtr<()>, *const u8, usize) -> bytes::Bytes, + /// fn(data, ptr, len) + pub drop: unsafe fn(&mut AtomicPtr<()>, *const u8, usize), +} + +impl From<RawBytes> for bytes::Bytes { + fn from(b: RawBytes) -> Self { + // SAFETY: RawBytes has the same layout as bytes::Bytes + // this is tested below, both are composed of usize-d ptrs/values + // thus aren't currently subject to rust's field re-ordering to minimize padding + unsafe { std::mem::transmute(b) } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::mem; + + const HELLO: &str = "hello"; + + // ===== impl StaticVtable ===== + + const STATIC_VTABLE: Vtable = Vtable { + clone: static_clone, + drop: static_drop, + }; + + unsafe fn static_clone( + _: &AtomicPtr<()>, + ptr: *const u8, + len: usize, + ) -> bytes::Bytes { + from_static(std::slice::from_raw_parts(ptr, len)).into() + } + + unsafe fn static_drop(_: &mut AtomicPtr<()>, _: *const u8, _: usize) { + // nothing to drop for &'static [u8] + } + + fn from_static(bytes: &'static [u8]) -> RawBytes { + RawBytes { + ptr: bytes.as_ptr(), + len: bytes.len(), + data: std::ptr::null_mut(), + vtable: &STATIC_VTABLE, + } + } + + #[test] + fn bytes_identity() { + let b1: bytes::Bytes = from_static(HELLO.as_bytes()).into(); + let b2 = bytes::Bytes::from_static(HELLO.as_bytes()); + assert_eq!(b1, b2); // Values are equal + } + + #[test] + fn bytes_layout() { + let u1: [usize; 4] = + unsafe { mem::transmute(from_static(HELLO.as_bytes())) }; + let u2: [usize; 4] = + unsafe { mem::transmute(bytes::Bytes::from_static(HELLO.as_bytes())) }; + assert_eq!(u1[..3], u2[..3]); // Struct bytes are equal besides Vtables + } +} diff --git a/serde_v8/magic/string_or_buffer.rs b/serde_v8/magic/string_or_buffer.rs index a48378b52..78954e60c 100644 --- a/serde_v8/magic/string_or_buffer.rs +++ b/serde_v8/magic/string_or_buffer.rs @@ -48,3 +48,12 @@ impl FromV8 for StringOrBuffer { Err(Error::ExpectedBuffer) } } + +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(), + } + } +} diff --git a/serde_v8/magic/v8slice.rs b/serde_v8/magic/v8slice.rs index 6950e29d9..452c857a3 100644 --- a/serde_v8/magic/v8slice.rs +++ b/serde_v8/magic/v8slice.rs @@ -3,7 +3,9 @@ use std::ops::Deref; use std::ops::DerefMut; use std::ops::Range; +use std::rc::Rc; +use super::rawbytes; use super::transl8::FromV8; /// A V8Slice encapsulates a slice that's been borrowed from a JavaScript @@ -117,3 +119,52 @@ impl AsMut<[u8]> for V8Slice { self.as_slice_mut() } } + +// Implement V8Slice -> bytes::Bytes +impl V8Slice { + fn rc_into_byte_parts(self: Rc<Self>) -> (*const u8, usize, *mut V8Slice) { + let (ptr, len) = { + let slice = self.as_ref(); + (slice.as_ptr(), slice.len()) + }; + let rc_raw = Rc::into_raw(self); + let data = rc_raw as *mut V8Slice; + (ptr, len, data) + } +} + +impl From<V8Slice> for bytes::Bytes { + fn from(v8slice: V8Slice) -> Self { + let (ptr, len, data) = Rc::new(v8slice).rc_into_byte_parts(); + rawbytes::RawBytes::new_raw(ptr, len, data.cast(), &V8SLICE_VTABLE) + } +} + +// NOTE: in the limit we could avoid extra-indirection and use the C++ shared_ptr +// but we can't store both the underlying data ptr & ctrl ptr ... so instead we +// use a shared rust ptr (Rc/Arc) that itself controls the C++ shared_ptr +const V8SLICE_VTABLE: rawbytes::Vtable = rawbytes::Vtable { + clone: v8slice_clone, + drop: v8slice_drop, +}; + +unsafe fn v8slice_clone( + data: &rawbytes::AtomicPtr<()>, + ptr: *const u8, + len: usize, +) -> bytes::Bytes { + let rc = Rc::from_raw(*data as *const V8Slice); + let (_, _, data) = rc.clone().rc_into_byte_parts(); + std::mem::forget(rc); + // NOTE: `bytes::Bytes` does bounds checking so we trust its ptr, len inputs + // and must use them to allow cloning Bytes it has sliced + rawbytes::RawBytes::new_raw(ptr, len, data.cast(), &V8SLICE_VTABLE) +} + +unsafe fn v8slice_drop( + data: &mut rawbytes::AtomicPtr<()>, + _: *const u8, + _: usize, +) { + drop(Rc::from_raw(*data as *const V8Slice)) +} |