wasm_module: Create DataSection::load_into

This commit is contained in:
Brian Carroll 2022-11-25 09:19:35 +00:00
parent a0fe01dd5f
commit 96bff3e304
No known key found for this signature in database
GPG key ID: 5C7B2EC4101703C0
2 changed files with 28 additions and 1 deletions

View file

@ -1,4 +1,5 @@
use std::fmt::{Debug, Formatter};
use std::io::Write;
use bumpalo::collections::vec::Vec;
use bumpalo::Bump;
@ -1490,6 +1491,29 @@ impl<'a> DataSection<'a> {
segment.serialize(&mut self.bytes);
index
}
pub fn load_into(&self, memory: &mut [u8]) -> Result<(), String> {
let mut cursor = 0;
for _ in 0..self.count {
let mode =
DataMode::parse((), &self.bytes, &mut cursor).map_err(|e| format!("{:?}", e))?;
let start = match mode {
DataMode::Active {
offset: ConstExpr::I32(addr),
} => addr as usize,
_ => {
continue;
}
};
let len32 = u32::parse((), &self.bytes, &mut cursor).map_err(|e| format!("{:?}", e))?;
let len = len32 as usize;
let mut target_slice = &mut memory[start..][..len];
target_slice
.write(&self.bytes[cursor..][..len])
.map_err(|e| format!("{:?}", e))?;
}
Ok(())
}
}
impl<'a> Parse<&'a Bump> for DataSection<'a> {