| 1 |
use crate::{encode_section, Encode, Section, SectionId}; |
| 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 |
#[derive(Clone, Default, Debug)] |
| 27 |
pub struct MemorySection { |
| 28 |
bytes: Vec<u8>, |
| 29 |
num_added: u32, |
| 30 |
} |
| 31 |
|
| 32 |
impl MemorySection { |
| 33 |
|
| 34 |
pub fn new() -> Self { |
| 35 |
Self::default() |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
pub fn len(&self) -> u32 { |
| 40 |
self.num_added |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
pub fn is_empty(&self) -> bool { |
| 45 |
self.num_added == 0 |
| 46 |
} |
| 47 |
|
| 48 |
|
| 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 |
|
| 69 |
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 70 |
pub struct MemoryType { |
| 71 |
|
| 72 |
pub minimum: u64, |
| 73 |
|
| 74 |
pub maximum: Option<u64>, |
| 75 |
|
| 76 |
pub memory64: bool, |
| 77 |
|
| 78 |
pub shared: bool, |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 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 |
} |