diff --git a/crates/swc_common/src/syntax_pos.rs b/crates/swc_common/src/syntax_pos.rs index 39dcf55c2aa2..775801839f3a 100644 --- a/crates/swc_common/src/syntax_pos.rs +++ b/crates/swc_common/src/syntax_pos.rs @@ -816,6 +816,55 @@ impl Sub for NonNarrowChar { } } +/// This is not a public interface, workaround for https://github.com/swc-project/swc/issues/7238 +#[doc(hidden)] +#[cfg(feature = "rkyv-impl")] +#[derive(Debug, Clone, Copy)] +pub struct EncodeArcString; + +#[cfg(feature = "rkyv-impl")] +impl rkyv::with::ArchiveWith> for EncodeArcString { + type Archived = rkyv::Archived; + type Resolver = rkyv::Resolver; + + unsafe fn resolve_with( + field: &Lrc, + pos: usize, + resolver: Self::Resolver, + out: *mut Self::Archived, + ) { + let s = field.to_string(); + rkyv::Archive::resolve(&s, pos, resolver, out); + } +} + +#[cfg(feature = "rkyv-impl")] +impl rkyv::with::SerializeWith, S> for EncodeArcString +where + S: ?Sized + rkyv::ser::Serializer, +{ + fn serialize_with(field: &Lrc, serializer: &mut S) -> Result { + rkyv::string::ArchivedString::serialize_from_str(field, serializer) + } +} + +#[cfg(feature = "rkyv-impl")] +impl rkyv::with::DeserializeWith, Lrc, D> for EncodeArcString +where + D: ?Sized + rkyv::Fallible, +{ + fn deserialize_with( + field: &rkyv::Archived, + deserializer: &mut D, + ) -> Result, D::Error> { + use rkyv::Deserialize; + + let s: String = field.deserialize(deserializer)?; + + Ok(s.into()) + } +} + /// A single source in the SourceMap. #[cfg_attr( any(feature = "rkyv-impl"), @@ -836,6 +885,7 @@ pub struct SourceFile { /// Indicates which crate this `SourceFile` was imported from. pub crate_of_origin: u32, /// The complete source code + #[cfg_attr(any(feature = "rkyv-impl"), with(EncodeArcString))] pub src: Lrc, /// The source code's hash pub src_hash: u128, 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 56ace2fbd89d..0d52373ef642 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 @@ -9,6 +9,19 @@ use swc_common::plugin::serialized::{deserialize_from_ptr, PluginSerializedBytes )] pub struct AllocatedBytesPtr(pub u32, pub u32); +#[cfg(target_arch = "wasm32")] +extern "C" { + fn __free(ptr: *mut u8, size: i32) -> i32; +} +#[cfg(target_arch = "wasm32")] +impl Drop for AllocatedBytesPtr { + fn drop(&mut self) { + unsafe { + __free(self.0 as _, self.1 as _); + } + } +} + #[cfg(not(feature = "__rkyv"))] fn read_returned_result_from_host_inner(f: F) -> Option { unimplemented!("Plugin proxy does not work without serialization support")