Skip to content

Commit e34a864

Browse files
authored
feat(plugin): Add versioned wrapper struct (#5062)
1 parent 1de0581 commit e34a864

File tree

22 files changed

+169
-14
lines changed

22 files changed

+169
-14
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ast_node/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ pub fn ast_node(
213213
feature = "rkyv",
214214
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
215215
)]
216+
#[cfg_attr(
217+
feature = "rkyv",
218+
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
219+
)]
216220
#[cfg_attr(
217221
feature = "rkyv",
218222
archive(bound(
@@ -269,6 +273,10 @@ pub fn ast_node(
269273
feature = "rkyv",
270274
archive(bound(serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace"))
271275
)]
276+
#[cfg_attr(
277+
feature = "rkyv",
278+
archive_attr(repr(C), derive(bytecheck::CheckBytes))
279+
)]
272280
serde_tag
273281
#[serde(rename_all = "camelCase")]
274282
serde_rename

crates/swc_atoms/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ version = "0.2.12"
1313
bench = false
1414

1515
[dependencies]
16+
bytecheck = { version = "0.6.8", optional = true }
1617
once_cell = "1"
1718
rkyv = { version = "0.7.39", optional = true }
1819
rustc-hash = "1.1.0"

crates/swc_atoms/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ include!(concat!(env!("OUT_DIR"), "/js_word.rs"));
3636
feature = "rkyv",
3737
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
3838
)]
39+
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
3940
pub struct Atom(Arc<str>);
4041

