use crate::PrimaryMap;
use core::ops::{Index, Range};
use serde_derive::{Deserialize, Serialize};
use wasmtime_types::{ModuleInternedRecGroupIndex, ModuleInternedTypeIndex, WasmSubType};
#[derive(Default, Serialize, Deserialize)]
pub struct ModuleTypes {
rec_groups: PrimaryMap<ModuleInternedRecGroupIndex, Range<ModuleInternedTypeIndex>>,
wasm_types: PrimaryMap<ModuleInternedTypeIndex, WasmSubType>,
}
impl ModuleTypes {
pub fn wasm_types(
&self,
) -> impl ExactSizeIterator<Item = (ModuleInternedTypeIndex, &WasmSubType)> {
self.wasm_types.iter()
}
pub fn get(&self, ty: ModuleInternedTypeIndex) -> Option<&WasmSubType> {
self.wasm_types.get(ty)
}
pub fn rec_groups(
&self,
) -> impl ExactSizeIterator<Item = (ModuleInternedRecGroupIndex, Range<ModuleInternedTypeIndex>)> + '_
{
self.rec_groups.iter().map(|(k, v)| (k, v.clone()))
}
pub fn rec_group_elements(
&self,
rec_group: ModuleInternedRecGroupIndex,
) -> impl ExactSizeIterator<Item = ModuleInternedTypeIndex> {
let range = &self.rec_groups[rec_group];
(range.start.as_u32()..range.end.as_u32()).map(|i| ModuleInternedTypeIndex::from_u32(i))
}
pub fn len_types(&self) -> usize {
self.wasm_types.len()
}
pub fn push(&mut self, ty: WasmSubType) -> ModuleInternedTypeIndex {
self.wasm_types.push(ty)
}
pub fn push_rec_group(
&mut self,
range: Range<ModuleInternedTypeIndex>,
) -> ModuleInternedRecGroupIndex {
self.rec_groups.push(range)
}
pub fn reserve(&mut self, amt: usize) {
self.wasm_types.reserve(amt)
}
pub fn next_rec_group(&self) -> ModuleInternedRecGroupIndex {
self.rec_groups.next_key()
}
pub fn next_ty(&self) -> ModuleInternedTypeIndex {
self.wasm_types.next_key()
}
}
impl Index<ModuleInternedTypeIndex> for ModuleTypes {
type Output = WasmSubType;
fn index(&self, sig: ModuleInternedTypeIndex) -> &WasmSubType {
&self.wasm_types[sig]
}
}