Create reserve_padded_u32, nice for readability

This commit is contained in:
Brian Carroll 2021-10-28 22:43:39 +01:00
parent 7841ef959a
commit 2020e89db7
2 changed files with 14 additions and 6 deletions

View file

@ -43,14 +43,11 @@ pub struct WasmBackend<'a> {
impl<'a> WasmBackend<'a> {
pub fn new(env: &'a Env<'a>, proc_symbols: Vec<'a, Symbol>) -> Self {
// Code section is prefixed with the number of Wasm functions
// For now, this is the same as the number of IR procedures (until we start inlining!)
let mut code_section_bytes = std::vec::Vec::with_capacity(4096);
// Reserve space for code section header: inner byte length and number of functions
// Padded to the maximum 5 bytes each, so we can update later without moving everything
code_section_bytes.resize(10, 0);
code_section_bytes.overwrite_padded_u32(5, proc_symbols.len() as u32); // gets modified in unit tests
// Code section header
code_section_bytes.reserve_padded_u32(); // byte length, to be written at the end
code_section_bytes.encode_padded_u32(proc_symbols.len() as u32); // modified later in unit tests
WasmBackend {
env,

View file

@ -79,6 +79,7 @@ pub trait SerialBuffer {
encode_sleb128!(encode_i32, i32);
encode_sleb128!(encode_i64, i64);
fn reserve_padded_u32(&mut self) -> usize;
fn encode_padded_u32(&mut self, value: u32) -> usize;
fn overwrite_padded_u32(&mut self, index: usize, value: u32);
@ -120,6 +121,11 @@ impl SerialBuffer for std::vec::Vec<u8> {
fn size(&self) -> usize {
self.len()
}
fn reserve_padded_u32(&mut self) -> usize {
let index = self.len();
self.resize(index + 5, 0xff);
index
}
fn encode_padded_u32(&mut self, value: u32) -> usize {
let index = self.len();
let new_len = index + 5;
@ -142,6 +148,11 @@ impl<'a> SerialBuffer for Vec<'a, u8> {
fn size(&self) -> usize {
self.len()
}
fn reserve_padded_u32(&mut self) -> usize {
let index = self.len();
self.resize(index + 5, 0xff);
index
}
fn encode_padded_u32(&mut self, value: u32) -> usize {
let index = self.len();
let new_len = index + 5;