use super::GlobalIdx;
use crate::{Error, ExternType, Module, collections::map::Iter as MapIter};
use alloc::boxed::Box;
#[derive(Debug, Copy, Clone)]
pub struct FuncIdx(u32);
impl From<u32> for FuncIdx {
fn from(index: u32) -> Self {
Self(index)
}
}
impl FuncIdx {
pub fn into_u32(self) -> u32 {
self.0
}
}
#[derive(Debug, Copy, Clone)]
pub struct TableIdx(u32);
impl From<u32> for TableIdx {
fn from(index: u32) -> Self {
Self(index)
}
}
impl TableIdx {
pub fn into_u32(self) -> u32 {
self.0
}
}
#[derive(Debug, Copy, Clone)]
pub struct MemoryIdx(u32);
impl From<u32> for MemoryIdx {
fn from(index: u32) -> Self {
Self(index)
}
}
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, Error> {
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::from(index))),
wasmparser::ExternalKind::Tag => {
panic!("wasmi does not support the `exception-handling` Wasm proposal")
}
}
}
}
#[derive(Debug)]
pub struct ModuleExportsIter<'module> {
exports: MapIter<'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.module_header().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 }
})
}
}