Skip to content

Commit

Permalink
feat(serialized): deserialize with bytechecked
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed May 30, 2023
1 parent 56ac9eb commit 9221639
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
35 changes: 22 additions & 13 deletions crates/swc_common/src/plugin/serialized.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -99,18 +99,27 @@ impl PluginSerializedBytes {
}

#[tracing::instrument(level = "info", skip_all)]
pub fn deserialize<W>(&self) -> Result<VersionedSerializable<W>, Error>
pub fn deserialize<'a, W>(&'a self) -> Result<VersionedSerializable<W>, Error>
where
W: rkyv::Archive,
W::Archived: rkyv::Deserialize<W, rkyv::de::deserializers::SharedDeserializeMap>,
W: rkyv::Archive + 'a,
W::Archived: 'a
+ rkyv::CheckBytes<rkyv::validation::validators::DefaultValidator<'a>>
+ rkyv::Deserialize<W, rkyv::de::deserializers::SharedDeserializeMap>,
{
use anyhow::Context;

let archived = unsafe { rkyv::archived_root::<VersionedSerializable<W>>(&self.field[..]) };

archived
.deserialize(&mut rkyv::de::deserializers::SharedDeserializeMap::new())
.with_context(|| format!("failed to deserialize `{}`", type_name::<W>()))
use rkyv::validation::{validators::CheckDeserializeError, CheckArchiveError};

let result = rkyv::from_bytes::<VersionedSerializable<W>>(&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(),
},
})
}
}

Expand Down
1 change: 0 additions & 1 deletion crates/swc_css_ast/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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::*};

Expand Down
1 change: 1 addition & 0 deletions crates/swc_plugin_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9221639

Please sign in to comment.