mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-10 18:08:55 +03:00
wasm_module: Create DataSection::load_into
This commit is contained in:
parent
a0fe01dd5f
commit
96bff3e304
@ -1,5 +1,6 @@
|
||||
use bumpalo::{collections::Vec, Bump};
|
||||
use std::fmt::Write;
|
||||
use std::iter;
|
||||
|
||||
use roc_wasm_module::opcodes::OpCode;
|
||||
use roc_wasm_module::parse::Parse;
|
||||
@ -59,6 +60,8 @@ impl<'a> ExecutionState<'a> {
|
||||
e.offset, e.message
|
||||
)
|
||||
})?;
|
||||
let mut memory = Vec::from_iter_in(iter::repeat(0).take(mem_bytes as usize), arena);
|
||||
module.data.load_into(&mut memory)?;
|
||||
|
||||
let globals = module.global.initial_values(arena);
|
||||
|
||||
@ -110,7 +113,7 @@ impl<'a> ExecutionState<'a> {
|
||||
};
|
||||
|
||||
Ok(ExecutionState {
|
||||
memory: Vec::with_capacity_in(mem_bytes as usize, arena),
|
||||
memory,
|
||||
call_stack,
|
||||
value_stack,
|
||||
globals,
|
||||
|
@ -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…
Reference in New Issue
Block a user