mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-04 12:18:19 +00:00
wasm_module: Create DataSection::load_into
This commit is contained in:
parent
a0fe01dd5f
commit
96bff3e304
2 changed files with 28 additions and 1 deletions
|
@ -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> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue