diff --git a/src/optimizer.rs b/src/optimizer.rs index a58f8ed..4c2817e 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -380,4 +380,33 @@ mod tests { assert!(result.is_err()); } + + #[test] + fn minimal() { + let mut module = builder::module() + .function() + .signature().param().i32().build() + .body().build() + .build() + .function() + .signature().param().i32().param().i32().build() + .body().build() + .build() + .export() + .field("_call") + .internal().func(0).build() + .export() + .field("_random") + .internal().func(1).build() + .build(); + assert_eq!(module.export_section().expect("export section to be generated").entries().len(), 2); + + optimize(&mut module, vec!["_call"]).expect("optimizer to succeed"); + + assert_eq!( + 1, + module.export_section().expect("export section to be generated").entries().len(), + "There should only 1 (one) export entry in the optimized module" + ); + } } \ No newline at end of file diff --git a/src/symbols.rs b/src/symbols.rs index 098f767..b86efb9 100644 --- a/src/symbols.rs +++ b/src/symbols.rs @@ -12,15 +12,17 @@ pub enum Symbol { pub fn resolve_function(module: &elements::Module, index: u32) -> Symbol { let mut functions = 0; - for (item_index, item) in module.import_section().expect("Functions section to exist").entries().iter().enumerate() { - match item.external() { - &elements::External::Function(_) => { - if functions == index { - return Symbol::Import(item_index as usize); - } - functions += 1; - }, - _ => {} + if let Some(import_section) = module.import_section() { + for (item_index, item) in import_section.entries().iter().enumerate() { + match item.external() { + &elements::External::Function(_) => { + if functions == index { + return Symbol::Import(item_index as usize); + } + functions += 1; + }, + _ => {} + } } } @@ -29,15 +31,17 @@ pub fn resolve_function(module: &elements::Module, index: u32) -> Symbol { pub fn resolve_global(module: &elements::Module, index: u32) -> Symbol { let mut globals = 0; - for (item_index, item) in module.import_section().expect("Functions section to exist").entries().iter().enumerate() { - match item.external() { - &elements::External::Global(_) => { - if globals == index { - return Symbol::Import(item_index as usize); - } - globals += 1; - }, - _ => {} + if let Some(import_section) = module.import_section() { + for (item_index, item) in import_section.entries().iter().enumerate() { + match item.external() { + &elements::External::Global(_) => { + if globals == index { + return Symbol::Import(item_index as usize); + } + globals += 1; + }, + _ => {} + } } }