From c9acbed9a2e70d281e5e58aa9b9f410e81430ec6 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Wed, 6 Jul 2022 14:42:49 -0700 Subject: [PATCH 1/7] refactor(swc/common): explicitly require versioned wrapper for plugin serialize --- crates/swc_common/src/plugin.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/crates/swc_common/src/plugin.rs b/crates/swc_common/src/plugin.rs index cced6c938c42..28caa4a63b0b 100644 --- a/crates/swc_common/src/plugin.rs +++ b/crates/swc_common/src/plugin.rs @@ -41,6 +41,12 @@ pub enum PluginError { Serialize(String), } +impl Default for PluginError { + 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(t: &W) -> Result + pub fn try_serialize(t: &VersionedSerializable) -> Result where W: rkyv::Serialize>, { @@ -105,7 +111,7 @@ impl PluginSerializedBytes { (self.field.as_ptr(), self.field.len()) } - pub fn deserialize(&self) -> Result + pub fn deserialize(&self) -> Result, Error> where W: rkyv::Archive, W::Archived: rkyv::Deserialize, @@ -113,7 +119,7 @@ impl PluginSerializedBytes { use anyhow::Context; use rkyv::Deserialize; - let archived = unsafe { rkyv::archived_root::(&self.field[..]) }; + let archived = unsafe { rkyv::archived_root::>(&self.field[..]) }; archived .deserialize(&mut rkyv::de::deserializers::SharedDeserializeMap::new()) @@ -130,7 +136,7 @@ impl PluginSerializedBytes { pub unsafe fn deserialize_from_ptr( raw_allocated_ptr: *const u8, raw_allocated_ptr_len: i32, -) -> Result +) -> Result, Error> where W: rkyv::Archive, W::Archived: rkyv::Deserialize, @@ -153,7 +159,7 @@ where pub unsafe fn deserialize_from_ptr_into_fallible( raw_allocated_ptr: *const u8, raw_allocated_ptr_len: i32, -) -> Result +) -> Result, Error> where W: rkyv::Archive, W::Archived: rkyv::Deserialize, From d61d2a3c279c34051c42cff136ee1d9c8c2c247a Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Wed, 6 Jul 2022 14:44:22 -0700 Subject: [PATCH 2/7] refactor(swc/common): implement default for versioned.take() --- crates/swc_common/src/comments.rs | 10 +++++++ crates/swc_common/src/errors/diagnostic.rs | 13 +++++++++ crates/swc_common/src/syntax_pos.rs | 32 +++++++++++++++++++--- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/crates/swc_common/src/comments.rs b/crates/swc_common/src/comments.rs index d50d43c961eb..d6ab67e847f1 100644 --- a/crates/swc_common/src/comments.rs +++ b/crates/swc_common/src/comments.rs @@ -548,6 +548,16 @@ pub struct Comment { pub text: String, } +impl Default for Comment { + fn default() -> Self { + Comment { + kind: CommentKind::Line, + span: DUMMY_SP, + text: "".into(), + } + } +} + impl Spanned for Comment { fn span(&self) -> Span { self.span diff --git a/crates/swc_common/src/errors/diagnostic.rs b/crates/swc_common/src/errors/diagnostic.rs index 0a0766e91e3b..6f436eb0ff9c 100644 --- a/crates/swc_common/src/errors/diagnostic.rs +++ b/crates/swc_common/src/errors/diagnostic.rs @@ -36,6 +36,19 @@ pub struct Diagnostic { pub suggestions: Vec, } +impl Default for Diagnostic { + 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", diff --git a/crates/swc_common/src/syntax_pos.rs b/crates/swc_common/src/syntax_pos.rs index fabc27ef6d66..682aeca1497d 100644 --- a/crates/swc_common/src/syntax_pos.rs +++ b/crates/swc_common/src/syntax_pos.rs @@ -130,6 +130,12 @@ pub enum FileName { Custom(String), } +impl Default for FileName { + 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 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)] 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, +)] #[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)] 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)] pub struct Loc { /// Information about the original source pub file: Lrc, @@ -1169,6 +1177,15 @@ pub struct SourceFileAndBytePos { pub pos: BytePos, } +impl Default for SourceFileAndBytePos { + 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)] #[cfg_attr( feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) @@ -1224,6 +1242,12 @@ pub enum SpanLinesError { DistinctSources(DistinctSources), } +impl Default for SpanLinesError { + fn default() -> Self { + SpanLinesError::IllFormedSpan(DUMMY_SP) + } +} + #[derive(Clone, PartialEq, Eq, Debug)] pub enum SpanSnippetError { DummyBytePos, From c9f936fa6be2138c6aa1798a45b453c5e0828534 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Wed, 6 Jul 2022 14:45:04 -0700 Subject: [PATCH 3/7] refactor(swc/plugin): reexport versionedserializable --- crates/swc_plugin/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_plugin/src/lib.rs b/crates/swc_plugin/src/lib.rs index d2122cdee7d7..2f32ccd85322 100644 --- a/crates/swc_plugin/src/lib.rs +++ b/crates/swc_plugin/src/lib.rs @@ -3,7 +3,7 @@ // Reexports pub use swc_common::{ chain, - plugin::{deserialize_from_ptr, PluginError, PluginSerializedBytes}, + plugin::{deserialize_from_ptr, PluginError, PluginSerializedBytes, VersionedSerializable}, }; pub mod comments { From b845acda5015c5c907cf606e0bab9a63990b19a1 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Wed, 6 Jul 2022 14:48:02 -0700 Subject: [PATCH 4/7] refactor(swc/*): update references to serialized to versioned --- crates/swc/src/plugin.rs | 44 ++++++++++---- crates/swc_common/src/syntax_pos/hygiene.rs | 20 ++++--- crates/swc_plugin/src/handler.rs | 8 ++- crates/swc_plugin_macro/src/lib.rs | 9 +-- .../src/comments/plugin_comments_proxy.rs | 13 +++-- .../read_returned_result_from_host.rs | 19 +++--- .../src/source_map/plugin_source_map_proxy.rs | 13 ++++- crates/swc_plugin_runner/benches/invoke.rs | 12 +++- .../src/imported_fn/comments.rs | 42 ++++++++------ .../src/imported_fn/handler.rs | 3 +- .../src/imported_fn/hygiene.rs | 30 +++++----- .../src/imported_fn/source_map.rs | 19 +++--- .../swc_plugin_runner/src/memory_interop.rs | 7 ++- .../src/transform_executor.rs | 2 +- crates/swc_plugin_runner/tests/integration.rs | 58 ++++++++++++------- 15 files changed, 192 insertions(+), 107 deletions(-) diff --git a/crates/swc/src/plugin.rs b/crates/swc/src/plugin.rs index b5d90245a7e4..e7d112f6dfde 100644 --- a/crates/swc/src/plugin.rs +++ b/crates/swc/src/plugin.rs @@ -80,7 +80,10 @@ impl RustPlugins { use std::{path::PathBuf, sync::Arc}; use anyhow::Context; - use swc_common::{plugin::PluginSerializedBytes, FileName}; + use swc_common::{ + plugin::{PluginSerializedBytes, VersionedSerializable}, + FileName, + }; use swc_ecma_loader::resolve::Resolve; // swc_plugin_macro will not inject proxy to the comments if comments is empty @@ -92,7 +95,8 @@ impl RustPlugins { inner: self.comments.clone(), }, || { - let mut serialized = PluginSerializedBytes::try_serialize(&n)?; + let program = VersionedSerializable::new(n); + let mut serialized = PluginSerializedBytes::try_serialize(&program)?; // Run plugin transformation against current program. // We do not serialize / deserialize between each plugin execution but @@ -111,11 +115,19 @@ impl RustPlugins { let config_json = serde_json::to_string(&p.1) .context("Failed to serialize plugin config as json") - .and_then(|value| PluginSerializedBytes::try_serialize(&value))?; + .and_then(|value| { + PluginSerializedBytes::try_serialize(&VersionedSerializable::new( + value, + )) + })?; let context_json = serde_json::to_string(&self.plugin_context) .context("Failed to serialize plugin context as json") - .and_then(|value| PluginSerializedBytes::try_serialize(&value))?; + .and_then(|value| { + PluginSerializedBytes::try_serialize(&VersionedSerializable::new( + value, + )) + })?; let resolved_path = self .resolver @@ -152,7 +164,7 @@ impl RustPlugins { // Plugin transformation is done. Deserialize transformed bytes back // into Program - serialized.deserialize() + serialized.deserialize().map(|mut v| v.take()) }, ) } @@ -162,7 +174,10 @@ impl RustPlugins { use std::{path::PathBuf, sync::Arc}; use anyhow::Context; - use swc_common::{plugin::PluginSerializedBytes, FileName}; + use swc_common::{ + plugin::{PluginSerializedBytes, VersionedSerializable}, + FileName, + }; use swc_ecma_loader::resolve::Resolve; let should_enable_comments_proxy = self.comments.is_some(); @@ -172,17 +187,26 @@ impl RustPlugins { inner: self.comments.clone(), }, || { - let mut serialized = PluginSerializedBytes::try_serialize(&n)?; + let program = VersionedSerializable::new(n); + let mut serialized = PluginSerializedBytes::try_serialize(&program)?; if let Some(plugins) = &self.plugins { for p in plugins { let config_json = serde_json::to_string(&p.1) .context("Failed to serialize plugin config as json") - .and_then(|value| PluginSerializedBytes::try_serialize(&value))?; + .and_then(|value| { + PluginSerializedBytes::try_serialize(&VersionedSerializable::new( + value, + )) + })?; let context_json = serde_json::to_string(&self.plugin_context) .context("Failed to serialize plugin context as json") - .and_then(|value| PluginSerializedBytes::try_serialize(&value))?; + .and_then(|value| { + PluginSerializedBytes::try_serialize(&VersionedSerializable::new( + value, + )) + })?; serialized = swc_plugin_runner::apply_transform_plugin( &p.0, @@ -197,7 +221,7 @@ impl RustPlugins { } } - serialized.deserialize() + serialized.deserialize().map(|mut v| v.take()) }, ) } diff --git a/crates/swc_common/src/syntax_pos/hygiene.rs b/crates/swc_common/src/syntax_pos/hygiene.rs index 730a7e6528f3..85bfd650b83e 100644 --- a/crates/swc_common/src/syntax_pos/hygiene.rs +++ b/crates/swc_common/src/syntax_pos/hygiene.rs @@ -67,6 +67,7 @@ struct MarkData { is_builtin: bool, } +#[derive(Default)] #[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); diff --git a/crates/swc_plugin/src/handler.rs b/crates/swc_plugin/src/handler.rs index 57e7d59dc585..abb486cf01fe 100644 --- a/crates/swc_plugin/src/handler.rs +++ b/crates/swc_plugin/src/handler.rs @@ -1,4 +1,7 @@ -use swc_common::{plugin::PluginSerializedBytes, sync::OnceCell}; +use swc_common::{ + plugin::{PluginSerializedBytes, VersionedSerializable}, + sync::OnceCell, +}; use crate::pseudo_scoped_key::PseudoScopedKey; @@ -18,7 +21,8 @@ pub struct PluginDiagnosticsEmitter; impl swc_common::errors::Emitter for PluginDiagnosticsEmitter { #[cfg_attr(not(target_arch = "wasm32"), allow(unused))] fn emit(&mut self, db: &swc_common::errors::DiagnosticBuilder<'_>) { - let diag = PluginSerializedBytes::try_serialize(&*db.diagnostic) + let diagnostic = VersionedSerializable::new((*db.diagnostic).clone()); + let diag = PluginSerializedBytes::try_serialize(&diagnostic) .expect("Should able to serialize Diagnostic"); let (ptr, len) = diag.as_ptr(); diff --git a/crates/swc_plugin_macro/src/lib.rs b/crates/swc_plugin_macro/src/lib.rs index 1cb6e2ac4899..4fc9502c3863 100644 --- a/crates/swc_plugin_macro/src/lib.rs +++ b/crates/swc_plugin_macro/src/lib.rs @@ -43,6 +43,7 @@ fn handle_func(func: ItemFn) -> TokenStream { /// Internal function plugin_macro uses to create ptr to PluginError. fn construct_error_ptr(plugin_error: swc_plugin::PluginError) -> i32 { + let plugin_error = swc_plugin::VersionedSerializable::new(plugin_error); let ret = swc_plugin::PluginSerializedBytes::try_serialize(&plugin_error).expect("Should able to serialize PluginError"); let (ptr, len) = ret.as_ptr(); @@ -61,14 +62,14 @@ fn handle_func(func: ItemFn) -> TokenStream { pub fn #process_impl_ident(ast_ptr: *const u8, ast_ptr_len: i32, config_str_ptr: *const u8, config_str_ptr_len: i32, context_str_ptr: *const u8, context_str_ptr_len: i32, should_enable_comments_proxy: i32) -> i32 { // Reconstruct `Program` & config string from serialized program // Host (SWC) should allocate memory, copy bytes and pass ptr to plugin. - let program = unsafe { swc_plugin::deserialize_from_ptr(ast_ptr, ast_ptr_len) }; + let program = unsafe { swc_plugin::deserialize_from_ptr(ast_ptr, ast_ptr_len).map(|mut v| v.take()) }; if program.is_err() { let err = swc_plugin::PluginError::Deserialize("Failed to deserialize program received from host".to_string()); return construct_error_ptr(err); } let program: Program = program.expect("Should be a program"); - let config = unsafe { swc_plugin::deserialize_from_ptr(config_str_ptr, config_str_ptr_len) }; + let config = unsafe { swc_plugin::deserialize_from_ptr(config_str_ptr, config_str_ptr_len).map(|mut v| v.take()) }; if config.is_err() { let err = swc_plugin::PluginError::Deserialize( "Failed to deserialize config string received from host".to_string() @@ -77,7 +78,7 @@ fn handle_func(func: ItemFn) -> TokenStream { } let config: String = config.expect("Should be a string"); - let context = unsafe { swc_plugin::deserialize_from_ptr(context_str_ptr, context_str_ptr_len) }; + let context = unsafe { swc_plugin::deserialize_from_ptr(context_str_ptr, context_str_ptr_len).map(|mut v| v.take()) }; if context.is_err() { let err = swc_plugin::PluginError::Deserialize("Failed to deserialize context string received from host".to_string()); return construct_error_ptr(err); @@ -112,7 +113,7 @@ fn handle_func(func: ItemFn) -> TokenStream { let transformed_program = #ident(program, metadata); // Serialize transformed result, return back to the host. - let serialized_result = swc_plugin::PluginSerializedBytes::try_serialize(&transformed_program); + let serialized_result = swc_plugin::PluginSerializedBytes::try_serialize(&swc_plugin::VersionedSerializable::new(transformed_program)); if serialized_result.is_err() { let err = swc_plugin::PluginError::Serialize("Failed to serialize transformed program".to_string()); diff --git a/crates/swc_plugin_proxy/src/comments/plugin_comments_proxy.rs b/crates/swc_plugin_proxy/src/comments/plugin_comments_proxy.rs index 8054cb299e52..6e7cff5f921a 100644 --- a/crates/swc_plugin_proxy/src/comments/plugin_comments_proxy.rs +++ b/crates/swc_plugin_proxy/src/comments/plugin_comments_proxy.rs @@ -44,13 +44,14 @@ impl PluginCommentsProxy { /// comment_buffer as serialized to pass param from guest to the host for /// the fn like add_leading*. #[cfg_attr(not(target_arch = "wasm32"), allow(unused))] - fn allocate_comments_buffer_to_host(&self, value: &T) + fn allocate_comments_buffer_to_host(&self, value: T) where T: rkyv::Serialize>, { #[cfg(target_arch = "wasm32")] { - let serialized = swc_common::plugin::PluginSerializedBytes::try_serialize(value) + let value = swc_common::plugin::VersionedSerializable::new(value); + let serialized = swc_common::plugin::PluginSerializedBytes::try_serialize(&value) .expect("Should able to serialize value"); let (serialized_comment_ptr, serialized_comment_ptr_len) = serialized.as_ptr(); unsafe { @@ -73,7 +74,7 @@ impl PluginCommentsProxy { #[cfg_attr(not(target_arch = "wasm32"), allow(unused))] impl Comments for PluginCommentsProxy { fn add_leading(&self, pos: BytePos, cmt: Comment) { - self.allocate_comments_buffer_to_host(&cmt); + self.allocate_comments_buffer_to_host(cmt); #[cfg(target_arch = "wasm32")] unsafe { __add_leading_comment_proxy(pos.0); @@ -81,7 +82,7 @@ impl Comments for PluginCommentsProxy { } fn add_leading_comments(&self, pos: BytePos, comments: Vec) { - self.allocate_comments_buffer_to_host(&comments); + self.allocate_comments_buffer_to_host(comments); #[cfg(target_arch = "wasm32")] unsafe { __add_leading_comments_proxy(pos.0); @@ -129,7 +130,7 @@ impl Comments for PluginCommentsProxy { } fn add_trailing(&self, pos: BytePos, cmt: Comment) { - self.allocate_comments_buffer_to_host(&cmt); + self.allocate_comments_buffer_to_host(cmt); #[cfg(target_arch = "wasm32")] unsafe { __add_trailing_comment_proxy(pos.0); @@ -137,7 +138,7 @@ impl Comments for PluginCommentsProxy { } fn add_trailing_comments(&self, pos: BytePos, comments: Vec) { - self.allocate_comments_buffer_to_host(&comments); + self.allocate_comments_buffer_to_host(comments); #[cfg(target_arch = "wasm32")] unsafe { __add_trailing_comments_proxy(pos.0); diff --git a/crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs b/crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs index d661742cd63c..87776c5b2fdd 100644 --- a/crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs +++ b/crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs @@ -1,10 +1,11 @@ #[cfg_attr(not(target_arch = "wasm32"), allow(unused))] use swc_common::plugin::{ deserialize_from_ptr, deserialize_from_ptr_into_fallible, PluginSerializedBytes, + VersionedSerializable, }; /// A struct to exchange allocated data between memory spaces. -#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Default)] #[archive_attr(repr(C), derive(bytecheck::CheckBytes))] pub struct AllocatedBytesPtr(pub i32, pub i32); @@ -22,7 +23,7 @@ where F: FnOnce(i32) -> i32, { // Allocate AllocatedBytesPtr to get return value from the host - let allocated_bytes_ptr = AllocatedBytesPtr(0, 0); + let allocated_bytes_ptr = VersionedSerializable::new(AllocatedBytesPtr(0, 0)); let serialized_allocated_bytes_ptr = PluginSerializedBytes::try_serialize(&allocated_bytes_ptr) .expect("Should able to serialize AllocatedBytesPtr"); let (serialized_allocated_bytes_raw_ptr, serialized_allocated_bytes_raw_ptr_size) = @@ -50,6 +51,7 @@ where .expect("Should able to convert ptr length"), ) .expect("Should able to deserialize AllocatedBytesPtr") + .take() }) } @@ -60,7 +62,7 @@ where pub fn read_returned_result_from_host(f: F) -> Option where F: FnOnce(i32) -> i32, - R: rkyv::Archive, + R: rkyv::Archive + std::default::Default, R::Archived: rkyv::Deserialize, { let allocated_returned_value_ptr = read_returned_result_from_host_inner(f); @@ -72,6 +74,7 @@ where allocated_returned_value_ptr.1, ) .expect("Returned value should be serializable") + .take() }) } @@ -85,11 +88,11 @@ where pub fn read_returned_result_from_host_fallible(f: F) -> Option where F: FnOnce(i32) -> i32, - R: rkyv::Archive, + R: rkyv::Archive + std::default::Default, R::Archived: rkyv::Deserialize, { // Allocate AllocatedBytesPtr to get return value from the host - let allocated_bytes_ptr = AllocatedBytesPtr(0, 0); + let allocated_bytes_ptr = VersionedSerializable::new(AllocatedBytesPtr(0, 0)); let serialized_allocated_bytes_ptr = PluginSerializedBytes::try_serialize(&allocated_bytes_ptr) .expect("Should able to serialize AllocatedBytesPtr"); let (serialized_allocated_bytes_raw_ptr, serialized_allocated_bytes_raw_ptr_size) = @@ -112,11 +115,10 @@ where let allocated_returned_value_ptr: AllocatedBytesPtr = unsafe { deserialize_from_ptr( serialized_allocated_bytes_raw_ptr, - serialized_allocated_bytes_raw_ptr_size - .try_into() - .expect("Should able to convert ptr length"), + serialized_allocated_bytes_raw_ptr_size as i32, ) .expect("Should able to deserialize AllocatedBytesPtr") + .take() }; // Using AllocatedBytesPtr's value, reconstruct actual return value @@ -126,5 +128,6 @@ where allocated_returned_value_ptr.1, ) .expect("Returned value should be serializable") + .take() }) } diff --git a/crates/swc_plugin_proxy/src/source_map/plugin_source_map_proxy.rs b/crates/swc_plugin_proxy/src/source_map/plugin_source_map_proxy.rs index ce9661490270..190486e4f012 100644 --- a/crates/swc_plugin_proxy/src/source_map/plugin_source_map_proxy.rs +++ b/crates/swc_plugin_proxy/src/source_map/plugin_source_map_proxy.rs @@ -2,6 +2,7 @@ #![allow(unused_variables)] #[cfg(feature = "plugin-mode")] use swc_common::{ + plugin::VersionedSerializable, source_map::{ DistinctSources, FileLinesResult, MalformedSourceMapPositions, Pos, SpanSnippetError, }, @@ -174,11 +175,15 @@ impl SourceMapper for PluginSourceMapProxy { fn span_to_lines(&self, sp: Span) -> FileLinesResult { #[cfg(target_arch = "wasm32")] + todo!( + "Need to implement way to take Result from read_returned_result_from_host_fallible" + ); + /* return read_returned_result_from_host_fallible(|serialized_ptr| unsafe { __span_to_lines_proxy(sp.lo.0, sp.hi.0, sp.ctxt.as_u32(), serialized_ptr) }) .expect("Host should return FileLinesResult"); - + */ #[cfg(not(target_arch = "wasm32"))] unimplemented!("Sourcemap proxy cannot be called in this context") } @@ -225,8 +230,10 @@ impl SourceMapper for PluginSourceMapProxy { ctxt: swc_common::SyntaxContext::empty(), }; - let serialized = swc_common::plugin::PluginSerializedBytes::try_serialize(&span) - .expect("Should be serializable"); + let serialized = swc_common::plugin::PluginSerializedBytes::try_serialize( + &VersionedSerializable::new(span), + ) + .expect("Should be serializable"); let (ptr, len) = serialized.as_ptr(); let ret = __merge_spans_proxy( diff --git a/crates/swc_plugin_runner/benches/invoke.rs b/crates/swc_plugin_runner/benches/invoke.rs index 5681917701fe..2ab1bf195718 100644 --- a/crates/swc_plugin_runner/benches/invoke.rs +++ b/crates/swc_plugin_runner/benches/invoke.rs @@ -9,7 +9,10 @@ use std::{ use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion}; use once_cell::sync::Lazy; -use swc_common::{plugin::PluginSerializedBytes, FileName, FilePathMapping, SourceMap}; +use swc_common::{ + plugin::{PluginSerializedBytes, VersionedSerializable}, + FileName, FilePathMapping, SourceMap, +}; use swc_ecma_ast::EsVersion; use swc_ecma_parser::parse_file_as_program; use swc_plugin_runner::cache::PluginModuleCache; @@ -56,6 +59,7 @@ fn bench_transform(b: &mut Bencher, plugin_dir: &Path) { ) .unwrap(); + let program = VersionedSerializable::new(program); let program_ser = PluginSerializedBytes::try_serialize(&program).unwrap(); let res = swc_plugin_runner::apply_transform_plugin( @@ -67,8 +71,10 @@ fn bench_transform(b: &mut Bencher, plugin_dir: &Path) { .join("swc_internal_plugin.wasm"), &cache, program_ser, - PluginSerializedBytes::try_serialize(&String::from("{}")).unwrap(), - PluginSerializedBytes::try_serialize(&String::from("{}")).unwrap(), + PluginSerializedBytes::try_serialize(&VersionedSerializable::new(String::from("{}"))) + .unwrap(), + PluginSerializedBytes::try_serialize(&VersionedSerializable::new(String::from("{}"))) + .unwrap(), true, &cm, ) diff --git a/crates/swc_plugin_runner/src/imported_fn/comments.rs b/crates/swc_plugin_runner/src/imported_fn/comments.rs index 97f2c60306bd..8fcc58fdb9c8 100644 --- a/crates/swc_plugin_runner/src/imported_fn/comments.rs +++ b/crates/swc_plugin_runner/src/imported_fn/comments.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use parking_lot::Mutex; use swc_common::{ comments::{Comments, SingleThreadedComments}, - plugin::PluginSerializedBytes, + plugin::{PluginSerializedBytes, VersionedSerializable}, BytePos, }; use swc_plugin_proxy::COMMENTS; @@ -124,7 +124,8 @@ pub fn add_leading_comment_proxy(env: &CommentHostEnvironment, byte_pos: u32) { byte_pos, serialized .deserialize() - .expect("Should be able to deserialize"), + .expect("Should be able to deserialize") + .take(), ); }); } @@ -135,7 +136,8 @@ pub fn add_leading_comments_proxy(env: &CommentHostEnvironment, byte_pos: u32) { byte_pos, serialized .deserialize() - .expect("Should be able to deserialize"), + .expect("Should be able to deserialize") + .take(), ); }); } @@ -160,9 +162,10 @@ pub fn take_leading_comments_proxy( |comments, memory, alloc_guest_memory| { let leading_comments = comments.take_leading(BytePos(byte_pos)); if let Some(leading_comments) = leading_comments { - let serialized_leading_comments_vec_bytes = - PluginSerializedBytes::try_serialize(&leading_comments) - .expect("Should be serializable"); + let serialized_leading_comments_vec_bytes = PluginSerializedBytes::try_serialize( + &VersionedSerializable::new(leading_comments), + ) + .expect("Should be serializable"); allocate_return_values_into_guest( memory, @@ -194,9 +197,10 @@ pub fn get_leading_comments_proxy( |comments, memory, alloc_guest_memory| { let leading_comments = comments.get_leading(BytePos(byte_pos)); if let Some(leading_comments) = leading_comments { - let serialized_leading_comments_vec_bytes = - PluginSerializedBytes::try_serialize(&leading_comments) - .expect("Should be serializable"); + let serialized_leading_comments_vec_bytes = PluginSerializedBytes::try_serialize( + &VersionedSerializable::new(leading_comments), + ) + .expect("Should be serializable"); allocate_return_values_into_guest( memory, @@ -219,7 +223,8 @@ pub fn add_trailing_comment_proxy(env: &CommentHostEnvironment, byte_pos: u32) { byte_pos, serialized .deserialize() - .expect("Should be able to deserialize"), + .expect("Should be able to deserialize") + .take(), ); }); } @@ -230,7 +235,8 @@ pub fn add_trailing_comments_proxy(env: &CommentHostEnvironment, byte_pos: u32) byte_pos, serialized .deserialize() - .expect("Should be able to deserialize"), + .expect("Should be able to deserialize") + .take(), ); }); } @@ -258,9 +264,10 @@ pub fn take_trailing_comments_proxy( |comments, memory, alloc_guest_memory| { let trailing_comments = comments.take_trailing(BytePos(byte_pos)); if let Some(leading_comments) = trailing_comments { - let serialized_leading_comments_vec_bytes = - PluginSerializedBytes::try_serialize(&leading_comments) - .expect("Should be serializable"); + let serialized_leading_comments_vec_bytes = PluginSerializedBytes::try_serialize( + &VersionedSerializable::new(leading_comments), + ) + .expect("Should be serializable"); allocate_return_values_into_guest( memory, @@ -287,9 +294,10 @@ pub fn get_trailing_comments_proxy( |comments, memory, alloc_guest_memory| { let trailing_comments = comments.get_trailing(BytePos(byte_pos)); if let Some(leading_comments) = trailing_comments { - let serialized_leading_comments_vec_bytes = - PluginSerializedBytes::try_serialize(&leading_comments) - .expect("Should be serializable"); + let serialized_leading_comments_vec_bytes = PluginSerializedBytes::try_serialize( + &VersionedSerializable::new(leading_comments), + ) + .expect("Should be serializable"); allocate_return_values_into_guest( memory, diff --git a/crates/swc_plugin_runner/src/imported_fn/handler.rs b/crates/swc_plugin_runner/src/imported_fn/handler.rs index bd9caba4ca3a..9f2d236e1d08 100644 --- a/crates/swc_plugin_runner/src/imported_fn/handler.rs +++ b/crates/swc_plugin_runner/src/imported_fn/handler.rs @@ -12,7 +12,8 @@ pub fn emit_diagnostics(env: &BaseHostEnvironment, bytes_ptr: i32, bytes_ptr_len let diagnostics_bytes = copy_bytes_into_host(memory, bytes_ptr, bytes_ptr_len); let serialized = PluginSerializedBytes::from_slice(&diagnostics_bytes[..]); let diagnostic = PluginSerializedBytes::deserialize::(&serialized) - .expect("Should able to be deserialized into diagnostic"); + .expect("Should able to be deserialized into diagnostic") + .take(); let mut builder = swc_common::errors::DiagnosticBuilder::new_diagnostic(handler, diagnostic); diff --git a/crates/swc_plugin_runner/src/imported_fn/hygiene.rs b/crates/swc_plugin_runner/src/imported_fn/hygiene.rs index c372e1805f1c..1164d485fe31 100644 --- a/crates/swc_plugin_runner/src/imported_fn/hygiene.rs +++ b/crates/swc_plugin_runner/src/imported_fn/hygiene.rs @@ -1,4 +1,8 @@ -use swc_common::{hygiene::MutableMarkContext, plugin::PluginSerializedBytes, Mark, SyntaxContext}; +use swc_common::{ + hygiene::MutableMarkContext, + plugin::{PluginSerializedBytes, VersionedSerializable}, + Mark, SyntaxContext, +}; use crate::{host_environment::BaseHostEnvironment, memory_interop::write_into_memory_view}; @@ -38,12 +42,13 @@ pub fn mark_is_descendant_of_proxy( let return_value = self_mark.is_descendant_of(ancestor); if let Some(memory) = env.memory_ref() { - let serialized_bytes = PluginSerializedBytes::try_serialize(&MutableMarkContext( + let context = VersionedSerializable::new(MutableMarkContext( self_mark.as_u32(), 0, return_value as u32, - )) - .expect("Should be serializable"); + )); + let serialized_bytes = + PluginSerializedBytes::try_serialize(&context).expect("Should be serializable"); write_into_memory_view(memory, &serialized_bytes, |_| allocated_ptr); } @@ -56,12 +61,10 @@ pub fn mark_least_ancestor_proxy(env: &BaseHostEnvironment, a: u32, b: u32, allo let return_value = Mark::least_ancestor(a, b).as_u32(); if let Some(memory) = env.memory_ref() { - let serialized_bytes = PluginSerializedBytes::try_serialize(&MutableMarkContext( - a.as_u32(), - b.as_u32(), - return_value, - )) - .expect("Should be serializable"); + let context = + VersionedSerializable::new(MutableMarkContext(a.as_u32(), b.as_u32(), return_value)); + let serialized_bytes = + PluginSerializedBytes::try_serialize(&context).expect("Should be serializable"); write_into_memory_view(memory, &serialized_bytes, |_| allocated_ptr); } @@ -83,12 +86,13 @@ pub fn syntax_context_remove_mark_proxy( let return_value = self_mark.remove_mark(); if let Some(memory) = env.memory_ref() { - let serialized_bytes = PluginSerializedBytes::try_serialize(&MutableMarkContext( + let context = VersionedSerializable::new(MutableMarkContext( self_mark.as_u32(), 0, return_value.as_u32(), - )) - .expect("Should be serializable"); + )); + let serialized_bytes = + PluginSerializedBytes::try_serialize(&context).expect("Should be serializable"); write_into_memory_view(memory, &serialized_bytes, |_| allocated_ptr); } diff --git a/crates/swc_plugin_runner/src/imported_fn/source_map.rs b/crates/swc_plugin_runner/src/imported_fn/source_map.rs index 07db095977aa..72d8e336c9d4 100644 --- a/crates/swc_plugin_runner/src/imported_fn/source_map.rs +++ b/crates/swc_plugin_runner/src/imported_fn/source_map.rs @@ -1,7 +1,10 @@ use std::sync::Arc; use parking_lot::Mutex; -use swc_common::{plugin::PluginSerializedBytes, BytePos, SourceMap, Span, SyntaxContext}; +use swc_common::{ + plugin::{PluginSerializedBytes, VersionedSerializable}, + BytePos, SourceMap, Span, SyntaxContext, +}; use wasmer::{LazyInit, Memory, NativeFunc}; use crate::memory_interop::{allocate_return_values_into_guest, write_into_memory_view}; @@ -41,7 +44,8 @@ pub fn lookup_char_pos_proxy( allocated_ret_ptr: i32, ) -> i32 { if let Some(memory) = env.memory_ref() { - let ret = (env.source_map.lock()).lookup_char_pos(BytePos(byte_pos)); + let ret = + VersionedSerializable::new((env.source_map.lock()).lookup_char_pos(BytePos(byte_pos))); let serialized_loc_bytes = PluginSerializedBytes::try_serialize(&ret).expect("Should be serializable"); @@ -92,7 +96,8 @@ pub fn merge_spans_proxy( let ret = (env.source_map.lock()).merge_spans(sp_lhs, sp_rhs); if let Some(span) = ret { let serialized_bytes = - PluginSerializedBytes::try_serialize(&span).expect("Should be serializable"); + PluginSerializedBytes::try_serialize(&VersionedSerializable::new(span)) + .expect("Should be serializable"); write_into_memory_view(memory, &serialized_bytes, |_| allocated_ptr); 1 } else { @@ -116,7 +121,7 @@ pub fn span_to_lines_proxy( hi: BytePos(span_hi), ctxt: SyntaxContext::from_u32(span_ctxt), }; - let ret = (env.source_map.lock()).span_to_lines(span); + let ret = VersionedSerializable::new((env.source_map.lock()).span_to_lines(span)); let serialized_loc_bytes = PluginSerializedBytes::try_serialize(&ret).expect("Should be serializable"); @@ -144,7 +149,7 @@ pub fn lookup_byte_offset_proxy( ) -> i32 { if let Some(memory) = env.memory_ref() { let byte_pos = BytePos(byte_pos); - let ret = (env.source_map.lock()).lookup_byte_offset(byte_pos); + let ret = VersionedSerializable::new((env.source_map.lock()).lookup_byte_offset(byte_pos)); let serialized_loc_bytes = PluginSerializedBytes::try_serialize(&ret).expect("Should be serializable"); @@ -178,7 +183,7 @@ pub fn span_to_string_proxy( hi: BytePos(span_hi), ctxt: SyntaxContext::from_u32(span_ctxt), }; - let ret = (env.source_map.lock()).span_to_string(span); + let ret = VersionedSerializable::new((env.source_map.lock()).span_to_string(span)); let serialized_loc_bytes = PluginSerializedBytes::try_serialize(&ret).expect("Should be serializable"); @@ -211,7 +216,7 @@ pub fn span_to_filename_proxy( hi: BytePos(span_hi), ctxt: SyntaxContext::from_u32(span_ctxt), }; - let ret = (env.source_map.lock()).span_to_filename(span); + let ret = VersionedSerializable::new((env.source_map.lock()).span_to_filename(span)); let serialized_loc_bytes = PluginSerializedBytes::try_serialize(&ret).expect("Should be serializable"); diff --git a/crates/swc_plugin_runner/src/memory_interop.rs b/crates/swc_plugin_runner/src/memory_interop.rs index 3afcfbcffe95..5c79dfff4241 100644 --- a/crates/swc_plugin_runner/src/memory_interop.rs +++ b/crates/swc_plugin_runner/src/memory_interop.rs @@ -1,4 +1,4 @@ -use swc_common::plugin::PluginSerializedBytes; +use swc_common::plugin::{PluginSerializedBytes, VersionedSerializable}; use swc_plugin_proxy::AllocatedBytesPtr; use wasmer::{Array, Memory, NativeFunc, WasmPtr}; @@ -92,10 +92,11 @@ pub fn allocate_return_values_into_guest( .expect("Should able to allocate memory in the plugin") }); + let allocated_bytes = + VersionedSerializable::new(AllocatedBytesPtr(allocated_ptr, allocated_ptr_len)); // Retuning (allocated_ptr, len) into caller (plugin) let comment_ptr_serialized = - PluginSerializedBytes::try_serialize(&AllocatedBytesPtr(allocated_ptr, allocated_ptr_len)) - .expect("Should be serializable"); + PluginSerializedBytes::try_serialize(&allocated_bytes).expect("Should be serializable"); write_into_memory_view(memory, &comment_ptr_serialized, |_| allocated_ret_ptr); } diff --git a/crates/swc_plugin_runner/src/transform_executor.rs b/crates/swc_plugin_runner/src/transform_executor.rs index 9b15df6f7ac7..f51f0fc92ccb 100644 --- a/crates/swc_plugin_runner/src/transform_executor.rs +++ b/crates/swc_plugin_runner/src/transform_executor.rs @@ -86,7 +86,7 @@ impl TransformExecutor { if returned_ptr_result == 0 { Ok(ret) } else { - let err: PluginError = ret.deserialize()?; + let err: PluginError = ret.deserialize()?.take(); match err { PluginError::SizeInteropFailure(msg) => Err(anyhow!( "Failed to convert pointer size to calculate: {}", diff --git a/crates/swc_plugin_runner/tests/integration.rs b/crates/swc_plugin_runner/tests/integration.rs index e9c498ff391d..8382670835a1 100644 --- a/crates/swc_plugin_runner/tests/integration.rs +++ b/crates/swc_plugin_runner/tests/integration.rs @@ -5,7 +5,12 @@ use std::{ }; use anyhow::{anyhow, Error}; -use swc_common::{errors::HANDLER, plugin::PluginSerializedBytes, sync::Lazy, FileName}; +use swc_common::{ + errors::HANDLER, + plugin::{PluginSerializedBytes, VersionedSerializable}, + sync::Lazy, + FileName, +}; use swc_ecma_ast::{CallExpr, Callee, EsVersion, Expr, Lit, MemberExpr, Program, Str}; use swc_ecma_parser::{parse_file_as_program, EsConfig, Syntax}; use swc_ecma_visit::{Visit, VisitWith}; @@ -80,12 +85,14 @@ fn internal() -> Result<(), Error> { ) .unwrap(); - let program = PluginSerializedBytes::try_serialize(&program).expect("Should serializable"); + let program = PluginSerializedBytes::try_serialize(&VersionedSerializable::new(program)) + .expect("Should serializable"); let config = - PluginSerializedBytes::try_serialize(&"{}".to_string()).expect("Should serializable"); - let context = PluginSerializedBytes::try_serialize( - &"{sourceFileName: 'single_plugin_test'}".to_string(), - ) + PluginSerializedBytes::try_serialize(&VersionedSerializable::new("{}".to_string())) + .expect("Should serializable"); + let context = PluginSerializedBytes::try_serialize(&VersionedSerializable::new( + "{sourceFileName: 'single_plugin_test'}".to_string(), + )) .expect("Should serializable"); let cache: Lazy = Lazy::new(PluginModuleCache::new); @@ -104,7 +111,8 @@ fn internal() -> Result<(), Error> { let program: Program = program_bytes .deserialize() - .expect("Should able to deserialize"); + .expect("Should able to deserialize") + .take(); let mut visitor = TestVisitor { plugin_transform_found: false, }; @@ -132,12 +140,14 @@ fn internal() -> Result<(), Error> { ) .unwrap(); - let program = PluginSerializedBytes::try_serialize(&program).expect("Should serializable"); + let program = PluginSerializedBytes::try_serialize(&VersionedSerializable::new(program)) + .expect("Should serializable"); let config = - PluginSerializedBytes::try_serialize(&"{}".to_string()).expect("Should serializable"); - let context = PluginSerializedBytes::try_serialize( - &"{sourceFileName: 'single_plugin_handler_test'}".to_string(), - ) + PluginSerializedBytes::try_serialize(&VersionedSerializable::new("{}".to_string())) + .expect("Should serializable"); + let context = PluginSerializedBytes::try_serialize(&VersionedSerializable::new( + "{sourceFileName: 'single_plugin_handler_test'}".to_string(), + )) .expect("Should serializable"); let cache: Lazy = Lazy::new(PluginModuleCache::new); @@ -176,7 +186,8 @@ fn internal() -> Result<(), Error> { .unwrap(); let mut serialized_program = - PluginSerializedBytes::try_serialize(&program).expect("Should serializable"); + PluginSerializedBytes::try_serialize(&VersionedSerializable::new(program)) + .expect("Should serializable"); let cache: Lazy = Lazy::new(PluginModuleCache::new); serialized_program = swc_plugin_runner::apply_transform_plugin( @@ -184,10 +195,11 @@ fn internal() -> Result<(), Error> { &path, &cache, serialized_program, - PluginSerializedBytes::try_serialize(&"{}".to_string()).expect("Should serializable"), - PluginSerializedBytes::try_serialize( - &"{sourceFileName: 'multiple_plugin_test'}".to_string(), - ) + PluginSerializedBytes::try_serialize(&VersionedSerializable::new("{}".to_string())) + .expect("Should serializable"), + PluginSerializedBytes::try_serialize(&VersionedSerializable::new( + "{sourceFileName: 'multiple_plugin_test'}".to_string(), + )) .expect("Should serializable"), false, &cm, @@ -200,10 +212,11 @@ fn internal() -> Result<(), Error> { &path, &cache, serialized_program, - PluginSerializedBytes::try_serialize(&"{}".to_string()).expect("Should serializable"), - PluginSerializedBytes::try_serialize( - &"{sourceFileName: 'multiple_plugin_test2'}".to_string(), - ) + PluginSerializedBytes::try_serialize(&VersionedSerializable::new("{}".to_string())) + .expect("Should serializable"), + PluginSerializedBytes::try_serialize(&VersionedSerializable::new( + "{sourceFileName: 'multiple_plugin_test2'}".to_string(), + )) .expect("Should serializable"), false, &cm, @@ -212,7 +225,8 @@ fn internal() -> Result<(), Error> { let program: Program = serialized_program .deserialize() - .expect("Should able to deserialize"); + .expect("Should able to deserialize") + .take(); let mut visitor = TestVisitor { plugin_transform_found: false, }; From 4a7040feacc9c95ea053083abcd0972c081f0136 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Wed, 6 Jul 2022 14:48:34 -0700 Subject: [PATCH 5/7] build(cargo): update lockfile --- .../rust-plugins/swc_internal_plugin/Cargo.lock | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/rust-plugins/swc_internal_plugin/Cargo.lock b/tests/rust-plugins/swc_internal_plugin/Cargo.lock index 3bf1ac63a518..cf12fb8a5de8 100644 --- a/tests/rust-plugins/swc_internal_plugin/Cargo.lock +++ b/tests/rust-plugins/swc_internal_plugin/Cargo.lock @@ -722,7 +722,7 @@ dependencies = [ [[package]] name = "swc_common" -version = "0.20.2" +version = "0.21.0" dependencies = [ "ahash", "anyhow", @@ -750,7 +750,7 @@ dependencies = [ [[package]] name = "swc_ecma_ast" -version = "0.81.0" +version = "0.82.0" dependencies = [ "bitflags", "bytecheck", @@ -767,7 +767,7 @@ dependencies = [ [[package]] name = "swc_ecma_parser" -version = "0.108.1" +version = "0.109.1" dependencies = [ "either", "enum_kind", @@ -784,7 +784,7 @@ dependencies = [ [[package]] name = "swc_ecma_utils" -version = "0.90.0" +version = "0.91.0" dependencies = [ "indexmap", "once_cell", @@ -798,7 +798,7 @@ dependencies = [ [[package]] name = "swc_ecma_visit" -version = "0.67.1" +version = "0.68.0" dependencies = [ "num-bigint", "swc_atoms", @@ -810,7 +810,7 @@ dependencies = [ [[package]] name = "swc_ecmascript" -version = "0.175.0" +version = "0.176.0" dependencies = [ "swc_ecma_ast", "swc_ecma_parser", @@ -848,7 +848,7 @@ dependencies = [ [[package]] name = "swc_plugin" -version = "0.71.0" +version = "0.72.0" dependencies = [ "swc_atoms", "swc_common", @@ -868,7 +868,7 @@ dependencies = [ [[package]] name = "swc_plugin_proxy" -version = "0.6.0" +version = "0.7.0" dependencies = [ "better_scoped_tls", "bytecheck", From 42df1494cc82fdf42a856aa49bcbf849b1f58b3e Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Wed, 6 Jul 2022 22:29:29 -0700 Subject: [PATCH 6/7] refactor(swc/common): use default for string --- crates/swc_common/src/comments.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_common/src/comments.rs b/crates/swc_common/src/comments.rs index d6ab67e847f1..9fcdb6c684bd 100644 --- a/crates/swc_common/src/comments.rs +++ b/crates/swc_common/src/comments.rs @@ -553,7 +553,7 @@ impl Default for Comment { Comment { kind: CommentKind::Line, span: DUMMY_SP, - text: "".into(), + text: Default::default(), } } } From 509e766ce374c07c10afb2e5dd1f0d91b97eddaa Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Wed, 6 Jul 2022 23:28:53 -0700 Subject: [PATCH 7/7] refactor(swc/common): use into_inner instead of take --- crates/swc/src/plugin.rs | 4 +-- crates/swc_common/src/comments.rs | 10 ------ crates/swc_common/src/errors/diagnostic.rs | 13 -------- crates/swc_common/src/plugin.rs | 13 ++------ crates/swc_common/src/syntax_pos.rs | 32 +++---------------- crates/swc_common/src/syntax_pos/hygiene.rs | 7 ++-- crates/swc_ecma_ast/src/module.rs | 6 ---- crates/swc_plugin_macro/src/lib.rs | 6 ++-- .../read_returned_result_from_host.rs | 14 ++++---- .../src/source_map/plugin_source_map_proxy.rs | 6 +--- .../src/imported_fn/comments.rs | 8 ++--- .../src/imported_fn/handler.rs | 2 +- .../src/transform_executor.rs | 2 +- crates/swc_plugin_runner/tests/integration.rs | 4 +-- 14 files changed, 30 insertions(+), 97 deletions(-) diff --git a/crates/swc/src/plugin.rs b/crates/swc/src/plugin.rs index e7d112f6dfde..1ee0d40f0bd4 100644 --- a/crates/swc/src/plugin.rs +++ b/crates/swc/src/plugin.rs @@ -164,7 +164,7 @@ impl RustPlugins { // Plugin transformation is done. Deserialize transformed bytes back // into Program - serialized.deserialize().map(|mut v| v.take()) + serialized.deserialize().map(|v| v.into_inner()) }, ) } @@ -221,7 +221,7 @@ impl RustPlugins { } } - serialized.deserialize().map(|mut v| v.take()) + serialized.deserialize().map(|v| v.into_inner()) }, ) } diff --git a/crates/swc_common/src/comments.rs b/crates/swc_common/src/comments.rs index 9fcdb6c684bd..d50d43c961eb 100644 --- a/crates/swc_common/src/comments.rs +++ b/crates/swc_common/src/comments.rs @@ -548,16 +548,6 @@ pub struct Comment { pub text: String, } -impl Default for Comment { - fn default() -> Self { - Comment { - kind: CommentKind::Line, - span: DUMMY_SP, - text: Default::default(), - } - } -} - impl Spanned for Comment { fn span(&self) -> Span { self.span diff --git a/crates/swc_common/src/errors/diagnostic.rs b/crates/swc_common/src/errors/diagnostic.rs index 6f436eb0ff9c..0a0766e91e3b 100644 --- a/crates/swc_common/src/errors/diagnostic.rs +++ b/crates/swc_common/src/errors/diagnostic.rs @@ -36,19 +36,6 @@ pub struct Diagnostic { pub suggestions: Vec, } -impl Default for Diagnostic { - 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", diff --git a/crates/swc_common/src/plugin.rs b/crates/swc_common/src/plugin.rs index 28caa4a63b0b..edc978abc4b0 100644 --- a/crates/swc_common/src/plugin.rs +++ b/crates/swc_common/src/plugin.rs @@ -41,12 +41,6 @@ pub enum PluginError { Serialize(String), } -impl Default for PluginError { - 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 @@ -200,10 +194,7 @@ impl VersionedSerializable { &self.0 .1 } - pub fn take(&mut self) -> T - where - T: Default, - { - mem::take(&mut self.0 .1) + pub fn into_inner(self) -> T { + self.0 .1 } } diff --git a/crates/swc_common/src/syntax_pos.rs b/crates/swc_common/src/syntax_pos.rs index 682aeca1497d..fabc27ef6d66 100644 --- a/crates/swc_common/src/syntax_pos.rs +++ b/crates/swc_common/src/syntax_pos.rs @@ -130,12 +130,6 @@ pub enum FileName { Custom(String), } -impl Default for FileName { - 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 @@ -811,7 +805,7 @@ impl Sub for NonNarrowChar { derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] #[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))] -#[derive(Clone, Default)] +#[derive(Clone)] 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, @@ -1004,9 +998,7 @@ 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, Default, -)] +#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)] #[serde(transparent)] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] #[cfg_attr( @@ -1040,7 +1032,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, Default)] +#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] pub struct CharPos(pub usize); // FIXME: Lots of boilerplate in these impls, but so far my attempts to fix @@ -1136,7 +1128,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, Default)] +#[derive(Debug, Clone)] pub struct Loc { /// Information about the original source pub file: Lrc, @@ -1177,15 +1169,6 @@ pub struct SourceFileAndBytePos { pub pos: BytePos, } -impl Default for SourceFileAndBytePos { - fn default() -> Self { - SourceFileAndBytePos { - sf: Lrc::new(SourceFile::default()), - pos: Default::default(), - } - } -} - #[derive(Copy, Clone, Debug, PartialEq, Eq)] #[cfg_attr( feature = "rkyv", @@ -1210,7 +1193,6 @@ pub struct LineCol { pub col: u32, } -#[derive(Default)] #[cfg_attr( feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) @@ -1242,12 +1224,6 @@ pub enum SpanLinesError { DistinctSources(DistinctSources), } -impl Default for SpanLinesError { - fn default() -> Self { - SpanLinesError::IllFormedSpan(DUMMY_SP) - } -} - #[derive(Clone, PartialEq, Eq, Debug)] pub enum SpanSnippetError { DummyBytePos, diff --git a/crates/swc_common/src/syntax_pos/hygiene.rs b/crates/swc_common/src/syntax_pos/hygiene.rs index 85bfd650b83e..0cd7e8864ec0 100644 --- a/crates/swc_common/src/syntax_pos/hygiene.rs +++ b/crates/swc_common/src/syntax_pos/hygiene.rs @@ -67,7 +67,6 @@ struct MarkData { is_builtin: bool, } -#[derive(Default)] #[cfg_attr( feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) @@ -199,7 +198,7 @@ impl Mark { len.try_into().expect("Should able to convert ptr length"), ) .expect("Should able to deserialize") - .take() + .into_inner() }; self = Mark::from_u32(context.0); @@ -238,7 +237,7 @@ impl Mark { len.try_into().expect("Should able to convert ptr length"), ) .expect("Should able to deserialize") - .take() + .into_inner() }; a = Mark::from_u32(context.0); b = Mark::from_u32(context.1); @@ -403,7 +402,7 @@ impl SyntaxContext { len.try_into().expect("Should able to convert ptr length"), ) .expect("Should able to deserialize") - .take() + .into_inner() }; *self = SyntaxContext(context.0); diff --git a/crates/swc_ecma_ast/src/module.rs b/crates/swc_ecma_ast/src/module.rs index 838a33bf3350..4bc570f110a4 100644 --- a/crates/swc_ecma_ast/src/module.rs +++ b/crates/swc_ecma_ast/src/module.rs @@ -14,12 +14,6 @@ pub enum Program { Script(Script), } -impl Default for Program { - fn default() -> Self { - Program::Module(Module::dummy()) - } -} - #[ast_node("Module")] #[derive(Eq, Hash, EqIgnoreSpan)] pub struct Module { diff --git a/crates/swc_plugin_macro/src/lib.rs b/crates/swc_plugin_macro/src/lib.rs index 4fc9502c3863..5118dab97a97 100644 --- a/crates/swc_plugin_macro/src/lib.rs +++ b/crates/swc_plugin_macro/src/lib.rs @@ -62,14 +62,14 @@ fn handle_func(func: ItemFn) -> TokenStream { pub fn #process_impl_ident(ast_ptr: *const u8, ast_ptr_len: i32, config_str_ptr: *const u8, config_str_ptr_len: i32, context_str_ptr: *const u8, context_str_ptr_len: i32, should_enable_comments_proxy: i32) -> i32 { // Reconstruct `Program` & config string from serialized program // Host (SWC) should allocate memory, copy bytes and pass ptr to plugin. - let program = unsafe { swc_plugin::deserialize_from_ptr(ast_ptr, ast_ptr_len).map(|mut v| v.take()) }; + let program = unsafe { swc_plugin::deserialize_from_ptr(ast_ptr, ast_ptr_len).map(|v| v.into_inner()) }; if program.is_err() { let err = swc_plugin::PluginError::Deserialize("Failed to deserialize program received from host".to_string()); return construct_error_ptr(err); } let program: Program = program.expect("Should be a program"); - let config = unsafe { swc_plugin::deserialize_from_ptr(config_str_ptr, config_str_ptr_len).map(|mut v| v.take()) }; + let config = unsafe { swc_plugin::deserialize_from_ptr(config_str_ptr, config_str_ptr_len).map(|v| v.into_inner()) }; if config.is_err() { let err = swc_plugin::PluginError::Deserialize( "Failed to deserialize config string received from host".to_string() @@ -78,7 +78,7 @@ fn handle_func(func: ItemFn) -> TokenStream { } let config: String = config.expect("Should be a string"); - let context = unsafe { swc_plugin::deserialize_from_ptr(context_str_ptr, context_str_ptr_len).map(|mut v| v.take()) }; + let context = unsafe { swc_plugin::deserialize_from_ptr(context_str_ptr, context_str_ptr_len).map(|v| v.into_inner()) }; if context.is_err() { let err = swc_plugin::PluginError::Deserialize("Failed to deserialize context string received from host".to_string()); return construct_error_ptr(err); diff --git a/crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs b/crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs index 87776c5b2fdd..79f1827c6461 100644 --- a/crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs +++ b/crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs @@ -5,7 +5,7 @@ use swc_common::plugin::{ }; /// A struct to exchange allocated data between memory spaces. -#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Default)] +#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] #[archive_attr(repr(C), derive(bytecheck::CheckBytes))] pub struct AllocatedBytesPtr(pub i32, pub i32); @@ -51,7 +51,7 @@ where .expect("Should able to convert ptr length"), ) .expect("Should able to deserialize AllocatedBytesPtr") - .take() + .into_inner() }) } @@ -62,7 +62,7 @@ where pub fn read_returned_result_from_host(f: F) -> Option where F: FnOnce(i32) -> i32, - R: rkyv::Archive + std::default::Default, + R: rkyv::Archive, R::Archived: rkyv::Deserialize, { let allocated_returned_value_ptr = read_returned_result_from_host_inner(f); @@ -74,7 +74,7 @@ where allocated_returned_value_ptr.1, ) .expect("Returned value should be serializable") - .take() + .into_inner() }) } @@ -88,7 +88,7 @@ where pub fn read_returned_result_from_host_fallible(f: F) -> Option where F: FnOnce(i32) -> i32, - R: rkyv::Archive + std::default::Default, + R: rkyv::Archive, R::Archived: rkyv::Deserialize, { // Allocate AllocatedBytesPtr to get return value from the host @@ -118,7 +118,7 @@ where serialized_allocated_bytes_raw_ptr_size as i32, ) .expect("Should able to deserialize AllocatedBytesPtr") - .take() + .into_inner() }; // Using AllocatedBytesPtr's value, reconstruct actual return value @@ -128,6 +128,6 @@ where allocated_returned_value_ptr.1, ) .expect("Returned value should be serializable") - .take() + .into_inner() }) } diff --git a/crates/swc_plugin_proxy/src/source_map/plugin_source_map_proxy.rs b/crates/swc_plugin_proxy/src/source_map/plugin_source_map_proxy.rs index 190486e4f012..0f15ce9ae065 100644 --- a/crates/swc_plugin_proxy/src/source_map/plugin_source_map_proxy.rs +++ b/crates/swc_plugin_proxy/src/source_map/plugin_source_map_proxy.rs @@ -175,15 +175,11 @@ impl SourceMapper for PluginSourceMapProxy { fn span_to_lines(&self, sp: Span) -> FileLinesResult { #[cfg(target_arch = "wasm32")] - todo!( - "Need to implement way to take Result from read_returned_result_from_host_fallible" - ); - /* return read_returned_result_from_host_fallible(|serialized_ptr| unsafe { __span_to_lines_proxy(sp.lo.0, sp.hi.0, sp.ctxt.as_u32(), serialized_ptr) }) .expect("Host should return FileLinesResult"); - */ + #[cfg(not(target_arch = "wasm32"))] unimplemented!("Sourcemap proxy cannot be called in this context") } diff --git a/crates/swc_plugin_runner/src/imported_fn/comments.rs b/crates/swc_plugin_runner/src/imported_fn/comments.rs index 8fcc58fdb9c8..b2dd534f1446 100644 --- a/crates/swc_plugin_runner/src/imported_fn/comments.rs +++ b/crates/swc_plugin_runner/src/imported_fn/comments.rs @@ -125,7 +125,7 @@ pub fn add_leading_comment_proxy(env: &CommentHostEnvironment, byte_pos: u32) { serialized .deserialize() .expect("Should be able to deserialize") - .take(), + .into_inner(), ); }); } @@ -137,7 +137,7 @@ pub fn add_leading_comments_proxy(env: &CommentHostEnvironment, byte_pos: u32) { serialized .deserialize() .expect("Should be able to deserialize") - .take(), + .into_inner(), ); }); } @@ -224,7 +224,7 @@ pub fn add_trailing_comment_proxy(env: &CommentHostEnvironment, byte_pos: u32) { serialized .deserialize() .expect("Should be able to deserialize") - .take(), + .into_inner(), ); }); } @@ -236,7 +236,7 @@ pub fn add_trailing_comments_proxy(env: &CommentHostEnvironment, byte_pos: u32) serialized .deserialize() .expect("Should be able to deserialize") - .take(), + .into_inner(), ); }); } diff --git a/crates/swc_plugin_runner/src/imported_fn/handler.rs b/crates/swc_plugin_runner/src/imported_fn/handler.rs index 9f2d236e1d08..7b64cf78d50a 100644 --- a/crates/swc_plugin_runner/src/imported_fn/handler.rs +++ b/crates/swc_plugin_runner/src/imported_fn/handler.rs @@ -13,7 +13,7 @@ pub fn emit_diagnostics(env: &BaseHostEnvironment, bytes_ptr: i32, bytes_ptr_len let serialized = PluginSerializedBytes::from_slice(&diagnostics_bytes[..]); let diagnostic = PluginSerializedBytes::deserialize::(&serialized) .expect("Should able to be deserialized into diagnostic") - .take(); + .into_inner(); let mut builder = swc_common::errors::DiagnosticBuilder::new_diagnostic(handler, diagnostic); diff --git a/crates/swc_plugin_runner/src/transform_executor.rs b/crates/swc_plugin_runner/src/transform_executor.rs index f51f0fc92ccb..9810d7a38eb2 100644 --- a/crates/swc_plugin_runner/src/transform_executor.rs +++ b/crates/swc_plugin_runner/src/transform_executor.rs @@ -86,7 +86,7 @@ impl TransformExecutor { if returned_ptr_result == 0 { Ok(ret) } else { - let err: PluginError = ret.deserialize()?.take(); + let err: PluginError = ret.deserialize()?.into_inner(); match err { PluginError::SizeInteropFailure(msg) => Err(anyhow!( "Failed to convert pointer size to calculate: {}", diff --git a/crates/swc_plugin_runner/tests/integration.rs b/crates/swc_plugin_runner/tests/integration.rs index 8382670835a1..1dee19c2bfc8 100644 --- a/crates/swc_plugin_runner/tests/integration.rs +++ b/crates/swc_plugin_runner/tests/integration.rs @@ -112,7 +112,7 @@ fn internal() -> Result<(), Error> { let program: Program = program_bytes .deserialize() .expect("Should able to deserialize") - .take(); + .into_inner(); let mut visitor = TestVisitor { plugin_transform_found: false, }; @@ -226,7 +226,7 @@ fn internal() -> Result<(), Error> { let program: Program = serialized_program .deserialize() .expect("Should able to deserialize") - .take(); + .into_inner(); let mut visitor = TestVisitor { plugin_transform_found: false, };