#![allow(missing_docs)]
use super::relocation::{ArchivedRelocation, Relocation, RelocationLike};
use crate::lib::std::vec::Vec;
use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use wasmer_types::entity_impl;
#[derive(
RkyvSerialize,
RkyvDeserialize,
Archive,
Copy,
Clone,
PartialEq,
Eq,
Hash,
PartialOrd,
Ord,
Debug,
Default,
)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
#[rkyv(derive(Debug), compare(PartialEq, PartialOrd))]
pub struct SectionIndex(u32);
entity_impl!(SectionIndex);
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq)]
#[rkyv(derive(Debug), compare(PartialEq, PartialOrd))]
#[repr(u8)]
pub enum CustomSectionProtection {
Read,
ReadExecute,
}
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq)]
#[rkyv(derive(Debug), compare(PartialEq))]
pub struct CustomSection {
pub protection: CustomSectionProtection,
pub bytes: SectionBody,
pub relocations: Vec<Relocation>,
}
#[allow(missing_docs)]
pub trait CustomSectionLike<'a> {
type Relocations: RelocationLike;
fn protection(&self) -> CustomSectionProtection;
fn bytes(&self) -> &[u8];
fn relocations(&'a self) -> &[Self::Relocations];
}
impl<'a> CustomSectionLike<'a> for CustomSection {
type Relocations = Relocation;
fn protection(&self) -> CustomSectionProtection {
self.protection.clone()
}
fn bytes(&self) -> &[u8] {
self.bytes.0.as_ref()
}
fn relocations(&'a self) -> &[Self::Relocations] {
self.relocations.as_slice()
}
}
impl<'a> CustomSectionLike<'a> for ArchivedCustomSection {
type Relocations = ArchivedRelocation;
fn protection(&self) -> CustomSectionProtection {
let protection = rkyv::deserialize::<CustomSectionProtection, ()>(&self.protection);
protection.unwrap()
}
fn bytes(&self) -> &[u8] {
self.bytes.0.as_ref()
}
fn relocations(&'a self) -> &[Self::Relocations] {
self.relocations.as_slice()
}
}
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq, Default)]
#[rkyv(derive(Debug), compare(PartialEq, PartialOrd))]
pub struct SectionBody(#[cfg_attr(feature = "enable-serde", serde(with = "serde_bytes"))] Vec<u8>);
impl SectionBody {
pub fn new_with_vec(contents: Vec<u8>) -> Self {
Self(contents)
}
pub fn as_ptr(&self) -> *const u8 {
self.0.as_ptr()
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn as_slice(&self) -> &[u8] {
self.0.as_slice()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl ArchivedSectionBody {
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}