(self, options: &ParserOptions)
| 331 | } |
| 332 | |
| 333 | pub(crate) fn into_module(self, options: &ParserOptions) -> Result<Module> { |
| 334 | if !self.end_reached { |
| 335 | return Err(ParseError::EndNotReached); |
| 336 | } |
| 337 | |
| 338 | if self.code_type_addrs.len() != self.code.len() { |
| 339 | return Err(ParseError::Other("Code and code type address count mismatch".to_string())); |
| 340 | } |
| 341 | |
| 342 | let import_mem_count = imported_memory_count(&self.imports); |
| 343 | let has_local_mem_export = |
| 344 | self.exports.iter().any(|export| export.kind == ExternalKind::Memory && export.index >= import_mem_count); |
| 345 | let has_active_data_segment_on_local_memory = self.data.iter().any(|data| match &data.kind { |
| 346 | DataKind::Active { mem, .. } => *mem >= import_mem_count, |
| 347 | DataKind::Passive => false, |
| 348 | }); |
| 349 | let optimize_local_memory_allocation = options.optimize_local_memory_allocation(); |
| 350 | let mut local_memory_allocation = if self.memory_types.is_empty() { |
| 351 | LocalMemoryAllocation::Skip |
| 352 | } else if !optimize_local_memory_allocation || has_active_data_segment_on_local_memory { |
| 353 | LocalMemoryAllocation::Eager |
| 354 | } else if has_local_mem_export { |
| 355 | LocalMemoryAllocation::Lazy |
| 356 | } else { |
| 357 | LocalMemoryAllocation::Skip |
| 358 | }; |
| 359 | |
| 360 | let func_type_idxs = self |
| 361 | .imports |
| 362 | .iter() |
| 363 | .filter_map(|import| match import.kind { |
| 364 | ImportKind::Function(type_idx) => Some(type_idx), |
| 365 | _ => None, |
| 366 | }) |
| 367 | .chain(self.code_type_addrs.iter().copied()) |
| 368 | .collect(); |
| 369 | |
| 370 | let funcs = self |
| 371 | .code |
| 372 | .into_iter() |
| 373 | .zip(self.code_type_addrs) |
| 374 | .map(|(code, ty_idx)| { |
| 375 | let ty = self.func_types.get(ty_idx as usize).expect("No func type for func, this is a bug").clone(); |
| 376 | let params = ValueCounts::from_iter(ty.params()); |
| 377 | let results = ValueCounts::from_iter(ty.results()); |
| 378 | if code.uses_local_memory { |
| 379 | local_memory_allocation = LocalMemoryAllocation::Eager; |
| 380 | } |
| 381 | |
| 382 | Arc::new(WasmFunction { |
| 383 | instructions: code.instructions.into(), |
| 384 | data: code.data, |
| 385 | locals: code.locals, |
| 386 | params, |
| 387 | results, |
| 388 | ty, |
| 389 | }) |
| 390 | }) |
no test coverage detected