Skip to content

Commit e5feaf1

Browse files
quininerkdy1Copilot
authored
refactor(plugin): Switch plugin abi to flexible serialization (#11198)
**Description:** This is a follow up to #11100, which will result in plugin abi break. It currently passes the plugin tests, but I still need to do some code and configuration cleanup, and add a regression test. <img width="2053" height="336" alt="plugin tests" src="https://github.com/user-attachments/assets/868b2ae8-105c-44ad-8d18-641a2dd33c9f" /> **BREAKING CHANGE:** Since we switched serialization schemes, this will result in breaking changes on the plugin ABI and api. --------- Co-authored-by: Donny/강동윤 <[email protected]> Co-authored-by: Donny/강동윤 <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 2ae37d2 commit e5feaf1

File tree

54 files changed

+1026
-2845
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1026
-2845
lines changed

Cargo.lock

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

crates/ast_node/src/encoding/decode.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use syn::{spanned::Spanned, Data, DeriveInput};
22

3-
use super::{is_unknown, is_with, EnumType};
3+
use super::{is_ignore, is_unknown, is_with, EnumType};
44

55
pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
66
match data {
@@ -23,9 +23,11 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
2323
.zip(names.iter())
2424
.map(|(field, field_name)| -> syn::Stmt {
2525
let ty = &field.ty;
26-
let value: syn::Expr = match is_with(&field.attrs) {
27-
Some(with_type) => syn::parse_quote!(<#with_type<#ty> as cbor4ii::core::dec::Decode<'_>>::decode(reader)?.0),
28-
None => syn::parse_quote!(<#ty as cbor4ii::core::dec::Decode<'_>>::decode(reader)?)
26+
let value: syn::Expr = match (is_with(&field.attrs), is_ignore(&field.attrs)) {
27+
(Some(with_type), false) => syn::parse_quote!(<#with_type<#ty> as cbor4ii::core::dec::Decode<'_>>::decode(reader)?.0),
28+
(None, false) => syn::parse_quote!(<#ty as cbor4ii::core::dec::Decode<'_>>::decode(reader)?),
29+
(None, true) => syn::parse_quote!(<#ty as Default>::default()),
30+
(Some(_), true) => panic!("Cannot use both #[encoding(with)] and #[encoding(ignore)] attributes on the same field")
2931
};
3032

3133
syn::parse_quote!{
@@ -124,7 +126,7 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
124126
syn::parse_quote! {
125127
tag => {
126128
let tag: u32 = tag.try_into().map_err(|_| cbor4ii::core::error::DecodeError::CastOverflow {
127-
name: &"tag",
129+
name: &"unknown-tag",
128130
})?;
129131
let val = <#val_ty as cbor4ii::core::dec::Decode<'_>>::decode(reader)?;
130132
#ident::#name(tag, val)
@@ -236,10 +238,10 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
236238
Some(arm) => arm,
237239
None => {
238240
syn::parse_quote! {
239-
_ => {
240-
let err = cbor4ii::core::error::DecodeError::Mismatch {
241+
tag => {
242+
let err = cbor4ii::core::error::DecodeError::Custom {
241243
name: &stringify!(#ident),
242-
found: 0
244+
num: tag as u32
243245
};
244246
return Err(err);
245247
}

crates/ast_node/src/encoding/encode.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use syn::{spanned::Spanned, Data, DeriveInput};
22

3-
use super::{is_unknown, is_with, EnumType};
3+
use super::{is_ignore, is_unknown, is_with, EnumType};
44

55
pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
66
match data {
@@ -9,6 +9,7 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
99
.fields
1010
.iter()
1111
.enumerate()
12+
.filter(|(_, field)| !is_ignore(&field.attrs))
1213
.map(|(idx, field)| -> syn::Stmt {
1314
let fieldpath: syn::ExprField = match field.ident.as_ref() {
1415
Some(name) => syn::parse_quote!(self.#name),

crates/ast_node/src/encoding/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,17 @@ fn is_with(attrs: &[syn::Attribute]) -> Option<syn::Path> {
4343
with_type
4444
})
4545
}
46+
47+
fn is_ignore(attrs: &[syn::Attribute]) -> bool {
48+
attrs
49+
.iter()
50+
.filter(|attr| attr.path().is_ident("encoding"))
51+
.any(|attr| {
52+
let mut has_ignore = false;
53+
let _ = attr.parse_nested_meta(|meta| {
54+
has_ignore |= meta.path.is_ident("ignore");
55+
Ok(())
56+
});
57+
has_ignore
58+
})
59+
}

crates/swc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ isolated-dts = ["swc_typescript"]
3131
node = ["napi", "napi-derive", "swc_compiler_base/node"]
3232
plugin = [
3333
"swc_plugin_runner/ecma",
34-
"swc_plugin_runner/rkyv-impl",
34+
"swc_plugin_runner/encoding-impl",
3535
"swc_plugin_proxy/plugin-rt",
3636
"tokio",
3737
]

crates/swc_cli_impl/src/commands/plugin.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ swc_core = {{ version = "{swc_core_version}", features = ["ecma_plugin_transform
191191
# Alias to build actual plugin binary for the specified target.
192192
build-wasip1 = "build --target wasm32-wasip1"
193193
build-wasm32 = "build --target wasm32-unknown-unknown"
194+
195+
[target.'cfg(target_arch = "wasm32")']
196+
rustflags = [
197+
"--cfg=swc_ast_unknown"
198+
]
194199
"#
195200
.as_bytes(),
196201
)

crates/swc_common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ bench = false
1919
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(swc_ast_unknown)'] }
2020

2121
[features]
22-
__plugin = []
22+
__plugin = ["encoding-impl"]
2323
__plugin_mode = []
2424
__plugin_rt = []
2525
concurrent = ["parking_lot"]

crates/swc_common/src/comments.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ impl SingleThreadedComments {
636636
)]
637637
#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))]
638638
#[cfg_attr(feature = "rkyv-impl", repr(C))]
639+
#[cfg_attr(feature = "encoding-impl", derive(crate::Encode, crate::Decode))]
639640
pub struct Comment {
640641
pub kind: CommentKind,
641642
pub span: Span,
@@ -656,6 +657,7 @@ impl Spanned for Comment {
656657
)]
657658
#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))]
658659
#[cfg_attr(feature = "rkyv-impl", repr(u32))]
660+
#[cfg_attr(feature = "encoding-impl", derive(crate::Encode, crate::Decode))]
659661
pub enum CommentKind {
660662
Line = 0,
661663
Block = 1,

crates/swc_common/src/errors/diagnostic.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ use crate::syntax_pos::{MultiSpan, Span};
2424
)]
2525
#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))]
2626
#[cfg_attr(feature = "rkyv-impl", repr(C))]
27+
#[cfg_attr(
28+
feature = "encoding-impl",
29+
derive(::ast_node::Encode, ::ast_node::Decode)
30+
)]
2731
pub struct Message(pub String, pub Style);
2832

2933
#[must_use]
@@ -38,6 +42,10 @@ pub struct Message(pub String, pub Style);
3842
)]
3943
#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))]
4044
#[cfg_attr(feature = "rkyv-impl", repr(C))]
45+
#[cfg_attr(
46+
feature = "encoding-impl",
47+
derive(::ast_node::Encode, ::ast_node::Decode)
48+
)]
4149
/// Represents a diagnostic message with its level, message, unique identifier,
4250
/// span, children, and suggestions.
4351
pub struct Diagnostic {
@@ -47,6 +55,10 @@ pub struct Diagnostic {
4755
pub message: Vec<Message>,
4856
/// A unique identifier for the diagnostic, which can be used to look up
4957
/// more information
58+
#[cfg_attr(
59+
feature = "encoding-impl",
60+
encoding(with = "cbor4ii::core::types::Maybe")
61+
)]
5062
pub code: Option<DiagnosticId>,
5163
/// The span of the source code where the diagnostic is located
5264
pub span: MultiSpan,
@@ -67,6 +79,10 @@ pub struct Diagnostic {
6779
)]
6880
#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))]
6981
#[cfg_attr(feature = "rkyv-impl", repr(u32))]
82+
#[cfg_attr(
83+
feature = "encoding-impl",
84+
derive(::ast_node::Encode, ::ast_node::Decode)
85+
)]
7086
pub enum DiagnosticId {
7187
Error(String),
7288
Lint(String),
@@ -84,10 +100,18 @@ pub enum DiagnosticId {
84100
)]
85101
#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))]
86102
#[cfg_attr(feature = "rkyv-impl", repr(C))]
103+
#[cfg_attr(
104+
feature = "encoding-impl",
105+
derive(::ast_node::Encode, ::ast_node::Decode)
106+
)]
87107
pub struct SubDiagnostic {
88108
pub level: Level,
89109
pub message: Vec<Message>,
90110
pub span: MultiSpan,
111+
#[cfg_attr(
112+
feature = "encoding-impl",
113+
encoding(with = "cbor4ii::core::types::Maybe")
114+
)]
91115
pub render_span: Option<MultiSpan>,
92116
}
93117

crates/swc_common/src/errors/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ mod styled_buffer;
5454
)]
5555
#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))]
5656
#[cfg_attr(feature = "rkyv-impl", repr(u32))]
57+
#[cfg_attr(
58+
feature = "encoding-impl",
59+
derive(::ast_node::Encode, ::ast_node::Decode)
60+
)]
5761
pub enum Applicability {
5862
MachineApplicable,
5963
HasPlaceholders,
@@ -72,6 +76,10 @@ pub enum Applicability {
7276
)]
7377
#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))]
7478
#[cfg_attr(feature = "rkyv-impl", repr(C))]
79+
#[cfg_attr(
80+
feature = "encoding-impl",
81+
derive(::ast_node::Encode, ::ast_node::Decode)
82+
)]
7583
pub struct CodeSuggestion {
7684
/// Each substitute can have multiple variants due to multiple
7785
/// applicable suggestions
@@ -125,6 +133,10 @@ pub struct CodeSuggestion {
125133
)]
126134
#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))]
127135
#[cfg_attr(feature = "rkyv-impl", repr(C))]
136+
#[cfg_attr(
137+
feature = "encoding-impl",
138+
derive(::ast_node::Encode, ::ast_node::Decode)
139+
)]
128140
pub struct Substitution {
129141
pub parts: Vec<SubstitutionPart>,
130142
}
@@ -140,6 +152,10 @@ pub struct Substitution {
140152
)]
141153
#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))]
142154
#[cfg_attr(feature = "rkyv-impl", repr(C))]
155+
#[cfg_attr(
156+
feature = "encoding-impl",
157+
derive(::ast_node::Encode, ::ast_node::Decode)
158+
)]
143159
pub struct SubstitutionPart {
144160
pub span: Span,
145161
pub snippet: String,
@@ -901,6 +917,10 @@ impl Handler {
901917
)]
902918
#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))]
903919
#[cfg_attr(feature = "rkyv-impl", repr(u32))]
920+
#[cfg_attr(
921+
feature = "encoding-impl",
922+
derive(::ast_node::Encode, ::ast_node::Decode)
923+
)]
904924
pub enum Level {
905925
Bug,
906926
Fatal,

0 commit comments

Comments
 (0)