wasmer_types/
initializers.rs

1/*
2 * ! Remove me once rkyv generates doc-comments for fields or generates an #[allow(missing_docs)]
3 * on their own.
4 */
5#![allow(missing_docs)]
6
7use crate::indexes::{FunctionIndex, MemoryIndex, TableIndex};
8use crate::lib::std::boxed::Box;
9use crate::types::InitExpr;
10
11use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
12#[cfg(feature = "enable-serde")]
13use serde::{Deserialize, Serialize};
14
15/// A WebAssembly table initializer.
16#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
17#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
18#[derive(Clone, Debug, Hash, PartialEq, Eq, RkyvSerialize, RkyvDeserialize, Archive)]
19#[rkyv(derive(Debug))]
20pub struct TableInitializer {
21    /// The index of a table to initialize.
22    pub table_index: TableIndex,
23    /// Serialized offset expression.
24    pub offset_expr: InitExpr,
25    /// The values to write into the table elements.
26    pub elements: Box<[FunctionIndex]>,
27}
28
29/// A memory index and offset within that memory where a data initialization
30/// should be performed.
31#[derive(Clone, Debug, PartialEq, Eq, RkyvSerialize, RkyvDeserialize, Archive)]
32#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
33#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
34#[rkyv(derive(Debug))]
35pub struct DataInitializerLocation {
36    /// The index of the memory to initialize.
37    pub memory_index: MemoryIndex,
38
39    /// Serialized offset expression.
40    pub offset_expr: InitExpr,
41}
42
43/// Any struct that acts like a `DataInitializerLocation`.
44#[allow(missing_docs)]
45pub trait DataInitializerLocationLike {
46    fn memory_index(&self) -> MemoryIndex;
47    fn offset_expr(&self) -> InitExpr;
48}
49
50impl DataInitializerLocationLike for &DataInitializerLocation {
51    fn memory_index(&self) -> MemoryIndex {
52        self.memory_index
53    }
54
55    fn offset_expr(&self) -> InitExpr {
56        self.offset_expr.clone()
57    }
58}
59
60impl DataInitializerLocationLike for &ArchivedDataInitializerLocation {
61    fn memory_index(&self) -> MemoryIndex {
62        MemoryIndex::from_u32(rkyv::deserialize::<_, ()>(&self.memory_index).unwrap().0)
63    }
64
65    fn offset_expr(&self) -> InitExpr {
66        rkyv::deserialize::<_, rkyv::rancor::Error>(&self.offset_expr).unwrap()
67    }
68}
69
70/// A data initializer for linear memory.
71#[derive(Debug)]
72#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
73pub struct DataInitializer<'data> {
74    /// The location where the initialization is to be performed.
75    pub location: DataInitializerLocation,
76
77    /// The initialization data.
78    pub data: &'data [u8],
79}
80
81/// As `DataInitializer` but owning the data rather than
82/// holding a reference to it
83#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
84#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
85#[derive(Debug, Clone, PartialEq, Eq, RkyvSerialize, RkyvDeserialize, Archive)]
86#[rkyv(derive(Debug))]
87pub struct OwnedDataInitializer {
88    /// The location where the initialization is to be performed.
89    pub location: DataInitializerLocation,
90
91    /// The initialization owned data.
92    pub data: Box<[u8]>,
93}
94
95/// Any struct that acts like a `DataInitializer`.
96#[allow(missing_docs)]
97pub trait DataInitializerLike<'a> {
98    type Location: DataInitializerLocationLike + Copy + 'a;
99
100    fn location(&self) -> Self::Location;
101    fn data(&self) -> &'a [u8];
102}
103
104impl OwnedDataInitializer {
105    /// Creates a new `OwnedDataInitializer` from a `DataInitializer`.
106    pub fn new(borrowed: &DataInitializer<'_>) -> Self {
107        Self {
108            location: borrowed.location.clone(),
109            data: borrowed.data.to_vec().into_boxed_slice(),
110        }
111    }
112}
113
114impl<'a> DataInitializerLike<'a> for &'a OwnedDataInitializer {
115    type Location = &'a DataInitializerLocation;
116
117    fn location(&self) -> Self::Location {
118        &self.location
119    }
120
121    fn data(&self) -> &'a [u8] {
122        self.data.as_ref()
123    }
124}
125
126impl<'a> DataInitializerLike<'a> for &'a ArchivedOwnedDataInitializer {
127    type Location = &'a ArchivedDataInitializerLocation;
128
129    fn location(&self) -> Self::Location {
130        &self.location
131    }
132
133    fn data(&self) -> &'a [u8] {
134        self.data.as_ref()
135    }
136}