wasm-ast 0.1.0

A WebAssembly syntax model useful for generate, reading, and emitting WebAssembly code.
Documentation
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! Model for types in the WebAssembly syntax.

/// Number types classify numeric values.
/// Number types are transparent, meaning that their bit patterns can be observed.
/// Values of number type can be stored in memories.
///
/// See <https://webassembly.github.io/spec/core/syntax/types.html#number-types>
///
/// # Examples
/// ```rust
/// use wasm_ast::{ValueType, NumberType};
///
/// assert_eq!(ValueType::I32, NumberType::I32.into());
/// assert_eq!(ValueType::I64, NumberType::I64.into());
/// assert_eq!(ValueType::F32, NumberType::F32.into());
/// assert_eq!(ValueType::F64, NumberType::F64.into());
/// ```
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum NumberType {
    I32,
    I64,
    F32,
    F64,
}

impl From<IntegerType> for NumberType {
    fn from(kind: IntegerType) -> Self {
        match kind {
            IntegerType::I32 => NumberType::I32,
            IntegerType::I64 => NumberType::I64,
        }
    }
}

impl From<FloatType> for NumberType {
    fn from(kind: FloatType) -> Self {
        match kind {
            FloatType::F32 => NumberType::F32,
            FloatType::F64 => NumberType::F64,
        }
    }
}

/// The types 𝗂πŸ₯𝟀 and π—‚πŸ¨πŸ¦ classify 32 and 64 bit integers, respectively.
/// Integers are not inherently signed or unsigned, their interpretation is determined by individual operations.
///
/// # Examples
/// ```rust
/// use wasm_ast::{ValueType, NumberType, IntegerType};
///
/// assert_eq!(ValueType::I32, IntegerType::I32.into());
/// assert_eq!(NumberType::I32, IntegerType::I32.into());
/// assert_eq!(ValueType::I64, IntegerType::I64.into());
/// assert_eq!(NumberType::I64, IntegerType::I64.into());
/// ```
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum IntegerType {
    I32,
    I64,
}

/// The types 𝖿πŸ₯𝟀 and π–ΏπŸ¨πŸ¦ classify 32 and 64 bit floating-point data, respectively.
/// They correspond to the respective binary floating-point representations,
/// also known as single and double precision, as defined by the IEEE 754-2019 standard (Section 3.3).
///
/// # Examples
/// ```rust
/// use wasm_ast::{ValueType, NumberType, FloatType};
///
/// assert_eq!(ValueType::F32, FloatType::F32.into());
/// assert_eq!(NumberType::F32, FloatType::F32.into());
/// assert_eq!(ValueType::F64, FloatType::F64.into());
/// assert_eq!(NumberType::F64, FloatType::F64.into());
/// ```
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum FloatType {
    F32,
    F64,
}

/// Reference types classify first-class references to objects in the runtime store.
/// The type π–Ώπ—Žπ—‡π–Όπ—‹π–Ύπ–Ώ denotes the infinite union of all references to functions,
/// regardless of their function types.
/// The type 𝖾𝗑𝗍𝖾𝗋𝗇𝗋𝖾𝖿 denotes the infinite union of all references to objects owned by the
/// embedder and that can be passed into WebAssembly under this type.
/// Reference types are opaque, meaning that neither their size nor their bit pattern can be observed.
/// Values of reference type can be stored in tables.
///
/// See <https://webassembly.github.io/spec/core/syntax/types.html#reference-types>
///
/// # Examples
/// ```rust
/// use wasm_ast::{ValueType, ReferenceType};
///
/// assert_eq!(ValueType::FunctionReference, ReferenceType::Function.into());
/// assert_eq!(ValueType::ExternalReference, ReferenceType::External.into());
/// ```
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ReferenceType {
    Function,
    External,
}

