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
|
|
|
use serde::ser;
|
|
|
|
use serde::ser::Serialize;
|
|
|
|
|
|
|
|
use std::cell::RefCell;
|
2022-10-07 06:54:01 -04:00
|
|
|
use std::ops::DerefMut;
|
2021-10-19 18:00:45 -04:00
|
|
|
|
2023-01-14 23:18:58 -05:00
|
|
|
use crate::error::Error;
|
|
|
|
use crate::error::Result;
|
2021-10-19 18:00:45 -04:00
|
|
|
use crate::keys::v8_struct_key;
|
2023-01-14 23:18:58 -05:00
|
|
|
use crate::magic;
|
|
|
|
use crate::magic::transl8::opaque_deref_mut;
|
|
|
|
use crate::magic::transl8::opaque_recv;
|
|
|
|
use crate::magic::transl8::MagicType;
|
|
|
|
use crate::magic::transl8::ToV8;
|
2022-03-23 16:15:01 -04:00
|
|
|
use crate::magic::transl8::MAGIC_FIELD;
|
2023-03-22 00:13:24 -04:00
|
|
|
use crate::AnyValue;
|
2023-03-16 12:59:47 -04:00
|
|
|
use crate::BigInt;
|
2023-01-14 23:18:58 -05:00
|
|
|
use crate::ByteString;
|
|
|
|
use crate::DetachedBuffer;
|
2023-02-22 12:32:38 -05:00
|
|
|
use crate::ExternalPointer;
|
2023-01-14 23:18:58 -05:00
|
|
|
use crate::StringOrBuffer;
|
|
|
|
use crate::U16String;
|
|
|
|
use crate::ZeroCopyBuf;
|
2021-10-19 18:00:45 -04:00
|
|
|
|
|
|
|
type JsValue<'s> = v8::Local<'s, v8::Value>;
|
|
|
|
type JsResult<'s> = Result<JsValue<'s>>;
|
|
|
|
|
|
|
|
type ScopePtr<'a, 'b, 'c> = &'c RefCell<&'b mut v8::HandleScope<'a>>;
|
|
|
|
|
|
|
|
pub fn to_v8<'a, T>(scope: &mut v8::HandleScope<'a>, input: T) -> JsResult<'a>
|
|
|
|
where
|
|
|
|
T: Serialize,
|
|
|
|
{
|
|
|
|
let scopeptr = RefCell::new(scope);
|
|
|
|
let serializer = Serializer::new(&scopeptr);
|
|
|
|
|
|
|
|
input.serialize(serializer)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps other serializers into an enum tagged variant form.
|
|
|
|
/// Uses {"Variant": ...payload...} for compatibility with serde-json.
|
|
|
|
pub struct VariantSerializer<'a, 'b, 'c, S> {
|
|
|
|
inner: S,
|
|
|
|
scope: ScopePtr<'a, 'b, 'c>,
|
|
|
|
variant: &'static str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'c, S> VariantSerializer<'a, 'b, 'c, S> {
|
|
|
|
pub fn new(
|
|
|
|
scope: ScopePtr<'a, 'b, 'c>,
|
|
|
|
variant: &'static str,
|
|
|
|
inner: S,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
inner,
|
|
|
|
scope,
|
|
|
|
variant,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self, inner: impl FnOnce(S) -> JsResult<'a>) -> JsResult<'a> {
|
|
|
|
let value = inner(self.inner)?;
|
|
|
|
let scope = &mut *self.scope.borrow_mut();
|
|
|
|
let null = v8::null(scope).into();
|
|
|
|
let key = v8_struct_key(scope, self.variant).into();
|
|
|
|
let obj =
|
|
|
|
v8::Object::with_prototype_and_properties(scope, null, &[key], &[value]);
|
|
|
|
Ok(obj.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'c, S> ser::SerializeTupleVariant
|
|
|
|
for VariantSerializer<'a, 'b, 'c, S>
|
|
|
|
where
|
|
|
|
S: ser::SerializeTupleStruct<Ok = JsValue<'a>, Error = Error>,
|
|
|
|
{
|
|
|
|
type Ok = JsValue<'a>;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn serialize_field<T: ?Sized + Serialize>(
|
|
|
|
&mut self,
|
|
|
|
value: &T,
|
|
|
|
) -> Result<()> {
|
|
|
|
self.inner.serialize_field(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> JsResult<'a> {
|
|
|
|
self.end(S::end)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'c, S> ser::SerializeStructVariant
|
|
|
|
for VariantSerializer<'a, 'b, 'c, S>
|
|
|
|
where
|
|
|
|
S: ser::SerializeStruct<Ok = JsValue<'a>, Error = Error>,
|
|
|
|
{
|
|
|
|
type Ok = JsValue<'a>;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn serialize_field<T: ?Sized + Serialize>(
|
|
|
|
&mut self,
|
|
|
|
key: &'static str,
|
|
|
|
value: &T,
|
|
|
|
) -> Result<()> {
|
|
|
|
self.inner.serialize_field(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> JsResult<'a> {
|
|
|
|
self.end(S::end)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ArraySerializer<'a, 'b, 'c> {
|
|
|
|
pending: Vec<JsValue<'a>>,
|
|
|
|
scope: ScopePtr<'a, 'b, 'c>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'c> ArraySerializer<'a, 'b, 'c> {
|
|
|
|
pub fn new(scope: ScopePtr<'a, 'b, 'c>, len: Option<usize>) -> Self {
|
|
|
|
let pending = match len {
|
|
|
|
Some(len) => Vec::with_capacity(len),
|
|
|
|
None => vec![],
|
|
|
|
};
|
|
|
|
Self { pending, scope }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'c> ser::SerializeSeq for ArraySerializer<'a, 'b, 'c> {
|
|
|
|
type Ok = JsValue<'a>;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn serialize_element<T: ?Sized + Serialize>(
|
|
|
|
&mut self,
|
|
|
|
value: &T,
|
|
|
|
) -> Result<()> {
|
|
|
|
let x = value.serialize(Serializer::new(self.scope))?;
|
|
|
|
self.pending.push(x);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> JsResult<'a> {
|
|
|
|
let elements = self.pending.iter().as_slice();
|
|
|
|
let scope = &mut *self.scope.borrow_mut();
|
|
|
|
let arr = v8::Array::new_with_elements(scope, elements);
|
|
|
|
Ok(arr.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'c> ser::SerializeTuple for ArraySerializer<'a, 'b, 'c> {
|
|
|
|
type Ok = JsValue<'a>;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn serialize_element<T: ?Sized + Serialize>(
|
|
|
|
&mut self,
|
|
|
|
value: &T,
|
|
|
|
) -> Result<()> {
|
|
|
|
ser::SerializeSeq::serialize_element(self, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> JsResult<'a> {
|
|
|
|
ser::SerializeSeq::end(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'c> ser::SerializeTupleStruct for ArraySerializer<'a, 'b, 'c> {
|
|
|
|
type Ok = JsValue<'a>;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn serialize_field<T: ?Sized + Serialize>(
|
|
|
|
&mut self,
|
|
|
|
value: &T,
|
|
|
|
) -> Result<()> {
|
|
|
|
ser::SerializeTuple::serialize_element(self, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> JsResult<'a> {
|
|
|
|
ser::SerializeTuple::end(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ObjectSerializer<'a, 'b, 'c> {
|
|
|
|
scope: ScopePtr<'a, 'b, 'c>,
|
|
|
|
keys: Vec<v8::Local<'a, v8::Name>>,
|
|
|
|
values: Vec<JsValue<'a>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'c> ObjectSerializer<'a, 'b, 'c> {
|
|
|
|
pub fn new(scope: ScopePtr<'a, 'b, 'c>, len: usize) -> Self {
|
|
|
|
let keys = Vec::with_capacity(len);
|
|
|
|
let values = Vec::with_capacity(len);
|
|
|
|
Self {
|
|
|
|
scope,
|
|
|
|
keys,
|
|
|
|
values,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'c> ser::SerializeStruct for ObjectSerializer<'a, 'b, 'c> {
|
|
|
|
type Ok = JsValue<'a>;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn serialize_field<T: ?Sized + Serialize>(
|
|
|
|
&mut self,
|
|
|
|
key: &'static str,
|
|
|
|
value: &T,
|
|
|
|
) -> Result<()> {
|
|
|
|
let value = value.serialize(Serializer::new(self.scope))?;
|
|
|
|
let scope = &mut *self.scope.borrow_mut();
|
|
|
|
let key = v8_struct_key(scope, key).into();
|
|
|
|
self.keys.push(key);
|
|
|
|
self.values.push(value);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> JsResult<'a> {
|
|
|
|
let scope = &mut *self.scope.borrow_mut();
|
|
|
|
let null = v8::null(scope);
|
|
|
|
let obj = v8::Object::with_prototype_and_properties(
|
|
|
|
scope,
|
|
|
|
null.into(),
|
|
|
|
&self.keys[..],
|
|
|
|
&self.values[..],
|
|
|
|
);
|
|
|
|
Ok(obj.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 16:15:01 -04:00
|
|
|
pub struct MagicalSerializer<'a, 'b, 'c, T> {
|
2021-10-19 18:00:45 -04:00
|
|
|
scope: ScopePtr<'a, 'b, 'c>,
|
2022-03-23 16:15:01 -04:00
|
|
|
opaque: u64,
|
|
|
|
p1: std::marker::PhantomData<T>,
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
|
2022-03-23 16:15:01 -04:00
|
|
|
impl<'a, 'b, 'c, T> MagicalSerializer<'a, 'b, 'c, T> {
|
|
|
|
pub fn new(scope: ScopePtr<'a, 'b, 'c>) -> MagicalSerializer<'a, 'b, 'c, T> {
|
2021-10-19 18:00:45 -04:00
|
|
|
Self {
|
|
|
|
scope,
|
2022-03-23 16:15:01 -04:00
|
|
|
opaque: 0,
|
|
|
|
p1: std::marker::PhantomData::<T> {},
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 16:15:01 -04:00
|
|
|
impl<'a, 'b, 'c, T: MagicType + ToV8> ser::SerializeStruct
|
|
|
|
for MagicalSerializer<'a, 'b, 'c, T>
|
2021-10-19 18:00:45 -04:00
|
|
|
{
|
|
|
|
type Ok = JsValue<'a>;
|
|
|
|
type Error = Error;
|
|
|
|
|
2022-03-23 16:15:01 -04:00
|
|
|
fn serialize_field<U: ?Sized + Serialize>(
|
2022-03-15 19:22:00 -04:00
|
|
|
&mut self,
|
|
|
|
key: &'static str,
|
2022-03-23 16:15:01 -04:00
|
|
|
value: &U,
|
2022-03-15 19:22:00 -04:00
|
|
|
) -> Result<()> {
|
2022-03-23 16:15:01 -04:00
|
|
|
assert_eq!(key, MAGIC_FIELD);
|
|
|
|
let ptr: &U = value;
|
|
|
|
// SAFETY: MagicalSerializer only ever receives single field u64s,
|
|
|
|
// type-safety is ensured by MAGIC_NAME checks in `serialize_struct()`
|
|
|
|
self.opaque = unsafe { opaque_recv(ptr) };
|
2022-03-15 19:22:00 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> JsResult<'a> {
|
2022-03-23 16:15:01 -04:00
|
|
|
// SAFETY: transerialization assumptions imply `T` is still alive.
|
2022-09-01 06:24:40 -04:00
|
|
|
let x: &mut T = unsafe { opaque_deref_mut(self.opaque) };
|
2022-03-15 19:22:00 -04:00
|
|
|
let scope = &mut *self.scope.borrow_mut();
|
2022-03-23 16:15:01 -04:00
|
|
|
x.to_v8(scope)
|
2022-03-15 19:22:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:00:45 -04:00
|
|
|
// Dispatches between magic and regular struct serializers
|
|
|
|
pub enum StructSerializers<'a, 'b, 'c> {
|
2023-02-22 12:32:38 -05:00
|
|
|
ExternalPointer(MagicalSerializer<'a, 'b, 'c, magic::ExternalPointer>),
|
2022-03-23 16:15:01 -04:00
|
|
|
Magic(MagicalSerializer<'a, 'b, 'c, magic::Value<'a>>),
|
2022-04-25 10:56:47 -04:00
|
|
|
ZeroCopyBuf(MagicalSerializer<'a, 'b, 'c, ZeroCopyBuf>),
|
2023-03-22 00:13:24 -04:00
|
|
|
MagicAnyValue(MagicalSerializer<'a, 'b, 'c, AnyValue>),
|
2022-04-02 07:35:57 -04:00
|
|
|
MagicDetached(MagicalSerializer<'a, 'b, 'c, DetachedBuffer>),
|
2022-03-23 16:15:01 -04:00
|
|
|
MagicByteString(MagicalSerializer<'a, 'b, 'c, ByteString>),
|
|
|
|
MagicU16String(MagicalSerializer<'a, 'b, 'c, U16String>),
|
2022-04-24 08:28:46 -04:00
|
|
|
MagicStringOrBuffer(MagicalSerializer<'a, 'b, 'c, StringOrBuffer>),
|
2023-03-16 12:59:47 -04:00
|
|
|
MagicBigInt(MagicalSerializer<'a, 'b, 'c, BigInt>),
|
2021-10-19 18:00:45 -04:00
|
|
|
Regular(ObjectSerializer<'a, 'b, 'c>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'c> ser::SerializeStruct for StructSerializers<'a, 'b, 'c> {
|
|
|
|
type Ok = JsValue<'a>;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn serialize_field<T: ?Sized + Serialize>(
|
|
|
|
&mut self,
|
|
|
|
key: &'static str,
|
|
|
|
value: &T,
|
|
|
|
) -> Result<()> {
|
|
|
|
match self {
|
2023-02-22 12:32:38 -05:00
|
|
|
StructSerializers::ExternalPointer(s) => s.serialize_field(key, value),
|
2021-10-19 18:00:45 -04:00
|
|
|
StructSerializers::Magic(s) => s.serialize_field(key, value),
|
2022-04-25 10:56:47 -04:00
|
|
|
StructSerializers::ZeroCopyBuf(s) => s.serialize_field(key, value),
|
2023-03-22 00:13:24 -04:00
|
|
|
StructSerializers::MagicAnyValue(s) => s.serialize_field(key, value),
|
2022-04-02 07:35:57 -04:00
|
|
|
StructSerializers::MagicDetached(s) => s.serialize_field(key, value),
|
2021-10-19 18:00:45 -04:00
|
|
|
StructSerializers::MagicByteString(s) => s.serialize_field(key, value),
|
2022-03-15 19:22:00 -04:00
|
|
|
StructSerializers::MagicU16String(s) => s.serialize_field(key, value),
|
2022-04-24 08:28:46 -04:00
|
|
|
StructSerializers::MagicStringOrBuffer(s) => {
|
|
|
|
s.serialize_field(key, value)
|
|
|
|
}
|
2023-03-16 12:59:47 -04:00
|
|
|
StructSerializers::MagicBigInt(s) => s.serialize_field(key, value),
|
2021-10-19 18:00:45 -04:00
|
|
|
StructSerializers::Regular(s) => s.serialize_field(key, value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> JsResult<'a> {
|
|
|
|
match self {
|
2023-02-22 12:32:38 -05:00
|
|
|
StructSerializers::ExternalPointer(s) => s.end(),
|
2021-10-19 18:00:45 -04:00
|
|
|
StructSerializers::Magic(s) => s.end(),
|
2022-04-25 10:56:47 -04:00
|
|
|
StructSerializers::ZeroCopyBuf(s) => s.end(),
|
2023-03-22 00:13:24 -04:00
|
|
|
StructSerializers::MagicAnyValue(s) => s.end(),
|
2022-04-02 07:35:57 -04:00
|
|
|
StructSerializers::MagicDetached(s) => s.end(),
|
2021-10-19 18:00:45 -04:00
|
|
|
StructSerializers::MagicByteString(s) => s.end(),
|
2022-03-15 19:22:00 -04:00
|
|
|
StructSerializers::MagicU16String(s) => s.end(),
|
2022-04-24 08:28:46 -04:00
|
|
|
StructSerializers::MagicStringOrBuffer(s) => s.end(),
|
2023-03-16 12:59:47 -04:00
|
|
|
StructSerializers::MagicBigInt(s) => s.end(),
|
2021-10-19 18:00:45 -04:00
|
|
|
StructSerializers::Regular(s) => s.end(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serializes to JS Objects, NOT JS Maps ...
|
|
|
|
pub struct MapSerializer<'a, 'b, 'c> {
|
|
|
|
scope: ScopePtr<'a, 'b, 'c>,
|
|
|
|
keys: Vec<v8::Local<'a, v8::Name>>,
|
|
|
|
values: Vec<JsValue<'a>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'c> MapSerializer<'a, 'b, 'c> {
|
|
|
|
pub fn new(scope: ScopePtr<'a, 'b, 'c>, len: Option<usize>) -> Self {
|
|
|
|
let keys = Vec::with_capacity(len.unwrap_or_default());
|
|
|
|
let values = Vec::with_capacity(len.unwrap_or_default());
|
|
|
|
Self {
|
|
|
|
scope,
|
|
|
|
keys,
|
|
|
|
values,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'c> ser::SerializeMap for MapSerializer<'a, 'b, 'c> {
|
|
|
|
type Ok = JsValue<'a>;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn serialize_key<T: ?Sized + Serialize>(&mut self, key: &T) -> Result<()> {
|
|
|
|
let key = key.serialize(Serializer::new(self.scope))?;
|
|
|
|
self.keys.push(key.try_into().map_err(|_| {
|
|
|
|
Error::Message("Serialized Maps expect String keys".into())
|
|
|
|
})?);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_value<T: ?Sized + Serialize>(
|
|
|
|
&mut self,
|
|
|
|
value: &T,
|
|
|
|
) -> Result<()> {
|
|
|
|
let v8_value = value.serialize(Serializer::new(self.scope))?;
|
|
|
|
self.values.push(v8_value);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(self) -> JsResult<'a> {
|
|
|
|
debug_assert!(self.keys.len() == self.values.len());
|
|
|
|
let scope = &mut *self.scope.borrow_mut();
|
|
|
|
let null = v8::null(scope).into();
|
|
|
|
let obj = v8::Object::with_prototype_and_properties(
|
|
|
|
scope,
|
|
|
|
null,
|
|
|
|
&self.keys[..],
|
|
|
|
&self.values[..],
|
|
|
|
);
|
|
|
|
Ok(obj.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Serializer<'a, 'b, 'c> {
|
|
|
|
scope: ScopePtr<'a, 'b, 'c>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'c> Serializer<'a, 'b, 'c> {
|
|
|
|
pub fn new(scope: ScopePtr<'a, 'b, 'c>) -> Self {
|
|
|
|
Serializer { scope }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! forward_to {
|
|
|
|
($($name:ident($ty:ty, $to:ident, $lt:lifetime);)*) => {
|
|
|
|
$(fn $name(self, v: $ty) -> JsResult<$lt> {
|
|
|
|
self.$to(v as _)
|
|
|
|
})*
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-22 12:32:38 -05:00
|
|
|
pub(crate) const MAX_SAFE_INTEGER: i64 = (1 << 53) - 1;
|
|
|
|
pub(crate) const MIN_SAFE_INTEGER: i64 = -MAX_SAFE_INTEGER;
|
2022-09-01 06:51:28 -04:00
|
|
|
|
2021-10-19 18:00:45 -04:00
|
|
|
impl<'a, 'b, 'c> ser::Serializer for Serializer<'a, 'b, 'c> {
|
|
|
|
type Ok = v8::Local<'a, v8::Value>;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
type SerializeSeq = ArraySerializer<'a, 'b, 'c>;
|
|
|
|
type SerializeTuple = ArraySerializer<'a, 'b, 'c>;
|
|
|
|
type SerializeTupleStruct = ArraySerializer<'a, 'b, 'c>;
|
|
|
|
type SerializeTupleVariant =
|
|
|
|
VariantSerializer<'a, 'b, 'c, ArraySerializer<'a, 'b, 'c>>;
|
|
|
|
type SerializeMap = MapSerializer<'a, 'b, 'c>;
|
|
|
|
type SerializeStruct = StructSerializers<'a, 'b, 'c>;
|
|
|
|
type SerializeStructVariant =
|
|
|
|
VariantSerializer<'a, 'b, 'c, StructSerializers<'a, 'b, 'c>>;
|
|
|
|
|
|
|
|
forward_to! {
|
|
|
|
serialize_i8(i8, serialize_i32, 'a);
|
|
|
|
serialize_i16(i16, serialize_i32, 'a);
|
|
|
|
|
|
|
|
serialize_u8(u8, serialize_u32, 'a);
|
|
|
|
serialize_u16(u16, serialize_u32, 'a);
|
|
|
|
|
|
|
|
serialize_f32(f32, serialize_f64, 'a);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_i32(self, v: i32) -> JsResult<'a> {
|
|
|
|
Ok(v8::Integer::new(&mut self.scope.borrow_mut(), v).into())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u32(self, v: u32) -> JsResult<'a> {
|
|
|
|
Ok(v8::Integer::new_from_unsigned(&mut self.scope.borrow_mut(), v).into())
|
|
|
|
}
|
|
|
|
|
2022-09-01 06:51:28 -04:00
|
|
|
fn serialize_i64(self, v: i64) -> JsResult<'a> {
|
|
|
|
let s = &mut self.scope.borrow_mut();
|
|
|
|
// If i64 can fit in max safe integer bounds then serialize as v8::Number
|
|
|
|
// otherwise serialize as v8::BigInt
|
|
|
|
if (MIN_SAFE_INTEGER..=MAX_SAFE_INTEGER).contains(&v) {
|
|
|
|
Ok(v8::Number::new(s, v as _).into())
|
|
|
|
} else {
|
|
|
|
Ok(v8::BigInt::new_from_i64(s, v).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_u64(self, v: u64) -> JsResult<'a> {
|
|
|
|
let s = &mut self.scope.borrow_mut();
|
|
|
|
// If u64 can fit in max safe integer bounds then serialize as v8::Number
|
|
|
|
// otherwise serialize as v8::BigInt
|
|
|
|
if v <= (MAX_SAFE_INTEGER as u64) {
|
|
|
|
Ok(v8::Number::new(s, v as _).into())
|
|
|
|
} else {
|
|
|
|
Ok(v8::BigInt::new_from_u64(s, v).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:00:45 -04:00
|
|
|
fn serialize_f64(self, v: f64) -> JsResult<'a> {
|
2022-10-07 06:54:01 -04:00
|
|
|
let scope = &mut self.scope.borrow_mut();
|
|
|
|
Ok(v8::Number::new(scope.deref_mut(), v).into())
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_bool(self, v: bool) -> JsResult<'a> {
|
2022-10-06 23:51:56 -04:00
|
|
|
Ok(v8::Boolean::new(&mut *self.scope.borrow_mut(), v).into())
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
|
2022-07-28 06:46:10 -04:00
|
|
|
fn serialize_char(self, v: char) -> JsResult<'a> {
|
|
|
|
self.serialize_str(&v.to_string())
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_str(self, v: &str) -> JsResult<'a> {
|
2022-09-01 16:20:11 -04:00
|
|
|
let maybe_str = v8::String::new(&mut self.scope.borrow_mut(), v);
|
|
|
|
|
|
|
|
// v8 string can return 'None' if buffer length > kMaxLength.
|
|
|
|
if let Some(str) = maybe_str {
|
|
|
|
Ok(str.into())
|
|
|
|
} else {
|
|
|
|
Err(Error::Message(String::from(
|
|
|
|
"Cannot allocate String: buffer exceeds maximum length.",
|
|
|
|
)))
|
|
|
|
}
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
|
2022-07-28 06:46:10 -04:00
|
|
|
fn serialize_bytes(self, v: &[u8]) -> JsResult<'a> {
|
|
|
|
Ok(slice_to_uint8array(&mut self.scope.borrow_mut(), v).into())
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_none(self) -> JsResult<'a> {
|
2022-10-06 23:51:56 -04:00
|
|
|
Ok(v8::null(&mut *self.scope.borrow_mut()).into())
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_some<T: ?Sized + Serialize>(self, value: &T) -> JsResult<'a> {
|
|
|
|
value.serialize(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_unit(self) -> JsResult<'a> {
|
2022-10-06 23:51:56 -04:00
|
|
|
Ok(v8::null(&mut *self.scope.borrow_mut()).into())
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_unit_struct(self, _name: &'static str) -> JsResult<'a> {
|
2022-10-06 23:51:56 -04:00
|
|
|
Ok(v8::null(&mut *self.scope.borrow_mut()).into())
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// For compatibility with serde-json, serialises unit variants as "Variant" strings.
|
|
|
|
fn serialize_unit_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
variant: &'static str,
|
|
|
|
) -> JsResult<'a> {
|
|
|
|
Ok(v8_struct_key(&mut self.scope.borrow_mut(), variant).into())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_newtype_struct<T: ?Sized + Serialize>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
value: &T,
|
|
|
|
) -> JsResult<'a> {
|
|
|
|
value.serialize(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_newtype_variant<T: ?Sized + Serialize>(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
variant: &'static str,
|
|
|
|
value: &T,
|
|
|
|
) -> JsResult<'a> {
|
|
|
|
let scope = self.scope;
|
|
|
|
let x = self.serialize_newtype_struct(variant, value)?;
|
|
|
|
VariantSerializer::new(scope, variant, x).end(Ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Serialises any Rust iterable into a JS Array
|
|
|
|
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
|
|
|
|
Ok(ArraySerializer::new(self.scope, len))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
|
|
|
|
self.serialize_seq(Some(len))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_tuple_struct(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
len: usize,
|
|
|
|
) -> Result<Self::SerializeTupleStruct> {
|
|
|
|
self.serialize_tuple(len)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_tuple_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
variant: &'static str,
|
|
|
|
len: usize,
|
|
|
|
) -> Result<Self::SerializeTupleVariant> {
|
|
|
|
Ok(VariantSerializer::new(
|
|
|
|
self.scope,
|
|
|
|
variant,
|
|
|
|
self.serialize_tuple_struct(variant, len)?,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
|
|
|
|
// Serializes a rust Map (e.g: BTreeMap, HashMap) to a v8 Object
|
|
|
|
// TODO: consider allowing serializing to v8 Maps (e.g: via a magic type)
|
|
|
|
// since they're lighter and better suited for K/V data
|
|
|
|
// and maybe restrict keys (e.g: strings and numbers)
|
|
|
|
Ok(MapSerializer::new(self.scope, len))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Serialises Rust typed structs into plain JS objects.
|
|
|
|
fn serialize_struct(
|
|
|
|
self,
|
|
|
|
name: &'static str,
|
|
|
|
len: usize,
|
|
|
|
) -> Result<Self::SerializeStruct> {
|
|
|
|
match name {
|
2023-02-22 12:32:38 -05:00
|
|
|
magic::ExternalPointer::MAGIC_NAME => {
|
|
|
|
let m = MagicalSerializer::<ExternalPointer>::new(self.scope);
|
|
|
|
Ok(StructSerializers::ExternalPointer(m))
|
|
|
|
}
|
2022-03-23 16:15:01 -04:00
|
|
|
ByteString::MAGIC_NAME => {
|
|
|
|
let m = MagicalSerializer::<ByteString>::new(self.scope);
|
2021-10-19 18:00:45 -04:00
|
|
|
Ok(StructSerializers::MagicByteString(m))
|
|
|
|
}
|
2022-03-23 16:15:01 -04:00
|
|
|
U16String::MAGIC_NAME => {
|
|
|
|
let m = MagicalSerializer::<U16String>::new(self.scope);
|
2022-03-15 19:22:00 -04:00
|
|
|
Ok(StructSerializers::MagicU16String(m))
|
|
|
|
}
|
2022-04-25 10:56:47 -04:00
|
|
|
ZeroCopyBuf::MAGIC_NAME => {
|
|
|
|
let m = MagicalSerializer::<ZeroCopyBuf>::new(self.scope);
|
|
|
|
Ok(StructSerializers::ZeroCopyBuf(m))
|
2022-03-23 16:15:01 -04:00
|
|
|
}
|
2023-03-22 00:13:24 -04:00
|
|
|
AnyValue::MAGIC_NAME => {
|
|
|
|
let m = MagicalSerializer::<AnyValue>::new(self.scope);
|
|
|
|
Ok(StructSerializers::MagicAnyValue(m))
|
|
|
|
}
|
2022-04-02 07:35:57 -04:00
|
|
|
DetachedBuffer::MAGIC_NAME => {
|
|
|
|
let m = MagicalSerializer::<DetachedBuffer>::new(self.scope);
|
|
|
|
Ok(StructSerializers::MagicDetached(m))
|
|
|
|
}
|
2022-04-24 08:28:46 -04:00
|
|
|
StringOrBuffer::MAGIC_NAME => {
|
|
|
|
let m = MagicalSerializer::<StringOrBuffer>::new(self.scope);
|
|
|
|
Ok(StructSerializers::MagicStringOrBuffer(m))
|
|
|
|
}
|
2023-03-16 12:59:47 -04:00
|
|
|
BigInt::MAGIC_NAME => {
|
|
|
|
let m = MagicalSerializer::<BigInt>::new(self.scope);
|
|
|
|
Ok(StructSerializers::MagicBigInt(m))
|
|
|
|
}
|
2022-03-23 16:15:01 -04:00
|
|
|
magic::Value::MAGIC_NAME => {
|
|
|
|
let m = MagicalSerializer::<magic::Value<'a>>::new(self.scope);
|
|
|
|
Ok(StructSerializers::Magic(m))
|
|
|
|
}
|
2021-10-19 18:00:45 -04:00
|
|
|
_ => {
|
2022-03-23 16:15:01 -04:00
|
|
|
// Regular structs
|
2021-10-19 18:00:45 -04:00
|
|
|
let o = ObjectSerializer::new(self.scope, len);
|
|
|
|
Ok(StructSerializers::Regular(o))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_struct_variant(
|
|
|
|
self,
|
|
|
|
_name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
variant: &'static str,
|
|
|
|
len: usize,
|
|
|
|
) -> Result<Self::SerializeStructVariant> {
|
|
|
|
let scope = self.scope;
|
|
|
|
let x = self.serialize_struct(variant, len)?;
|
|
|
|
Ok(VariantSerializer::new(scope, variant, x))
|
|
|
|
}
|
|
|
|
}
|
2022-07-28 06:46:10 -04:00
|
|
|
|
|
|
|
pub fn slice_to_uint8array<'a>(
|
|
|
|
scope: &mut v8::HandleScope<'a>,
|
|
|
|
buf: &[u8],
|
|
|
|
) -> v8::Local<'a, v8::Uint8Array> {
|
|
|
|
let buffer = if buf.is_empty() {
|
|
|
|
v8::ArrayBuffer::new(scope, 0)
|
|
|
|
} else {
|
|
|
|
let store: v8::UniqueRef<_> =
|
|
|
|
v8::ArrayBuffer::new_backing_store(scope, buf.len());
|
|
|
|
// SAFETY: raw memory copy into the v8 ArrayBuffer allocated above
|
|
|
|
unsafe {
|
|
|
|
std::ptr::copy_nonoverlapping(
|
|
|
|
buf.as_ptr(),
|
|
|
|
store.data().unwrap().as_ptr() as *mut u8,
|
|
|
|
buf.len(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
v8::ArrayBuffer::with_backing_store(scope, &store.make_shared())
|
|
|
|
};
|
|
|
|
v8::Uint8Array::new(scope, buffer, 0, buf.len())
|
|
|
|
.expect("Failed to create UintArray8")
|
|
|
|
}
|