From ba1cf56464f8e5c3c7bcc2dc75f10aad2cd56e6b Mon Sep 17 00:00:00 2001 From: Henry Date: Sat, 2 May 2026 21:14:02 +0200 Subject: chore: add more tests, fix issues with import types, cleanup doom example Signed-off-by: Henry --- crates/types/src/archive.rs | 2 +- crates/types/src/lib.rs | 72 ++++++++++++++++++++++++++++++++++++++++----- crates/types/src/value.rs | 3 +- 3 files changed, 67 insertions(+), 10 deletions(-) (limited to 'crates/types/src') diff --git a/crates/types/src/archive.rs b/crates/types/src/archive.rs index 2636230..92bb87a 100644 --- a/crates/types/src/archive.rs +++ b/crates/types/src/archive.rs @@ -23,7 +23,7 @@ fn validate_magic(wasm: &[u8]) -> Result { Ok(TWASM_MAGIC.len()) } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub enum TwasmError { InvalidMagic, InvalidVersion, diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index b65e4b7..8057518 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -101,6 +101,9 @@ pub struct ModuleInner { /// Corresponds to the `type` section of the original WebAssembly module. pub func_types: Arc<[Arc]>, + /// Function index to type index mapping in module index space, including imports. + pub func_type_idxs: Arc<[u32]>, + /// Exported items of the WebAssembly module. /// /// Corresponds to the `export` section of the original WebAssembly module. @@ -161,6 +164,22 @@ impl Module { /// /// The returned data mirrors the module's export section and preserves order. pub fn exports(&self) -> impl Iterator> { + fn imported_count(module: &ModuleInner, kind: ExternalKind) -> usize { + module + .imports + .iter() + .filter(|import| { + matches!( + (kind, &import.kind), + (ExternalKind::Func, ImportKind::Function(_)) + | (ExternalKind::Table, ImportKind::Table(_)) + | (ExternalKind::Memory, ImportKind::Memory(_)) + | (ExternalKind::Global, ImportKind::Global(_)) + ) + }) + .count() + } + fn imported_func_type(module: &ModuleInner, function_index: usize) -> Option<&FuncType> { let mut seen = 0usize; for import in module.imports.iter() { @@ -174,6 +193,32 @@ impl Module { None } + fn imported_table_type(module: &ModuleInner, table_index: usize) -> Option<&TableType> { + let mut seen = 0usize; + for import in module.imports.iter() { + if let ImportKind::Table(table_ty) = &import.kind { + if seen == table_index { + return Some(table_ty); + } + seen += 1; + } + } + None + } + + fn imported_memory_type(module: &ModuleInner, memory_index: usize) -> Option<&MemoryType> { + let mut seen = 0usize; + for import in module.imports.iter() { + if let ImportKind::Memory(memory_ty) = &import.kind { + if seen == memory_index { + return Some(memory_ty); + } + seen += 1; + } + } + None + } + fn imported_global_type(module: &Module, global_index: usize) -> Option<&GlobalType> { let mut seen = 0usize; for import in module.imports.iter() { @@ -188,12 +233,10 @@ impl Module { } self.0.exports.iter().filter_map(move |export| { - let imports = self.0.imports.iter(); let idx = export.index as usize; let ty = match export.kind { ExternalKind::Func => { - let imported_funcs = - imports.filter(|import| matches!(import.kind, ImportKind::Function(_))).count(); + let imported_funcs = imported_count(&self.0, ExternalKind::Func); if idx < imported_funcs { ExportType::Func(imported_func_type(&self.0, idx)?) } else { @@ -201,11 +244,26 @@ impl Module { ExportType::Func(&self.0.funcs.get(local_idx)?.ty) } } - ExternalKind::Table => ExportType::Table(self.0.table_types.get(idx)?), - ExternalKind::Memory => ExportType::Memory(self.0.memory_types.get(idx)?), + ExternalKind::Table => { + let imported_tables = imported_count(&self.0, ExternalKind::Table); + if idx < imported_tables { + ExportType::Table(imported_table_type(&self.0, idx)?) + } else { + let local_idx = idx - imported_tables; + ExportType::Table(self.0.table_types.get(local_idx)?) + } + } + ExternalKind::Memory => { + let imported_memories = imported_count(&self.0, ExternalKind::Memory); + if idx < imported_memories { + ExportType::Memory(imported_memory_type(&self.0, idx)?) + } else { + let local_idx = idx - imported_memories; + ExportType::Memory(self.0.memory_types.get(local_idx)?) + } + } ExternalKind::Global => { - let imported_globals = - imports.filter(|import| matches!(import.kind, ImportKind::Global(_))).count(); + let imported_globals = imported_count(&self.0, ExternalKind::Global); if idx < imported_globals { ExportType::Global(imported_global_type(self, idx)?) } else { diff --git a/crates/types/src/value.rs b/crates/types/src/value.rs index 20c822e..c57edc0 100644 --- a/crates/types/src/value.rs +++ b/crates/types/src/value.rs @@ -341,8 +341,7 @@ impl From for WasmType { } /// Type of a WebAssembly value. -#[derive(Clone, Copy, PartialEq, Eq)] -#[cfg_attr(feature = "debug", derive(Debug))] +#[derive(Clone, Copy, PartialEq, Eq, Debug)] #[cfg_attr(feature = "archive", derive(serde::Serialize, serde::Deserialize))] pub enum WasmType { /// A 32-bit integer. -- cgit v1.3.1