1#[cfg(not(feature = "std"))]
2use crate::lib::*;
3
4#[cfg(feature = "std")]
5use std::vec::IntoIter;
6
7use super::{
8 instr::Expr,
9 types::{FuncType, GlobalType, Limits, RefType, ValType},
10};
11
12pub type TypeIdx = u32;
13pub type FuncIdx = u32;
14pub type TableIdx = u32;
15pub type MemIdx = u32;
16pub type GlobalIdx = u32;
17pub type ElemIdx = u32;
18pub type DataIdx = u32;
19pub type LocalIdx = u32;
20pub type LabelIdx = u32;
21
22#[derive(Debug, PartialEq, Clone)]
23pub struct Func {
24 pub typeidx: TypeIdx,
25 pub locals: Vec<ValType>,
26 pub body: Expr,
27}
28
29#[derive(Debug, PartialEq, Clone)]
30pub struct Global {
31 pub type_: GlobalType,
32 pub value: Expr,
33}
34
35#[derive(Debug, PartialEq, Eq, Clone)]
36pub struct Import {
37 pub module: String,
38 pub name: String,
39 pub desc: ImportDesc,
40}
41
42#[derive(Debug, PartialEq, Eq, Clone)]
43pub struct Export {
44 pub name: String,
45 pub desc: ExportDesc,
46}
47
48#[derive(Debug, PartialEq, Clone)]
49pub struct Elem {
50 pub type_: RefType,
51 pub init: Vec<Expr>,
52 pub mode: ElemMode,
53}
54
55#[derive(Debug, PartialEq, Clone)]
56pub enum ElemMode {
57 Passiv,
58 Active { tableidx: TableIdx, offset: Expr },
59 Declarative,
60}
61
62#[derive(Debug, PartialEq, Eq, Clone)]
63pub enum ImportDesc {
64 Func(u32),
65 Table(Table),
66 Mem(Memory),
67 Global(GlobalType),
68}
69
70#[derive(Debug, PartialEq, Eq, Clone)]
71pub enum ExportDesc {
72 Func(FuncIdx),
73 Table(TableIdx),
74 Mem(MemIdx),
75 Global(GlobalIdx),
76}
77
78#[derive(Debug, PartialEq)]
79pub struct Code {
80 pub size: u32,
81 pub func: Func0,
82}
83
84#[derive(Debug, PartialEq, Eq)]
85pub struct Local {
86 pub n: u32,
87 pub type_: ValType,
88}
89
90#[derive(Debug, PartialEq)]
91pub struct Func0 {
92 pub locals: Vec<Local>,
93 pub body: Expr,
94}
95
96#[derive(Debug, PartialEq, Clone)]
97pub struct Data {
98 pub init: Vec<u8>,
99 pub mode: DataMode,
100}
101
102#[derive(Debug, PartialEq, Clone)]
103pub enum DataMode {
104 Passive,
105 Active { memidx: MemIdx, offset: Expr },
106}
107
108#[derive(Debug, PartialEq, Eq, Clone)]
109pub struct Table {
110 pub reftype: RefType,
111 pub limits: Limits,
112}
113
114#[derive(Debug, PartialEq, Eq, Clone)]
115pub struct Memory(pub Limits);
116
117#[derive(Debug, PartialEq, Eq, Clone)]
118pub struct Custom {
119 pub name: String,
120 pub bytes: Vec<u8>,
121}
122
123#[derive(Debug, PartialEq, Clone)]
124pub struct Section<T> {
125 pub size: u32,
126 pub value: T,
127}
128
129impl<T> IntoIterator for Section<Vec<T>> {
130 type Item = T;
131
132 type IntoIter = IntoIter<Self::Item>;
133
134 fn into_iter(self) -> Self::IntoIter {
135 self.value.into_iter()
136 }
137}
138
139pub type TypeSec = Section<Vec<FuncType>>;
140
141pub type ImportSec = Section<Vec<Import>>;
142
143pub type FuncSec = Section<Vec<TypeIdx>>;
144
145pub type TableSec = Section<Vec<Table>>;
146
147pub type MemSec = Section<Vec<Memory>>;
148
149pub type GlobalSec = Section<Vec<Global>>;
150
151pub type ExportSec = Section<Vec<Export>>;
152
153pub type StartSec = Section<FuncIdx>;
154
155pub type ElemSec = Section<Vec<Elem>>;
156
157pub type CodeSec = Section<Vec<Code>>;
158
159pub type DataSec = Section<Vec<Data>>;
160
161pub type DataCountSec = Section<u32>;
162
163pub type CustomSec = Section<Custom>;
164
165#[derive(Debug, PartialEq)]
166pub struct CustomSecList {
167 pub sec1: Vec<Custom>,
168 pub sec2: Vec<Custom>,
169 pub sec3: Vec<Custom>,
170 pub sec4: Vec<Custom>,
171 pub sec5: Vec<Custom>,
172 pub sec6: Vec<Custom>,
173 pub sec7: Vec<Custom>,
174 pub sec8: Vec<Custom>,
175 pub sec9: Vec<Custom>,
176 pub sec10: Vec<Custom>,
177 pub sec11: Vec<Custom>,
178 pub sec12: Vec<Custom>,
179 pub sec13: Vec<Custom>,
180}
181
182#[derive(Debug, PartialEq, Clone)]
183pub struct Module {
184 pub version: u8,
185 pub types: Vec<FuncType>,
186 pub funcs: Vec<Func>,
187 pub tables: Vec<Table>,
188 pub mems: Vec<Memory>,
189 pub globals: Vec<Global>,
190 pub elems: Vec<Elem>,
191 pub datas: Vec<Data>,
192 pub start: Option<FuncIdx>,
193 pub imports: Vec<Import>,
194 pub exports: Vec<Export>,
195}