/// An `import` statement and entry in a WebAssembly module. #[derive(Debug, Clone)] pubstruct Imports<'a> { /// Where this `import` statement was defined. pub span: Span, /// All items inside the `import` statement. pub items: ImportItems<'a>,
}
/// The individual items inside an `import` statement, possibly in one of the /// "compact" forms. #[derive(Debug, Clone)] pubenum ImportItems<'a> { /// A single import item: /// /// ```text /// (import "mod" "name" <type>) /// ```
Single { /// The name of the module being importing from.
module: &'a str, /// The name of the field in the module being imported from.
name: &'a str, /// The type of the item being imported.
sig: ItemSig<'a>,
},
/// A group of import items with a common module name: /// /// ```text /// (import "mod" (item "foo" <type>) (item "bar" <type>)) /// ```
Group1 { /// The name of the module being imported from.
module: &'a str, /// The individual items being imported (name/type).
items: Vec<ImportGroup1Item<'a>>,
},
/// A group of import items with a common module name and type: /// /// ```text /// (import "mod" (item "foo") (item "bar") <type>) /// ```
Group2 { /// The name of the module being imported from.
module: &'a str, /// The type of all the items being imported.
sig: ItemSig<'a>, /// The individual items being imported (names only).
items: Vec<ImportGroup2Item<'a>>,
},
}
/// An item in a group of compact imports sharing a module name. Used with [`ImportItems::Group1`]. /// /// ```text /// (item "foo" <type>) /// ``` #[derive(Debug, Clone)] pubstruct ImportGroup1Item<'a> { /// Where this `item` was defined. pub span: Span, /// The name of the item being imported. pub name: &'a str, /// The type of the item being imported. pub sig: ItemSig<'a>,
}
/// An item in a group of compact imports sharing a module name and type. Used with [`ImportItems::Group2`]. #[derive(Debug, Clone)] pubstruct ImportGroup2Item<'a> { /// Where this `item` was defined. pub span: Span, /// The name of the item being imported. pub name: &'a str,
}
impl<'a> Imports<'a> { /// Constructs an Imports object for a single import item. pubfn single(span: Span, module: &'a str, name: &'a str, sig: ItemSig<'a>) -> Self { Self {
span,
items: ImportItems::Single { module, name, sig },
}
}
/// Returns the number of import items defined in the group. pubfn num_items(&self) -> usize { match &self.items {
ImportItems::Single {
module: _,
name: _,
sig: _,
} => 1,
ImportItems::Group1 { module: _, items } => items.len(),
ImportItems::Group2 {
module: _,
sig: _,
items,
} => items.len(),
}
}
/// Returns the ItemSig for each defined import in the group. Items using /// compact encoding 2 will share an ItemSig. pubfn item_sigs(&self) -> Vec<&ItemSig<'a>> { let res = match &self.items {
ImportItems::Single {
module: _,
name: _,
sig,
} => vec![sig],
ImportItems::Group1 { module: _, items } => {
items.iter().map(|item| &item.sig).collect()
}
ImportItems::Group2 {
module: _,
sig,
items,
} => vec![sig; items.len()],
};
debug_assert!(res.len() == self.num_items());
res
}
/// Returns mutable references to each ItemSig defined in the group. This /// may be less than the number of imports defined in the group, if items /// share a sig. pubfn unique_sigs_mut(&mutself) -> Vec<&mut ItemSig<'a>> { match &mutself.items {
ImportItems::Single {
module: _,
name: _,
sig: item,
} => vec![item],
ImportItems::Group1 { module: _, items } => {
items.iter_mut().map(|item| &mut item.sig).collect()
}
ImportItems::Group2 {
module: _,
sig,
items: _,
} => vec![sig],
}
}
}
#[derive(Debug, Clone)] #[allow(missing_docs)] pubstruct ItemSig<'a> { /// Where this item is defined in the source. pub span: Span, /// An optional identifier used during name resolution to refer to this item /// from the rest of the module. pub id: Option<Id<'a>>, /// An optional name which, for functions, will be stored in the /// custom `name` section. pub name: Option<NameAnnotation<'a>>, /// What kind of item this is. pub kind: ItemKind<'a>,
}
impl<'a> Parse<'a> for ItemSig<'a> { fn parse(parser: Parser<'a>) -> Result<Self> { letmut l = parser.lookahead1(); if l.peek::<kw::func>()? { let span = parser.parse::<kw::func>()?.0; let id = parser.parse()?; let name = parser.parse()?; let kind = if parser.peek2::<kw::exact>()? {
ItemKind::FuncExact(parser.parens(|p| {
p.parse::<kw::exact>()?;
p.parse()
})?)
} else {
ItemKind::Func(parser.parse()?)
};
Ok(ItemSig {
span,
id,
name,
kind,
})
} elseif l.peek::<kw::table>()? { let span = parser.parse::<kw::table>()?.0;
Ok(ItemSig {
span,
id: parser.parse()?,
name: None,
kind: ItemKind::Table(parser.parse()?),
})
} elseif l.peek::<kw::memory>()? { let span = parser.parse::<kw::memory>()?.0;
Ok(ItemSig {
span,
id: parser.parse()?,
name: None,
kind: ItemKind::Memory(parser.parse()?),
})
} elseif l.peek::<kw::global>()? { let span = parser.parse::<kw::global>()?.0;
Ok(ItemSig {
span,
id: parser.parse()?,
name: None,
kind: ItemKind::Global(parser.parse()?),
})
} elseif l.peek::<kw::tag>()? { let span = parser.parse::<kw::tag>()?.0;
Ok(ItemSig {
span,
id: parser.parse()?,
name: None,
kind: ItemKind::Tag(parser.parse()?),
})
} else {
Err(l.error())
}
}
}
/// A listing of a inline `(import "foo")` statement. /// /// Note that when parsing this type it is somewhat unconventional that it /// parses its own surrounding parentheses. This is typically an optional type, /// so it's so far been a bit nicer to have the optionality handled through /// `Peek` rather than `Option<T>`. #[derive(Debug, Copy, Clone)] #[allow(missing_docs)] pubstruct InlineImport<'a> { pub module: &'a str, pub field: &'a str,
}
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.