diff --git a/src/module/exports.rs b/src/module/exports.rs index 3a259339..a676d6fb 100644 --- a/src/module/exports.rs +++ b/src/module/exports.rs @@ -1,6 +1,6 @@ //! Exported items in a wasm module. -use anyhow::Context; +use anyhow::{bail, Context}; use crate::emit::{Emit, EmitContext}; use crate::parse::IndicesToIds; @@ -138,15 +138,19 @@ impl ModuleExports { /// Delete an exported function by name from this module. pub fn delete_func_by_name(&mut self, name: impl AsRef) -> Result<()> { - let fid = self.get_func_by_name(name.as_ref()).context(format!( - "failed to find exported func with name [{}]", - name.as_ref() - ))?; - self.delete( - self.get_exported_func(fid) - .with_context(|| format!("failed to find exported func with ID [{fid:?}]"))? - .id(), - ); + let export = self + .iter() + .find(|e| e.name == name.as_ref()) + .with_context(|| { + format!("failed to find exported func with name [{}]", name.as_ref()) + })?; + + if let ExportItem::Function(_) = export.item { + self.delete(export.id()); + } else { + bail!("export [{}] is not an exported function", name.as_ref()); + } + Ok(()) } } diff --git a/src/module/imports.rs b/src/module/imports.rs index d74bc453..d2c48932 100644 --- a/src/module/imports.rs +++ b/src/module/imports.rs @@ -137,16 +137,23 @@ impl ModuleImports { module: impl AsRef, name: impl AsRef, ) -> Result<()> { - let fid = self - .get_func_by_name(module, name.as_ref()) + let import = self + .iter() + .find(|e| e.module == module.as_ref() && e.name == name.as_ref()) .with_context(|| { format!("failed to find imported func with name [{}]", name.as_ref()) })?; - self.delete( - self.get_imported_func(fid) - .with_context(|| format!("failed to find imported func with ID [{fid:?}]"))? - .id(), - ); + + if let ImportKind::Function(_) = import.kind { + self.delete(import.id()); + } else { + bail!( + "import [{}] in module [{}] is not an imported function", + name.as_ref(), + module.as_ref() + ); + } + Ok(()) } }