Skip to content

Commit

Permalink
refactor: address code review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Adossi <vadossi@cosmonic.com>
  • Loading branch information
vados-cosmonic committed Oct 12, 2023
1 parent 89a2841 commit 74966a4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
13 changes: 4 additions & 9 deletions src/module/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ impl ModuleExports {
}

/// Retrieve an exported function by name
pub fn get_func_by_name(&self, name: impl AsRef<str>) -> Result<&Export> {
pub fn get_func_by_name(&self, name: impl AsRef<str>) -> Result<FunctionId> {
self.iter()
.find(|expt| match expt.item {
ExportItem::Function(_) => expt.name == name.as_ref(),
_ => false,
.find_map(|expt| match expt.item {
ExportItem::Function(fid) if expt.name == name.as_ref() => Some(fid),
_ => None,
})
.with_context(|| format!("unable to find function export '{}'", name.as_ref()))
}
Expand Down Expand Up @@ -135,11 +135,6 @@ impl ModuleExports {
_ => false,
})
}

/// Check whether exports include the given function by ID
pub fn contains_func_by_id(&self, fid: FunctionId) -> bool {
self.get_exported_func(fid).is_some()
}
}

impl Module {
Expand Down
2 changes: 1 addition & 1 deletion src/module/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ impl Module {
F: FnOnce((&FuncParams, &FuncResults)) -> Result<Option<LocalFunction>>,
{
// If the function is in the imports, replace it
if let Some(original_imported_fn) = self.imports.get_func_by_id(fid) {
if let Some(original_imported_fn) = self.imports.get_imported_func(fid) {
// Change types of existing function
if let Function {
kind: FunctionKind::Import(ImportedFunction { ty: tid, .. }),
Expand Down
19 changes: 8 additions & 11 deletions src/module/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,26 @@ impl ModuleImports {
&self,
module: impl AsRef<str>,
name: impl AsRef<str>,
) -> Result<&Import> {
) -> Result<FunctionId> {
self.iter()
.find(|impt| match impt.kind {
ImportKind::Function(_) => {
impt.module == module.as_ref() && impt.name == name.as_ref()
.find_map(|impt| match impt.kind {
ImportKind::Function(fid)
if impt.module == module.as_ref() && impt.name == name.as_ref() =>
{
Some(fid)
}
_ => false,
_ => None,
})
.with_context(|| format!("unable to find function export '{}'", name.as_ref()))
}

/// Retrieve an imported function by ID
pub fn get_func_by_id(&self, id: FunctionId) -> Option<&Import> {
pub fn get_imported_func(&self, id: FunctionId) -> Option<&Import> {
self.arena.iter().find_map(|(_, import)| match import.kind {
ImportKind::Function(fid) if fid == id => Some(import),
_ => None,
})
}

/// Check whether imports include a function with the given ID
pub fn contains_function_by_id(&self, fid: FunctionId) -> bool {
self.get_func_by_id(fid).is_some()
}
}

impl Module {
Expand Down

0 comments on commit 74966a4

Please sign in to comment.