/// Value types classify the individual values that WebAssembly code can compute with and the values that a variable accepts.
/// They are either number types or reference types.
///
/// See <https://webassembly.github.io/spec/core/syntax/types.html#value-types>
///
/// # Examples
/// ```rust
/// use wasm_ast::{ValueType, ReferenceType, IntegerType, FloatType, NumberType};
///
/// assert_eq!(ValueType::I32, IntegerType::I32.into());
/// assert_eq!(ValueType::I32, NumberType::I32.into());
/// assert_eq!(ValueType::I64, IntegerType::I64.into());
/// assert_eq!(ValueType::I64, NumberType::I64.into());
/// assert_eq!(ValueType::F32, FloatType::F32.into());
/// assert_eq!(ValueType::F32, NumberType::F32.into());
/// assert_eq!(ValueType::F64, FloatType::F64.into());
/// assert_eq!(ValueType::F64, NumberType::F64.into());
/// assert_eq!(ValueType::FunctionReference, ReferenceType::Function.into());
/// assert_eq!(ValueType::ExternalReference, ReferenceType::External.into());
/// ```
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ValueType {
    I32,
    I64,
    F32,
    F64,
    FunctionReference,
    ExternalReference,
}

impl<T> From<T> for ValueType
where
    T: Into<NumberType>,
{
    fn from(kind: T) -> Self {
        match kind.into() {
            NumberType::I32 => ValueType::I32,
            NumberType::I64 => ValueType::I64,
            NumberType::F32 => ValueType::F32,
            NumberType::F64 => ValueType::F64,
        }
    }
}

impl From<ReferenceType> for ValueType {
    fn from(kind: ReferenceType) -> Self {
        match kind {
            ReferenceType::Function => ValueType::FunctionReference,
            ReferenceType::External => ValueType::ExternalReference,
        }
    }
}

/// Result types classify the result of executing instructions or functions,
/// which is a sequence of values, written with brackets.
///
/// See <https://webassembly.github.io/spec/core/syntax/types.html#result-types>
///
/// # Examples
///
/// ## Empty
/// ```rust
/// use wasm_ast::ResultType;
///
/// let result_type = ResultType::empty();
///
/// assert_eq!(result_type.len(), 0);
/// assert!(result_type.is_empty());
/// assert_eq!(result_type.kinds(), &[]);
/// ```
///
/// ## Non-Empty
/// ```rust
/// use wasm_ast::{ResultType, IntegerType, FloatType, ReferenceType, ValueType};
///
/// let result_type = ResultType::new(vec![
///     IntegerType::I32.into(),
///     IntegerType::I64.into(),
///     FloatType::F32.into(),
///     FloatType::F64.into(),
///     ReferenceType::Function.into(),
///     ReferenceType::External.into(),
/// ]);
///
/// assert_eq!(result_type.len(), 6);
/// assert!(!result_type.is_empty());
/// assert_eq!(
///     result_type.kinds(),
///     &[
///         ValueType::I32,
///         ValueType::I64,
///         ValueType::F32,
///         ValueType::F64,
///         ValueType::FunctionReference,
///         ValueType::ExternalReference,
///     ]
/// );
/// assert_eq!(
///     result_type,
///     vec![
///         ValueType::I32,
///         ValueType::I64,
///         ValueType::F32,
///         ValueType::F64,
///         ValueType::FunctionReference,
///         ValueType::ExternalReference,
///     ].into()
/// );
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ResultType {
    kinds: Vec<ValueType>,
}

impl ResultType {
    /// Creates a new `ResultType` with the given value types.
    pub fn new(kinds: Vec<ValueType>) -> Self {
        ResultType { kinds }
    }

    /// Creates a new empty `ResultType`.
    pub fn empty() -> Self {
        ResultType { kinds: vec![] }
    }

    /// A reference to a slice of the `ValueType`s.
    pub fn kinds(&self) -> &[ValueType] {
        &self.kinds
    }

    /// The length of the `ValueType` vector.
    pub fn len(&self) -> usize {
        self.kinds.len()
    }

    /// Returns true if this `ResultType` has a length of zero, false otherwise.
    pub fn is_empty(&self) -> bool {
        self.kinds.is_empty()
    }
}

impl From<Vec<ValueType>> for ResultType {
    fn from(kinds: Vec<ValueType>) -> Self {
        ResultType { kinds }
    }
}

