use super::GlobalIdx;
use crate::{errors::ModuleError, ExternType, Module};
use alloc::{boxed::Box, collections::btree_map::Iter as BTreeIter};
#[derive(Debug, Copy, Clone)]
pub struct FuncIdx(pub(crate) u32);
impl FuncIdx {
pub fn into_u32(self) -> u32 {
self.0
}
pub fn into_usize(self) -> usize {
self.0 as usize
}
}
#[derive(Debug, Copy, Clone)]
pub struct TableIdx(pub(crate) u32);
impl TableIdx {
pub fn into_u32(self) -> u32 {
self.0
}
}
#[derive(Debug, Copy, Clone)]
pub struct MemoryIdx(pub(crate) u32);
impl MemoryIdx {
pub fn into_u32(self) -> u32 {
self.0
}
}
#[derive(Debug, Copy, Clone)]
pub enum ExternIdx {
Func(FuncIdx),
Table(TableIdx),
Memory(MemoryIdx),
Global(GlobalIdx),
}
impl ExternIdx {
pub fn new(kind: wasmparser::ExternalKind, index: u32) -> Result<Self, ModuleError> {
match kind {
wasmparser::ExternalKind::Func => Ok(ExternIdx::Func(FuncIdx(index))),
wasmparser::ExternalKind::Table => Ok(ExternIdx::Table(TableIdx(index))),
wasmparser::ExternalKind::Memory => Ok(ExternIdx::Memory(MemoryIdx(index))),
wasmparser::ExternalKind::Global => Ok(ExternIdx::Global(GlobalIdx(index))),
wasmparser::ExternalKind::Tag => {
panic!("wasmi does not support the `exception-handling` Wasm proposal")
}
}
}
}
#[derive(Debug)]
pub struct ModuleExportsIter<'module> {
exports: BTreeIter<'module, Box<str>, ExternIdx>,
module: &'module Module,
}
#[derive(Debug)]
pub struct ExportType<'module> {
name: &'module str,
ty: ExternType,
}
impl<'module> ExportType<'module> {
pub fn name(&self) -> &'module str {
self.name
}
pub fn ty(&self) -> &ExternType {
&self.ty
}
}
impl<'module> ModuleExportsIter<'module> {
pub(super) fn new(module: &'module Module) -> Self {
Self {
exports: module.exports.iter(),
module,
}
}
}
impl<'module> Iterator for ModuleExportsIter<'module> {
type Item = ExportType<'module>;
fn next(&mut self) -> Option<Self::Item> {
self.exports.next().map(|(name, idx)| {
let ty = self.module.get_extern_type(*idx);
ExportType { name, ty }
})
}
}