2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-10-19 18:00:45 -04:00
|
|
|
|
2022-04-24 08:28:46 -04:00
|
|
|
use std::fmt::Debug;
|
2021-10-19 18:00:45 -04:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::ops::DerefMut;
|
|
|
|
|
2022-03-23 16:15:01 -04:00
|
|
|
use super::transl8::FromV8;
|
|
|
|
use super::transl8::ToV8;
|
2022-04-25 10:56:47 -04:00
|
|
|
use super::v8slice::V8Slice;
|
2022-03-23 16:15:01 -04:00
|
|
|
use crate::magic::transl8::impl_magic;
|
2021-10-19 18:00:45 -04:00
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
pub struct JsBuffer(V8Slice);
|
2022-04-22 06:49:08 -04:00
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
impl_magic!(JsBuffer);
|
2021-10-19 18:00:45 -04:00
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
impl Debug for JsBuffer {
|
2022-04-24 08:28:46 -04:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2023-06-22 17:37:56 -04:00
|
|
|
f.debug_list().entries(self.0.as_ref().iter()).finish()
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
2022-01-20 09:11:09 -05:00
|
|
|
}
|
2021-10-19 18:00:45 -04:00
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
impl Clone for JsBuffer {
|
2021-10-19 18:00:45 -04:00
|
|
|
fn clone(&self) -> Self {
|
2023-06-22 17:37:56 -04:00
|
|
|
Self(self.0.clone())
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
impl AsRef<[u8]> for JsBuffer {
|
2021-10-19 18:00:45 -04:00
|
|
|
fn as_ref(&self) -> &[u8] {
|
2023-06-22 17:37:56 -04:00
|
|
|
&self.0
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
impl AsMut<[u8]> for JsBuffer {
|
2021-10-19 18:00:45 -04:00
|
|
|
fn as_mut(&mut self) -> &mut [u8] {
|
2023-06-22 17:37:56 -04:00
|
|
|
&mut self.0
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
impl Deref for JsBuffer {
|
2021-10-19 18:00:45 -04:00
|
|
|
type Target = [u8];
|
|
|
|
fn deref(&self) -> &[u8] {
|
2023-06-22 17:37:56 -04:00
|
|
|
&self.0
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
impl DerefMut for JsBuffer {
|
2021-10-19 18:00:45 -04:00
|
|
|
fn deref_mut(&mut self) -> &mut [u8] {
|
2023-06-22 17:37:56 -04:00
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromV8 for JsBuffer {
|
|
|
|
fn from_v8(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
value: v8::Local<v8::Value>,
|
|
|
|
) -> Result<Self, crate::Error> {
|
|
|
|
Ok(Self(V8Slice::from_v8(scope, value)?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<JsBuffer> for bytes::Bytes {
|
|
|
|
fn from(zbuf: JsBuffer) -> bytes::Bytes {
|
|
|
|
zbuf.0.into()
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
// NOTE(bartlomieju): we use Option here, because `to_v8()` uses `&mut self`
|
|
|
|
// instead of `self` which is dictated by the `serde` API.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ToJsBuffer(Option<Box<[u8]>>);
|
|
|
|
|
|
|
|
impl_magic!(ToJsBuffer);
|
|
|
|
|
|
|
|
impl ToJsBuffer {
|
|
|
|
pub fn empty() -> Self {
|
|
|
|
ToJsBuffer(Some(vec![0_u8; 0].into_boxed_slice()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Box<[u8]>> for ToJsBuffer {
|
2021-10-19 18:00:45 -04:00
|
|
|
fn from(buf: Box<[u8]>) -> Self {
|
2023-06-22 17:37:56 -04:00
|
|
|
ToJsBuffer(Some(buf))
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
impl From<Vec<u8>> for ToJsBuffer {
|
2021-10-19 18:00:45 -04:00
|
|
|
fn from(vec: Vec<u8>) -> Self {
|
|
|
|
vec.into_boxed_slice().into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
impl ToV8 for ToJsBuffer {
|
2022-03-23 16:15:01 -04:00
|
|
|
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> {
|
2023-06-22 17:37:56 -04:00
|
|
|
let buf: Box<[u8]> = self.0.take().expect("RustToV8Buf was empty");
|
2021-10-19 18:00:45 -04:00
|
|
|
|
2022-03-23 16:15:01 -04:00
|
|
|
if buf.is_empty() {
|
|
|
|
let ab = v8::ArrayBuffer::new(scope, 0);
|
|
|
|
return Ok(
|
|
|
|
v8::Uint8Array::new(scope, ab, 0, 0)
|
|
|
|
.expect("Failed to create Uint8Array")
|
|
|
|
.into(),
|
|
|
|
);
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
2023-06-22 17:37:56 -04:00
|
|
|
let buf_len: usize = buf.len();
|
2022-03-23 16:15:01 -04:00
|
|
|
let backing_store =
|
|
|
|
v8::ArrayBuffer::new_backing_store_from_boxed_slice(buf);
|
|
|
|
let backing_store_shared = backing_store.make_shared();
|
|
|
|
let ab = v8::ArrayBuffer::with_backing_store(scope, &backing_store_shared);
|
|
|
|
Ok(
|
|
|
|
v8::Uint8Array::new(scope, ab, 0, buf_len)
|
|
|
|
.expect("Failed to create Uint8Array")
|
|
|
|
.into(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|