2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-10-19 18:00:45 -04:00
|
|
|
use std::any::TypeId;
|
|
|
|
use std::mem::transmute_copy;
|
|
|
|
|
2022-03-07 07:06:50 -05:00
|
|
|
use crate::ByteString;
|
2022-03-15 19:22:00 -04:00
|
|
|
use crate::U16String;
|
2022-04-25 10:56:47 -04:00
|
|
|
use crate::ZeroCopyBuf;
|
2022-03-07 07:06:50 -05:00
|
|
|
|
2021-10-19 18:00:45 -04:00
|
|
|
/// Serializable exists to allow boxing values as "objects" to be serialized later,
|
|
|
|
/// this is particularly useful for async op-responses. This trait is a more efficient
|
|
|
|
/// replacement for erased-serde that makes less allocations, since it's specific to serde_v8
|
|
|
|
/// (and thus doesn't have to have generic outputs, etc...)
|
|
|
|
pub trait Serializable {
|
|
|
|
fn to_v8<'a>(
|
2022-09-01 06:24:40 -04:00
|
|
|
&mut self,
|
2021-10-19 18:00:45 -04:00
|
|
|
scope: &mut v8::HandleScope<'a>,
|
|
|
|
) -> Result<v8::Local<'a, v8::Value>, crate::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Allows all implementors of `serde::Serialize` to implement Serializable
|
|
|
|
impl<T: serde::Serialize> Serializable for T {
|
|
|
|
fn to_v8<'a>(
|
2022-09-01 06:24:40 -04:00
|
|
|
&mut self,
|
2021-10-19 18:00:45 -04:00
|
|
|
scope: &mut v8::HandleScope<'a>,
|
|
|
|
) -> Result<v8::Local<'a, v8::Value>, crate::Error> {
|
|
|
|
crate::to_v8(scope, self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// SerializablePkg exists to provide a fast path for op returns,
|
|
|
|
/// allowing them to avoid boxing primtives (ints/floats/bool/unit/...)
|
|
|
|
pub enum SerializablePkg {
|
|
|
|
Primitive(Primitive),
|
|
|
|
Serializable(Box<dyn Serializable>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SerializablePkg {
|
|
|
|
pub fn to_v8<'a>(
|
2022-09-01 06:24:40 -04:00
|
|
|
&mut self,
|
2021-10-19 18:00:45 -04:00
|
|
|
scope: &mut v8::HandleScope<'a>,
|
|
|
|
) -> Result<v8::Local<'a, v8::Value>, crate::Error> {
|
2022-08-21 13:31:14 -04:00
|
|
|
match self {
|
2021-10-19 18:00:45 -04:00
|
|
|
Self::Primitive(x) => crate::to_v8(scope, x),
|
|
|
|
Self::Serializable(x) => x.to_v8(scope),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Primitive serves as a lightweight serializable wrapper around primitives
|
|
|
|
/// so that we can use them for async values
|
|
|
|
pub enum Primitive {
|
|
|
|
Unit,
|
|
|
|
Bool(bool),
|
|
|
|
Int8(i8),
|
|
|
|
Int16(i16),
|
|
|
|
Int32(i32),
|
|
|
|
Int64(i64),
|
|
|
|
UInt8(u8),
|
|
|
|
UInt16(u16),
|
|
|
|
UInt32(u32),
|
|
|
|
UInt64(u64),
|
|
|
|
Float32(f32),
|
|
|
|
Float64(f64),
|
2022-03-07 07:06:50 -05:00
|
|
|
String(String),
|
2022-04-25 10:56:47 -04:00
|
|
|
ZeroCopyBuf(ZeroCopyBuf),
|
2022-03-07 07:06:50 -05:00
|
|
|
ByteString(ByteString),
|
2022-03-15 19:22:00 -04:00
|
|
|
U16String(U16String),
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl serde::Serialize for Primitive {
|
2022-03-07 05:12:44 -05:00
|
|
|
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
|
2021-10-19 18:00:45 -04:00
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
2022-03-07 07:06:50 -05:00
|
|
|
match self {
|
2022-03-07 05:12:44 -05:00
|
|
|
Self::Unit => ().serialize(s),
|
|
|
|
Self::Bool(x) => x.serialize(s),
|
|
|
|
Self::Int8(x) => x.serialize(s),
|
|
|
|
Self::Int16(x) => x.serialize(s),
|
|
|
|
Self::Int32(x) => x.serialize(s),
|
|
|
|
Self::Int64(x) => x.serialize(s),
|
|
|
|
Self::UInt8(x) => x.serialize(s),
|
|
|
|
Self::UInt16(x) => x.serialize(s),
|
|
|
|
Self::UInt32(x) => x.serialize(s),
|
|
|
|
Self::UInt64(x) => x.serialize(s),
|
|
|
|
Self::Float32(x) => x.serialize(s),
|
|
|
|
Self::Float64(x) => x.serialize(s),
|
2022-03-07 07:06:50 -05:00
|
|
|
Self::String(x) => x.serialize(s),
|
2022-04-25 10:56:47 -04:00
|
|
|
Self::ZeroCopyBuf(x) => x.serialize(s),
|
2022-03-07 07:06:50 -05:00
|
|
|
Self::ByteString(x) => x.serialize(s),
|
2022-03-15 19:22:00 -04:00
|
|
|
Self::U16String(x) => x.serialize(s),
|
2021-10-19 18:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: serde::Serialize + 'static> From<T> for SerializablePkg {
|
|
|
|
fn from(x: T) -> Self {
|
2022-03-07 05:12:44 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn tc<T, U>(src: T) -> U {
|
2022-06-25 18:13:24 -04:00
|
|
|
// SAFETY: the caller has ensured via the TypeId that the T and U types
|
|
|
|
// are the same.
|
2022-03-07 07:06:50 -05:00
|
|
|
let x = unsafe { transmute_copy(&src) };
|
|
|
|
std::mem::forget(src);
|
|
|
|
x
|
2022-03-07 05:12:44 -05:00
|
|
|
}
|
2021-10-19 18:00:45 -04:00
|
|
|
|
2022-03-07 05:12:44 -05:00
|
|
|
let tid = TypeId::of::<T>();
|
2021-10-19 18:00:45 -04:00
|
|
|
if tid == TypeId::of::<()>() {
|
|
|
|
Self::Primitive(Primitive::Unit)
|
|
|
|
} else if tid == TypeId::of::<bool>() {
|
2022-03-07 05:12:44 -05:00
|
|
|
Self::Primitive(Primitive::Bool(tc(x)))
|
2021-10-19 18:00:45 -04:00
|
|
|
} else if tid == TypeId::of::<i8>() {
|
2022-03-07 05:12:44 -05:00
|
|
|
Self::Primitive(Primitive::Int8(tc(x)))
|
2021-10-19 18:00:45 -04:00
|
|
|
} else if tid == TypeId::of::<i16>() {
|
2022-03-07 05:12:44 -05:00
|
|
|
Self::Primitive(Primitive::Int16(tc(x)))
|
2021-10-19 18:00:45 -04:00
|
|
|
} else if tid == TypeId::of::<i32>() {
|
2022-03-07 05:12:44 -05:00
|
|
|
Self::Primitive(Primitive::Int32(tc(x)))
|
2021-10-19 18:00:45 -04:00
|
|
|
} else if tid == TypeId::of::<i64>() {
|
2022-03-07 05:12:44 -05:00
|
|
|
Self::Primitive(Primitive::Int64(tc(x)))
|
2021-10-19 18:00:45 -04:00
|
|
|
} else if tid == TypeId::of::<u8>() {
|
2022-03-07 05:12:44 -05:00
|
|
|
Self::Primitive(Primitive::UInt8(tc(x)))
|
2021-10-19 18:00:45 -04:00
|
|
|
} else if tid == TypeId::of::<u16>() {
|
2022-03-07 05:12:44 -05:00
|
|
|
Self::Primitive(Primitive::UInt16(tc(x)))
|
2021-10-19 18:00:45 -04:00
|
|
|
} else if tid == TypeId::of::<u32>() {
|
2022-03-07 05:12:44 -05:00
|
|
|
Self::Primitive(Primitive::UInt32(tc(x)))
|
2021-10-19 18:00:45 -04:00
|
|
|
} else if tid == TypeId::of::<u64>() {
|
2022-03-07 05:12:44 -05:00
|
|
|
Self::Primitive(Primitive::UInt64(tc(x)))
|
2021-10-19 18:00:45 -04:00
|
|
|
} else if tid == TypeId::of::<f32>() {
|
2022-03-07 05:12:44 -05:00
|
|
|
Self::Primitive(Primitive::Float32(tc(x)))
|
2021-10-19 18:00:45 -04:00
|
|
|
} else if tid == TypeId::of::<f64>() {
|
2022-03-07 05:12:44 -05:00
|
|
|
Self::Primitive(Primitive::Float64(tc(x)))
|
2022-03-07 07:06:50 -05:00
|
|
|
} else if tid == TypeId::of::<String>() {
|
|
|
|
Self::Primitive(Primitive::String(tc(x)))
|
2022-04-25 10:56:47 -04:00
|
|
|
} else if tid == TypeId::of::<ZeroCopyBuf>() {
|
|
|
|
Self::Primitive(Primitive::ZeroCopyBuf(tc(x)))
|
2022-03-07 07:06:50 -05:00
|
|
|
} else if tid == TypeId::of::<ByteString>() {
|
|
|
|
Self::Primitive(Primitive::ByteString(tc(x)))
|
2022-03-15 19:22:00 -04:00
|
|
|
} else if tid == TypeId::of::<U16String>() {
|
|
|
|
Self::Primitive(Primitive::U16String(tc(x)))
|
2021-10-19 18:00:45 -04:00
|
|
|
} else {
|
|
|
|
Self::Serializable(Box::new(x))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|