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
use crate::abi::GuestMemoryConfig;

#[derive(Copy, Clone)]
pub struct GuestProgramInit<'a> {
    ro_data: &'a [u8],
    rw_data: &'a [u8],
    bss_size: u32,
    stack_size: u32,
}

impl<'a> Default for GuestProgramInit<'a> {
    fn default() -> Self {
        Self::new()
    }
}

impl<'a> GuestProgramInit<'a> {
    pub fn new() -> Self {
        Self {
            ro_data: &[],
            rw_data: &[],
            bss_size: 0,
            stack_size: 0,
        }
    }

    pub fn ro_data(self) -> &'a [u8] {
        self.ro_data
    }

    pub fn with_ro_data(mut self, ro_data: &'a [u8]) -> Self {
        self.ro_data = ro_data;
        self
    }

    pub fn rw_data(self) -> &'a [u8] {
        self.rw_data
    }

    pub fn with_rw_data(mut self, rw_data: &'a [u8]) -> Self {
        self.rw_data = rw_data;
        self
    }

    pub fn bss_size(self) -> u32 {
        self.bss_size
    }

    pub fn with_bss(mut self, size: u32) -> Self {
        self.bss_size = size;
        self
    }

    pub fn stack_size(self) -> u32 {
        self.stack_size
    }

    pub fn with_stack(mut self, size: u32) -> Self {
        self.stack_size = size;
        self
    }

    pub fn memory_config(&self) -> Result<GuestMemoryConfig, &'static str> {
        GuestMemoryConfig::new(
            self.ro_data.len() as u64,
            self.rw_data.len() as u64,
            self.bss_size as u64,
            self.stack_size as u64,
        )
    }
}