From 1cc38f5155bdc5605d74cd959660fa04f782ac63 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Thu, 20 Jan 2022 15:11:09 +0100 Subject: feat(serde_v8): deserialize ArrayBuffers (#13436) Previously we would only deserialize `ArrayBufferView`s as zero-copy bufs This avoids rewrapping `ArrayBuffers` in `ArrayBufferView`s when implementing APIs that take [BufferSource](https://webidl.spec.whatwg.org/#BufferSource) args passed through the op-layer --- serde_v8/src/magic/buffer.rs | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'serde_v8/src/magic/buffer.rs') diff --git a/serde_v8/src/magic/buffer.rs b/serde_v8/src/magic/buffer.rs index 80f2f8bc7..e6f85324e 100644 --- a/serde_v8/src/magic/buffer.rs +++ b/serde_v8/src/magic/buffer.rs @@ -1,5 +1,6 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. +use std::convert::TryFrom; use std::fmt; use std::ops::Deref; use std::ops::DerefMut; @@ -15,22 +16,29 @@ pub enum MagicBuffer { } impl MagicBuffer { - pub fn new<'s>( - scope: &mut v8::HandleScope<'s>, - view: v8::Local, - ) -> Self { - Self::try_new(scope, view).unwrap() + pub fn empty() -> Self { + MagicBuffer::ToV8(Mutex::new(Some(vec![0_u8; 0].into_boxed_slice()))) } +} - pub fn try_new<'s>( - scope: &mut v8::HandleScope<'s>, - view: v8::Local, - ) -> Result { - Ok(Self::FromV8(ZeroCopyBuf::try_new(scope, view)?)) +impl<'s> TryFrom> for MagicBuffer { + type Error = v8::DataError; + fn try_from(buffer: v8::Local) -> Result { + Ok(Self::FromV8(ZeroCopyBuf::try_from(buffer)?)) } +} - pub fn empty() -> Self { - MagicBuffer::ToV8(Mutex::new(Some(vec![0_u8; 0].into_boxed_slice()))) +// TODO(@AaronO): consider streamlining this as "ScopedValue" ? +type ScopedView<'a, 'b, 's> = ( + &'s mut v8::HandleScope<'a>, + v8::Local<'b, v8::ArrayBufferView>, +); +impl<'a, 'b, 's> TryFrom> for MagicBuffer { + type Error = v8::DataError; + fn try_from( + scoped_view: ScopedView<'a, 'b, 's>, + ) -> Result { + Ok(Self::FromV8(ZeroCopyBuf::try_from(scoped_view)?)) } } -- cgit v1.2.3