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
use super::utils::{id_is_gensym, index_is_default};
use super::{Fmt, Formatter};
use wast::{Data, DataKind, DataVal, Memory, MemoryKind, MemoryType};

impl<'src> Fmt for &Memory<'src> {
    fn fmt(&self, formatter: &mut Formatter) {
        formatter.start_line();
        formatter.write("(memory ");
        if let Some(id) = &self.id {
            if !id_is_gensym(id) {
                formatter.fmt(id);
                formatter.write(" ");
            }
        };
        if !self.exports.names.is_empty() {
            formatter.fmt(&self.exports);
            formatter.write(" ");
        };
        formatter.fmt(&self.kind);
        formatter.write(")");
        formatter.end_line();
    }
}

impl<'src> Fmt for &MemoryKind<'src> {
    fn fmt(&self, formatter: &mut Formatter) {
        match self {
            MemoryKind::Import { import, ty } => {
                formatter.fmt(import);
                formatter.write(" ");
                formatter.fmt(ty);
            }
            MemoryKind::Normal(ty) => {
                formatter.fmt(ty);
            }
            MemoryKind::Inline { is_32: true, data } => {
                if !data.is_empty() {
                    formatter.write("(data ");
                    formatter.fmt(data);
                    formatter.write(")");
                }
            }
            MemoryKind::Inline {
                is_32: false,
                data: _,
            } => {
                unimplemented!()
            }
        }
    }
}

impl<'src> Fmt for &Vec<DataVal<'src>> {
    fn fmt(&self, formatter: &mut Formatter) {
        let mut iter = self.iter();
        if let Some(val) = iter.next() {
            formatter.fmt(val);
        }
        for val in iter {
            formatter.write(" ");
            formatter.fmt(val);
        }
    }
}

impl<'src> Fmt for &DataVal<'src> {
    fn fmt(&self, formatter: &mut Formatter) {
        match self {
            DataVal::String(bytes) => {
                let string = std::str::from_utf8(bytes).expect("valid utf8");
                formatter.fmt(string);
            }
            DataVal::Integral(..) => {
                // https://github.com/WebAssembly/wat-numeric-values
                unimplemented!();
            }
        }
    }
}

impl<'src> Fmt for &MemoryType {
    fn fmt(&self, formatter: &mut Formatter) {
        match self {
            MemoryType::B32 { limits, shared: _ } => {
                formatter.write(&limits.min.to_string());
                if let Some(max) = limits.max {
                    formatter.write(" ");
                    formatter.write(&max.to_string());
                }
            }
            MemoryType::B64 { limits, shared: _ } => {
                formatter.write(&limits.min.to_string());
                if let Some(max) = limits.max {
                    formatter.write(" ");
                    formatter.write(&max.to_string());
                }
            }
        }
    }
}

impl<'src> Fmt for &Data<'src> {
    fn fmt(&self, formatter: &mut Formatter) {
        formatter.start_line();
        formatter.write("(data ");
        if let Some(id) = &self.id {
            if !id_is_gensym(id) {
                formatter.fmt(id);
                formatter.write(" ");
            }
        }
        formatter.fmt(&self.kind);
        if !self.data.is_empty() {
            formatter.write(" ");
            formatter.fmt(&self.data);
        }
        formatter.write(")");
        formatter.end_line();
    }
}

impl<'src> Fmt for &DataKind<'src> {
    fn fmt(&self, formatter: &mut Formatter) {
        match self {
            DataKind::Passive => todo!(),
            DataKind::Active { memory, offset } => {
                let index = memory.unwrap_index();
                if !index_is_default(index) {
                    formatter.fmt(index);
                    formatter.write(" ");
                }
                formatter.fmt(offset);
            }
        }
    }
}