wasmer_types/
initializers.rs1use crate::indexes::{FunctionIndex, GlobalIndex, MemoryIndex, TableIndex};
2use crate::lib::std::boxed::Box;
3
4use enumset::__internal::EnumSetTypeRepr;
5use rkyv::{Archive, CheckBytes, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
6#[cfg(feature = "enable-serde")]
7use serde::{Deserialize, Serialize};
8
9#[derive(Clone, Debug, Hash, PartialEq, Eq, RkyvSerialize, RkyvDeserialize, Archive)]
11#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
12#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
13#[archive_attr(derive(CheckBytes, Debug))]
14pub struct TableInitializer {
15 pub table_index: TableIndex,
17 pub base: Option<GlobalIndex>,
19 pub offset: usize,
21 pub elements: Box<[FunctionIndex]>,
23}
24
25#[derive(Clone, Debug, PartialEq, Eq, RkyvSerialize, RkyvDeserialize, Archive)]
28#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
29#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
30#[archive_attr(derive(CheckBytes, Debug))]
31pub struct DataInitializerLocation {
32 pub memory_index: MemoryIndex,
34
35 pub base: Option<GlobalIndex>,
37
38 pub offset: usize,
40}
41
42#[allow(missing_docs)]
44pub trait DataInitializerLocationLike {
45 fn memory_index(&self) -> MemoryIndex;
46 fn base(&self) -> Option<GlobalIndex>;
47 fn offset(&self) -> usize;
48}
49
50impl DataInitializerLocationLike for &DataInitializerLocation {
51 fn memory_index(&self) -> MemoryIndex {
52 self.memory_index
53 }
54
55 fn base(&self) -> Option<GlobalIndex> {
56 self.base
57 }
58
59 fn offset(&self) -> usize {
60 self.offset
61 }
62}
63
64impl DataInitializerLocationLike for &ArchivedDataInitializerLocation {
65 fn memory_index(&self) -> MemoryIndex {
66 MemoryIndex::from_u32(self.memory_index.as_u32())
67 }
68
69 fn base(&self) -> Option<GlobalIndex> {
70 match self.base {
71 rkyv::option::ArchivedOption::None => None,
72 rkyv::option::ArchivedOption::Some(base) => Some(GlobalIndex::from_u32(base.as_u32())),
73 }
74 }
75
76 fn offset(&self) -> usize {
77 self.offset.to_usize()
78 }
79}
80
81#[derive(Debug)]
83#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
84pub struct DataInitializer<'data> {
85 pub location: DataInitializerLocation,
87
88 pub data: &'data [u8],
90}
91
92#[derive(Debug, Clone, PartialEq, Eq, RkyvSerialize, RkyvDeserialize, Archive)]
95#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
96#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
97#[archive_attr(derive(CheckBytes, Debug))]
98pub struct OwnedDataInitializer {
99 pub location: DataInitializerLocation,
101
102 pub data: Box<[u8]>,
104}
105
106#[allow(missing_docs)]
108pub trait DataInitializerLike<'a> {
109 type Location: DataInitializerLocationLike + Copy + 'a;
110
111 fn location(&self) -> Self::Location;
112 fn data(&self) -> &'a [u8];
113}
114
115impl OwnedDataInitializer {
116 pub fn new(borrowed: &DataInitializer<'_>) -> Self {
118 Self {
119 location: borrowed.location.clone(),
120 data: borrowed.data.to_vec().into_boxed_slice(),
121 }
122 }
123}
124
125impl<'a> DataInitializerLike<'a> for &'a OwnedDataInitializer {
126 type Location = &'a DataInitializerLocation;
127
128 fn location(&self) -> Self::Location {
129 &self.location
130 }
131
132 fn data(&self) -> &'a [u8] {
133 self.data.as_ref()
134 }
135}
136
137impl<'a> DataInitializerLike<'a> for &'a ArchivedOwnedDataInitializer {
138 type Location = &'a ArchivedDataInitializerLocation;
139
140 fn location(&self) -> Self::Location {
141 &self.location
142 }
143
144 fn data(&self) -> &'a [u8] {
145 self.data.as_ref()
146 }
147}