diff --git a/crates/swc_common/src/plugin/serialized.rs b/crates/swc_common/src/plugin/serialized.rs index f8b91d9427236..34b1c3f856b11 100644 --- a/crates/swc_common/src/plugin/serialized.rs +++ b/crates/swc_common/src/plugin/serialized.rs @@ -1,8 +1,8 @@ -use std::any::type_name; +//use std::any::type_name; use anyhow::Error; -#[cfg(feature = "__rkyv")] -use rkyv::Deserialize; +//#[cfg(feature = "__rkyv")] +//use rkyv::Deserialize; #[derive(Debug, Clone, PartialEq, Eq)] #[non_exhaustive] @@ -99,18 +99,27 @@ impl PluginSerializedBytes { } #[tracing::instrument(level = "info", skip_all)] - pub fn deserialize(&self) -> Result, Error> + pub fn deserialize<'a, W>(&'a self) -> Result, Error> where - W: rkyv::Archive, - W::Archived: rkyv::Deserialize, + W: rkyv::Archive + 'a, + W::Archived: 'a + + rkyv::CheckBytes> + + rkyv::Deserialize, { - use anyhow::Context; - - let archived = unsafe { rkyv::archived_root::>(&self.field[..]) }; - - archived - .deserialize(&mut rkyv::de::deserializers::SharedDeserializeMap::new()) - .with_context(|| format!("failed to deserialize `{}`", type_name::())) + use rkyv::validation::{validators::CheckDeserializeError, CheckArchiveError}; + + let result = rkyv::from_bytes::>(&self.field[..]); + result.map_err(move |err| match err { + CheckDeserializeError::DeserializeError(e) => e.into(), + CheckDeserializeError::CheckBytesError(e) => match e { + CheckArchiveError::CheckBytesError(_e) => { + // [TODO]: we can't forward CheckBytes::Error itself as it is Archived type of VersionedSerializable struct itself. + // Still we need to carry better diagnostics if this occurs. + Error::msg("CheckBytesError") + } + CheckArchiveError::ContextError(e) => e.into(), + }, + }) } } diff --git a/crates/swc_css_ast/src/lib.rs b/crates/swc_css_ast/src/lib.rs index 8173f6d8dcf46..4dd9aa25c4cab 100644 --- a/crates/swc_css_ast/src/lib.rs +++ b/crates/swc_css_ast/src/lib.rs @@ -1,6 +1,5 @@ #![deny(clippy::all)] #![allow(clippy::large_enum_variant)] - //! AST definitions for CSS. pub use self::{at_rule::*, base::*, selector::*, token::*, value::*}; diff --git a/crates/swc_plugin_macro/src/lib.rs b/crates/swc_plugin_macro/src/lib.rs index eea109b49d32e..fc5531e27ab13 100644 --- a/crates/swc_plugin_macro/src/lib.rs +++ b/crates/swc_plugin_macro/src/lib.rs @@ -131,6 +131,7 @@ fn handle_func(func: ItemFn, ast_type: Ident) -> TokenStream { // Reconstruct `Program` & config string from serialized program // Host (SWC) should allocate memory, copy bytes and pass ptr to plugin. let program = swc_core::common::plugin::serialized::PluginSerializedBytes::from_raw_ptr(ast_ptr, ast_ptr_len.try_into().expect("Should able to convert ptr length")).deserialize(); + let program = swc_core::common::plugin::serialized::PluginSerializedBytes::from_raw_ptr(ast_ptr, ast_ptr_len).deserialize(); if program.is_err() { let err = swc_core::common::plugin::serialized::PluginError::Deserialize("Failed to deserialize program received from host".to_string()); return construct_error_ptr(err);