wasm_encoder/core/
tables.rs1use crate::{encode_section, ConstExpr, Encode, RefType, Section, SectionId, ValType};
2use alloc::vec::Vec;
3
4#[derive(Clone, Default, Debug)]
28pub struct TableSection {
29 bytes: Vec<u8>,
30 num_added: u32,
31}
32
33impl TableSection {
34 pub fn new() -> Self {
36 Self::default()
37 }
38
39 pub fn len(&self) -> u32 {
41 self.num_added
42 }
43
44 pub fn is_empty(&self) -> bool {
46 self.num_added == 0
47 }
48
49 pub fn table(&mut self, table_type: TableType) -> &mut Self {
51 table_type.encode(&mut self.bytes);
52 self.num_added += 1;
53 self
54 }
55
56 pub fn table_with_init(&mut self, table_type: TableType, init: &ConstExpr) -> &mut Self {
60 self.bytes.push(0x40);
61 self.bytes.push(0x00);
62 table_type.encode(&mut self.bytes);
63 init.encode(&mut self.bytes);
64 self.num_added += 1;
65 self
66 }
67}
68
69impl Encode for TableSection {
70 fn encode(&self, sink: &mut Vec<u8>) {
71 encode_section(sink, self.num_added, &self.bytes);
72 }
73}
74
75impl Section for TableSection {
76 fn id(&self) -> u8 {
77 SectionId::Table.into()
78 }
79}
80
81#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
83pub struct TableType {
84 pub element_type: RefType,
86 pub table64: bool,
88 pub minimum: u64,
90 pub maximum: Option<u64>,
92 pub shared: bool,
96}
97
98impl TableType {
99 pub fn index_type(&self) -> ValType {
101 if self.table64 {
102 ValType::I64
103 } else {
104 ValType::I32
105 }
106 }
107}
108
109impl Encode for TableType {
110 fn encode(&self, sink: &mut Vec<u8>) {
111 let mut flags = 0;
112 if self.maximum.is_some() {
113 flags |= 0b001;
114 }
115 if self.shared {
116 flags |= 0b010;
117 }
118 if self.table64 {
119 flags |= 0b100;
120 }
121
122 self.element_type.encode(sink);
123 sink.push(flags);
124 self.minimum.encode(sink);
125
126 if let Some(max) = self.maximum {
127 max.encode(sink);
128 }
129 }
130}