-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(swc/common): wrap serialized struct with versioned (#5060: Part 3) #5128
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
Changes from 5 commits
c9acbed
d61d2a3
c9f936f
b845acd
4a7040f
42df149
509e766
e3bfe59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -548,6 +548,16 @@ pub struct Comment { | |
pub text: String, | ||
} | ||
|
||
impl Default for Comment { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this still required, even if we add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. default was added to satisfy mem::take in I'm bit unsure if I understood the suggestion correctly, mind elaborate bit more? The way I understood is let each struct implement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
fn default() -> Self { | ||
Comment { | ||
kind: CommentKind::Line, | ||
span: DUMMY_SP, | ||
text: "".into(), | ||
kdy1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
impl Spanned for Comment { | ||
fn span(&self) -> Span { | ||
self.span | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,19 @@ pub struct Diagnostic { | |
pub suggestions: Vec<CodeSuggestion>, | ||
} | ||
|
||
impl Default for Diagnostic { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
fn default() -> Self { | ||
Diagnostic { | ||
level: Level::Bug, | ||
message: vec![], | ||
code: None, | ||
span: MultiSpan::new(), | ||
children: vec![], | ||
suggestions: vec![], | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
#[cfg_attr( | ||
feature = "diagnostic-serde", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,12 @@ pub enum PluginError { | |
Serialize(String), | ||
} | ||
|
||
impl Default for PluginError { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
fn default() -> Self { | ||
PluginError::Deserialize("Default serialization error".to_string()) | ||
} | ||
} | ||
|
||
/// Wraps internal representation of serialized data for exchanging data between | ||
/// plugin to the host. Consumers should not rely on specific details of byte | ||
/// format struct contains: it is strict implementation detail which can | ||
|
@@ -62,12 +68,12 @@ impl PluginSerializedBytes { | |
} | ||
|
||
/** | ||
* Constructs an instance from given struct by serializing it. | ||
* Constructs an instance from versioned struct by serializing it. | ||
* | ||
* This is sort of mimic TryFrom behavior, since we can't use generic | ||
* to implement TryFrom trait | ||
*/ | ||
pub fn try_serialize<W>(t: &W) -> Result<Self, Error> | ||
pub fn try_serialize<W>(t: &VersionedSerializable<W>) -> Result<Self, Error> | ||
where | ||
W: rkyv::Serialize<rkyv::ser::serializers::AllocSerializer<512>>, | ||
{ | ||
|
@@ -105,15 +111,15 @@ impl PluginSerializedBytes { | |
(self.field.as_ptr(), self.field.len()) | ||
} | ||
|
||
pub fn deserialize<W>(&self) -> Result<W, Error> | ||
pub fn deserialize<W>(&self) -> Result<VersionedSerializable<W>, Error> | ||
where | ||
W: rkyv::Archive, | ||
W::Archived: rkyv::Deserialize<W, rkyv::de::deserializers::SharedDeserializeMap>, | ||
{ | ||
use anyhow::Context; | ||
use rkyv::Deserialize; | ||
|
||
let archived = unsafe { rkyv::archived_root::<W>(&self.field[..]) }; | ||
let archived = unsafe { rkyv::archived_root::<VersionedSerializable<W>>(&self.field[..]) }; | ||
|
||
archived | ||
.deserialize(&mut rkyv::de::deserializers::SharedDeserializeMap::new()) | ||
|
@@ -130,7 +136,7 @@ impl PluginSerializedBytes { | |
pub unsafe fn deserialize_from_ptr<W>( | ||
raw_allocated_ptr: *const u8, | ||
raw_allocated_ptr_len: i32, | ||
) -> Result<W, Error> | ||
) -> Result<VersionedSerializable<W>, Error> | ||
where | ||
W: rkyv::Archive, | ||
W::Archived: rkyv::Deserialize<W, rkyv::de::deserializers::SharedDeserializeMap>, | ||
|
@@ -153,7 +159,7 @@ where | |
pub unsafe fn deserialize_from_ptr_into_fallible<W>( | ||
raw_allocated_ptr: *const u8, | ||
raw_allocated_ptr_len: i32, | ||
) -> Result<W, Error> | ||
) -> Result<VersionedSerializable<W>, Error> | ||
where | ||
W: rkyv::Archive, | ||
W::Archived: rkyv::Deserialize<W, rkyv::de::deserializers::SharedDeserializeMap>, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,6 +130,12 @@ pub enum FileName { | |
Custom(String), | ||
} | ||
|
||
impl Default for FileName { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
fn default() -> Self { | ||
FileName::Anon | ||
} | ||
} | ||
|
||
/// A wrapper that attempts to convert a type to and from UTF-8. | ||
/// | ||
/// Types like `OsString` and `PathBuf` aren't guaranteed to be encoded as | ||
|
@@ -805,7 +811,7 @@ impl Sub<BytePos> for NonNarrowChar { | |
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) | ||
)] | ||
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))] | ||
#[derive(Clone)] | ||
#[derive(Clone, Default)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
pub struct SourceFile { | ||
/// The name of the file that the source came from. Source that doesn't | ||
/// originate from files has names between angle brackets by convention, | ||
|
@@ -998,7 +1004,9 @@ pub trait Pos { | |
/// - Values larger than `u32::MAX - 2^16` are reserved for the comments. | ||
/// | ||
/// `u32::MAX` is special value used to generate source map entries. | ||
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)] | ||
#[derive( | ||
Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize, Default, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
)] | ||
#[serde(transparent)] | ||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] | ||
#[cfg_attr( | ||
|
@@ -1032,7 +1040,7 @@ impl BytePos { | |
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) | ||
)] | ||
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))] | ||
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] | ||
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Default)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
pub struct CharPos(pub usize); | ||
|
||
// FIXME: Lots of boilerplate in these impls, but so far my attempts to fix | ||
|
@@ -1128,7 +1136,7 @@ impl Sub for CharPos { | |
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) | ||
)] | ||
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))] | ||
#[derive(Debug, Clone)] | ||
#[derive(Debug, Clone, Default)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
pub struct Loc { | ||
/// Information about the original source | ||
pub file: Lrc<SourceFile>, | ||
|
@@ -1169,6 +1177,15 @@ pub struct SourceFileAndBytePos { | |
pub pos: BytePos, | ||
} | ||
|
||
impl Default for SourceFileAndBytePos { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
fn default() -> Self { | ||
SourceFileAndBytePos { | ||
sf: Lrc::new(SourceFile::default()), | ||
pos: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
#[cfg_attr( | ||
feature = "rkyv", | ||
|
@@ -1193,6 +1210,7 @@ pub struct LineCol { | |
pub col: u32, | ||
} | ||
|
||
#[derive(Default)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
#[cfg_attr( | ||
feature = "rkyv", | ||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) | ||
|
@@ -1224,6 +1242,12 @@ pub enum SpanLinesError { | |
DistinctSources(DistinctSources), | ||
} | ||
|
||
impl Default for SpanLinesError { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
fn default() -> Self { | ||
SpanLinesError::IllFormedSpan(DUMMY_SP) | ||
} | ||
} | ||
|
||
#[derive(Clone, PartialEq, Eq, Debug)] | ||
pub enum SpanSnippetError { | ||
DummyBytePos, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,7 @@ struct MarkData { | |
is_builtin: bool, | ||
} | ||
|
||
#[derive(Default)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
#[cfg_attr( | ||
feature = "rkyv", | ||
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) | ||
|
@@ -179,9 +180,10 @@ impl Mark { | |
pub fn is_descendant_of(mut self, ancestor: Mark) -> bool { | ||
// This code path executed inside of the guest memory context. | ||
// In here, preallocate memory for the context. | ||
let serialized = | ||
crate::plugin::PluginSerializedBytes::try_serialize(&MutableMarkContext(0, 0, 0)) | ||
.expect("Should be serializable"); | ||
let serialized = crate::plugin::PluginSerializedBytes::try_serialize( | ||
&crate::plugin::VersionedSerializable::new(MutableMarkContext(0, 0, 0)), | ||
) | ||
.expect("Should be serializable"); | ||
let (ptr, len) = serialized.as_ptr(); | ||
|
||
// Calling host proxy fn. Inside of host proxy, host will | ||
|
@@ -197,6 +199,7 @@ impl Mark { | |
len.try_into().expect("Should able to convert ptr length"), | ||
) | ||
.expect("Should able to deserialize") | ||
.take() | ||
}; | ||
self = Mark::from_u32(context.0); | ||
|
||
|
@@ -219,9 +222,10 @@ impl Mark { | |
#[allow(unused_mut, unused_assignments)] | ||
#[cfg(all(feature = "plugin-mode", target_arch = "wasm32"))] | ||
pub fn least_ancestor(mut a: Mark, mut b: Mark) -> Mark { | ||
let serialized = | ||
crate::plugin::PluginSerializedBytes::try_serialize(&MutableMarkContext(0, 0, 0)) | ||
.expect("Should be serializable"); | ||
let serialized = crate::plugin::PluginSerializedBytes::try_serialize( | ||
&crate::plugin::VersionedSerializable::new(MutableMarkContext(0, 0, 0)), | ||
) | ||
.expect("Should be serializable"); | ||
let (ptr, len) = serialized.as_ptr(); | ||
|
||
unsafe { | ||
|
@@ -234,6 +238,7 @@ impl Mark { | |
len.try_into().expect("Should able to convert ptr length"), | ||
) | ||
.expect("Should able to deserialize") | ||
.take() | ||
}; | ||
a = Mark::from_u32(context.0); | ||
b = Mark::from_u32(context.1); | ||
|
@@ -383,7 +388,7 @@ impl SyntaxContext { | |
|
||
#[cfg(all(feature = "plugin-mode", target_arch = "wasm32"))] | ||
pub fn remove_mark(&mut self) -> Mark { | ||
let context = MutableMarkContext(0, 0, 0); | ||
let context = crate::plugin::VersionedSerializable::new(MutableMarkContext(0, 0, 0)); | ||
let serialized = crate::plugin::PluginSerializedBytes::try_serialize(&context) | ||
.expect("Should be serializable"); | ||
let (ptr, len) = serialized.as_ptr(); | ||
|
@@ -398,6 +403,7 @@ impl SyntaxContext { | |
len.try_into().expect("Should able to convert ptr length"), | ||
) | ||
.expect("Should able to deserialize") | ||
.take() | ||
}; | ||
|
||
*self = SyntaxContext(context.0); | ||
|
Uh oh!
There was an error while loading. Please reload this page.