summaryrefslogtreecommitdiff
path: root/serde_v8/src/magic/buffer.rs
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@gmail.com>2022-01-20 15:11:09 +0100
committerGitHub <noreply@github.com>2022-01-20 15:11:09 +0100
commit1cc38f5155bdc5605d74cd959660fa04f782ac63 (patch)
tree24c435bf3bdb0603625da3286f6133761b5073b5 /serde_v8/src/magic/buffer.rs
parent443d8fc41cab405dbdd967ef7eb86f520517280f (diff)
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
Diffstat (limited to 'serde_v8/src/magic/buffer.rs')
-rw-r--r--serde_v8/src/magic/buffer.rs32
1 files changed, 20 insertions, 12 deletions
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<v8::ArrayBufferView>,
- ) -> 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<v8::ArrayBufferView>,
- ) -> Result<Self, v8::DataError> {
- Ok(Self::FromV8(ZeroCopyBuf::try_new(scope, view)?))
+impl<'s> TryFrom<v8::Local<'s, v8::ArrayBuffer>> for MagicBuffer {
+ type Error = v8::DataError;
+ fn try_from(buffer: v8::Local<v8::ArrayBuffer>) -> Result<Self, Self::Error> {
+ 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<ScopedView<'a, 'b, 's>> for MagicBuffer {
+ type Error = v8::DataError;
+ fn try_from(
+ scoped_view: ScopedView<'a, 'b, 's>,
+ ) -> Result<Self, Self::Error> {
+ Ok(Self::FromV8(ZeroCopyBuf::try_from(scoped_view)?))
}
}