wast_encoder/wasi_values/
mod.rs

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