Skip to content

Commit

Permalink
feat: upstream ops from WASI-virt
Browse files Browse the repository at this point in the history
WASI-virt contains functions that are helpful for manipulating modules
and dealing with exports/imports, which would be helpful to an even
wider group if upstreamed here to walrus.

This commit copies and upstreams some operations that were introduced
in WASI-virt for wider use via walrus.

See also: bytecodealliance/WASI-Virt#20

Signed-off-by: Victor Adossi <[email protected]>
  • Loading branch information
vados-cosmonic committed Oct 13, 2023
1 parent 440dc03 commit ad31f36
Show file tree
Hide file tree
Showing 7 changed files with 427 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/arena_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::hash::Hash;
use std::ops;

/// A set of unique `T`s that are backed by an arena.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ArenaSet<T: Clone + Eq + Hash> {
arena: TombstoneArena<T>,
already_in_arena: HashMap<T, Id<T>>,
Expand Down
22 changes: 22 additions & 0 deletions src/module/exports.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Exported items in a wasm module.

use anyhow::Context;

use crate::emit::{Emit, EmitContext};
use crate::parse::IndicesToIds;
use crate::tombstone_arena::{Id, Tombstone, TombstoneArena};
Expand Down Expand Up @@ -100,6 +102,16 @@ impl ModuleExports {
})
}

/// Retrieve an exported function by name
pub fn get_func_by_name(&self, name: impl AsRef<str>) -> Result<FunctionId> {
self.iter()
.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()))
}

/// Get a reference to a table export given its table id.
pub fn get_exported_table(&self, t: TableId) -> Option<&Export> {
self.iter().find(|e| match e.item {
Expand Down Expand Up @@ -354,6 +366,16 @@ mod tests {
assert!(module.exports.get_exported_func(fn_id).is_none());
}

#[test]
fn get_func_by_name() {
let mut module = Module::default();
let fn_id: FunctionId = always_the_same_id();
let export_id: ExportId = module.exports.add("dummy", fn_id);
assert!(module.exports.get_func_by_name("dummy").is_ok());
module.exports.delete(export_id);
assert!(module.exports.get_func_by_name("dummy").is_err());
}

#[test]
fn iter_mut_can_update_export_item() {
let mut module = Module::default();
Expand Down
Loading

0 comments on commit ad31f36

Please sign in to comment.