1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use crate::*;
#[cfg_attr(feature = "io", derive(Serialize, Deserialize))]
pub(crate) struct ModulePayload {
pub(crate) name: Name,
pub(crate) functions: Vec<Function>,
pub(crate) globals: Vec<Value>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "io", derive(Serialize, Deserialize))]
pub struct Module(pub(crate) generational_arena::Index);
impl Named for Module {
fn get_name(&self, library: &Library) -> Name {
library.modules[self.0].name
}
}
impl Module {
/// Create a new function.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().build();
/// let function_builder = module.create_function(&mut library);
/// ```
pub fn create_function<'a>(&self, library: &'a mut Library) -> FunctionBuilder<'a> {
FunctionBuilder::with_library_and_module(library, *self)
}
/// Create a new global variable.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().build();
/// let global_builder = module.create_global(&mut library);
/// ```
pub fn create_global<'a>(&self, library: &'a mut Library) -> GlobalBuilder<'a> {
GlobalBuilder::with_library_and_module(library, *self)
}
/// Get all the globals in a module.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().with_name("module").build();
/// # let u32_ty = library.get_uint_type(32);
/// let global_a = module.create_global(&mut library).with_name("a").with_type(u32_ty).build();
/// let global_b = module.create_global(&mut library).with_name("b").with_type(u32_ty).build();
/// let mut globals = module.get_globals(&library);
/// assert_eq!(globals.nth(0).unwrap().get_name(&library).get_name(&library), "a");
/// assert_eq!(globals.nth(0).unwrap().get_name(&library).get_name(&library), "b");
/// ```
pub fn get_globals(&self, library: &Library) -> GlobalIterator {
let module = &library.modules[self.0];
GlobalIterator::new(&module.globals)
}
/// Get all the functions in a module.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().with_name("module").build();
/// let function_a = module.create_function(&mut library).with_name("a").build();
/// let function_b = module.create_function(&mut library).with_name("b").build();
/// let mut functions = module.get_functions(&library);
/// assert_eq!(functions.nth(0).unwrap().get_name(&library).get_name(&library), "a");
/// assert_eq!(functions.nth(0).unwrap().get_name(&library).get_name(&library), "b");
/// ```
pub fn get_functions(&self, library: &Library) -> FunctionIterator {
let module = &library.modules[self.0];
FunctionIterator::new(&module.functions)
}
/// Verify a library of modules.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().with_name("module").build();
/// let result = module.verify(&library);
/// # assert!(result.is_ok());
/// ```
pub fn verify<'a>(&self, library: &'a Library) -> Result<(), VerifyError<'a>> {
verify(library, *self)
}
}
pub struct ModuleBuilder<'a> {
library: &'a mut Library,
name: &'a str,
}
impl<'a> ModuleBuilder<'a> {
pub(crate) fn with_library(library: &'a mut Library) -> ModuleBuilder {
ModuleBuilder { library, name: "" }
}
/// Add a name for the module to the builder.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module_builder = library.create_module();
/// module_builder.with_name("my module");
/// ```
pub fn with_name(mut self, name: &'a str) -> Self {
self.name = name;
self
}
/// Finalize and build the module.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module_builder = library.create_module();
/// let module = module_builder.build();
/// ```
pub fn build(self) -> Module {
let module = ModulePayload {
name: self.library.get_name(self.name),
functions: Vec::new(),
globals: Vec::new(),
};
Module(self.library.modules.insert(module))
}
}
pub struct GlobalIterator {
vec: Vec<Value>,
next: usize,
}
impl GlobalIterator {
fn new(iter: &[Value]) -> GlobalIterator {
GlobalIterator {
vec: iter.to_vec(),
next: 0,
}
}
}
impl Iterator for GlobalIterator {
type Item = Value;
fn next(&mut self) -> Option<Self::Item> {
if self.next < self.vec.len() {
let next = self.next;
self.next += 1;
Some(self.vec[next])
} else {
None
}
}
}
pub struct FunctionIterator {
vec: Vec<Function>,
next: usize,
}
impl FunctionIterator {
fn new(iter: &[Function]) -> FunctionIterator {
FunctionIterator {
vec: iter.to_vec(),
next: 0,
}
}
}
impl Iterator for FunctionIterator {
type Item = Function;
fn next(&mut self) -> Option<Self::Item> {
if self.next < self.vec.len() {
let next = self.next;
self.next += 1;
Some(self.vec[next])
} else {
None
}
}
}