From 9221639a8d789f73d5c73965b56a9d4b92abee16 Mon Sep 17 00:00:00 2001 From: OJ Kwon <1210596+kwonoj@users.noreply.github.com> Date: Sun, 28 May 2023 19:02:48 -0700 Subject: [PATCH] feat(serialized): deserialize with bytechecked --- crates/swc_common/src/plugin/serialized.rs | 35 ++++++++++++++-------- crates/swc_css_ast/src/lib.rs | 1 - crates/swc_plugin_macro/src/lib.rs | 1 + 3 files changed, 23 insertions(+), 14 deletions(-) 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);