diff --git a/src/analysis/mod.rs b/src/analysis/mod.rs index e3c0d9d..c26ae57 100644 --- a/src/analysis/mod.rs +++ b/src/analysis/mod.rs @@ -13,6 +13,7 @@ // limitations under the License. use crate::component::*; +use crate::core::Mem; use crate::AstCustomization; use mappable_rc::Mrc; use std::cell::RefCell; @@ -233,6 +234,24 @@ impl AnalysisContext { Ok(result) } + /// Gets all the memories (not just the exported ones) from all modules within the WASM component + pub fn get_all_memories(&self) -> AnalysisResult> { + let mut result = Vec::new(); + + let mut component_stack = vec![self.get_component()]; + while let Some(component) = component_stack.pop() { + for module in component.modules() { + for mem in module.mems() { + result.push((*mem).clone()); + } + } + for inner_component in component.components() { + component_stack.push(inner_component.clone()); + } + } + Ok(result) + } + pub fn warnings(&self) -> Vec { self.warnings.borrow().clone() } diff --git a/src/core/mod.rs b/src/core/mod.rs index dba45ea..31fe3d1 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -371,8 +371,8 @@ impl Section for FuncType { /// #[derive(Debug, Clone, PartialEq, Eq)] pub struct Limits { - min: u64, - max: Option, + pub min: u64, + pub max: Option, } /// Memory types classify linear memories and their size range. diff --git a/tests/mems.rs b/tests/mems.rs new file mode 100644 index 0000000..65e3fb3 --- /dev/null +++ b/tests/mems.rs @@ -0,0 +1,83 @@ +use golem_wasm_ast::analysis::AnalysisContext; +use golem_wasm_ast::component::Component; +use golem_wasm_ast::core::{Limits, Mem, MemType}; +use golem_wasm_ast::IgnoreAllButMetadata; + +#[test] +fn mems_shopping_cart_component() { + let source_bytes = include_bytes!("../wasm/shopping-cart.wasm"); + let component: Component = Component::from_bytes(source_bytes).unwrap(); + + let state = AnalysisContext::new(component); + let mems = state.get_all_memories().unwrap(); + + pretty_assertions::assert_eq!( + mems, + vec![Mem { + mem_type: MemType { + limits: Limits { min: 17, max: None } + } + }] + ) +} + +#[test] +fn mems_shopping_cart_resource_component() { + let source_bytes = include_bytes!("../wasm/shopping-cart-resource.wasm"); + let component: Component = Component::from_bytes(source_bytes).unwrap(); + + let state = AnalysisContext::new(component); + let mems = state.get_all_memories().unwrap(); + + pretty_assertions::assert_eq!( + mems, + vec![Mem { + mem_type: MemType { + limits: Limits { min: 17, max: None } + } + }] + ) +} + +#[test] +fn mems_file_service_component() { + let source_bytes = include_bytes!("../wasm/file-service.wasm"); + let component: Component = Component::from_bytes(source_bytes).unwrap(); + + let state = AnalysisContext::new(component); + let mems = state.get_all_memories().unwrap(); + + pretty_assertions::assert_eq!( + mems, + vec![Mem { + mem_type: MemType { + limits: Limits { min: 17, max: None } + } + }] + ) +} + +#[test] +fn mems_auction_registry_composed_component() { + let source_bytes = include_bytes!("../wasm/auction_registry_composed.wasm"); + let component: Component = Component::from_bytes(source_bytes).unwrap(); + + let state = AnalysisContext::new(component); + let mems = state.get_all_memories().unwrap(); + + pretty_assertions::assert_eq!( + mems, + vec![ + Mem { + mem_type: MemType { + limits: Limits { min: 17, max: None } + } + }, + Mem { + mem_type: MemType { + limits: Limits { min: 17, max: None } + } + }, + ] + ) +}