Rename some SerialBuffer methods

This commit is contained in:
Brian Carroll 2021-11-01 08:42:02 +00:00
parent 7ad452d78a
commit d25b12328d
2 changed files with 29 additions and 27 deletions

View file

@ -35,7 +35,7 @@ struct SectionHeaderIndices {
/// Write a section header, returning the position of the encoded length /// Write a section header, returning the position of the encoded length
fn write_section_header<T: SerialBuffer>(buffer: &mut T, id: SectionId) -> SectionHeaderIndices { fn write_section_header<T: SerialBuffer>(buffer: &mut T, id: SectionId) -> SectionHeaderIndices {
buffer.append_byte(id as u8); buffer.append_u8(id as u8);
let size_index = buffer.reserve_padded_u32(); let size_index = buffer.reserve_padded_u32();
let body_index = buffer.size(); let body_index = buffer.size();
SectionHeaderIndices { SectionHeaderIndices {
@ -49,7 +49,7 @@ fn write_custom_section_header<T: SerialBuffer>(
buffer: &mut T, buffer: &mut T,
name: &str, name: &str,
) -> SectionHeaderIndices { ) -> SectionHeaderIndices {
buffer.append_byte(SectionId::Custom as u8); buffer.append_u8(SectionId::Custom as u8);
let size_index = buffer.reserve_padded_u32(); let size_index = buffer.reserve_padded_u32();
let body_index = buffer.size(); let body_index = buffer.size();
name.serialize(buffer); name.serialize(buffer);
@ -86,7 +86,7 @@ impl<'a> Serialize for [ValueType] {
fn serialize<T: SerialBuffer>(&self, buffer: &mut T) { fn serialize<T: SerialBuffer>(&self, buffer: &mut T) {
// reserve one byte for num_batches // reserve one byte for num_batches
let start = buffer.size(); let start = buffer.size();
buffer.append_byte(0); // mut buffer.append_u8(0);
if self.is_empty() { if self.is_empty() {
return; return;
@ -101,19 +101,19 @@ impl<'a> Serialize for [ValueType] {
batch_size += 1; batch_size += 1;
} else { } else {
buffer.encode_u32(batch_size); buffer.encode_u32(batch_size);
buffer.append_byte(batch_type as u8); buffer.append_u8(batch_type as u8);
batch_type = *t; batch_type = *t;
batch_size = 1; batch_size = 1;
num_batches += 1; num_batches += 1;
} }
} }
buffer.encode_u32(batch_size); buffer.encode_u32(batch_size);
buffer.append_byte(batch_type as u8); buffer.append_u8(batch_type as u8);
num_batches += 1; num_batches += 1;
// Go back and write the number of batches at the start // Go back and write the number of batches at the start
if num_batches < 128 { if num_batches < 128 {
buffer.set_byte(start, num_batches as u8); buffer.overwrite_u8(start, num_batches as u8);
} else { } else {
// We need more than 1 byte to encode num_batches! // We need more than 1 byte to encode num_batches!
// This is a ridiculous edge case, so just pad to 5 bytes for simplicity // This is a ridiculous edge case, so just pad to 5 bytes for simplicity
@ -130,11 +130,11 @@ struct Signature<'a> {
impl<'a> Serialize for Signature<'a> { impl<'a> Serialize for Signature<'a> {
fn serialize<T: SerialBuffer>(&self, buffer: &mut T) { fn serialize<T: SerialBuffer>(&self, buffer: &mut T) {
buffer.append_byte(0x60); buffer.append_u8(0x60);
self.param_types.serialize(buffer); self.param_types.serialize(buffer);
match self.ret_type { match self.ret_type {
Some(t) => [t].serialize(buffer), Some(t) => [t].serialize(buffer),
None => buffer.append_byte(0), // vector of length zero None => buffer.append_u8(0), // vector of length zero
} }
} }
} }
@ -444,7 +444,7 @@ impl Serialize for RelocationEntry {
offset, offset,
symbol_index, symbol_index,
} => { } => {
buffer.append_byte(*type_id as u8); buffer.append_u8(*type_id as u8);
buffer.encode_u32(*offset); buffer.encode_u32(*offset);
buffer.encode_u32(*symbol_index); buffer.encode_u32(*symbol_index);
} }
@ -454,7 +454,7 @@ impl Serialize for RelocationEntry {
symbol_index, symbol_index,
addend, addend,
} => { } => {
buffer.append_byte(*type_id as u8); buffer.append_u8(*type_id as u8);
buffer.encode_u32(*offset); buffer.encode_u32(*offset);
buffer.encode_u32(*symbol_index); buffer.encode_u32(*symbol_index);
buffer.encode_i32(*addend); buffer.encode_i32(*addend);
@ -696,7 +696,7 @@ impl SymInfo {
impl Serialize for SymInfo { impl Serialize for SymInfo {
fn serialize<T: SerialBuffer>(&self, buffer: &mut T) { fn serialize<T: SerialBuffer>(&self, buffer: &mut T) {
buffer.append_byte(match self.info { buffer.append_u8(match self.info {
SymInfoFields::Function(_) => 0, SymInfoFields::Function(_) => 0,
SymInfoFields::Data(_) => 1, SymInfoFields::Data(_) => 1,
SymInfoFields::Global(_) => 2, SymInfoFields::Global(_) => 2,
@ -736,7 +736,7 @@ pub enum LinkingSubSection<'a> {
impl<'a> Serialize for LinkingSubSection<'a> { impl<'a> Serialize for LinkingSubSection<'a> {
fn serialize<T: SerialBuffer>(&self, buffer: &mut T) { fn serialize<T: SerialBuffer>(&self, buffer: &mut T) {
buffer.append_byte(match self { buffer.append_u8(match self {
Self::SegmentInfo(_) => 5, Self::SegmentInfo(_) => 5,
Self::InitFuncs(_) => 6, Self::InitFuncs(_) => 6,
Self::ComdatInfo(_) => 7, Self::ComdatInfo(_) => 7,
@ -778,7 +778,7 @@ impl<'a> LinkingSection<'a> {
impl<'a> Serialize for LinkingSection<'a> { impl<'a> Serialize for LinkingSection<'a> {
fn serialize<T: SerialBuffer>(&self, buffer: &mut T) { fn serialize<T: SerialBuffer>(&self, buffer: &mut T) {
let header_indices = write_custom_section_header(buffer, "linking"); let header_indices = write_custom_section_header(buffer, "linking");
buffer.append_byte(LINKING_VERSION); buffer.append_u8(LINKING_VERSION);
for subsection in self.subsections.iter() { for subsection in self.subsections.iter() {
subsection.serialize(buffer); subsection.serialize(buffer);
} }
@ -846,7 +846,7 @@ impl<'a> WasmModule<'a> {
#[allow(dead_code)] #[allow(dead_code)]
fn serialize<T: SerialBuffer>(&mut self, buffer: &mut T) { fn serialize<T: SerialBuffer>(&mut self, buffer: &mut T) {
buffer.append_byte(0); buffer.append_u8(0);
buffer.append_slice("asm".as_bytes()); buffer.append_slice("asm".as_bytes());
buffer.write_unencoded_u32(Self::WASM_VERSION); buffer.write_unencoded_u32(Self::WASM_VERSION);

View file

@ -10,10 +10,10 @@ macro_rules! encode_uleb128 {
let mut x = value; let mut x = value;
let start_len = self.size(); let start_len = self.size();
while x >= 0x80 { while x >= 0x80 {
self.append_byte(0x80 | ((x & 0x7f) as u8)); self.append_u8(0x80 | ((x & 0x7f) as u8));
x >>= 7; x >>= 7;
} }
self.append_byte(x as u8); self.append_u8(x as u8);
self.size() - start_len self.size() - start_len
} }
}; };
@ -30,10 +30,10 @@ macro_rules! encode_sleb128 {
x >>= 7; x >>= 7;
let byte_is_negative = (byte & 0x40) != 0; let byte_is_negative = (byte & 0x40) != 0;
if ((x == 0 && !byte_is_negative) || (x == -1 && byte_is_negative)) { if ((x == 0 && !byte_is_negative) || (x == -1 && byte_is_negative)) {
self.append_byte(byte); self.append_u8(byte);
break; break;
} }
self.append_byte(byte | 0x80); self.append_u8(byte | 0x80);
} }
self.size() - start_len self.size() - start_len
} }
@ -47,7 +47,7 @@ macro_rules! write_unencoded {
let mut x = value; let mut x = value;
let size = std::mem::size_of::<$ty>(); let size = std::mem::size_of::<$ty>();
for _ in 0..size { for _ in 0..size {
self.append_byte((x & 0xff) as u8); self.append_u8((x & 0xff) as u8);
x >>= 8; x >>= 8;
} }
} }
@ -61,18 +61,19 @@ macro_rules! encode_padded_sleb128 {
let mut x = value; let mut x = value;
let size = (std::mem::size_of::<$ty>() / 4) * 5; let size = (std::mem::size_of::<$ty>() / 4) * 5;
for _ in 0..(size - 1) { for _ in 0..(size - 1) {
self.append_byte(0x80 | (x & 0x7f) as u8); self.append_u8(0x80 | (x & 0x7f) as u8);
x >>= 7; x >>= 7;
} }
self.append_byte((x & 0x7f) as u8); self.append_u8((x & 0x7f) as u8);
} }
}; };
} }
pub trait SerialBuffer { pub trait SerialBuffer {
fn append_byte(&mut self, b: u8); fn append_u8(&mut self, b: u8);
fn set_byte(&mut self, index: usize, b: u8); fn overwrite_u8(&mut self, index: usize, b: u8);
fn append_slice(&mut self, b: &[u8]); fn append_slice(&mut self, b: &[u8]);
fn size(&self) -> usize; fn size(&self) -> usize;
encode_uleb128!(encode_u32, u32); encode_uleb128!(encode_u32, u32);
@ -82,6 +83,7 @@ pub trait SerialBuffer {
/// Inserts extra entries at the given index by copying the following entries to higher indices /// Inserts extra entries at the given index by copying the following entries to higher indices
fn insert_space_at(&mut self, index: usize, size: usize); fn insert_space_at(&mut self, index: usize, size: usize);
fn reserve_padded_u32(&mut self) -> usize; fn reserve_padded_u32(&mut self) -> usize;
fn encode_padded_u32(&mut self, value: u32) -> usize; fn encode_padded_u32(&mut self, value: u32) -> usize;
fn overwrite_padded_u32(&mut self, index: usize, value: u32); fn overwrite_padded_u32(&mut self, index: usize, value: u32);
@ -122,10 +124,10 @@ fn overwrite_padded_u32_help(buffer: &mut [u8], value: u32) {
} }
impl SerialBuffer for std::vec::Vec<u8> { impl SerialBuffer for std::vec::Vec<u8> {
fn append_byte(&mut self, b: u8) { fn append_u8(&mut self, b: u8) {
self.push(b); self.push(b);
} }
fn set_byte(&mut self, index: usize, b: u8) { fn overwrite_u8(&mut self, index: usize, b: u8) {
self[index] = b; self[index] = b;
} }
fn append_slice(&mut self, b: &[u8]) { fn append_slice(&mut self, b: &[u8]) {
@ -157,10 +159,10 @@ impl SerialBuffer for std::vec::Vec<u8> {
} }
impl<'a> SerialBuffer for Vec<'a, u8> { impl<'a> SerialBuffer for Vec<'a, u8> {
fn append_byte(&mut self, b: u8) { fn append_u8(&mut self, b: u8) {
self.push(b); self.push(b);
} }
fn set_byte(&mut self, index: usize, b: u8) { fn overwrite_u8(&mut self, index: usize, b: u8) {
self[index] = b; self[index] = b;
} }
fn append_slice(&mut self, b: &[u8]) { fn append_slice(&mut self, b: &[u8]) {