1use super::utils::{id_is_gensym, index_is_default};
2use super::{Fmt, Formatter};
3use wast::core::{Data, DataKind, DataVal, Memory, MemoryKind, MemoryType};
4
5impl<'src> Fmt for &Memory<'src> {
6 fn fmt(&self, formatter: &mut Formatter) {
7 formatter.start_line();
8 formatter.write("(memory ");
9 if let Some(id) = &self.id {
10 if !id_is_gensym(id) {
11 formatter.fmt(id);
12 formatter.write(" ");
13 }
14 };
15 if !self.exports.names.is_empty() {
16 formatter.fmt(&self.exports);
17 formatter.write(" ");
18 };
19 formatter.fmt(&self.kind);
20 formatter.write(")");
21 formatter.end_line();
22 }
23}
24
25impl<'src> Fmt for &MemoryKind<'src> {
26 fn fmt(&self, formatter: &mut Formatter) {
27 match self {
28 MemoryKind::Import { import, ty } => {
29 formatter.fmt(import);
30 formatter.write(" ");
31 formatter.fmt(ty);
32 }
33 MemoryKind::Normal(ty) => {
34 formatter.fmt(ty);
35 }
36 MemoryKind::Inline { is_32: true, data } => {
37 if !data.is_empty() {
38 formatter.write("(data ");
39 formatter.fmt(data);
40 formatter.write(")");
41 }
42 }
43 MemoryKind::Inline {
44 is_32: false,
45 data: _,
46 } => {
47 unimplemented!()
48 }
49 }
50 }
51}
52
53impl<'src> Fmt for &Vec<DataVal<'src>> {
54 fn fmt(&self, formatter: &mut Formatter) {
55 let mut iter = self.iter();
56 if let Some(val) = iter.next() {
57 formatter.fmt(val);
58 }
59 for val in iter {
60 formatter.write(" ");
61 formatter.fmt(val);
62 }
63 }
64}
65
66impl<'src> Fmt for &DataVal<'src> {
67 fn fmt(&self, formatter: &mut Formatter) {
68 match self {
69 DataVal::String(bytes) => {
70 let string = std::str::from_utf8(bytes).expect("valid utf8");
71 formatter.fmt(string);
72 }
73 DataVal::Integral(..) => {
74 unimplemented!();
76 }
77 }
78 }
79}
80
81impl Fmt for &MemoryType {
82 fn fmt(&self, formatter: &mut Formatter) {
83 match self {
84 MemoryType::B32 { limits, shared: _ } => {
85 formatter.write(&limits.min.to_string());
86 if let Some(max) = limits.max {
87 formatter.write(" ");
88 formatter.write(&max.to_string());
89 }
90 }
91 MemoryType::B64 { limits, shared: _ } => {
92 formatter.write(&limits.min.to_string());
93 if let Some(max) = limits.max {
94 formatter.write(" ");
95 formatter.write(&max.to_string());
96 }
97 }
98 }
99 }
100}
101
102impl<'src> Fmt for &Data<'src> {
103 fn fmt(&self, formatter: &mut Formatter) {
104 formatter.start_line();
105 formatter.write("(data ");
106 if let Some(id) = &self.id {
107 if !id_is_gensym(id) {
108 formatter.fmt(id);
109 formatter.write(" ");
110 }
111 }
112 formatter.fmt(&self.kind);
113 if !self.data.is_empty() {
114 formatter.write(" ");
115 formatter.fmt(&self.data);
116 }
117 formatter.write(")");
118 formatter.end_line();
119 }
120}
121
122impl<'src> Fmt for &DataKind<'src> {
123 fn fmt(&self, formatter: &mut Formatter) {
124 match self {
125 DataKind::Passive => todo!(),
126 DataKind::Active { memory, offset } => {
127 if !index_is_default(memory) {
128 formatter.fmt(memory);
129 formatter.write(" ");
130 }
131 formatter.fmt(offset);
132 }
133 }
134 }
135}