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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
use super::{
invoke::{Identity, Invoke},
misc::{ValueTypeBuilder, ValueTypesBuilder},
};
use crate::elements;
use alloc::vec::Vec;
pub enum Signature {
TypeReference(u32),
Inline(elements::FunctionType),
}
pub struct SignatureBuilder<F = Identity> {
callback: F,
signature: elements::FunctionType,
}
impl SignatureBuilder {
pub fn new() -> Self {
SignatureBuilder::with_callback(Identity)
}
}
impl Default for SignatureBuilder {
fn default() -> Self {
Self::new()
}
}
impl<F> SignatureBuilder<F>
where
F: Invoke<elements::FunctionType>,
{
pub fn with_callback(callback: F) -> Self {
SignatureBuilder { callback, signature: elements::FunctionType::default() }
}
pub fn with_param(mut self, value_type: elements::ValueType) -> Self {
self.signature.params_mut().push(value_type);
self
}
pub fn with_params<I>(mut self, value_types: I) -> Self
where
I: IntoIterator<Item = elements::ValueType>,
{
self.signature.params_mut().extend(value_types);
self
}
pub fn param(self) -> ValueTypeBuilder<Self> {
ValueTypeBuilder::with_callback(self)
}
pub fn params(self) -> ValueTypesBuilder<Self> {
ValueTypesBuilder::with_callback(self)
}
pub fn with_result(mut self, value_type: elements::ValueType) -> Self {
self.signature.results_mut().push(value_type);
self
}
pub fn with_results<I>(mut self, value_types: I) -> Self
where
I: IntoIterator<Item = elements::ValueType>,
{
self.signature.results_mut().extend(value_types);
self
}
pub fn result(self) -> ValueTypeBuilder<Self> {
ValueTypeBuilder::with_callback(self)
}
pub fn results(self) -> ValueTypeBuilder<Self> {
ValueTypeBuilder::with_callback(self)
}
pub fn build(self) -> F::Result {
self.callback.invoke(self.signature)
}
pub fn build_sig(self) -> Signature {
Signature::Inline(self.signature)
}
}
impl<F, I> Invoke<I> for SignatureBuilder<F>
where
F: Invoke<elements::FunctionType>,
I: IntoIterator<Item = elements::ValueType>,
{
type Result = Self;
fn invoke(self, args: I) -> Self {
self.with_params(args)
}
}
impl<F> Invoke<elements::ValueType> for SignatureBuilder<F>
where
F: Invoke<elements::FunctionType>,
{
type Result = Self;
fn invoke(self, arg: elements::ValueType) -> Self {
self.with_result(arg)
}
}
pub struct TypeRefBuilder<F = Identity> {
callback: F,
type_ref: u32,
}
impl<F> TypeRefBuilder<F>
where
F: Invoke<u32>,
{
pub fn with_callback(callback: F) -> Self {
TypeRefBuilder { callback, type_ref: 0 }
}
pub fn val(mut self, val: u32) -> Self {
self.type_ref = val;
self
}
pub fn build(self) -> F::Result {
self.callback.invoke(self.type_ref)
}
}
pub struct SignaturesBuilder<F = Identity> {
callback: F,
section: Vec<Signature>,
}
impl SignaturesBuilder {
pub fn new() -> Self {
SignaturesBuilder::with_callback(Identity)
}
}
impl Default for SignaturesBuilder {
fn default() -> Self {
Self::new()
}
}
impl<F> SignaturesBuilder<F> {
pub fn with_callback(callback: F) -> Self {
SignaturesBuilder { callback, section: Vec::new() }
}
pub fn with_signature(mut self, signature: Signature) -> Self {
self.section.push(signature);
self
}
pub fn type_ref(self) -> TypeRefBuilder<Self> {
TypeRefBuilder::with_callback(self)
}
}
impl<F> SignaturesBuilder<F>
where
F: Invoke<SignatureBindings>,
{
pub fn signature(self) -> SignatureBuilder<Self> {
SignatureBuilder::with_callback(self)
}
}
impl<F> Invoke<elements::FunctionType> for SignaturesBuilder<F> {
type Result = Self;
fn invoke(self, signature: elements::FunctionType) -> Self {
self.with_signature(Signature::Inline(signature))
}
}
impl<F> Invoke<u32> for SignaturesBuilder<F> {
type Result = Self;
fn invoke(self, type_ref: u32) -> Self {
self.with_signature(Signature::TypeReference(type_ref))
}
}
impl<F> SignaturesBuilder<F>
where
F: Invoke<elements::FunctionSection>,
{
pub fn build(self) -> F::Result {
let mut result = elements::FunctionSection::default();
for f in self.section.into_iter() {
if let Signature::TypeReference(type_ref) = f {
result.entries_mut().push(elements::Func::new(type_ref));
} else {
unreachable!();
}
}
self.callback.invoke(result)
}
}
pub type SignatureBindings = Vec<Signature>;
impl<F> SignaturesBuilder<F>
where
F: Invoke<SignatureBindings>,
{
pub fn bind(self) -> F::Result {
self.callback.invoke(self.section)
}
}
pub struct FuncBodyBuilder<F = Identity> {
callback: F,
body: elements::FuncBody,
}
impl<F> FuncBodyBuilder<F> {
pub fn with_callback(callback: F) -> Self {
FuncBodyBuilder {
callback,
body: elements::FuncBody::new(Vec::new(), elements::Instructions::empty()),
}
}
}
impl<F> FuncBodyBuilder<F>
where
F: Invoke<elements::FuncBody>,
{
pub fn with_func(mut self, func: elements::FuncBody) -> Self {
self.body = func;
self
}
pub fn with_locals<I>(mut self, locals: I) -> Self
where
I: IntoIterator<Item = elements::Local>,
{
self.body.locals_mut().extend(locals);
self
}
pub fn with_instructions(mut self, instructions: elements::Instructions) -> Self {
*self.body.code_mut() = instructions;
self
}
pub fn build(self) -> F::Result {
self.callback.invoke(self.body)
}
}
pub struct FunctionDefinition {
pub is_main: bool,
pub signature: Signature,
pub code: elements::FuncBody,
}
impl Default for FunctionDefinition {
fn default() -> Self {
FunctionDefinition {
is_main: false,
signature: Signature::TypeReference(0),
code: elements::FuncBody::empty(),
}
}
}
pub struct FunctionBuilder<F = Identity> {
callback: F,
func: FunctionDefinition,
}
impl FunctionBuilder {
pub fn new() -> Self {
FunctionBuilder::with_callback(Identity)
}
}
impl Default for FunctionBuilder {
fn default() -> Self {
Self::new()
}
}
impl<F> FunctionBuilder<F>
where
F: Invoke<FunctionDefinition>,
{
pub fn with_callback(callback: F) -> Self {
FunctionBuilder { callback, func: Default::default() }
}
pub fn main(mut self) -> Self {
self.func.is_main = true;
self
}
pub fn signature(self) -> SignatureBuilder<Self> {
SignatureBuilder::with_callback(self)
}
pub fn with_signature(mut self, signature: Signature) -> Self {
self.func.signature = signature;
self
}
pub fn body(self) -> FuncBodyBuilder<Self> {
FuncBodyBuilder::with_callback(self)
}
pub fn with_body(mut self, body: elements::FuncBody) -> Self {
self.func.code = body;
self
}
pub fn build(self) -> F::Result {
self.callback.invoke(self.func)
}
}
impl<F> Invoke<elements::FunctionType> for FunctionBuilder<F>
where
F: Invoke<FunctionDefinition>,
{
type Result = Self;
fn invoke(self, signature: elements::FunctionType) -> Self {
self.with_signature(Signature::Inline(signature))
}
}
impl<F> Invoke<u32> for FunctionBuilder<F>
where
F: Invoke<FunctionDefinition>,
{
type Result = Self;
fn invoke(self, type_ref: u32) -> Self {
self.with_signature(Signature::TypeReference(type_ref))
}
}
impl<F> Invoke<elements::FuncBody> for FunctionBuilder<F>
where
F: Invoke<FunctionDefinition>,
{
type Result = Self;
fn invoke(self, body: elements::FuncBody) -> Self::Result {
self.with_body(body)
}
}
pub fn signatures() -> SignaturesBuilder {
SignaturesBuilder::new()
}
pub fn signature() -> SignatureBuilder {
SignatureBuilder::new()
}
pub fn function() -> FunctionBuilder {
FunctionBuilder::new()
}
#[cfg(test)]
mod tests {
use super::{function, signatures};
use crate::elements;
#[test]
fn example() {
let result = signatures().type_ref().val(1).build().build();
assert_eq!(result.entries().len(), 1);
let result = signatures()
.signature()
.param()
.i32()
.param()
.i32()
.result()
.i64()
.build()
.bind();
assert_eq!(result.len(), 1);
}
#[test]
fn func_example() {
let func = function()
.signature()
.param()
.i32()
.result()
.i32()
.build()
.body()
.with_instructions(elements::Instructions::empty())
.build()
.build();
assert_eq!(func.code.locals().len(), 0);
assert_eq!(func.code.code().elements().len(), 1);
}
#[test]
fn func_example_multi_result() {
let func = function()
.signature()
.param()
.i32()
.result()
.i32()
.result()
.i32()
.build()
.body()
.with_instructions(elements::Instructions::empty())
.build()
.build();
assert_eq!(func.code.locals().len(), 0);
assert_eq!(func.code.code().elements().len(), 1);
}
}