nyar_wasm/wasi_values/
mod.rs

1use crate::{
2    encoder::WastEncoder,
3    helpers::{EmitConstant, ToWasiType},
4    ArrayValue, WasiArrayType, WasiType,
5};
6use std::{
7    cmp::Ordering,
8    fmt::Write,
9    hash::{Hash, Hasher},
10};
11
12mod arithmetic;
13
14pub mod array;
15pub mod record;
16
17/// Static values that can be expressed in wasm/wasi
18#[derive(Debug, Clone)]
19pub enum WasiValue {
20    /// The boolean value, `true` or `false`
21    Boolean(bool),
22    /// The signed 8-bit integer, from `-128` to `127`
23    Integer8(i8),
24    /// The signed 16-bit integer, from `-32768` to `32767`
25    Integer16(i16),
26    /// The signed 32-bit integer, from `-2147483648` to `2147483647`
27    Integer32(i32),
28    /// The signed 64-bit integer, from `-9223372036854775808` to `9223372036854775807`
29    Integer64(i64),
30    /// The unsigned 8-bit integer, from `0` to `255`
31    Unsigned8(u8),
32    /// The unsigned 16-bit integer, from `0` to `65535`
33    Unsigned16(u16),
34    /// The unsigned 32-bit integer, from `0` to `4294967295`
35    Unsigned32(u32),
36    /// The unsigned 64-bit integer, from `0` to `18446744073709551615`
37    Unsigned64(u64),
38    /// The 32-bit floating point number
39    Float32(f32),
40    /// The 64-bit floating point number
41    Float64(f64),
42    DynamicArray(ArrayValue),
43}
44
45impl EmitConstant for WasiValue {
46    fn emit_constant<W: Write>(&self, w: &mut WastEncoder<W>) -> std::fmt::Result {
47        match self {
48            Self::Boolean(v) => write!(w, "i32.const {}", if *v { 1 } else { 0 })?,
49            Self::Integer8(v) => write!(w, "i32.const {}", v)?,
50            Self::Integer16(v) => write!(w, "i32.const {}", v)?,
51            Self::Integer32(v) => write!(w, "i32.const {}", v)?,
52            Self::Integer64(v) => write!(w, "i64.const {}", v)?,
53            Self::Unsigned8(v) => write!(w, "i32.const {}", v)?,
54            Self::Unsigned16(v) => write!(w, "i32.const {}", v)?,
55            Self::Unsigned32(v) => write!(w, "i32.const {}", v)?,
56            Self::Unsigned64(v) => write!(w, "i64.const {}", v)?,
57            Self::Float32(v) => write!(w, "f32.const {}", v)?,
58            Self::Float64(v) => write!(w, "f64.const {}", v)?,
59            Self::DynamicArray(v) => v.emit_constant(w)?,
60        }
61        Ok(())
62    }
63}