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

Method init_data

crates/tinywasm/src/store/mod.rs:473–517  ·  view source on GitHub ↗

Add data to the store, returning their addresses in the store

(
        &mut self,
        mem_addrs: &[MemAddr],
        global_addrs: &[Addr],
        func_addrs: &[FuncAddr],
        data: &[Data],
    )

Source from the content-addressed store, hash-verified

471
472 /// Add data to the store, returning their addresses in the store
473 pub(crate) fn init_data(
474 &mut self,
475 mem_addrs: &[MemAddr],
476 global_addrs: &[Addr],
477 func_addrs: &[FuncAddr],
478 data: &[Data],
479 ) -> Result<(Box<[Addr]>, Option<Trap>)> {
480 let data_count = self.state.data.len();
481 let mut data_addrs = Vec::with_capacity(data_count);
482 for (i, data) in data.iter().enumerate() {
483 let data_val = match &data.kind {
484 tinywasm_types::DataKind::Active { mem: mem_addr, offset } => {
485 let Some(mem_addr) = mem_addrs.get(*mem_addr as usize) else {
486 return Err(Error::Other(format!("memory {mem_addr} not found for data segment {i}")));
487 };
488
489 let offset = self.eval_size_const(offset, global_addrs, func_addrs)?;
490 let Some(mem) = self.state.memories.get_mut(*mem_addr as usize) else {
491 return Err(Error::Other(format!("memory {mem_addr} not found for data segment {i}")));
492 };
493
494 match mem.inner.write_all(offset as usize, &data.data) {
495 Some(()) => None,
496 None => {
497 return Ok((
498 data_addrs.into_boxed_slice(),
499 Some(crate::Trap::MemoryOutOfBounds {
500 offset: offset as usize,
501 len: data.data.len(),
502 max: mem.inner.len(),
503 }),
504 ));
505 }
506 }
507 }
508 tinywasm_types::DataKind::Passive => Some(data.data.to_vec()),
509 };
510
511 self.state.data.push(DataInstance::new(data_val));
512 data_addrs.push((i + data_count) as Addr);
513 }
514
515 // this should be optimized out by the compiler
516 Ok((data_addrs.into_boxed_slice(), None))
517 }
518
519 pub(crate) fn add_func(&mut self, func: FunctionInstance) -> FuncAddr {
520 self.state.funcs.push(func);

Callers 1

instantiate_no_startMethod · 0.80

Calls 5

eval_size_constMethod · 0.80
lenMethod · 0.45
getMethod · 0.45
write_allMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected