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 | |
16 | use crate::{BinaryReader, FromReader, Result, SectionLimited}; |
17 | |
18 | /// A reader for the export section of a WebAssembly module. |
19 | pub 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)] |
25 | pub 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 | } |
37 | |
38 | /// Represents an export in a WebAssembly module. |
39 | #[derive (Debug, Copy, Clone, Eq, PartialEq)] |
40 | pub struct Export<'a> { |
41 | /// The name of the exported item. |
42 | pub name: &'a str, |
43 | /// The kind of the export. |
44 | pub kind: ExternalKind, |
45 | /// The index of the exported item. |
46 | pub index: u32, |
47 | } |
48 | |
49 | impl<'a> FromReader<'a> for Export<'a> { |
50 | fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> { |
51 | Ok(Export { |
52 | name: reader.read_string()?, |
53 | kind: reader.read()?, |
54 | index: reader.read_var_u32()?, |
55 | }) |
56 | } |
57 | } |
58 | |
59 | impl<'a> FromReader<'a> for ExternalKind { |
60 | fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> { |
61 | let offset: usize = reader.original_position(); |
62 | let byte: u8 = reader.read_u8()?; |
63 | BinaryReader::external_kind_from_byte(byte, offset) |
64 | } |
65 | } |
66 | |