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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! [`Import`]s and [`Export`]s are how modules interface with the outside world.

use crate::{
    encode::{WasmDecode, WasmEncode},
    tables::TableType,
    GlobalType, Limit,
};

/// A function, table, memory, or global requested from the host environment.
///
/// They are required to be supplied for instantiation to succeed.
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Import {
    pub module: String,
    pub name: String,
    pub desc: ImportDesc,
}

impl WasmEncode for Import {
    fn size(&self) -> usize {
        self.module.size() + self.name.size() + self.desc.size()
    }

    fn encode(&self, v: &mut Vec<u8>) {
        self.module.encode(v);
        self.name.encode(v);
        self.desc.encode(v);
    }
}

impl WasmDecode for Import {
    fn decode(buf: &mut crate::encode::Buf<'_>) -> Result<Self, crate::encode::ErrorKind> {
        let module = String::decode(buf)?;
        let name = String::decode(buf)?;
        let desc = ImportDesc::decode(buf)?;
        Ok(Self { module, name, desc })
    }
}

/// The signature of an import
///
/// - A function's type signature index.
/// - A table's type.
/// - A memory's size.
/// - A global's type.
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum ImportDesc {
    Func { type_idx: u32 },
    Table { table_type: TableType },
    Memory { mem_type: Limit },
    Global { global_type: GlobalType },
}

impl WasmEncode for ImportDesc {
    fn size(&self) -> usize {
        use ImportDesc::*;
        1 + match self {
            Func { type_idx } => type_idx.size(),
            Table { table_type } => table_type.size(),
            Memory { mem_type } => mem_type.size(),
            Global { global_type } => global_type.size(),
        }
    }

    fn encode(&self, v: &mut Vec<u8>) {
        use ImportDesc::*;
        match self {
            Func { type_idx } => {
                v.push(0);
                type_idx.encode(v);
            }
            Table { table_type } => {
                v.push(1);
                table_type.encode(v);
            }
            Memory { mem_type } => {
                v.push(2);
                mem_type.encode(v);
            }
            Global { global_type } => {
                v.push(3);
                global_type.encode(v);
            }
        }
    }
}

impl WasmDecode for ImportDesc {
    fn decode(buf: &mut crate::encode::Buf<'_>) -> Result<Self, crate::encode::ErrorKind> {
        let d = u8::decode(buf)?;
        match d {
            0 => Ok(Self::Func {
                type_idx: u32::decode(buf)?,
            }),
            1 => Ok(Self::Table {
                table_type: TableType::decode(buf)?,
            }),
            2 => Ok(Self::Memory {
                mem_type: Limit::decode(buf)?,
            }),
            3 => Ok(Self::Global {
                global_type: GlobalType::decode(buf)?,
            }),
            _ => Err(crate::encode::ErrorKind::InvalidDiscriminant(d)),
        }
    }
}

/// A module's internal resource that is exported to the host environment.
///
/// The name must be unique within the module.
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Export {
    pub name: String,
    pub desc: ExportDesc,
}

impl WasmEncode for Export {
    fn size(&self) -> usize {
        self.name.size() + self.desc.size()
    }

    fn encode(&self, v: &mut Vec<u8>) {
        self.name.encode(v);
        self.desc.encode(v);
    }
}

impl WasmDecode for Export {
    fn decode(buf: &mut crate::encode::Buf<'_>) -> Result<Self, crate::encode::ErrorKind> {
        let name = String::decode(buf)?;
        let desc = ExportDesc::decode(buf)?;
        Ok(Self { name, desc })
    }
}

/// The index of an exported resource
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum ExportDesc {
    Func { func_idx: u32 },
    Table { table_idx: u32 },
    Memory { mem_idx: u32 },
    Global { global_idx: u32 },
}

impl WasmEncode for ExportDesc {
    fn size(&self) -> usize {
        1 + match self {
            ExportDesc::Func { func_idx } => func_idx.size(),
            ExportDesc::Table { table_idx } => table_idx.size(),
            ExportDesc::Memory { mem_idx } => mem_idx.size(),
            ExportDesc::Global { global_idx } => global_idx.size(),
        }
    }

    fn encode(&self, v: &mut Vec<u8>) {
        match self {
            ExportDesc::Func { func_idx } => {
                v.push(0);
                func_idx.encode(v);
            }
            ExportDesc::Table { table_idx } => {
                v.push(1);
                table_idx.encode(v);
            }
            ExportDesc::Memory { mem_idx } => {
                v.push(2);
                mem_idx.encode(v);
            }
            ExportDesc::Global { global_idx } => {
                v.push(3);
                global_idx.encode(v);
            }
        }
    }
}

impl WasmDecode for ExportDesc {
    fn decode(buf: &mut crate::encode::Buf<'_>) -> Result<Self, crate::encode::ErrorKind> {
        let d = u8::decode(buf)?;
        match d {
            0 => Ok(Self::Func {
                func_idx: u32::decode(buf)?,
            }),
            1 => Ok(Self::Table {
                table_idx: u32::decode(buf)?,
            }),
            2 => Ok(Self::Memory {
                mem_idx: u32::decode(buf)?,
            }),
            3 => Ok(Self::Global {
                global_idx: u32::decode(buf)?,
            }),
            _ => Err(crate::encode::ErrorKind::InvalidDiscriminant(d)),
        }
    }
}