MCPcopy Index your code
hub / github.com/explodingcamera/tinywasm / into_module

Method into_module

crates/parser/src/module.rs:333–408  ·  view source on GitHub ↗
(self, options: &ParserOptions)

Source from the content-addressed store, hash-verified

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 })

Callers 3

parse_module_bytesMethod · 0.80
parse_module_streamMethod · 0.80
try_fromMethod · 0.80

Calls 8

imported_memory_countFunction · 0.85
mapMethod · 0.80
paramsMethod · 0.80
resultsMethod · 0.80
lenMethod · 0.45
is_emptyMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected