wasmparser/readers/core/
exports.rs

1/* Copyright 2018 Mozilla Foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16use crate::{BinaryReader, FromReader, Result, SectionLimited};
17
18/// A reader for the export section of a WebAssembly module.
19pub type ExportSectionReader<'a> = SectionLimited<'a, Export<'a>>;
20
21/// External types as defined [here].
22///
23/// [here]: https://webassembly.github.io/spec/core/syntax/types.html#external-types
24#[derive(Debug, Copy, Clone, PartialEq, Eq)]
25pub enum ExternalKind {
26    /// The external kind is a function.
27    Func,
28    /// The external kind if a table.
29    Table,
30    /// The external kind is a memory.
31    Memory,
32    /// The external kind is a global.
33    Global,
34    /// The external kind is a tag.
35    Tag,
36    /// The external kind is a function with the exact type.
37    FuncExact,
38}
39
40/// Represents an export in a WebAssembly module.
41#[derive(Debug, Copy, Clone, Eq, PartialEq)]
42pub struct Export<'a> {
43    /// The name of the exported item.
44    pub name: &'a str,
45    /// The kind of the export.
46    pub kind: ExternalKind,
47    /// The index of the exported item.
48    pub index: u32,
49}
50
51impl<'a> FromReader<'a> for Export<'a> {
52    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
53        Ok(Export {
54            name: reader.read_string()?,
55            kind: match reader.read()? {
56                ExternalKind::FuncExact => {
57                    bail!(
58                        reader.original_position(),
59                        "Exact type is not allowed in the exports",
60                    );
61                }
62                x => x,
63            },
64            index: reader.read_var_u32()?,
65        })
66    }
67}
68
69impl<'a> FromReader<'a> for ExternalKind {
70    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
71        let offset = reader.original_position();
72        let byte = reader.read_u8()?;
73        BinaryReader::external_kind_from_byte(byte, offset)
74    }
75}