4142
impl Atom {

crates/swc_common/Cargo.toml

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,33 @@ perf = ["parking_lot"]
2424
plugin-base = ["anyhow", "rkyv-impl", "diagnostic-serde"]
2525
plugin-mode = ["plugin-base"]
2626
plugin-rt = ["plugin-base"]
27-
rkyv-impl = ["rkyv"]
27+
rkyv-impl = ["rkyv", "bytecheck"]
2828
tty-emitter = ["atty", "termcolor"]
2929

3030
[dependencies]
3131
ahash = "0.7.4"
32-
anyhow = {version = "1.0.45", optional = true}
33-
arbitrary = {version = "1", optional = true, features = ["derive"]}
34-
ast_node = {version = "0.7.5", path = "../ast_node"}
35-
atty = {version = "0.2", optional = true}
36-
better_scoped_tls = {version = "0.1.0", path = "../better_scoped_tls"}
32+
anyhow = { version = "1.0.45", optional = true }
33+
arbitrary = { version = "1", optional = true, features = ["derive"] }
34+
ast_node = { version = "0.7.5", path = "../ast_node" }
35+
atty = { version = "0.2", optional = true }
36+
better_scoped_tls = { version = "0.1.0", path = "../better_scoped_tls" }
37+
bytecheck = { version = "0.6.8", optional = true }
3738
cfg-if = "1.0.0"
3839
debug_unreachable = "0.1.1"
3940
either = "1.5"
40-
from_variant = {version = "0.1.3", path = "../from_variant"}
41+
from_variant = { version = "0.1.3", path = "../from_variant" }
4142
num-bigint = "0.4"
4243
once_cell = "1.10.0"
43-
parking_lot = {version = "0.12.0", optional = true}
44+
parking_lot = { version = "0.12.0", optional = true }
4445
rkyv = { version = "0.7.39", optional = true }
4546
rustc-hash = "1.1.0"
46-
serde = {version = "1.0.119", features = ["derive"]}
47+
serde = { version = "1.0.119", features = ["derive"] }
4748
siphasher = "0.3.9"
48-
sourcemap = {version = "6", optional = true}
49+
sourcemap = { version = "6", optional = true }
4950
string_cache = "0.8.4"
50-
swc_eq_ignore_macros = {version = "0.1", path = "../swc_eq_ignore_macros"}
51-
swc_visit = {version = "0.3.0", path = "../swc_visit"}
52-
termcolor = {version = "1.0", optional = true}
51+
swc_eq_ignore_macros = { version = "0.1", path = "../swc_eq_ignore_macros" }
52+
swc_visit = { version = "0.3.0", path = "../swc_visit" }
53+
termcolor = { version = "1.0", optional = true }
5354
tracing = "0.1.32"
5455
unicode-width = "0.1.4"
5556
url = "2.2.2"

crates/swc_common/src/comments.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ impl SingleThreadedComments {
541541
feature = "rkyv",
542542
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
543543
)]
544+
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
544545
pub struct Comment {
545546
pub kind: CommentKind,
546547
pub span: Span,
@@ -558,6 +559,10 @@ impl Spanned for Comment {
558559
feature = "rkyv",
559560
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
560561
)]
562+
#[cfg_attr(
563+
feature = "rkyv",
564+
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
565+
)]
561566
pub enum CommentKind {
562567
Line,
563568
Block,

crates/swc_common/src/errors/diagnostic.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ use crate::syntax_pos::{MultiSpan, Span};
2323
feature = "plugin-base",
2424
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
2525
)]
26+
#[cfg_attr(
27+
feature = "plugin-base",
28+
archive_attr(repr(C), derive(bytecheck::CheckBytes))
29+
)]
2630
pub struct Diagnostic {
2731
pub level: Level,
2832
pub message: Vec<(String, Style)>,
@@ -41,6 +45,10 @@ pub struct Diagnostic {
4145
feature = "plugin-base",
4246
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
4347
)]
48+
#[cfg_attr(
49+
feature = "plugin-base",
50+
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
51+
)]
4452
pub enum DiagnosticId {
4553
Error(String),
4654
Lint(String),
@@ -56,6 +64,10 @@ pub enum DiagnosticId {
5664
feature = "plugin-base",
5765
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
5866
)]
67+
#[cfg_attr(
68+
feature = "plugin-base",
69+
archive_attr(repr(C), derive(bytecheck::CheckBytes))
70+
)]
5971
pub struct SubDiagnostic {
6072
pub level: Level,
6173
pub message: Vec<(String, Style)>,

crates/swc_common/src/errors/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// TODO: https://github.com/rkyv/bytecheck/issues/20
12+
#![allow(ambiguous_associated_items)]
13+
1114
use std::{
1215
borrow::Cow,
1316
cell::RefCell,
@@ -50,6 +53,10 @@ mod styled_buffer;
5053
feature = "plugin-base",
5154
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
5255
)]
56+
#[cfg_attr(
57+
feature = "plugin-base",
58+
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
59+
)]
5360
pub enum Applicability {
5461
MachineApplicable,
5562
HasPlaceholders,
@@ -66,6 +73,10 @@ pub enum Applicability {
6673
feature = "plugin-base",
6774
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
6875
)]
76+
#[cfg_attr(
77+
feature = "plugin-base",
78+
archive_attr(repr(C), derive(bytecheck::CheckBytes))
79+
)]
6980
pub struct CodeSuggestion {
7081
/// Each substitute can have multiple variants due to multiple
7182
/// applicable suggestions
@@ -117,6 +128,10 @@ pub struct CodeSuggestion {
117128
feature = "plugin-base",
118129
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
119130
)]
131+
#[cfg_attr(
132+
feature = "plugin-base",
133+
archive_attr(repr(C), derive(bytecheck::CheckBytes))
134+
)]
120135
pub struct Substitution {
121136
pub parts: Vec<SubstitutionPart>,
122137
}
@@ -130,6 +145,10 @@ pub struct Substitution {
130145
feature = "plugin-base",
131146
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
132147
)]
148+
#[cfg_attr(
149+
feature = "plugin-base",
150+
archive_attr(repr(C), derive(bytecheck::CheckBytes))
151+
)]
133152
pub struct SubstitutionPart {
134153
pub span: Span,
135154
pub snippet: String,
@@ -877,6 +896,10 @@ impl Handler {
877896
feature = "plugin-base",
878897
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
879898
)]
899+
#[cfg_attr(
900+
feature = "plugin-base",
901+
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
902+
)]
880903
pub enum Level {
881904
Bug,
882905
Fatal,

crates/swc_common/src/errors/snippet.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ pub struct StyledString {
184184
feature = "plugin-base",
185185
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
186186
)]
187+
#[cfg_attr(
188+
feature = "plugin-base",
189+
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
190+
)]
187191
pub enum Style {
188192
MainHeaderMsg,
189193
HeaderMsg,

crates/swc_common/src/plugin.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use std::any::type_name;
88

99
use anyhow::Error;
10+
use bytecheck::CheckBytes;
11+
use rkyv::{with::AsBox, Archive, Deserialize, Serialize};
1012

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

@@ -16,6 +18,10 @@ use crate::{syntax_pos::Mark, SyntaxContext};
1618
feature = "plugin-base",
1719
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
1820
)]
21+
#[cfg_attr(
22+
feature = "plugin-base",
23+
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
24+
)]
1925
/// Enum for possible errors while running transform via plugin.
2026
/// This error indicates internal operation failure either in plugin_runner
2127
/// or plugin_macro. Plugin's transform fn itself does not allow to return
@@ -157,3 +163,15 @@ impl Serialized {
157163
}
158164
}
159165
}
166+
167+
/// A wrapper type for the structures to be passed into plugins
168+
/// serializes the contained value out-of-line so that newer
169+
/// versions can be viewed as the older version.
170+
///
171+
/// First field indicate version of struct type (schema). Any consumers like
172+
/// swc_plugin_macro can use this to validate compatiblility before attempt to
173+
/// serialize.
174+
#[derive(Archive, Deserialize, Serialize)]
175+
#[repr(transparent)]
176+
#[archive_attr(repr(transparent), derive(CheckBytes))]
177+
pub struct VersionedSerializable<T>(#[with(AsBox)] pub (u32, T));

0 commit comments

Comments
 (0)