Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(swc/common): add versioned wrapper struct (#5060: Part 1) #5062

Merged
merged 2 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions crates/ast_node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ pub fn ast_node(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "rkyv",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
#[cfg_attr(
feature = "rkyv",
archive(bound(
Expand Down Expand Up @@ -269,6 +273,10 @@ pub fn ast_node(
feature = "rkyv",
archive(bound(serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace"))
)]
#[cfg_attr(
feature = "rkyv",
archive_attr(repr(C), derive(bytecheck::CheckBytes))
)]
serde_tag
#[serde(rename_all = "camelCase")]
serde_rename
Expand Down
1 change: 1 addition & 0 deletions crates/swc_atoms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ version = "0.2.12"
bench = false

[dependencies]
bytecheck = { version = "0.6.8", optional = true }
once_cell = "1"
rkyv = { version = "0.7.39", optional = true }
rustc-hash = "1.1.0"
Expand Down
1 change: 1 addition & 0 deletions crates/swc_atoms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ include!(concat!(env!("OUT_DIR"), "/js_word.rs"));
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
pub struct Atom(Arc<str>);

impl Atom {
Expand Down
27 changes: 14 additions & 13 deletions crates/swc_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,33 @@ perf = ["parking_lot"]
plugin-base = ["anyhow", "rkyv-impl", "diagnostic-serde"]
plugin-mode = ["plugin-base"]
plugin-rt = ["plugin-base"]
rkyv-impl = ["rkyv"]
rkyv-impl = ["rkyv", "bytecheck"]
tty-emitter = ["atty", "termcolor"]

[dependencies]
ahash = "0.7.4"
anyhow = {version = "1.0.45", optional = true}
arbitrary = {version = "1", optional = true, features = ["derive"]}
ast_node = {version = "0.7.5", path = "../ast_node"}
atty = {version = "0.2", optional = true}
better_scoped_tls = {version = "0.1.0", path = "../better_scoped_tls"}
anyhow = { version = "1.0.45", optional = true }
arbitrary = { version = "1", optional = true, features = ["derive"] }
ast_node = { version = "0.7.5", path = "../ast_node" }
atty = { version = "0.2", optional = true }
better_scoped_tls = { version = "0.1.0", path = "../better_scoped_tls" }
bytecheck = { version = "0.6.8", optional = true }
cfg-if = "1.0.0"
debug_unreachable = "0.1.1"
either = "1.5"
from_variant = {version = "0.1.3", path = "../from_variant"}
from_variant = { version = "0.1.3", path = "../from_variant" }
num-bigint = "0.4"
once_cell = "1.10.0"
parking_lot = {version = "0.12.0", optional = true}
parking_lot = { version = "0.12.0", optional = true }
rkyv = { version = "0.7.39", optional = true }
rustc-hash = "1.1.0"
serde = {version = "1.0.119", features = ["derive"]}
serde = { version = "1.0.119", features = ["derive"] }
siphasher = "0.3.9"
sourcemap = {version = "6", optional = true}
sourcemap = { version = "6", optional = true }
string_cache = "0.8.4"
swc_eq_ignore_macros = {version = "0.1", path = "../swc_eq_ignore_macros"}
swc_visit = {version = "0.3.0", path = "../swc_visit"}
termcolor = {version = "1.0", optional = true}
swc_eq_ignore_macros = { version = "0.1", path = "../swc_eq_ignore_macros" }
swc_visit = { version = "0.3.0", path = "../swc_visit" }
termcolor = { version = "1.0", optional = true }
tracing = "0.1.32"
unicode-width = "0.1.4"
url = "2.2.2"
Expand Down
5 changes: 5 additions & 0 deletions crates/swc_common/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ impl SingleThreadedComments {
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
pub struct Comment {
pub kind: CommentKind,
pub span: Span,
Expand All @@ -558,6 +559,10 @@ impl Spanned for Comment {
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "rkyv",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
pub enum CommentKind {
Line,
Block,
Expand Down
12 changes: 12 additions & 0 deletions crates/swc_common/src/errors/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ use crate::syntax_pos::{MultiSpan, Span};
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(C), derive(bytecheck::CheckBytes))
)]
pub struct Diagnostic {
pub level: Level,
pub message: Vec<(String, Style)>,
Expand All @@ -41,6 +45,10 @@ pub struct Diagnostic {
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
pub enum DiagnosticId {
Error(String),
Lint(String),
Expand All @@ -56,6 +64,10 @@ pub enum DiagnosticId {
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(C), derive(bytecheck::CheckBytes))
)]
pub struct SubDiagnostic {
pub level: Level,
pub message: Vec<(String, Style)>,
Expand Down
23 changes: 23 additions & 0 deletions crates/swc_common/src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// TODO: https://github.com/rkyv/bytecheck/issues/20
#![allow(ambiguous_associated_items)]

use std::{
borrow::Cow,
cell::RefCell,
Expand Down Expand Up @@ -50,6 +53,10 @@ mod styled_buffer;
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
pub enum Applicability {
MachineApplicable,
HasPlaceholders,
Expand All @@ -66,6 +73,10 @@ pub enum Applicability {
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(C), derive(bytecheck::CheckBytes))
)]
pub struct CodeSuggestion {
/// Each substitute can have multiple variants due to multiple
/// applicable suggestions
Expand Down Expand Up @@ -117,6 +128,10 @@ pub struct CodeSuggestion {
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(C), derive(bytecheck::CheckBytes))
)]
pub struct Substitution {
pub parts: Vec<SubstitutionPart>,
}
Expand All @@ -130,6 +145,10 @@ pub struct Substitution {
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(C), derive(bytecheck::CheckBytes))
)]
pub struct SubstitutionPart {
pub span: Span,
pub snippet: String,
Expand Down Expand Up @@ -877,6 +896,10 @@ impl Handler {
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
pub enum Level {
Bug,
Fatal,
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_common/src/errors/snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ pub struct StyledString {
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
pub enum Style {
MainHeaderMsg,
HeaderMsg,
Expand Down
18 changes: 18 additions & 0 deletions crates/swc_common/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use std::any::type_name;

use anyhow::Error;
use bytecheck::CheckBytes;
use rkyv::{with::AsBox, Archive, Deserialize, Serialize};

use crate::{syntax_pos::Mark, SyntaxContext};

Expand All @@ -16,6 +18,10 @@ use crate::{syntax_pos::Mark, SyntaxContext};
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
/// Enum for possible errors while running transform via plugin.
/// This error indicates internal operation failure either in plugin_runner
/// or plugin_macro. Plugin's transform fn itself does not allow to return
Expand Down Expand Up @@ -157,3 +163,15 @@ impl Serialized {
}
}
}

/// A wrapper type for the structures to be passed into plugins
/// serializes the contained value out-of-line so that newer
/// versions can be viewed as the older version.
///
/// First field indicate version of struct type (schema). Any consumers like
/// swc_plugin_macro can use this to validate compatiblility before attempt to
/// serialize.
#[derive(Archive, Deserialize, Serialize)]
#[repr(transparent)]
#[archive_attr(repr(transparent), derive(CheckBytes))]
pub struct VersionedSerializable<T>(#[with(AsBox)] pub (u32, T));
Loading