From 2ba8b39fa0f2ff7e553d74a4c5ef0f04660a8c40 Mon Sep 17 00:00:00 2001 From: OJ Kwon <1210596+kwonoj@users.noreply.github.com> Date: Tue, 5 Jul 2022 21:42:00 -0700 Subject: [PATCH] refactor(common): Bikeshed plugin apis (#5120) --- crates/swc/src/plugin.rs | 20 +- crates/swc_common/src/plugin.rs | 175 +++++++++--------- crates/swc_common/src/syntax_pos/hygiene.rs | 32 ++-- crates/swc_plugin/src/handler.rs | 13 +- crates/swc_plugin/src/lib.rs | 2 +- crates/swc_plugin_macro/src/lib.rs | 27 +-- .../src/comments/plugin_comments_proxy.rs | 9 +- .../read_returned_result_from_host.rs | 30 +-- .../src/source_map/plugin_source_map_proxy.rs | 8 +- crates/swc_plugin_runner/benches/invoke.rs | 8 +- .../src/imported_fn/comments.rs | 52 ++++-- .../src/imported_fn/handler.rs | 6 +- .../src/imported_fn/hygiene.rs | 15 +- .../src/imported_fn/source_map.rs | 20 +- crates/swc_plugin_runner/src/lib.rs | 10 +- .../swc_plugin_runner/src/memory_interop.rs | 15 +- .../src/transform_executor.rs | 22 ++- crates/swc_plugin_runner/tests/integration.rs | 54 +++--- .../swc_internal_plugin/Cargo.lock | 30 +-- 19 files changed, 290 insertions(+), 258 deletions(-) diff --git a/crates/swc/src/plugin.rs b/crates/swc/src/plugin.rs index bb001bd4331f..b5d90245a7e4 100644 --- a/crates/swc/src/plugin.rs +++ b/crates/swc/src/plugin.rs @@ -80,7 +80,7 @@ impl RustPlugins { use std::{path::PathBuf, sync::Arc}; use anyhow::Context; - use swc_common::{plugin::Serialized, FileName}; + use swc_common::{plugin::PluginSerializedBytes, FileName}; use swc_ecma_loader::resolve::Resolve; // swc_plugin_macro will not inject proxy to the comments if comments is empty @@ -92,7 +92,7 @@ impl RustPlugins { inner: self.comments.clone(), }, || { - let mut serialized = Serialized::serialize(&n)?; + let mut serialized = PluginSerializedBytes::try_serialize(&n)?; // Run plugin transformation against current program. // We do not serialize / deserialize between each plugin execution but @@ -111,11 +111,11 @@ impl RustPlugins { let config_json = serde_json::to_string(&p.1) .context("Failed to serialize plugin config as json") - .and_then(|value| Serialized::serialize(&value))?; + .and_then(|value| PluginSerializedBytes::try_serialize(&value))?; let context_json = serde_json::to_string(&self.plugin_context) .context("Failed to serialize plugin context as json") - .and_then(|value| Serialized::serialize(&value))?; + .and_then(|value| PluginSerializedBytes::try_serialize(&value))?; let resolved_path = self .resolver @@ -152,7 +152,7 @@ impl RustPlugins { // Plugin transformation is done. Deserialize transformed bytes back // into Program - Serialized::deserialize(&serialized) + serialized.deserialize() }, ) } @@ -162,7 +162,7 @@ impl RustPlugins { use std::{path::PathBuf, sync::Arc}; use anyhow::Context; - use swc_common::{plugin::Serialized, FileName}; + use swc_common::{plugin::PluginSerializedBytes, FileName}; use swc_ecma_loader::resolve::Resolve; let should_enable_comments_proxy = self.comments.is_some(); @@ -172,17 +172,17 @@ impl RustPlugins { inner: self.comments.clone(), }, || { - let mut serialized = Serialized::serialize(&n)?; + let mut serialized = PluginSerializedBytes::try_serialize(&n)?; 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| Serialized::serialize(&value))?; + .and_then(|value| PluginSerializedBytes::try_serialize(&value))?; let context_json = serde_json::to_string(&self.plugin_context) .context("Failed to serialize plugin context as json") - .and_then(|value| Serialized::serialize(&value))?; + .and_then(|value| PluginSerializedBytes::try_serialize(&value))?; serialized = swc_plugin_runner::apply_transform_plugin( &p.0, @@ -197,7 +197,7 @@ impl RustPlugins { } } - Serialized::deserialize(&serialized) + serialized.deserialize() }, ) } diff --git a/crates/swc_common/src/plugin.rs b/crates/swc_common/src/plugin.rs index ff55d2c706ea..cced6c938c42 100644 --- a/crates/swc_common/src/plugin.rs +++ b/crates/swc_common/src/plugin.rs @@ -41,48 +41,38 @@ pub enum PluginError { Serialize(String), } -/// Wraps internal representation of serialized data. Consumers should not -/// rely on specific details of byte format struct contains: it is -/// strictly implementation detail which can change anytime. -pub struct Serialized { - field: rkyv::AlignedVec, +/// 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 +/// change anytime. +pub struct PluginSerializedBytes { + pub(crate) field: rkyv::AlignedVec, } #[cfg(feature = "plugin-base")] -impl Serialized { - pub fn new_for_plugin(bytes: &[u8], len: i32) -> Serialized { - let mut vec = rkyv::AlignedVec::with_capacity( - len.try_into() - .expect("Cannot determine size of the serialized bytes"), - ); - vec.extend_from_slice(bytes); - Serialized { field: vec } +impl PluginSerializedBytes { + /** + * Constructs an instance from already serialized byte + * slices. + */ + pub fn from_slice(bytes: &[u8]) -> PluginSerializedBytes { + let mut field = rkyv::AlignedVec::new(); + field.extend_from_slice(bytes); + PluginSerializedBytes { field } } - pub fn from(vec: rkyv::AlignedVec) -> Serialized { - Serialized { field: vec } - } - - /// Not an actual trait Into impl: simple wrapper to deserialize:expect() - pub fn into(self) -> T - where - T: rkyv::Archive, - T::Archived: rkyv::Deserialize, - { - Serialized::deserialize(&self).expect("Should able to deserialize") - } - - #[allow(clippy::should_implement_trait)] - pub fn as_ref(&self) -> &rkyv::AlignedVec { - &self.field - } - - pub fn serialize(t: &W) -> Result + /** + * Constructs an instance from given 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 where W: rkyv::Serialize>, { rkyv::to_bytes::<_, 512>(t) - .map(Serialized::from) + .map(|field| PluginSerializedBytes { field }) .map_err(|err| match err { rkyv::ser::serializers::CompositeSerializerError::SerializerError(e) => e.into(), rkyv::ser::serializers::CompositeSerializerError::ScratchSpaceError(e) => { @@ -94,7 +84,28 @@ impl Serialized { }) } - pub fn deserialize(bytes: &Serialized) -> Result + /* + * Internal fn to constructs an instance from raw bytes ptr. + */ + fn from_raw_ptr( + raw_allocated_ptr: *const u8, + raw_allocated_ptr_len: usize, + ) -> PluginSerializedBytes { + let raw_ptr_bytes = + unsafe { std::slice::from_raw_parts(raw_allocated_ptr, raw_allocated_ptr_len) }; + + PluginSerializedBytes::from_slice(raw_ptr_bytes) + } + + pub fn as_slice(&self) -> &[u8] { + self.field.as_slice() + } + + pub fn as_ptr(&self) -> (*const u8, usize) { + (self.field.as_ptr(), self.field.len()) + } + + pub fn deserialize(&self) -> Result where W: rkyv::Archive, W::Archived: rkyv::Deserialize, @@ -102,65 +113,57 @@ impl Serialized { use anyhow::Context; use rkyv::Deserialize; - let bytes = &bytes.field; - let archived = unsafe { rkyv::archived_root::(&bytes[..]) }; + let archived = unsafe { rkyv::archived_root::(&self.field[..]) }; archived .deserialize(&mut rkyv::de::deserializers::SharedDeserializeMap::new()) .with_context(|| format!("failed to deserialize `{}`", type_name::())) } +} - /// Convenient wrapper to Serialized::* to construct actual struct from raw - /// ptr. This is common workflow on both of runtime (host / plugin) to - /// deserialize struct from allocated / copied ptr. - /// - /// # Safety - /// This is naturally unsafe by constructing bytes slice from raw ptr. - pub unsafe fn deserialize_from_ptr( - raw_allocated_ptr: *const u8, - raw_allocated_ptr_len: i32, - ) -> Result - where - W: rkyv::Archive, - W::Archived: rkyv::Deserialize, - { - // Create serialized bytes slice from ptr - let raw_ptr_bytes = unsafe { - std::slice::from_raw_parts(raw_allocated_ptr, raw_allocated_ptr_len.try_into()?) - }; - - let serialized = Serialized::new_for_plugin(raw_ptr_bytes, raw_allocated_ptr_len); - Serialized::deserialize(&serialized) - } - - /// Deserialize `Fallible` struct from raw ptr. This is similar to - /// `deserialize_from_ptr` but for the struct requires bounds to the - /// SharedSerializeRegistry which cannot be Infallible. Internally this does - /// not call deserialize with Infallible deserializer, use - /// SharedDeserializeMap instead. - /// - /// # Safety - /// This is unsafe by construting bytes slice from raw ptr also deserialize - /// it without slice bound check. - pub unsafe fn deserialize_from_ptr_fallible( - raw_allocated_ptr: *const u8, - raw_allocated_ptr_len: i32, - ) -> Result - where - W: rkyv::Archive, - W::Archived: rkyv::Deserialize, - { - // Create serialized bytes slice from ptr - let raw_ptr_bytes = unsafe { - std::slice::from_raw_parts(raw_allocated_ptr, raw_allocated_ptr_len.try_into()?) - }; - - let serialized = Serialized::new_for_plugin(raw_ptr_bytes, raw_allocated_ptr_len); +/// Simple wrapper around constructing PluginSerializedBytes from raw +/// ptr to call deserialize to support common workflow on both of runtime +/// (host / plugin) to instantiate a struct from allocated / copied ptr. +/// +/// # Safety +/// This is naturally unsafe by constructing bytes slice from raw ptr. +pub unsafe fn deserialize_from_ptr( + raw_allocated_ptr: *const u8, + raw_allocated_ptr_len: i32, +) -> Result +where + W: rkyv::Archive, + W::Archived: rkyv::Deserialize, +{ + let serialized = + PluginSerializedBytes::from_raw_ptr(raw_allocated_ptr, raw_allocated_ptr_len as usize); + + serialized.deserialize() +} - unsafe { - rkyv::from_bytes_unchecked(serialized.as_ref()) - .map_err(|err| Error::msg("Failed to deserialize given ptr")) - } +/// Deserialize `Fallible` struct from raw ptr. This is similar to +/// `deserialize_from_ptr` but for the struct requires bounds to the +/// SharedSerializeRegistry which cannot be Infallible. Internally this does +/// not call deserialize with Infallible deserializer, use +/// SharedDeserializeMap instead. +/// +/// # Safety +/// This is unsafe by construting bytes slice from raw ptr also deserialize +/// it without slice bound check. +pub unsafe fn deserialize_from_ptr_into_fallible( + raw_allocated_ptr: *const u8, + raw_allocated_ptr_len: i32, +) -> Result +where + W: rkyv::Archive, + W::Archived: rkyv::Deserialize, +{ + let serialized = + PluginSerializedBytes::from_raw_ptr(raw_allocated_ptr, raw_allocated_ptr_len as usize); + + unsafe { + rkyv::from_bytes_unchecked(&serialized.field) + .map_err(|err| Error::msg("Failed to deserialize given ptr")) } } diff --git a/crates/swc_common/src/syntax_pos/hygiene.rs b/crates/swc_common/src/syntax_pos/hygiene.rs index 0c58cb15e451..730a7e6528f3 100644 --- a/crates/swc_common/src/syntax_pos/hygiene.rs +++ b/crates/swc_common/src/syntax_pos/hygiene.rs @@ -179,11 +179,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::Serialized::serialize(&MutableMarkContext(0, 0, 0)) - .expect("Should be serializable"); - let serialized_ref = serialized.as_ref(); - let ptr = serialized_ref.as_ptr(); - let len = serialized_ref.len(); + let serialized = + crate::plugin::PluginSerializedBytes::try_serialize(&MutableMarkContext(0, 0, 0)) + .expect("Should be serializable"); + let (ptr, len) = serialized.as_ptr(); // Calling host proxy fn. Inside of host proxy, host will // write the result into allocated context in the guest memory space. @@ -193,7 +192,7 @@ impl Mark { // Deserialize result, assign / return values as needed. let context: MutableMarkContext = unsafe { - crate::plugin::Serialized::deserialize_from_ptr( + crate::plugin::deserialize_from_ptr( ptr, len.try_into().expect("Should able to convert ptr length"), ) @@ -220,18 +219,17 @@ 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::Serialized::serialize(&MutableMarkContext(0, 0, 0)) - .expect("Should be serializable"); - let serialized_ref = serialized.as_ref(); - let ptr = serialized_ref.as_ptr(); - let len = serialized_ref.len(); + let serialized = + crate::plugin::PluginSerializedBytes::try_serialize(&MutableMarkContext(0, 0, 0)) + .expect("Should be serializable"); + let (ptr, len) = serialized.as_ptr(); unsafe { __mark_least_ancestor(a.0, b.0, ptr as _); } let context: MutableMarkContext = unsafe { - crate::plugin::Serialized::deserialize_from_ptr( + crate::plugin::deserialize_from_ptr( ptr, len.try_into().expect("Should able to convert ptr length"), ) @@ -386,18 +384,16 @@ impl SyntaxContext { #[cfg(all(feature = "plugin-mode", target_arch = "wasm32"))] pub fn remove_mark(&mut self) -> Mark { let context = MutableMarkContext(0, 0, 0); - let serialized = - crate::plugin::Serialized::serialize(&context).expect("Should be serializable"); - let serialized_ref = serialized.as_ref(); - let ptr = serialized_ref.as_ptr(); - let len = serialized_ref.len(); + let serialized = crate::plugin::PluginSerializedBytes::try_serialize(&context) + .expect("Should be serializable"); + let (ptr, len) = serialized.as_ptr(); unsafe { __syntax_context_remove_mark_proxy(self.0, ptr as _); } let context: MutableMarkContext = unsafe { - crate::plugin::Serialized::deserialize_from_ptr( + crate::plugin::deserialize_from_ptr( ptr, len.try_into().expect("Should able to convert ptr length"), ) diff --git a/crates/swc_plugin/src/handler.rs b/crates/swc_plugin/src/handler.rs index 26bef379d7a3..57e7d59dc585 100644 --- a/crates/swc_plugin/src/handler.rs +++ b/crates/swc_plugin/src/handler.rs @@ -1,4 +1,4 @@ -use swc_common::{plugin::Serialized, sync::OnceCell}; +use swc_common::{plugin::PluginSerializedBytes, sync::OnceCell}; use crate::pseudo_scoped_key::PseudoScopedKey; @@ -18,16 +18,13 @@ 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 = - Serialized::serialize(&*db.diagnostic).expect("Should able to serialize Diagnostic"); - let diag_ref = diag.as_ref(); - - let ptr = diag_ref.as_ptr() as i32; - let len = diag_ref.len() as i32; + let diag = PluginSerializedBytes::try_serialize(&*db.diagnostic) + .expect("Should able to serialize Diagnostic"); + let (ptr, len) = diag.as_ptr(); #[cfg(target_arch = "wasm32")] // Allow testing unsafe { - __emit_diagnostics(ptr, len); + __emit_diagnostics(ptr as i32, len as i32); } } } diff --git a/crates/swc_plugin/src/lib.rs b/crates/swc_plugin/src/lib.rs index 997fa442c9d3..d2122cdee7d7 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::{PluginError, Serialized}, + plugin::{deserialize_from_ptr, PluginError, PluginSerializedBytes}, }; pub mod comments { diff --git a/crates/swc_plugin_macro/src/lib.rs b/crates/swc_plugin_macro/src/lib.rs index 5d02c83587ef..1cb6e2ac4899 100644 --- a/crates/swc_plugin_macro/src/lib.rs +++ b/crates/swc_plugin_macro/src/lib.rs @@ -43,12 +43,12 @@ 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 ret = swc_plugin::Serialized::serialize(&plugin_error).expect("Should able to serialize PluginError"); - let ret_ref = ret.as_ref(); + let ret = swc_plugin::PluginSerializedBytes::try_serialize(&plugin_error).expect("Should able to serialize PluginError"); + let (ptr, len) = ret.as_ptr(); send_transform_result_to_host( - ret_ref.as_ptr() as _, - std::convert::TryInto::try_into(ret_ref.len()).expect("Should able to convert size of PluginError") + ptr as _, + len as i32 ); 1 } @@ -61,14 +61,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::Serialized::deserialize_from_ptr(ast_ptr, ast_ptr_len) }; + let program = unsafe { swc_plugin::deserialize_from_ptr(ast_ptr, ast_ptr_len) }; 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::Serialized::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) }; if config.is_err() { let err = swc_plugin::PluginError::Deserialize( "Failed to deserialize config string received from host".to_string() @@ -77,7 +77,7 @@ fn handle_func(func: ItemFn) -> TokenStream { } let config: String = config.expect("Should be a string"); - let context = unsafe { swc_plugin::Serialized::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) }; 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 +112,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::Serialized::serialize(&transformed_program); + let serialized_result = swc_plugin::PluginSerializedBytes::try_serialize(&transformed_program); if serialized_result.is_err() { let err = swc_plugin::PluginError::Serialize("Failed to serialize transformed program".to_string()); @@ -120,16 +120,9 @@ fn handle_func(func: ItemFn) -> TokenStream { } let serialized_result = serialized_result.expect("Should be a realized transformed program"); - let serialized_result = serialized_result.as_ref(); + let (serialized_result_ptr, serialized_result_ptr_len) = serialized_result.as_ptr(); - let serialized_result_len: Result = std::convert::TryInto::try_into(serialized_result.len()); - - if serialized_result_len.is_err() { - let err = swc_plugin::PluginError::SizeInteropFailure("Failed to convert size of transformed AST pointer".to_string()); - return construct_error_ptr(err); - } - - send_transform_result_to_host(serialized_result.as_ptr() as _, serialized_result_len.expect("Should be an i32")); + send_transform_result_to_host(serialized_result_ptr as _, serialized_result_ptr_len as i32); 0 } }; 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 4c7fe68fea92..8054cb299e52 100644 --- a/crates/swc_plugin_proxy/src/comments/plugin_comments_proxy.rs +++ b/crates/swc_plugin_proxy/src/comments/plugin_comments_proxy.rs @@ -50,18 +50,17 @@ impl PluginCommentsProxy { { #[cfg(target_arch = "wasm32")] { - let serialized = swc_common::plugin::Serialized::serialize(value) + let serialized = swc_common::plugin::PluginSerializedBytes::try_serialize(value) .expect("Should able to serialize value"); - let serialized_comment_ptr_ref = serialized.as_ref(); + let (serialized_comment_ptr, serialized_comment_ptr_len) = serialized.as_ptr(); unsafe { // We need to copy PluginCommentProxy's param for add_leading (Comment, or // Vec) to the host, before calling proxy to the host. This'll fill in // CommentHostEnvironment's buffer, subsequent proxy call will read & // deserialize it. __copy_comment_to_host_env( - serialized_comment_ptr_ref.as_ptr() as _, - serialized_comment_ptr_ref - .len() + serialized_comment_ptr as i32, + serialized_comment_ptr_len .try_into() .expect("Should able to convert ptr length"), ); 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 c5b6b2fe3c97..d661742cd63c 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,5 +1,7 @@ #[cfg_attr(not(target_arch = "wasm32"), allow(unused))] -use swc_common::plugin::Serialized; +use swc_common::plugin::{ + deserialize_from_ptr, deserialize_from_ptr_into_fallible, PluginSerializedBytes, +}; /// A struct to exchange allocated data between memory spaces. #[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] @@ -21,10 +23,10 @@ where { // Allocate AllocatedBytesPtr to get return value from the host let allocated_bytes_ptr = AllocatedBytesPtr(0, 0); - let serialized_allocated_bytes_ptr = Serialized::serialize(&allocated_bytes_ptr) + let serialized_allocated_bytes_ptr = PluginSerializedBytes::try_serialize(&allocated_bytes_ptr) .expect("Should able to serialize AllocatedBytesPtr"); - let serialized_allocated_bytes_ptr_ref = serialized_allocated_bytes_ptr.as_ref(); - let serialized_allocated_bytes_raw_ptr = serialized_allocated_bytes_ptr_ref.as_ptr(); + let (serialized_allocated_bytes_raw_ptr, serialized_allocated_bytes_raw_ptr_size) = + serialized_allocated_bytes_ptr.as_ptr(); #[cfg(target_arch = "wasm32")] { @@ -41,10 +43,9 @@ where // Return reconstructted AllocatedBytesPtr to reveal ptr to the allocated bytes Some(unsafe { - Serialized::deserialize_from_ptr( + deserialize_from_ptr( serialized_allocated_bytes_raw_ptr, - serialized_allocated_bytes_ptr_ref - .len() + serialized_allocated_bytes_raw_ptr_size .try_into() .expect("Should able to convert ptr length"), ) @@ -66,7 +67,7 @@ where // Using AllocatedBytesPtr's value, reconstruct actual return value allocated_returned_value_ptr.map(|allocated_returned_value_ptr| unsafe { - Serialized::deserialize_from_ptr( + deserialize_from_ptr( allocated_returned_value_ptr.0 as _, allocated_returned_value_ptr.1, ) @@ -89,10 +90,10 @@ where { // Allocate AllocatedBytesPtr to get return value from the host let allocated_bytes_ptr = AllocatedBytesPtr(0, 0); - let serialized_allocated_bytes_ptr = Serialized::serialize(&allocated_bytes_ptr) + let serialized_allocated_bytes_ptr = PluginSerializedBytes::try_serialize(&allocated_bytes_ptr) .expect("Should able to serialize AllocatedBytesPtr"); - let serialized_allocated_bytes_ptr_ref = serialized_allocated_bytes_ptr.as_ref(); - let serialized_allocated_bytes_raw_ptr = serialized_allocated_bytes_ptr_ref.as_ptr(); + let (serialized_allocated_bytes_raw_ptr, serialized_allocated_bytes_raw_ptr_size) = + serialized_allocated_bytes_ptr.as_ptr(); #[cfg(target_arch = "wasm32")] { @@ -109,10 +110,9 @@ where // Now reconstruct AllocatedBytesPtr to reveal ptr to the allocated bytes let allocated_returned_value_ptr: AllocatedBytesPtr = unsafe { - Serialized::deserialize_from_ptr( + deserialize_from_ptr( serialized_allocated_bytes_raw_ptr, - serialized_allocated_bytes_ptr_ref - .len() + serialized_allocated_bytes_raw_ptr_size .try_into() .expect("Should able to convert ptr length"), ) @@ -121,7 +121,7 @@ where // Using AllocatedBytesPtr's value, reconstruct actual return value Some(unsafe { - Serialized::deserialize_from_ptr_fallible( + deserialize_from_ptr_into_fallible( allocated_returned_value_ptr.0 as _, allocated_returned_value_ptr.1, ) 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 eb1bb5dcf452..ce9661490270 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 @@ -225,11 +225,9 @@ impl SourceMapper for PluginSourceMapProxy { ctxt: swc_common::SyntaxContext::empty(), }; - let serialized = - swc_common::plugin::Serialized::serialize(&span).expect("Should be serializable"); - let serialized_ref = serialized.as_ref(); - let ptr = serialized_ref.as_ptr(); - let len = serialized_ref.len(); + let serialized = swc_common::plugin::PluginSerializedBytes::try_serialize(&span) + .expect("Should be serializable"); + let (ptr, len) = serialized.as_ptr(); let ret = __merge_spans_proxy( sp_lhs.lo.0, diff --git a/crates/swc_plugin_runner/benches/invoke.rs b/crates/swc_plugin_runner/benches/invoke.rs index e577b506cb05..5681917701fe 100644 --- a/crates/swc_plugin_runner/benches/invoke.rs +++ b/crates/swc_plugin_runner/benches/invoke.rs @@ -9,7 +9,7 @@ use std::{ use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion}; use once_cell::sync::Lazy; -use swc_common::{plugin::Serialized, FileName, FilePathMapping, SourceMap}; +use swc_common::{plugin::PluginSerializedBytes, FileName, FilePathMapping, SourceMap}; use swc_ecma_ast::EsVersion; use swc_ecma_parser::parse_file_as_program; use swc_plugin_runner::cache::PluginModuleCache; @@ -56,7 +56,7 @@ fn bench_transform(b: &mut Bencher, plugin_dir: &Path) { ) .unwrap(); - let program_ser = Serialized::serialize(&program).unwrap(); + let program_ser = PluginSerializedBytes::try_serialize(&program).unwrap(); let res = swc_plugin_runner::apply_transform_plugin( "test", @@ -67,8 +67,8 @@ fn bench_transform(b: &mut Bencher, plugin_dir: &Path) { .join("swc_internal_plugin.wasm"), &cache, program_ser, - Serialized::serialize(&String::from("{}")).unwrap(), - Serialized::serialize(&String::from("{}")).unwrap(), + PluginSerializedBytes::try_serialize(&String::from("{}")).unwrap(), + PluginSerializedBytes::try_serialize(&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 5de3b9222cdd..97f2c60306bd 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::Serialized, + plugin::PluginSerializedBytes, BytePos, }; use swc_plugin_proxy::COMMENTS; @@ -102,19 +102,13 @@ where /// Common logics for add_*_comment/comments. fn add_comments_inner(env: &CommentHostEnvironment, byte_pos: u32, f: F) where - F: FnOnce(&SingleThreadedComments, BytePos, Serialized), + F: FnOnce(&SingleThreadedComments, BytePos, PluginSerializedBytes), { unwrap_comments_storage(|comments| { let byte_pos = BytePos(byte_pos); // PluginCommentProxy in the guest should've copied buffer already let comment_byte = &mut (*env.mutable_comment_buffer.lock()); - let serialized = Serialized::new_for_plugin( - comment_byte, - comment_byte - .len() - .try_into() - .expect("Should able to convert ptr length"), - ); + let serialized = PluginSerializedBytes::from_slice(comment_byte); f(comments, byte_pos, serialized); @@ -126,13 +120,23 @@ where pub fn add_leading_comment_proxy(env: &CommentHostEnvironment, byte_pos: u32) { add_comments_inner(env, byte_pos, |comments, byte_pos, serialized| { - comments.add_leading(byte_pos, serialized.into()); + comments.add_leading( + byte_pos, + serialized + .deserialize() + .expect("Should be able to deserialize"), + ); }); } pub fn add_leading_comments_proxy(env: &CommentHostEnvironment, byte_pos: u32) { add_comments_inner(env, byte_pos, |comments, byte_pos, serialized| { - comments.add_leading_comments(byte_pos, serialized.into()); + comments.add_leading_comments( + byte_pos, + serialized + .deserialize() + .expect("Should be able to deserialize"), + ); }); } @@ -157,7 +161,8 @@ pub fn take_leading_comments_proxy( let leading_comments = comments.take_leading(BytePos(byte_pos)); if let Some(leading_comments) = leading_comments { let serialized_leading_comments_vec_bytes = - Serialized::serialize(&leading_comments).expect("Should be serializable"); + PluginSerializedBytes::try_serialize(&leading_comments) + .expect("Should be serializable"); allocate_return_values_into_guest( memory, @@ -190,7 +195,8 @@ pub fn get_leading_comments_proxy( let leading_comments = comments.get_leading(BytePos(byte_pos)); if let Some(leading_comments) = leading_comments { let serialized_leading_comments_vec_bytes = - Serialized::serialize(&leading_comments).expect("Should be serializable"); + PluginSerializedBytes::try_serialize(&leading_comments) + .expect("Should be serializable"); allocate_return_values_into_guest( memory, @@ -209,13 +215,23 @@ pub fn get_leading_comments_proxy( pub fn add_trailing_comment_proxy(env: &CommentHostEnvironment, byte_pos: u32) { add_comments_inner(env, byte_pos, |comments, byte_pos, serialized| { - comments.add_trailing(byte_pos, serialized.into()); + comments.add_trailing( + byte_pos, + serialized + .deserialize() + .expect("Should be able to deserialize"), + ); }); } pub fn add_trailing_comments_proxy(env: &CommentHostEnvironment, byte_pos: u32) { add_comments_inner(env, byte_pos, |comments, byte_pos, serialized| { - comments.add_trailing_comments(byte_pos, serialized.into()); + comments.add_trailing_comments( + byte_pos, + serialized + .deserialize() + .expect("Should be able to deserialize"), + ); }); } @@ -243,7 +259,8 @@ pub fn take_trailing_comments_proxy( let trailing_comments = comments.take_trailing(BytePos(byte_pos)); if let Some(leading_comments) = trailing_comments { let serialized_leading_comments_vec_bytes = - Serialized::serialize(&leading_comments).expect("Should be serializable"); + PluginSerializedBytes::try_serialize(&leading_comments) + .expect("Should be serializable"); allocate_return_values_into_guest( memory, @@ -271,7 +288,8 @@ pub fn get_trailing_comments_proxy( let trailing_comments = comments.get_trailing(BytePos(byte_pos)); if let Some(leading_comments) = trailing_comments { let serialized_leading_comments_vec_bytes = - Serialized::serialize(&leading_comments).expect("Should be serializable"); + PluginSerializedBytes::try_serialize(&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 988f0cf2e817..bd9caba4ca3a 100644 --- a/crates/swc_plugin_runner/src/imported_fn/handler.rs +++ b/crates/swc_plugin_runner/src/imported_fn/handler.rs @@ -1,6 +1,6 @@ use swc_common::{ errors::{Diagnostic, HANDLER}, - plugin::Serialized, + plugin::PluginSerializedBytes, }; use crate::{host_environment::BaseHostEnvironment, memory_interop::copy_bytes_into_host}; @@ -10,8 +10,8 @@ pub fn emit_diagnostics(env: &BaseHostEnvironment, bytes_ptr: i32, bytes_ptr_len if HANDLER.is_set() { HANDLER.with(|handler| { let diagnostics_bytes = copy_bytes_into_host(memory, bytes_ptr, bytes_ptr_len); - let serialized = Serialized::new_for_plugin(&diagnostics_bytes[..], bytes_ptr_len); - let diagnostic = Serialized::deserialize::(&serialized) + let serialized = PluginSerializedBytes::from_slice(&diagnostics_bytes[..]); + let diagnostic = PluginSerializedBytes::deserialize::(&serialized) .expect("Should able to be deserialized into diagnostic"); let mut builder = diff --git a/crates/swc_plugin_runner/src/imported_fn/hygiene.rs b/crates/swc_plugin_runner/src/imported_fn/hygiene.rs index 7c4e9506956a..c372e1805f1c 100644 --- a/crates/swc_plugin_runner/src/imported_fn/hygiene.rs +++ b/crates/swc_plugin_runner/src/imported_fn/hygiene.rs @@ -1,4 +1,4 @@ -use swc_common::{hygiene::MutableMarkContext, plugin::Serialized, Mark, SyntaxContext}; +use swc_common::{hygiene::MutableMarkContext, plugin::PluginSerializedBytes, Mark, SyntaxContext}; use crate::{host_environment::BaseHostEnvironment, memory_interop::write_into_memory_view}; @@ -38,7 +38,7 @@ 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 = Serialized::serialize(&MutableMarkContext( + let serialized_bytes = PluginSerializedBytes::try_serialize(&MutableMarkContext( self_mark.as_u32(), 0, return_value as u32, @@ -56,9 +56,12 @@ 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 = - Serialized::serialize(&MutableMarkContext(a.as_u32(), b.as_u32(), return_value)) - .expect("Should be serializable"); + let serialized_bytes = PluginSerializedBytes::try_serialize(&MutableMarkContext( + a.as_u32(), + b.as_u32(), + return_value, + )) + .expect("Should be serializable"); write_into_memory_view(memory, &serialized_bytes, |_| allocated_ptr); } @@ -80,7 +83,7 @@ pub fn syntax_context_remove_mark_proxy( let return_value = self_mark.remove_mark(); if let Some(memory) = env.memory_ref() { - let serialized_bytes = Serialized::serialize(&MutableMarkContext( + let serialized_bytes = PluginSerializedBytes::try_serialize(&MutableMarkContext( self_mark.as_u32(), 0, return_value.as_u32(), 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 134b2e7993e9..07db095977aa 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,7 @@ use std::sync::Arc; use parking_lot::Mutex; -use swc_common::{plugin::Serialized, BytePos, SourceMap, Span, SyntaxContext}; +use swc_common::{plugin::PluginSerializedBytes, BytePos, SourceMap, Span, SyntaxContext}; use wasmer::{LazyInit, Memory, NativeFunc}; use crate::memory_interop::{allocate_return_values_into_guest, write_into_memory_view}; @@ -42,7 +42,8 @@ pub fn lookup_char_pos_proxy( ) -> i32 { if let Some(memory) = env.memory_ref() { let ret = (env.source_map.lock()).lookup_char_pos(BytePos(byte_pos)); - let serialized_loc_bytes = Serialized::serialize(&ret).expect("Should be serializable"); + let serialized_loc_bytes = + PluginSerializedBytes::try_serialize(&ret).expect("Should be serializable"); if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() { allocate_return_values_into_guest( @@ -90,7 +91,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 = Serialized::serialize(&span).expect("Should be serializable"); + let serialized_bytes = + PluginSerializedBytes::try_serialize(&span).expect("Should be serializable"); write_into_memory_view(memory, &serialized_bytes, |_| allocated_ptr); 1 } else { @@ -116,7 +118,8 @@ pub fn span_to_lines_proxy( }; let ret = (env.source_map.lock()).span_to_lines(span); - let serialized_loc_bytes = Serialized::serialize(&ret).expect("Should be serializable"); + let serialized_loc_bytes = + PluginSerializedBytes::try_serialize(&ret).expect("Should be serializable"); if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() { allocate_return_values_into_guest( @@ -143,7 +146,8 @@ pub fn lookup_byte_offset_proxy( let byte_pos = BytePos(byte_pos); let ret = (env.source_map.lock()).lookup_byte_offset(byte_pos); - let serialized_loc_bytes = Serialized::serialize(&ret).expect("Should be serializable"); + let serialized_loc_bytes = + PluginSerializedBytes::try_serialize(&ret).expect("Should be serializable"); if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() { allocate_return_values_into_guest( @@ -175,7 +179,8 @@ pub fn span_to_string_proxy( ctxt: SyntaxContext::from_u32(span_ctxt), }; let ret = (env.source_map.lock()).span_to_string(span); - let serialized_loc_bytes = Serialized::serialize(&ret).expect("Should be serializable"); + let serialized_loc_bytes = + PluginSerializedBytes::try_serialize(&ret).expect("Should be serializable"); if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() { allocate_return_values_into_guest( @@ -207,7 +212,8 @@ pub fn span_to_filename_proxy( ctxt: SyntaxContext::from_u32(span_ctxt), }; let ret = (env.source_map.lock()).span_to_filename(span); - let serialized_loc_bytes = Serialized::serialize(&ret).expect("Should be serializable"); + let serialized_loc_bytes = + PluginSerializedBytes::try_serialize(&ret).expect("Should be serializable"); if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() { allocate_return_values_into_guest( diff --git a/crates/swc_plugin_runner/src/lib.rs b/crates/swc_plugin_runner/src/lib.rs index 8093282d5eaa..ef468470b46b 100644 --- a/crates/swc_plugin_runner/src/lib.rs +++ b/crates/swc_plugin_runner/src/lib.rs @@ -2,7 +2,7 @@ use std::{path::Path, sync::Arc}; use anyhow::{Context, Error}; use once_cell::sync::Lazy; -use swc_common::{plugin::Serialized, SourceMap}; +use swc_common::{plugin::PluginSerializedBytes, SourceMap}; use transform_executor::TransformExecutor; pub mod cache; @@ -18,12 +18,12 @@ pub fn apply_transform_plugin( plugin_name: &str, path: &Path, cache: &Lazy, - program: Serialized, - config_json: Serialized, - context_json: Serialized, + program: PluginSerializedBytes, + config_json: PluginSerializedBytes, + context_json: PluginSerializedBytes, should_enable_comments_proxy: bool, source_map: &Arc, -) -> Result { +) -> Result { (|| -> Result<_, Error> { let mut transform_tracker = TransformExecutor::new(path, cache, source_map)?; let should_enable_comments_proxy = if should_enable_comments_proxy { 1 } else { 0 }; diff --git a/crates/swc_plugin_runner/src/memory_interop.rs b/crates/swc_plugin_runner/src/memory_interop.rs index 49b5a19f548b..3afcfbcffe95 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::Serialized; +use swc_common::plugin::PluginSerializedBytes; use swc_plugin_proxy::AllocatedBytesPtr; use wasmer::{Array, Memory, NativeFunc, WasmPtr}; @@ -23,14 +23,13 @@ pub fn copy_bytes_into_host(memory: &Memory, bytes_ptr: i32, bytes_ptr_len: i32) #[tracing::instrument(level = "info", skip_all)] pub fn write_into_memory_view( memory: &Memory, - serialized_bytes: &Serialized, + serialized_bytes: &PluginSerializedBytes, get_allocated_ptr: F, ) -> (i32, i32) where F: Fn(usize) -> i32, { - let serialized = serialized_bytes.as_ref(); - let serialized_len = serialized.len(); + let serialized_len = serialized_bytes.as_ptr().1; let ptr_start = get_allocated_ptr(serialized_len); let ptr_start_size = ptr_start @@ -52,7 +51,7 @@ where // https://github.com/swc-project/swc/blob/1ef8f3749b6454eb7d40a36a5f9366137fa97928/crates/swc_plugin_runner/src/lib.rs#L56-L61 unsafe { view.subarray(ptr_start_size, ptr_start_size + serialized_len_size) - .copy_from(serialized); + .copy_from(serialized_bytes.as_slice()); } ( @@ -71,9 +70,9 @@ pub fn allocate_return_values_into_guest( memory: &Memory, alloc_guest_memory: &NativeFunc, allocated_ret_ptr: i32, - serialized_bytes: &Serialized, + serialized_bytes: &PluginSerializedBytes, ) { - let serialized_bytes_len = serialized_bytes.as_ref().len(); + let serialized_bytes_len = serialized_bytes.as_ptr().1; let (allocated_ptr, allocated_ptr_len) = write_into_memory_view(memory, serialized_bytes, |_| { @@ -95,7 +94,7 @@ pub fn allocate_return_values_into_guest( // Retuning (allocated_ptr, len) into caller (plugin) let comment_ptr_serialized = - Serialized::serialize(&AllocatedBytesPtr(allocated_ptr, allocated_ptr_len)) + PluginSerializedBytes::try_serialize(&AllocatedBytesPtr(allocated_ptr, allocated_ptr_len)) .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 632eeadc8e9a..9b15df6f7ac7 100644 --- a/crates/swc_plugin_runner/src/transform_executor.rs +++ b/crates/swc_plugin_runner/src/transform_executor.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use anyhow::{anyhow, Error}; use parking_lot::Mutex; use swc_common::{ - plugin::{PluginError, Serialized}, + plugin::{PluginError, PluginSerializedBytes}, SourceMap, }; use wasmer::Instance; @@ -60,7 +60,7 @@ impl TransformExecutor { /// Once transformation completes, host should free allocated memory. fn write_bytes_into_guest( &mut self, - serialized_bytes: &Serialized, + serialized_bytes: &PluginSerializedBytes, ) -> Result<(i32, i32), Error> { let memory = self.instance.exports.get_memory("memory")?; @@ -76,15 +76,17 @@ impl TransformExecutor { /// Copy guest's memory into host, construct serialized struct from raw /// bytes. - fn read_bytes_from_guest(&mut self, returned_ptr_result: i32) -> Result { + fn read_bytes_from_guest( + &mut self, + returned_ptr_result: i32, + ) -> Result { let transformed_result = &(*self.transform_result.lock()); - let ret = - Serialized::new_for_plugin(&transformed_result[..], transformed_result.len() as i32); + let ret = PluginSerializedBytes::from_slice(&transformed_result[..]); if returned_ptr_result == 0 { Ok(ret) } else { - let err: PluginError = Serialized::deserialize(&ret)?; + let err: PluginError = ret.deserialize()?; match err { PluginError::SizeInteropFailure(msg) => Err(anyhow!( "Failed to convert pointer size to calculate: {}", @@ -103,11 +105,11 @@ impl TransformExecutor { #[tracing::instrument(level = "info", skip_all)] pub fn transform( &mut self, - program: &Serialized, - config: &Serialized, - context: &Serialized, + program: &PluginSerializedBytes, + config: &PluginSerializedBytes, + context: &PluginSerializedBytes, should_enable_comments_proxy: i32, - ) -> Result { + ) -> Result { let guest_program_ptr = self.write_bytes_into_guest(program)?; let config_str_ptr = self.write_bytes_into_guest(config)?; let context_str_ptr = self.write_bytes_into_guest(context)?; diff --git a/crates/swc_plugin_runner/tests/integration.rs b/crates/swc_plugin_runner/tests/integration.rs index 2d25406c192b..e9c498ff391d 100644 --- a/crates/swc_plugin_runner/tests/integration.rs +++ b/crates/swc_plugin_runner/tests/integration.rs @@ -5,7 +5,7 @@ use std::{ }; use anyhow::{anyhow, Error}; -use swc_common::{errors::HANDLER, plugin::Serialized, sync::Lazy, FileName}; +use swc_common::{errors::HANDLER, plugin::PluginSerializedBytes, 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,10 +80,13 @@ fn internal() -> Result<(), Error> { ) .unwrap(); - let program = Serialized::serialize(&program).expect("Should serializable"); - let config = Serialized::serialize(&"{}".to_string()).expect("Should serializable"); - let context = Serialized::serialize(&"{sourceFileName: 'single_plugin_test'}".to_string()) - .expect("Should serializable"); + let program = PluginSerializedBytes::try_serialize(&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(), + ) + .expect("Should serializable"); let cache: Lazy = Lazy::new(PluginModuleCache::new); @@ -99,8 +102,9 @@ fn internal() -> Result<(), Error> { ) .expect("Plugin should apply transform"); - let program: Program = - Serialized::deserialize(&program_bytes).expect("Should able to deserialize"); + let program: Program = program_bytes + .deserialize() + .expect("Should able to deserialize"); let mut visitor = TestVisitor { plugin_transform_found: false, }; @@ -128,11 +132,13 @@ fn internal() -> Result<(), Error> { ) .unwrap(); - let program = Serialized::serialize(&program).expect("Should serializable"); - let config = Serialized::serialize(&"{}".to_string()).expect("Should serializable"); - let context = - Serialized::serialize(&"{sourceFileName: 'single_plugin_handler_test'}".to_string()) - .expect("Should serializable"); + let program = PluginSerializedBytes::try_serialize(&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(), + ) + .expect("Should serializable"); let cache: Lazy = Lazy::new(PluginModuleCache::new); @@ -169,7 +175,8 @@ fn internal() -> Result<(), Error> { ) .unwrap(); - let mut serialized_program = Serialized::serialize(&program).expect("Should serializable"); + let mut serialized_program = + PluginSerializedBytes::try_serialize(&program).expect("Should serializable"); let cache: Lazy = Lazy::new(PluginModuleCache::new); serialized_program = swc_plugin_runner::apply_transform_plugin( @@ -177,9 +184,11 @@ fn internal() -> Result<(), Error> { &path, &cache, serialized_program, - Serialized::serialize(&"{}".to_string()).expect("Should serializable"), - Serialized::serialize(&"{sourceFileName: 'multiple_plugin_test'}".to_string()) - .expect("Should serializable"), + PluginSerializedBytes::try_serialize(&"{}".to_string()).expect("Should serializable"), + PluginSerializedBytes::try_serialize( + &"{sourceFileName: 'multiple_plugin_test'}".to_string(), + ) + .expect("Should serializable"), false, &cm, ) @@ -191,16 +200,19 @@ fn internal() -> Result<(), Error> { &path, &cache, serialized_program, - Serialized::serialize(&"{}".to_string()).expect("Should serializable"), - Serialized::serialize(&"{sourceFileName: 'multiple_plugin_test2'}".to_string()) - .expect("Should serializable"), + PluginSerializedBytes::try_serialize(&"{}".to_string()).expect("Should serializable"), + PluginSerializedBytes::try_serialize( + &"{sourceFileName: 'multiple_plugin_test2'}".to_string(), + ) + .expect("Should serializable"), false, &cm, ) .expect("Plugin should apply transform"); - let program: Program = - Serialized::deserialize(&serialized_program).expect("Should able to deserialize"); + let program: Program = serialized_program + .deserialize() + .expect("Should able to deserialize"); let mut visitor = TestVisitor { plugin_transform_found: false, }; diff --git a/tests/rust-plugins/swc_internal_plugin/Cargo.lock b/tests/rust-plugins/swc_internal_plugin/Cargo.lock index 83059c4279d2..3bf1ac63a518 100644 --- a/tests/rust-plugins/swc_internal_plugin/Cargo.lock +++ b/tests/rust-plugins/swc_internal_plugin/Cargo.lock @@ -40,7 +40,7 @@ checksum = "84450d0b4a8bd1ba4144ce8ce718fbc5d071358b1e5384bace6536b3d1f2d5b3" [[package]] name = "ast_node" -version = "0.7.7" +version = "0.8.1" dependencies = [ "darling", "pmutil", @@ -709,9 +709,11 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "swc_atoms" -version = "0.2.12" +version = "0.2.13" dependencies = [ + "bytecheck", "once_cell", + "rkyv", "rustc-hash", "serde", "string_cache", @@ -720,12 +722,13 @@ dependencies = [ [[package]] name = "swc_common" -version = "0.18.9" +version = "0.20.2" dependencies = [ "ahash", "anyhow", "ast_node", "better_scoped_tls", + "bytecheck", "cfg-if", "debug_unreachable", "either", @@ -737,6 +740,7 @@ dependencies = [ "serde", "siphasher", "string_cache", + "swc_atoms", "swc_eq_ignore_macros", "swc_visit", "tracing", @@ -746,9 +750,10 @@ dependencies = [ [[package]] name = "swc_ecma_ast" -version = "0.79.0" +version = "0.81.0" dependencies = [ "bitflags", + "bytecheck", "is-macro", "num-bigint", "rkyv", @@ -762,7 +767,7 @@ dependencies = [ [[package]] name = "swc_ecma_parser" -version = "0.105.7" +version = "0.108.1" dependencies = [ "either", "enum_kind", @@ -779,7 +784,7 @@ dependencies = [ [[package]] name = "swc_ecma_utils" -version = "0.86.2" +version = "0.90.0" dependencies = [ "indexmap", "once_cell", @@ -793,7 +798,7 @@ dependencies = [ [[package]] name = "swc_ecma_visit" -version = "0.65.0" +version = "0.67.1" dependencies = [ "num-bigint", "swc_atoms", @@ -805,7 +810,7 @@ dependencies = [ [[package]] name = "swc_ecmascript" -version = "0.167.0" +version = "0.175.0" dependencies = [ "swc_ecma_ast", "swc_ecma_parser", @@ -843,7 +848,7 @@ dependencies = [ [[package]] name = "swc_plugin" -version = "0.63.0" +version = "0.71.0" dependencies = [ "swc_atoms", "swc_common", @@ -863,9 +868,10 @@ dependencies = [ [[package]] name = "swc_plugin_proxy" -version = "0.4.0" +version = "0.6.0" dependencies = [ "better_scoped_tls", + "bytecheck", "rkyv", "swc_common", "swc_ecma_ast", @@ -873,7 +879,7 @@ dependencies = [ [[package]] name = "swc_visit" -version = "0.3.0" +version = "0.3.2" dependencies = [ "either", "swc_visit_macros", @@ -881,7 +887,7 @@ dependencies = [ [[package]] name = "swc_visit_macros" -version = "0.3.1" +version = "0.3.2" dependencies = [ "Inflector", "pmutil",