| 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::{ |
| 17 | BinaryReader, ExternalKind, FromReader, GlobalType, MemoryType, Result, SectionLimited, |
| 18 | TableType, TagType, |
| 19 | }; |
| 20 | |
| 21 | /// Represents a reference to a type definition in a WebAssembly module. |
| 22 | #[derive (Debug, Clone, Copy, Eq, PartialEq)] |
| 23 | pub enum TypeRef { |
| 24 | /// The type is a function. |
| 25 | /// |
| 26 | /// The value is an index into the type section. |
| 27 | Func(u32), |
| 28 | /// The type is a table. |
| 29 | Table(TableType), |
| 30 | /// The type is a memory. |
| 31 | Memory(MemoryType), |
| 32 | /// The type is a global. |
| 33 | Global(GlobalType), |
| 34 | /// The type is a tag. |
| 35 | /// |
| 36 | /// This variant is only used for the exception handling proposal. |
| 37 | /// |
| 38 | /// The value is an index in the types index space. |
| 39 | Tag(TagType), |
| 40 | } |
| 41 | |
| 42 | /// Represents an import in a WebAssembly module. |
| 43 | #[derive (Debug, Copy, Clone, Eq, PartialEq)] |
| 44 | pub struct Import<'a> { |
| 45 | /// The module being imported from. |
| 46 | pub module: &'a str, |
| 47 | /// The name of the imported item. |
| 48 | pub name: &'a str, |
| 49 | /// The type of the imported item. |
| 50 | pub ty: TypeRef, |
| 51 | } |
| 52 | |
| 53 | /// A reader for the import section of a WebAssembly module. |
| 54 | pub type ImportSectionReader<'a> = SectionLimited<'a, Import<'a>>; |
| 55 | |
| 56 | impl<'a> FromReader<'a> for Import<'a> { |
| 57 | fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> { |
| 58 | Ok(Import { |
| 59 | module: reader.read()?, |
| 60 | name: reader.read()?, |
| 61 | ty: reader.read()?, |
| 62 | }) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | impl<'a> FromReader<'a> for TypeRef { |
| 67 | fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> { |
| 68 | Ok(match reader.read()? { |
| 69 | ExternalKind::Func => TypeRef::Func(reader.read_var_u32()?), |
| 70 | ExternalKind::Table => TypeRef::Table(reader.read()?), |
| 71 | ExternalKind::Memory => TypeRef::Memory(reader.read()?), |
| 72 | ExternalKind::Global => TypeRef::Global(reader.read()?), |
| 73 | ExternalKind::Tag => TypeRef::Tag(reader.read()?), |
| 74 | }) |
| 75 | } |
| 76 | } |
| 77 | |