mirror of
https://github.com/swc-project/swc.git
synced 2024-11-26 09:54:22 +03:00
feat(es/ast): Add rkyv
support (#3166)
ast_node: - `#[ast_node]`: Add support for `rkyv`. swc_common: - Add a feature flag for `rkyv`. swc_ecma_ast: - Add a feature flag for `rkyv`.
This commit is contained in:
parent
b1a72315db
commit
bf751cb690
84
Cargo.lock
generated
84
Cargo.lock
generated
@ -266,6 +266,27 @@ version = "3.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f1e260c3a9040a7c19a12468758f4c16f31a81a1fe087482be9570ec864bb6c"
|
||||
|
||||
[[package]]
|
||||
name = "bytecheck"
|
||||
version = "0.6.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "314889ea31cda264cb7c3d6e6e5c9415a987ecb0e72c17c00d36fbb881d34abe"
|
||||
dependencies = [
|
||||
"bytecheck_derive",
|
||||
"ptr_meta",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bytecheck_derive"
|
||||
version = "0.6.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4a2b3b92c135dae665a6f760205b89187638e83bed17ef3e44e83c712cf30600"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.1.0"
|
||||
@ -1690,6 +1711,26 @@ dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ptr_meta"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0738ccf7ea06b608c10564b31debd4f5bc5e197fc8bfe088f68ae5ce81e7a4f1"
|
||||
dependencies = [
|
||||
"ptr_meta_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ptr_meta_derive"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "16b845dbfca988fa33db069c0e230574d15a3088f147a87b64c7589eb662c9ac"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.10"
|
||||
@ -1877,6 +1918,15 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rend"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "79af64b4b6362ffba04eef3a4e10829718a4896dac19daa741851c86781edf95"
|
||||
dependencies = [
|
||||
"bytecheck",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "repr_offset"
|
||||
version = "0.2.1"
|
||||
@ -1927,6 +1977,31 @@ version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "11000e6ba5020e53e7cc26f73b91ae7d5496b4977851479edb66b694c0675c21"
|
||||
|
||||
[[package]]
|
||||
name = "rkyv"
|
||||
version = "0.7.28"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "631f7d2a2854abb66724f492ce5256e79685a673dc210ac022194cedd5c914d3"
|
||||
dependencies = [
|
||||
"bytecheck",
|
||||
"hashbrown",
|
||||
"ptr_meta",
|
||||
"rend",
|
||||
"rkyv_derive",
|
||||
"seahash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rkyv_derive"
|
||||
version = "0.7.28"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c067e650861a749720952aed722fb344449bc95de33e6456d426f5c7d44f71c0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rplugin"
|
||||
version = "0.3.0"
|
||||
@ -2015,6 +2090,12 @@ version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||
|
||||
[[package]]
|
||||
name = "seahash"
|
||||
version = "4.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b"
|
||||
|
||||
[[package]]
|
||||
name = "security-framework"
|
||||
version = "2.4.2"
|
||||
@ -2428,6 +2509,7 @@ dependencies = [
|
||||
"owning_ref",
|
||||
"parking_lot",
|
||||
"rayon",
|
||||
"rkyv",
|
||||
"rustc-hash",
|
||||
"scoped-tls",
|
||||
"serde",
|
||||
@ -2546,6 +2628,7 @@ dependencies = [
|
||||
"arbitrary",
|
||||
"is-macro",
|
||||
"num-bigint",
|
||||
"rkyv",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"string_enum",
|
||||
@ -2719,6 +2802,7 @@ version = "0.7.1"
|
||||
dependencies = [
|
||||
"abi_stable",
|
||||
"num-bigint",
|
||||
"rkyv",
|
||||
"rplugin",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
@ -19,5 +19,5 @@ quote = "1"
|
||||
swc_macros_common = {version = "0.3.2", path = "../swc_macros_common"}
|
||||
|
||||
[dependencies.syn]
|
||||
features = ["derive", "fold", "parsing", "printing"]
|
||||
features = ["derive", "fold", "parsing", "printing", "visit-mut"]
|
||||
version = "1"
|
||||
|
@ -5,7 +5,7 @@ extern crate proc_macro;
|
||||
use darling;
|
||||
use pmutil::{smart_quote, Quote, ToTokensExt};
|
||||
use swc_macros_common::prelude::*;
|
||||
use syn::{self, *};
|
||||
use syn::{self, visit_mut::VisitMut, *};
|
||||
|
||||
mod ast_node_macro;
|
||||
mod enum_deserialize;
|
||||
@ -145,6 +145,15 @@ pub fn ast_serde(
|
||||
print("ast_serde", item)
|
||||
}
|
||||
|
||||
struct AddAttr;
|
||||
|
||||
impl VisitMut for AddAttr {
|
||||
fn visit_field_mut(&mut self, f: &mut Field) {
|
||||
f.attrs
|
||||
.push(parse_quote!(#[cfg_attr(feature = "rkyv", omit_bounds)]));
|
||||
}
|
||||
}
|
||||
|
||||
/// Alias for
|
||||
/// `#[derive(Spanned, Fold, Clone, Debug, PartialEq)]` for a struct and
|
||||
/// `#[derive(Spanned, Fold, Clone, Debug, PartialEq, FromVariant)]` for an
|
||||
@ -157,7 +166,9 @@ pub fn ast_node(
|
||||
args: proc_macro::TokenStream,
|
||||
input: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
let input: DeriveInput = parse(input).expect("failed to parse input as a DeriveInput");
|
||||
let mut input: DeriveInput = parse(input).expect("failed to parse input as a DeriveInput");
|
||||
|
||||
AddAttr.visit_data_mut(&mut input.data);
|
||||
|
||||
// we should use call_site
|
||||
let mut item = Quote::new(Span::call_site());
|
||||
@ -177,6 +188,16 @@ pub fn ast_node(
|
||||
::serde::Serialize,
|
||||
::swc_common::DeserializeEnum,
|
||||
)]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
archive(bound(
|
||||
serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace"
|
||||
))
|
||||
)]
|
||||
#[serde(untagged)]
|
||||
input
|
||||
}))
|
||||
@ -219,6 +240,14 @@ pub fn ast_node(
|
||||
item.quote_with(smart_quote!(Vars { input, serde_tag, serde_rename }, {
|
||||
#[derive(::swc_common::Spanned, Clone, Debug, PartialEq)]
|
||||
#[derive(::serde::Serialize, ::serde::Deserialize)]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
archive(bound(serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace"))
|
||||
)]
|
||||
serde_tag
|
||||
#[serde(rename_all = "camelCase")]
|
||||
serde_rename
|
||||
|
@ -23,6 +23,7 @@ diagnostic-serde = []
|
||||
plugin-base = ["abi_stable", "anyhow", "bincode", "diagnostic-serde"]
|
||||
plugin-mode = ["plugin-base"]
|
||||
plugin-rt = ["plugin-base"]
|
||||
rkyv-impl = ["rkyv"]
|
||||
tty-emitter = ["atty", "termcolor"]
|
||||
|
||||
[dependencies]
|
||||
@ -41,6 +42,7 @@ num-bigint = "0.2"
|
||||
once_cell = "1"
|
||||
owning_ref = "0.4"
|
||||
parking_lot = {version = "0.11", optional = true}
|
||||
rkyv = {version = "0.7.28", optional = true}
|
||||
rustc-hash = "1.1.0"
|
||||
scoped-tls = "1"
|
||||
serde = {version = "1.0.119", features = ["derive"]}
|
||||
|
@ -28,13 +28,20 @@ pub mod hygiene;
|
||||
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "abi_stable", repr(C))]
|
||||
#[cfg_attr(feature = "abi_stable", derive(abi_stable::StableAbi))]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
pub struct Span {
|
||||
#[serde(rename = "start")]
|
||||
#[cfg_attr(feature = "rkyv", omit_bounds)]
|
||||
pub lo: BytePos,
|
||||
#[serde(rename = "end")]
|
||||
#[cfg_attr(feature = "rkyv", omit_bounds)]
|
||||
pub hi: BytePos,
|
||||
/// Information about where the macro came from, if this piece of
|
||||
/// code was created by a macro expansion.
|
||||
#[cfg_attr(feature = "rkyv", omit_bounds)]
|
||||
pub ctxt: SyntaxContext,
|
||||
}
|
||||
|
||||
@ -806,7 +813,11 @@ pub trait Pos {
|
||||
#[cfg_attr(feature = "abi_stable", repr(transparent))]
|
||||
#[cfg_attr(feature = "abi_stable", derive(abi_stable::StableAbi))]
|
||||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
|
||||
pub struct BytePos(pub u32);
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
pub struct BytePos(#[cfg_attr(feature = "rkyv", omit_bounds)] pub u32);
|
||||
|
||||
/// A character offset. Because of multibyte utf8 characters, a byte offset
|
||||
/// is not equivalent to a character offset. The SourceMap will convert BytePos
|
||||
|
@ -29,7 +29,11 @@ use std::{
|
||||
#[serde(transparent)]
|
||||
#[cfg_attr(feature = "abi_stable", repr(transparent))]
|
||||
#[cfg_attr(feature = "abi_stable", derive(abi_stable::StableAbi))]
|
||||
pub struct SyntaxContext(u32);
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
pub struct SyntaxContext(#[cfg_attr(feature = "rkyv", omit_bounds)] u32);
|
||||
|
||||
#[cfg(feature = "arbitrary")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
|
||||
|
@ -14,7 +14,16 @@ pub struct Class {
|
||||
pub struct Tuple(#[span] HasSpan, usize, usize);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Spanned, Serialize, Deserialize)]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
archive(bound(serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace"))
|
||||
)]
|
||||
pub struct HasSpan {
|
||||
#[cfg_attr(feature = "rkyv", omit_bounds)]
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
|
@ -15,11 +15,13 @@ rustdoc-args = ["--cfg", "docsrs"]
|
||||
[features]
|
||||
default = []
|
||||
fuzzing = ["arbitrary", "swc_common/arbitrary"]
|
||||
rkyv-impl = ["rkyv", "swc_common/rkyv-impl"]
|
||||
|
||||
[dependencies]
|
||||
arbitrary = {version = "1", optional = true, features = ["derive"]}
|
||||
is-macro = "0.1"
|
||||
num-bigint = {version = "0.2", features = ["serde"]}
|
||||
rkyv = {version = "0.7.28", optional = true}
|
||||
serde = {version = "1.0.133", features = ["derive"]}
|
||||
string_enum = {version = "0.3.1", path = "../string_enum"}
|
||||
swc_atoms = {version = "0.2", path = "../swc_atoms"}
|
||||
|
@ -250,6 +250,10 @@ pub struct Decorator {
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, EqIgnoreSpan)]
|
||||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
pub enum MethodKind {
|
||||
#[serde(rename = "method")]
|
||||
Method,
|
||||
|
@ -105,6 +105,10 @@ impl Take for VarDecl {
|
||||
|
||||
#[derive(StringEnum, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, EqIgnoreSpan)]
|
||||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
pub enum VarDeclKind {
|
||||
/// `var`
|
||||
Var,
|
||||
|
@ -715,11 +715,21 @@ impl Take for Super {
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Eq, Hash, EqIgnoreSpan)]
|
||||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
archive(bound(serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace"))
|
||||
)]
|
||||
pub struct ExprOrSpread {
|
||||
#[serde(default)]
|
||||
#[cfg_attr(feature = "rkyv", omit_bounds)]
|
||||
pub spread: Option<Span>,
|
||||
|
||||
#[serde(rename = "expression")]
|
||||
#[cfg_attr(feature = "rkyv", omit_bounds)]
|
||||
pub expr: Box<Expr>,
|
||||
}
|
||||
|
||||
|
@ -10,11 +10,21 @@ use unicode_xid::UnicodeXID;
|
||||
/// Identifier used as a pattern.
|
||||
#[derive(Spanned, Clone, Debug, PartialEq, Eq, Hash, EqIgnoreSpan, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
archive(bound(serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace"))
|
||||
)]
|
||||
pub struct BindingIdent {
|
||||
#[span]
|
||||
#[serde(flatten)]
|
||||
#[cfg_attr(feature = "rkyv", omit_bounds)]
|
||||
pub id: Ident,
|
||||
#[serde(default, rename = "typeAnnotation")]
|
||||
#[cfg_attr(feature = "rkyv", omit_bounds)]
|
||||
pub type_ann: Option<TsTypeAnn>,
|
||||
}
|
||||
|
||||
@ -79,6 +89,7 @@ impl From<Ident> for BindingIdent {
|
||||
pub struct Ident {
|
||||
pub span: Span,
|
||||
#[serde(rename = "value")]
|
||||
#[cfg_attr(feature = "rkyv", with(crate::EncodeJsWord))]
|
||||
pub sym: JsWord,
|
||||
|
||||
/// TypeScript only. Used in case of an optional parameter.
|
||||
|
@ -196,7 +196,9 @@ pub enum JSXAttrValue {
|
||||
#[derive(Eq, Hash, EqIgnoreSpan)]
|
||||
pub struct JSXText {
|
||||
pub span: Span,
|
||||
#[cfg_attr(feature = "rkyv", with(crate::EncodeJsWord))]
|
||||
pub value: JsWord,
|
||||
#[cfg_attr(feature = "rkyv", with(crate::EncodeJsWord))]
|
||||
pub raw: JsWord,
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||
#![deny(unreachable_patterns)]
|
||||
#![deny(missing_copy_implementations)]
|
||||
#![deny(missing_debug_implementations)]
|
||||
#![deny(trivial_casts)]
|
||||
#![deny(trivial_numeric_casts)]
|
||||
#![deny(unreachable_pub)]
|
||||
@ -130,3 +129,109 @@ impl Default for EsVersion {
|
||||
EsVersion::Es5
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "rkyv")]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct EncodeJsWord;
|
||||
|
||||
#[cfg(feature = "rkyv")]
|
||||
impl rkyv::with::ArchiveWith<swc_atoms::JsWord> for EncodeJsWord {
|
||||
type Archived = rkyv::Archived<String>;
|
||||
|
||||
type Resolver = rkyv::Resolver<String>;
|
||||
|
||||
unsafe fn resolve_with(
|
||||
field: &swc_atoms::JsWord,
|
||||
pos: usize,
|
||||
resolver: Self::Resolver,
|
||||
out: *mut Self::Archived,
|
||||
) {
|
||||
use rkyv::Archive;
|
||||
|
||||
let s = field.to_string();
|
||||
s.resolve(pos, resolver, out);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "rkyv")]
|
||||
impl<S> rkyv::with::SerializeWith<swc_atoms::JsWord, S> for EncodeJsWord
|
||||
where
|
||||
S: ?Sized + rkyv::ser::Serializer,
|
||||
{
|
||||
fn serialize_with(
|
||||
field: &swc_atoms::JsWord,
|
||||
serializer: &mut S,
|
||||
) -> Result<Self::Resolver, S::Error> {
|
||||
rkyv::string::ArchivedString::serialize_from_str(field, serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "rkyv")]
|
||||
impl<D> rkyv::with::DeserializeWith<rkyv::Archived<String>, swc_atoms::JsWord, D> for EncodeJsWord
|
||||
where
|
||||
D: ?Sized + rkyv::Fallible,
|
||||
{
|
||||
fn deserialize_with(
|
||||
field: &rkyv::Archived<String>,
|
||||
deserializer: &mut D,
|
||||
) -> Result<swc_atoms::JsWord, D::Error> {
|
||||
use rkyv::Deserialize;
|
||||
|
||||
let s: String = field.deserialize(deserializer)?;
|
||||
|
||||
Ok(s.into())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "rkyv")]
|
||||
impl rkyv::with::ArchiveWith<Option<swc_atoms::JsWord>> for EncodeJsWord {
|
||||
type Archived = rkyv::Archived<Option<String>>;
|
||||
|
||||
type Resolver = rkyv::Resolver<Option<String>>;
|
||||
|
||||
unsafe fn resolve_with(
|
||||
field: &Option<swc_atoms::JsWord>,
|
||||
pos: usize,
|
||||
resolver: Self::Resolver,
|
||||
out: *mut Self::Archived,
|
||||
) {
|
||||
use rkyv::Archive;
|
||||
|
||||
let s = field.as_ref().map(|s| s.to_string());
|
||||
s.resolve(pos, resolver, out);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "rkyv")]
|
||||
impl<S> rkyv::with::SerializeWith<Option<swc_atoms::JsWord>, S> for EncodeJsWord
|
||||
where
|
||||
S: ?Sized + rkyv::ser::Serializer,
|
||||
{
|
||||
fn serialize_with(
|
||||
value: &Option<swc_atoms::JsWord>,
|
||||
serializer: &mut S,
|
||||
) -> Result<Self::Resolver, S::Error> {
|
||||
value
|
||||
.as_ref()
|
||||
.map(|value| rkyv::string::ArchivedString::serialize_from_str(&value, serializer))
|
||||
.transpose()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "rkyv")]
|
||||
impl<D> rkyv::with::DeserializeWith<rkyv::Archived<Option<String>>, Option<swc_atoms::JsWord>, D>
|
||||
for EncodeJsWord
|
||||
where
|
||||
D: ?Sized + rkyv::Fallible,
|
||||
{
|
||||
fn deserialize_with(
|
||||
field: &rkyv::Archived<Option<String>>,
|
||||
deserializer: &mut D,
|
||||
) -> Result<Option<swc_atoms::JsWord>, D::Error> {
|
||||
use rkyv::Deserialize;
|
||||
|
||||
let s: Option<String> = field.deserialize(deserializer)?;
|
||||
|
||||
Ok(s.map(|s| s.into()))
|
||||
}
|
||||
}
|
||||
|
@ -39,9 +39,61 @@ pub enum Lit {
|
||||
#[derive(Eq, Hash, EqIgnoreSpan)]
|
||||
pub struct BigInt {
|
||||
pub span: Span,
|
||||
#[cfg_attr(feature = "rkyv", with(EncodeBigInt))]
|
||||
pub value: BigIntValue,
|
||||
}
|
||||
|
||||
#[cfg(feature = "rkyv")]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct EncodeBigInt;
|
||||
|
||||
#[cfg(feature = "rkyv")]
|
||||
impl rkyv::with::ArchiveWith<BigIntValue> for EncodeBigInt {
|
||||
type Archived = rkyv::Archived<String>;
|
||||
|
||||
type Resolver = rkyv::Resolver<String>;
|
||||
|
||||
unsafe fn resolve_with(
|
||||
field: &BigIntValue,
|
||||
pos: usize,
|
||||
resolver: Self::Resolver,
|
||||
out: *mut Self::Archived,
|
||||
) {
|
||||
use rkyv::Archive;
|
||||
|
||||
let s = field.to_string();
|
||||
s.resolve(pos, resolver, out);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "rkyv")]
|
||||
impl<S> rkyv::with::SerializeWith<BigIntValue, S> for EncodeBigInt
|
||||
where
|
||||
S: ?Sized + rkyv::ser::Serializer,
|
||||
{
|
||||
fn serialize_with(field: &BigIntValue, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
|
||||
let field = field.to_string();
|
||||
rkyv::string::ArchivedString::serialize_from_str(&field, serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "rkyv")]
|
||||
impl<D> rkyv::with::DeserializeWith<rkyv::Archived<String>, BigIntValue, D> for EncodeBigInt
|
||||
where
|
||||
D: ?Sized + rkyv::Fallible,
|
||||
{
|
||||
fn deserialize_with(
|
||||
field: &rkyv::Archived<String>,
|
||||
deserializer: &mut D,
|
||||
) -> Result<BigIntValue, D::Error> {
|
||||
use rkyv::Deserialize;
|
||||
|
||||
let s: String = field.deserialize(deserializer)?;
|
||||
|
||||
Ok(s.parse().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "arbitrary")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
|
||||
impl<'a> arbitrary::Arbitrary<'a> for BigInt {
|
||||
@ -68,6 +120,7 @@ impl<'a> arbitrary::Arbitrary<'a> for BigInt {
|
||||
pub struct Str {
|
||||
pub span: Span,
|
||||
|
||||
#[cfg_attr(feature = "rkyv", with(crate::EncodeJsWord))]
|
||||
pub value: JsWord,
|
||||
|
||||
/// This includes line escape.
|
||||
@ -92,6 +145,10 @@ impl Take for Str {
|
||||
/// THis enum determines how string literal should be printed.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
pub enum StrKind {
|
||||
/// Span of string points to original source code, and codegen should use
|
||||
/// it.
|
||||
@ -105,6 +162,7 @@ pub enum StrKind {
|
||||
/// True for string literals generated by parser, false for string
|
||||
/// literals generated by various passes.
|
||||
#[serde(rename = "containsQuote")]
|
||||
#[cfg_attr(feature = "rkyv", omit_bounds)]
|
||||
contains_quote: bool,
|
||||
},
|
||||
/// If the span of string does not point a string literal, mainly because
|
||||
@ -185,9 +243,11 @@ pub struct Regex {
|
||||
pub span: Span,
|
||||
|
||||
#[serde(rename = "pattern")]
|
||||
#[cfg_attr(feature = "rkyv", with(crate::EncodeJsWord))]
|
||||
pub exp: JsWord,
|
||||
|
||||
#[serde(default)]
|
||||
#[cfg_attr(feature = "rkyv", with(crate::EncodeJsWord))]
|
||||
pub flags: JsWord,
|
||||
}
|
||||
|
||||
|
@ -21,6 +21,7 @@ pub struct Module {
|
||||
pub body: Vec<ModuleItem>,
|
||||
|
||||
#[serde(default, rename = "interpreter")]
|
||||
#[cfg_attr(feature = "rkyv", with(crate::EncodeJsWord))]
|
||||
pub shebang: Option<JsWord>,
|
||||
}
|
||||
|
||||
@ -56,6 +57,7 @@ pub struct Script {
|
||||
pub body: Vec<Stmt>,
|
||||
|
||||
#[serde(default, rename = "interpreter")]
|
||||
#[cfg_attr(feature = "rkyv", with(crate::EncodeJsWord))]
|
||||
pub shebang: Option<JsWord>,
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,10 @@ use swc_common::EqIgnoreSpan;
|
||||
|
||||
#[derive(StringEnum, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, EqIgnoreSpan)]
|
||||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
pub enum BinaryOp {
|
||||
/// `==`
|
||||
EqEq,
|
||||
@ -104,6 +108,10 @@ impl BinaryOp {
|
||||
|
||||
#[derive(StringEnum, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, EqIgnoreSpan)]
|
||||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
pub enum AssignOp {
|
||||
/// `=`
|
||||
Assign,
|
||||
@ -145,6 +153,10 @@ pub enum AssignOp {
|
||||
|
||||
#[derive(StringEnum, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, EqIgnoreSpan)]
|
||||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
pub enum UpdateOp {
|
||||
/// `++`
|
||||
PlusPlus,
|
||||
@ -154,6 +166,10 @@ pub enum UpdateOp {
|
||||
|
||||
#[derive(StringEnum, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, EqIgnoreSpan)]
|
||||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
pub enum UnaryOp {
|
||||
/// `-`
|
||||
Minus,
|
||||
|
@ -349,6 +349,10 @@ pub struct TsKeywordType {
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, EqIgnoreSpan)]
|
||||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
pub enum TsKeywordTypeKind {
|
||||
#[serde(rename = "any")]
|
||||
TsAnyKeyword,
|
||||
@ -624,6 +628,10 @@ pub struct TsTypeOperator {
|
||||
|
||||
#[derive(StringEnum, Clone, Copy, PartialEq, Eq, Hash, EqIgnoreSpan)]
|
||||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
pub enum TsTypeOperatorOp {
|
||||
/// `keyof`
|
||||
KeyOf,
|
||||
@ -646,6 +654,10 @@ pub struct TsIndexedAccessType {
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EqIgnoreSpan)]
|
||||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
pub enum TruePlusMinus {
|
||||
True,
|
||||
Plus,
|
||||
@ -992,6 +1004,10 @@ pub struct TsNonNullExpr {
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Eq, Hash, EqIgnoreSpan)]
|
||||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
|
||||
#[cfg_attr(
|
||||
feature = "rkyv",
|
||||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
|
||||
)]
|
||||
pub enum Accessibility {
|
||||
#[serde(rename = "public")]
|
||||
Public,
|
||||
|
@ -12,10 +12,11 @@ version = "0.7.1"
|
||||
[dependencies]
|
||||
abi_stable = "0.10.3"
|
||||
num-bigint = "0.2"
|
||||
rkyv = "0.7.28"
|
||||
rplugin = {version = "0.3.0", path = "../rplugin/"}
|
||||
swc_atoms = {version = "0.2", path = "../swc_atoms"}
|
||||
swc_common = {version = "0.15.0", path = "../swc_common", features = ["plugin-base"]}
|
||||
swc_ecma_ast = {version = "0.60.0", path = "../swc_ecma_ast/"}
|
||||
swc_ecma_ast = {version = "0.60.0", path = "../swc_ecma_ast/", features = ["rkyv-impl"]}
|
||||
|
||||
[dev-dependencies]
|
||||
serde = "1.0.133"
|
||||
|
@ -4,6 +4,7 @@
|
||||
extern crate swc_node_base;
|
||||
extern crate test;
|
||||
|
||||
use rkyv::Deserialize;
|
||||
use rplugin::StableAst;
|
||||
use std::{hint::black_box, path::Path};
|
||||
use swc_common::input::SourceFileInput;
|
||||
@ -30,6 +31,30 @@ fn input() -> Program {
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn rkyv_serialize(b: &mut Bencher) {
|
||||
let program = input();
|
||||
|
||||
b.iter(|| {
|
||||
let v = rkyv::to_bytes::<_, 512>(&program).unwrap();
|
||||
black_box(v);
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn rkyv_deserialize(b: &mut Bencher) {
|
||||
let program = input();
|
||||
let bytes = rkyv::to_bytes::<_, 512>(&program).unwrap();
|
||||
|
||||
b.iter(|| {
|
||||
let archived = unsafe { rkyv::archived_root::<Program>(&bytes[..]) };
|
||||
|
||||
let v: Program = archived.deserialize(&mut rkyv::Infallible).unwrap();
|
||||
|
||||
black_box(v);
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn json_serialize(b: &mut Bencher) {
|
||||
let program = input();
|
||||
|
@ -114,6 +114,7 @@
|
||||
"regs",
|
||||
"repr",
|
||||
"rfind",
|
||||
"rkyv",
|
||||
"rplugin",
|
||||
"rpos",
|
||||
"rposition",
|
||||
|
Loading…
Reference in New Issue
Block a user