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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use super::invoke::{Invoke, Identity};
use super::code::{self, SignaturesBuilder, FunctionBuilder};
use super::import;
use elements;
pub struct ModuleBuilder<F=Identity> {
callback: F,
module: ModuleScaffold,
}
pub struct CodeLocation {
pub signature: u32,
pub body: u32,
}
#[derive(Default)]
struct ModuleScaffold {
pub types: elements::TypeSection,
pub import: elements::ImportSection,
pub functions: elements::FunctionsSection,
pub table: elements::TableSection,
pub memory: elements::MemorySection,
pub global: elements::GlobalSection,
pub export: elements::ExportSection,
pub start: Option<u32>,
pub element: elements::ElementSection,
pub code: elements::CodeSection,
pub data: elements::DataSection,
pub other: Vec<elements::Section>,
}
impl From<elements::Module> for ModuleScaffold {
fn from(module: elements::Module) -> Self {
let mut types: Option<elements::TypeSection> = None;
let mut import: Option<elements::ImportSection> = None;
let mut funcs: Option<elements::FunctionsSection> = None;
let mut table: Option<elements::TableSection> = None;
let mut memory: Option<elements::MemorySection> = None;
let mut global: Option<elements::GlobalSection> = None;
let mut export: Option<elements::ExportSection> = None;
let mut start: Option<u32> = None;
let mut element: Option<elements::ElementSection> = None;
let mut code: Option<elements::CodeSection> = None;
let mut data: Option<elements::DataSection> = None;
let mut sections = module.into_sections();
while let Some(section) = sections.pop() {
match section {
elements::Section::Type(sect) => { types = Some(sect); }
elements::Section::Import(sect) => { import = Some(sect); }
elements::Section::Function(sect) => { funcs = Some(sect); }
elements::Section::Table(sect) => { table = Some(sect); }
elements::Section::Memory(sect) => { memory = Some(sect); }
elements::Section::Global(sect) => { global = Some(sect); }
elements::Section::Export(sect) => { export = Some(sect); }
elements::Section::Start(index) => { start = Some(index); }
elements::Section::Element(sect) => { element = Some(sect); }
elements::Section::Code(sect) => { code = Some(sect); }
elements::Section::Data(sect) => { data = Some(sect); }
_ => {}
}
}
ModuleScaffold {
types: types.unwrap_or_default(),
import: import.unwrap_or_default(),
functions: funcs.unwrap_or_default(),
table: table.unwrap_or_default(),
memory: memory.unwrap_or_default(),
global: global.unwrap_or_default(),
export: export.unwrap_or_default(),
start: start,
element: element.unwrap_or_default(),
code: code.unwrap_or_default(),
data: data.unwrap_or_default(),
other: sections,
}
}
}
impl From<ModuleScaffold> for elements::Module {
fn from(module: ModuleScaffold) -> Self {
let mut sections = Vec::new();
let types = module.types;
if types.types().len() > 0 {
sections.push(elements::Section::Type(types));
}
let import = module.import;
if import.entries().len() > 0 {
sections.push(elements::Section::Import(import));
}
let functions = module.functions;
if functions.entries().len() > 0 {
sections.push(elements::Section::Function(functions));
}
let table = module.table;
if table.entries().len() > 0 {
sections.push(elements::Section::Table(table));
}
let memory = module.memory;
if memory.entries().len() > 0 {
sections.push(elements::Section::Memory(memory));
}
let global = module.global;
if global.entries().len() > 0 {
sections.push(elements::Section::Global(global));
}
let export = module.export;
if export.entries().len() > 0 {
sections.push(elements::Section::Export(export));
}
if let Some(start) = module.start {
sections.push(elements::Section::Start(start));
}
let element = module.element;
if element.entries().len() > 0 {
sections.push(elements::Section::Element(element));
}
let code = module.code;
if code.bodies().len() > 0 {
sections.push(elements::Section::Code(code));
}
let data = module.data;
if data.entries().len() > 0 {
sections.push(elements::Section::Data(data));
}
sections.extend(module.other);
elements::Module::new(sections)
}
}
impl ModuleBuilder {
pub fn new() -> Self {
ModuleBuilder::with_callback(Identity)
}
}
impl<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
pub fn with_callback(callback: F) -> Self {
ModuleBuilder {
callback: callback,
module: Default::default(),
}
}
pub fn with_module(mut self, module: elements::Module) -> Self {
self.module = module.into();
self
}
pub fn with_sections<I>(mut self, sections: I) -> Self
where I: IntoIterator<Item=elements::Section>
{
self.module.other.extend(sections);
self
}
pub fn with_section(mut self, section: elements::Section) -> Self {
self.module.other.push(section);
self
}
pub fn with_signatures(mut self, bindings: code::SignatureBindings) -> Self {
self.push_signatures(bindings);
self
}
pub fn push_function(&mut self, func: code::FunctionDefinition) -> CodeLocation {
let signature = func.signature;
let body = func.code;
let type_ref = self.resolve_type_ref(signature);
self.module.functions.entries_mut().push(elements::Func::new(type_ref));
let signature_index = self.module.functions.entries_mut().len() as u32 - 1;
self.module.code.bodies_mut().push(body);
let body_index = self.module.code.bodies_mut().len() as u32 - 1;
CodeLocation {
signature: signature_index,
body: body_index,
}
}
fn resolve_type_ref(&mut self, signature: code::Signature) -> u32 {
match signature {
code::Signature::Inline(func_type) => {
self.module.types.types_mut().push(elements::Type::Function(func_type));
self.module.types.types().len() as u32 - 1
}
code::Signature::TypeReference(type_ref) => {
type_ref
}
}
}
pub fn push_signature(&mut self, signature: code::Signature) -> u32 {
self.resolve_type_ref(signature)
}
pub fn push_signatures(&mut self, signatures: code::SignatureBindings) -> Vec<u32> {
signatures.into_iter().map(|binding|
self.resolve_type_ref(binding)
).collect()
}
pub fn push_import(&mut self, import: elements::ImportEntry) -> u32 {
self.module.import.entries_mut().push(import);
self.module.import.entries_mut().len() as u32 - 1
}
pub fn function(self) -> FunctionBuilder<Self> {
FunctionBuilder::with_callback(self)
}
pub fn functions(self) -> SignaturesBuilder<Self> {
SignaturesBuilder::with_callback(self)
}
pub fn with_import(mut self, entry: elements::ImportEntry) -> Self {
self.module.import.entries_mut().push(entry);
self
}
pub fn import(self) -> import::ImportBuilder<Self> {
import::ImportBuilder::with_callback(self)
}
pub fn build(self) -> F::Result {
self.callback.invoke(self.module.into())
}
}
impl<F> Invoke<elements::FunctionsSection> for ModuleBuilder<F>
where F: Invoke<elements::Module>
{
type Result = Self;
fn invoke(self, section: elements::FunctionsSection) -> Self {
self.with_section(elements::Section::Function(section))
}
}
impl<F> Invoke<code::SignatureBindings> for ModuleBuilder<F>
where F: Invoke<elements::Module>
{
type Result = Self;
fn invoke(self, bindings: code::SignatureBindings) -> Self {
self.with_signatures(bindings)
}
}
impl<F> Invoke<code::FunctionDefinition> for ModuleBuilder<F>
where F: Invoke<elements::Module>
{
type Result = Self;
fn invoke(self, def: code::FunctionDefinition) -> Self {
let mut b = self;
b.push_function(def);
b
}
}
impl<F> Invoke<elements::ImportEntry> for ModuleBuilder<F>
where F: Invoke<elements::Module>
{
type Result = Self;
fn invoke(self, entry: elements::ImportEntry) -> Self::Result {
self.with_import(entry)
}
}
pub fn module() -> ModuleBuilder {
ModuleBuilder::new()
}
pub fn from_module(module: elements::Module) -> ModuleBuilder {
ModuleBuilder::new().with_module(module)
}
#[cfg(test)]
mod tests {
use super::module;
#[test]
fn smoky() {
let module = module().build();
assert_eq!(module.sections().len(), 0);
}
#[test]
fn functions() {
let module = module()
.function()
.signature().param().i32().build()
.body().build()
.build()
.build();
assert_eq!(module.type_section().expect("type section to exist").types().len(), 1);
assert_eq!(module.functions_section().expect("function section to exist").entries().len(), 1);
assert_eq!(module.code_section().expect("code section to exist").bodies().len(), 1);
}
}