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
use super::utils::{expr_is_const, id_is_gensym, index_is_default}; use super::{Fmt, Formatter}; use wast::{ Elem, ElemKind, ElemPayload, HeapType, Index, Limits, RefType, Table, TableKind, TableType, }; impl<'src> Fmt for &Table<'src> { fn fmt(&self, formatter: &mut Formatter) { formatter.start_line(); formatter.write("(table "); 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 &TableKind<'src> { fn fmt(&self, formatter: &mut Formatter) { match self { TableKind::Import { import, ty } => { formatter.fmt(import); formatter.write(" "); formatter.fmt(ty); } TableKind::Normal(ty) => { formatter.fmt(ty); } TableKind::Inline { elem, payload } => { formatter.fmt(elem); if !elem_payload_is_empty(payload) { formatter.write(" "); formatter.fmt(payload); } } } } } impl<'src> Fmt for &TableType<'src> { fn fmt(&self, formatter: &mut Formatter) { formatter.fmt(&self.limits); formatter.write(" "); formatter.fmt(&self.elem); } } impl<'src> Fmt for &Limits { fn fmt(&self, formatter: &mut Formatter) { formatter.fmt(self.min); if let Some(max) = self.max { formatter.write(" "); formatter.fmt(max); } } } impl<'src> Fmt for &RefType<'src> { fn fmt(&self, formatter: &mut Formatter) { match self.heap { HeapType::Func => { formatter.write("funcref"); } HeapType::Extern => unimplemented!(), HeapType::Any => unimplemented!(), HeapType::Exn => unimplemented!(), HeapType::Eq => unimplemented!(), HeapType::I31 => unimplemented!(), HeapType::Index(..) => unimplemented!(), } } } impl<'src> Fmt for &Elem<'src> { fn fmt(&self, formatter: &mut Formatter) { formatter.start_line(); formatter.write("(elem "); if let Some(id) = &self.id { if !id_is_gensym(id) { formatter.fmt(id); formatter.write(" "); } } formatter.fmt(&self.kind); if !elem_payload_is_empty(&self.payload) { formatter.write(" "); formatter.fmt(&self.payload); } formatter.write(")"); formatter.end_line(); } } impl<'src> Fmt for &ElemKind<'src> { fn fmt(&self, formatter: &mut Formatter) { match self { ElemKind::Passive => todo!(), ElemKind::Declared => todo!(), ElemKind::Active { table, offset } => { let index = table.unwrap_index(); if !index_is_default(index) { formatter.fmt(index); formatter.write(" "); } if expr_is_const(offset) { formatter.fmt(offset); } else { formatter.write("(offset "); formatter.fmt(offset); formatter.write(")"); } } }; } } impl<'src> Fmt for &ElemPayload<'src> { fn fmt(&self, formatter: &mut Formatter) { match self { ElemPayload::Indices(refs) => { let indices: Vec<Index<'src>> = refs .iter() .map(|item_ref| *item_ref.unwrap_index()) .collect(); formatter.fmt(&indices); } ElemPayload::Exprs { .. } => unimplemented!(), } } } fn elem_payload_is_empty(payload: &ElemPayload) -> bool { match payload { ElemPayload::Indices(indices) => indices.is_empty(), ElemPayload::Exprs { exprs, .. } => exprs.is_empty(), } }