1 | use crate::{encode_section, Encode, Section, SectionId}; |
2 | |
3 | /// An encoder for the memory section. |
4 | /// |
5 | /// Memory sections are only supported for modules. |
6 | /// |
7 | /// # Example |
8 | /// |
9 | /// ``` |
10 | /// use wasm_encoder::{Module, MemorySection, MemoryType}; |
11 | /// |
12 | /// let mut memories = MemorySection::new(); |
13 | /// memories.memory(MemoryType { |
14 | /// minimum: 1, |
15 | /// maximum: None, |
16 | /// memory64: false, |
17 | /// shared: false, |
18 | /// page_size_log2: None, |
19 | /// }); |
20 | /// |
21 | /// let mut module = Module::new(); |
22 | /// module.section(&memories); |
23 | /// |
24 | /// let wasm_bytes = module.finish(); |
25 | /// ``` |
26 | #[derive (Clone, Default, Debug)] |
27 | pub struct MemorySection { |
28 | bytes: Vec<u8>, |
29 | num_added: u32, |
30 | } |
31 | |
32 | impl MemorySection { |
33 | /// Create a new memory section encoder. |
34 | pub fn new() -> Self { |
35 | Self::default() |
36 | } |
37 | |
38 | /// The number of memories in the section. |
39 | pub fn len(&self) -> u32 { |
40 | self.num_added |
41 | } |
42 | |
43 | /// Determines if the section is empty. |
44 | pub fn is_empty(&self) -> bool { |
45 | self.num_added == 0 |
46 | } |
47 | |
48 | /// Define a memory. |
49 | pub fn memory(&mut self, memory_type: MemoryType) -> &mut Self { |
50 | memory_type.encode(&mut self.bytes); |
51 | self.num_added += 1; |
52 | self |
53 | } |
54 | } |
55 | |
56 | impl Encode for MemorySection { |
57 | fn encode(&self, sink: &mut Vec<u8>) { |
58 | encode_section(sink, self.num_added, &self.bytes); |
59 | } |
60 | } |
61 | |
62 | impl Section for MemorySection { |
63 | fn id(&self) -> u8 { |
64 | SectionId::Memory.into() |
65 | } |
66 | } |
67 | |
68 | /// A memory's type. |
69 | #[derive (Clone, Copy, Debug, PartialEq, Eq, Hash)] |
70 | pub struct MemoryType { |
71 | /// Minimum size, in pages, of this memory |
72 | pub minimum: u64, |
73 | /// Maximum size, in pages, of this memory |
74 | pub maximum: Option<u64>, |
75 | /// Whether or not this is a 64-bit memory. |
76 | pub memory64: bool, |
77 | /// Whether or not this memory is shared. |
78 | pub shared: bool, |
79 | /// The log base 2 of a custom page size for this memory. |
80 | /// |
81 | /// The default page size for Wasm memories is 64KiB, i.e. 2<sup>16</sup> or |
82 | /// `65536`. |
83 | /// |
84 | /// After the introduction of [the custom-page-sizes |
85 | /// proposal](https://github.com/WebAssembly/custom-page-sizes), Wasm can |
86 | /// customize the page size. It must be a power of two that is less than or |
87 | /// equal to 64KiB. Attempting to encode an invalid page size may panic. |
88 | pub page_size_log2: Option<u32>, |
89 | } |
90 | |
91 | impl Encode for MemoryType { |
92 | fn encode(&self, sink: &mut Vec<u8>) { |
93 | let mut flags = 0; |
94 | if self.maximum.is_some() { |
95 | flags |= 0b0001; |
96 | } |
97 | if self.shared { |
98 | flags |= 0b0010; |
99 | } |
100 | if self.memory64 { |
101 | flags |= 0b0100; |
102 | } |
103 | if self.page_size_log2.is_some() { |
104 | flags |= 0b1000; |
105 | } |
106 | sink.push(flags); |
107 | self.minimum.encode(sink); |
108 | if let Some(max) = self.maximum { |
109 | max.encode(sink); |
110 | } |
111 | if let Some(p) = self.page_size_log2 { |
112 | p.encode(sink); |
113 | } |
114 | } |
115 | } |
116 | |