/// Function types classify the signature of functions,
/// mapping a vector of parameters to a vector of results.
/// They are also used to classify the inputs and outputs of instructions
///
/// See <https://webassembly.github.io/spec/core/syntax/types.html#function-types>
///
/// # Examples
///
/// ## Input & Output
/// ```rust
/// use wasm_ast::{FunctionType, ResultType};
///
/// let function_type = FunctionType::new(ResultType::empty(), ResultType::empty());
///
/// assert!(function_type.parameters().is_empty());
/// assert!(function_type.results().is_empty());
/// ```
///
/// ## Input Only
/// ```rust
/// use wasm_ast::{FunctionType, ResultType, ValueType};
///
/// let function_type = FunctionType::side_effect(ResultType::from(vec![ValueType::I32]));
///
/// assert_eq!(function_type.parameters().kinds(), &[ValueType::I32]);
/// assert!(function_type.results().is_empty());
/// ```
///
/// ## Output Only
/// ```rust
/// use wasm_ast::{FunctionType, ResultType, ValueType};
///
/// let function_type = FunctionType::nullary(ResultType::from(vec![ValueType::I32]));
///
/// assert!(function_type.parameters().is_empty());
/// assert_eq!(function_type.results().kinds(), &[ValueType::I32]);
/// ```
///
/// ## No Input or Output
/// ```rust
/// use wasm_ast::{FunctionType, ResultType, ValueType};
///
/// let function_type = FunctionType::runnable();
///
/// assert!(function_type.parameters().is_empty());
/// assert!(function_type.results().is_empty());
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FunctionType {
    parameters: ResultType,
    results: ResultType,
}

impl FunctionType {
    /// Creates a new function signature with the given parameter and result types.
    pub fn new(parameters: ResultType, results: ResultType) -> Self {
        FunctionType {
            parameters,
            results,
        }
    }

    /// Creates a new function signature with the given parameter types and no result types.
    pub fn side_effect(parameters: ResultType) -> Self {
        FunctionType {
            parameters,
            results: ResultType::empty(),
        }
    }

    /// Creates a new function signature with the given result types and no parameter types.
    pub fn nullary(results: ResultType) -> Self {
        FunctionType {
            parameters: ResultType::empty(),
            results,
        }
    }

    /// Creates a new function signature with the no parameter or result types.
    pub fn runnable() -> Self {
        FunctionType {
            parameters: ResultType::empty(),
            results: ResultType::empty(),
        }
    }

    /// The parameter types of this `FunctionType`.
    pub fn parameters(&self) -> &ResultType {
        &self.parameters
    }

    /// The result types of this `FunctionType`.
    pub fn results(&self) -> &ResultType {
        &self.results
    }
}

/// Limits classify the size range of resizeable storage associated with memory types and table types.
///
/// See <https://webassembly.github.io/spec/core/syntax/types.html#limits>
///
/// # Examples
///
/// ## New
/// ```rust
/// use wasm_ast::Limit;
///
/// let max = Some(2);
/// let min = 0;
/// let limit = Limit::new(min, max);
///
/// assert_eq!(limit.min(), min);
/// assert_eq!(limit.max(), max);
/// ```
///
/// ## Unbounded
/// ```rust
/// use wasm_ast::Limit;
///
/// assert_eq!(Limit::unbounded(2), Limit::new(2, None));
/// ```
///
/// /// ## Unbounded
/// ```rust
/// use wasm_ast::Limit;
///
/// assert_eq!(Limit::bounded(2, 5), Limit::new(2, Some(5)));
/// ```
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Limit {
    min: u32,
    max: Option<u32>,
}

impl Limit {
    /// Creates a new limit with a required minimum and optional maximum.
    pub fn new(min: u32, max: Option<u32>) -> Self {
        Limit { min, max }
    }

    /// Creates a new limit with a required minimum and no maximum.
    pub fn unbounded(min: u32) -> Self {
        Limit { min, max: None }
    }

    /// Creates a new limit with a required minimum and maximum.
    pub fn bounded(min: u32, max: u32) -> Self {
        Limit {
            min,
            max: Some(max),
        }
    }

    /// The minimum value of the limit.
    pub fn min(&self) -> u32 {
        self.min
    }

    /// The optional maximum value of the limit.
    pub fn max(&self) -> Option<u32> {
        self.max
    }
}

