/// Represents a reference to a type definition in a WebAssembly module. #[derive(Debug, Clone, Copy, Eq, PartialEq)] pubenum TypeRef { /// The type is a function.
Func(u32), /// The type is a table.
Table(TableType), /// The type is a memory.
Memory(MemoryType), /// The type is a global.
Global(GlobalType), /// The type is a tag. /// /// This variant is only used for the exception handling proposal. /// /// The value is an index in the types index space.
Tag(TagType), /// The type is a function.
FuncExact(u32),
}
/// Represents a group of imports in a WebAssembly module. #[derive(Debug, Clone)] pubenum Imports<'a> { /// The group contains a single import.
Single(usize, Import<'a>), /// The group contains many imports that share the same module name, but have different types.
Compact1 { /// The module being imported from.
module: &'a str, /// The imported items.
items: SectionLimited<'a, ImportItemCompact<'a>>,
}, /// The group contains many imports that share the same module name and type.
Compact2 { /// The module each item will be imported from.
module: &'a str, /// The type of the imported items.
ty: TypeRef, /// The imported item names.
names: SectionLimited<'a, &'a str>,
},
}
/// Represents an import in a WebAssembly module. #[derive(Debug, Copy, Clone, Eq, PartialEq)] pubstruct Import<'a> { /// The module being imported from. pub module: &'a str, /// The name of the imported item. pub name: &'a str, /// The type of the imported item. pub ty: TypeRef,
}
/// A single compact import item. #[derive(Debug, Copy, Clone, Eq, PartialEq)] pubstruct ImportItemCompact<'a> { /// The name of the imported item. pub name: &'a str, /// The type of the imported item. pub ty: TypeRef,
}
/// A reader for the import section of a WebAssembly module. pubtype ImportSectionReader<'a> = SectionLimited<'a, Imports<'a>>;
impl<'a> FromReader<'a> for Imports<'a> { fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> { let start = reader.original_position(); let module = reader.read_string()?; let single_item_name = reader.read_string()?; let discriminator = reader.peek_bytes(1)?[0]; match (single_item_name, discriminator) {
("", 0x7F) => { if !reader.compact_imports() {
bail!(
reader.original_position(), "invalid leading byte 0x7F with compact imports \
proposal disabled"
);
} // Compact encoding 1: one module name, many item names / types
reader.read_bytes(1)?; // FIXME(#188) shouldn't need to skip here let items = reader.skip(|reader| { let count = reader.read_var_u32()?; for _ in0..count {
reader.skip_string()?;
reader.read::<TypeRef>()?;
}
Ok(())
})?;
Ok(Imports::Compact1 {
module,
items: SectionLimited::new(items)?,
})
}
("", 0x7E) => { if !reader.compact_imports() {
bail!(
reader.original_position(), "invalid leading byte 0x7E with compact imports \
proposal disabled"
);
} // Compact encoding 2: one module name / type, many item names
reader.read_bytes(1)?; let ty: TypeRef = reader.read()?; // FIXME(#188) shouldn't need to skip here let names = reader.skip(|reader| { let count = reader.read_var_u32()?; for _ in0..count {
reader.skip_string()?;
}
Ok(())
})?;
Ok(Imports::Compact2 {
module,
ty,
names: SectionLimited::new(names)?,
})
}
_ => Ok(Imports::Single(
start,
Import {
module: module,
name: single_item_name,
ty: reader.read()?,
},
)),
}
}
}
// Iterator implementations to streamline usage of the Imports type in its // various possible encodings
impl<'a> SectionLimited<'a, Imports<'a>> { /// Converts the section into an iterator over individual [`Import`]s, flattening any groups /// of compact imports. pubfn into_imports(self) -> impl Iterator<Item = Result<Import<'a>>> { self.into_imports_with_offsets()
.map(|res| res.map(|(_, import)| import))
}
/// Converts the section into an iterator over individual [`Import`]s and their offsets, /// flattening any groups of compact imports. pubfn into_imports_with_offsets(self) -> impl Iterator<Item = Result<(usize, Import<'a>)>> { self.into_iter().flat_map(|res| match res {
Ok(imports) => imports.into_iter(),
Err(e) => ImportsIter {
state: ImportsIterState::Error(e),
},
})
}
}
impl<'a> IntoIterator for Imports<'a> { type Item = Result<(usize, Import<'a>)>; type IntoIter = ImportsIter<'a>;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.