/// Memory types classify linear memories and their size range.
/// The limits constrain the minimum and optionally the maximum size of a memory.
/// The limits are given in units of page size.
///
/// See <https://webassembly.github.io/spec/core/syntax/types.html#memory-types>
///
/// # Examples
/// ```rust
/// use wasm_ast::{Limit, MemoryType};
///
/// let limit = Limit::unbounded(0);
/// let memory_type = MemoryType::new(limit.clone());
///
/// assert_eq!(memory_type.limits(), &limit);
/// assert_eq!(memory_type, limit.into());
/// ```
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct MemoryType {
    limits: Limit,
}

impl MemoryType {
    /// Creates a new memory type from the given limits.
    pub fn new(limit: Limit) -> Self {
        MemoryType { limits: limit }
    }

    /// The limits of the number of pages for this `MemoryType`.
    pub fn limits(&self) -> &Limit {
        &self.limits
    }
}

impl From<Limit> for MemoryType {
    fn from(limit: Limit) -> Self {
        MemoryType { limits: limit }
    }
}

/// Table types classify tables over elements of reference type within a size range.
/// Like memories, tables are constrained by limits for their minimum and optionally maximum size.
/// The limits are given in numbers of entries.
///
/// See <https://webassembly.github.io/spec/core/syntax/types.html#table-types>
///
/// # Examples
/// ```rust
/// use wasm_ast::{Limit, TableType, ReferenceType};
///
/// let limit = Limit::unbounded(0);
/// let table_type = TableType::new( ReferenceType::External,limit.clone());
///
/// assert_eq!(table_type.limits(), &limit);
/// assert_eq!(table_type.kind(), ReferenceType::External);
/// ```
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct TableType {
    limits: Limit,
    kind: ReferenceType,
}

impl TableType {
    /// Creates a new `TableType` for the given limits and reference type.
    pub fn new(kind: ReferenceType, limits: Limit) -> Self {
        TableType { limits, kind }
    }

    /// The limits of the number of elements for this `TableType`.
    pub fn limits(&self) -> &Limit {
        &self.limits
    }

    /// The reference type of the elements of this `TableType`.
    pub fn kind(&self) -> ReferenceType {
        self.kind
    }
}

/// Global types classify global variables, which hold a value and can either be mutable or immutable.
///
/// See <https://webassembly.github.io/spec/core/syntax/types.html#global-types>
///
/// # Examples
/// ## Mutable
/// ```rust
/// use wasm_ast::{ValueType, GlobalType, Mutability};
///
/// let mutable = GlobalType::mutable(ValueType::I64);
///
/// assert_eq!(mutable.mutability(), Mutability::Mutable);
/// assert_eq!(mutable.kind(), ValueType::I64);
/// assert_eq!(mutable, GlobalType::new( ValueType::I64,Mutability::Mutable));
/// ```
///
/// ## Immutable
/// ```rust
/// use wasm_ast::{ValueType, GlobalType, Mutability};
///
/// let immutable = GlobalType::immutable(ValueType::F64);
///
/// assert_eq!(immutable.mutability(), Mutability::Immutable);
/// assert_eq!(immutable.kind(), ValueType::F64);
/// assert_eq!(immutable, GlobalType::new( ValueType::F64,Mutability::Immutable));
/// ```
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct GlobalType {
    mutability: Mutability,
    kind: ValueType,
}

impl GlobalType {
    /// Creates a new `GlobalType` for a global variable with the given mutability and value type.
    pub fn new(kind: ValueType, mutability: Mutability) -> Self {
        GlobalType { mutability, kind }
    }

    /// Creates a new `GlobalType` for a mutable global variable.
    pub fn mutable(kind: ValueType) -> Self {
        GlobalType {
            mutability: Mutability::Mutable,
            kind,
        }
    }

    /// Creates a new `GlobalType` for an immutable (i.e. constant) global variable.
    pub fn immutable(kind: ValueType) -> Self {
        GlobalType {
            mutability: Mutability::Immutable,
            kind,
        }
    }

    /// The `ValueType` of the global variable defined by this `GlobalType`.
    pub fn kind(&self) -> ValueType {
        self.kind
    }

    /// The mutability (i.e. variable versus constant) of the global variable type.
    pub fn mutability(&self) -> Mutability {
        self.mutability
    }
}

/// The mutability of a global variable.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Mutability {
    Mutable,
    Immutable,
}